Computing Uptime and Using It

stevewood
Honored Contributor II
Honored Contributor II

Back in January we had a discussion here about finding the uptime of a
machine and displaying a message to users to reboot their machines. I just
wanted to follow up on that and hand out what I've done here to implement
that.

First of all, the reason I want to implement this (because I know that was a
question) is because my users have a tendency to not restart for over 7
days, some up to a month or longer. And, I get calls about performance
degradation or strange things happening. They don't even restart when I
display a message that says "Software updates have been installed, please
reboot as soon as possible."

So, here's what I've done:

  1. Create the following script, named it uptoolong.sh:

#!/bin/sh

# Name: uptoolong.sh
# Date: 3 March 2010
# Author: Steve Wood (swood at integer.com)
# Purpose: this script will check how long a machine has been up and display
a notification
# using Growl and Growl Notify.

# set some variables and grab the time
days=uptime | awk '{ print $4 }' | sed 's/,//g'
num=uptime | awk '{ print $3 }'

if [ $days = "days" ];

then
if [ $num -gt 5 ]; then
# I'm up too long and need a restart /usr/sbin/jamf policy -trigger uptoolong
/usr/local/bin/growlnotify -a "Finder" -t "Restart Required" -m "Your
computer has been up for $num days. Please restart as soon as possible." -s else # chcek to see if somehow we restarted and left the UpTooLong receipt if [ -e "/Library/Application Support/JAMF/Receipts/UpTooLong.pkg" ]
then
rm /Library/Application Support/JAMF/Receipts/UpTooLong.pkg
fi fi
fi
exit 0

  1. Create a policy that runs every day (during the week) at 11 am. The
    policy runs the above script and updates inventory. It is scoped to all
    computers.

  2. Create another policy with a trigger of "uptoolong" that installs a
    package, UpTooLong.pkg, and updates inventory. As you can see from the
    script, I use Growl with growlnotify to display a sticky note on the
    computers that have been up to long asking them to please restart.

  3. Create a Smart Group that simply looks for the UpTooLong.pkg receipt.

  4. Create a third policy that is triggered on Startup that simply removes
    the UpTooLong.pkg receipt and runs inventory. This is scoped to Smart Group
    in step 4.

In my testing this works perfectly. The user gets notified and hopefully
they restart their machines.

And the reason I am running update inventory so much is because I want to
make sure, regardless of if they need to restart, that I get a good
inventory every day.

Okay, now, what have I missed, or what can I do better/differently?

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475

2 REPLIES 2

jwojda
Valued Contributor II

I was thinking of implementing something like this, however I was wondering if it could be modified to use CocoaDialog?

Further you mentioned the script, but not much info on the pkg you deployed. is it just a dummy receipt or something?

stevewood
Honored Contributor II
Honored Contributor II

Sure, you can use CD instead. In fact, I updated that script to do just that:

#!/bin/sh

# Name: uptoolong.sh
# Date:  3 March 2010
# Author: Steve Wood (swood@integer.com)
# Purpose: this script will check how long a machine has been up and display a notification
# using Growl and Growl Notify.
# updated:  22 March 2010 - moved email address grab inside of if then loop
# updated:  20 March 2013 - removed Growl bits and added cocoaDialog instead
# updated:  25 October 2013 - removed EA bits, cleaned up cocoaDialog

# set some variables and grab the time
days=`uptime | awk '{ print $4 }' | sed 's/,//g'`
num=`uptime | awk '{ print $3 }'`

# path to cocoaDialog
CD="<your path to CD>/cocoaDialog.app/Contents/MacOS/cocoaDialog"
cdTitle="Machine Needs A Restart"

# we want the computer name
computername=$2

# set the nasty message we are going to send
message1="Your computer has now been up for $num days.  It is important for you to restart"
message2="your machine regularly to help it run efficiently and to apply any updates or changes"
message3="that have been pushed in the background.  Please restart your machine ASAP.  Thank you."
echo $days
echo $num

# now check how long they've been awake
if [ $days = "days" ];

then
    if [ $num -gt 5 ];
        # we have a narcaleptic Machine
        then
            # this is serious
            if [ $num -gt 9 ];

                then

                    # grab the current user and use that to query OD for the email address
                    CurrentUser=`ls -l /dev/console | awk '{ print $3 }'`

                    #Grab the email address of the user - this was for an OpenDirectory setup, needs to be rewritten for AD
                    email=`dscl /LDAPv3/<OD Server> -read /Users/$CurrentUser | grep EMailAddress | awk '{ print $2 }'`

                    # new method of an extension attribute
                    cdIcon="<set your icon>"
                    cdText="Your computer has not been restarted in more than 10 days.  Please restart ASAP.  Thank you."

                    bubble=`$CD bubble --title "$cdTitle" --no-timeout --text "$cdText" --icon-file $cdIcon`

                    echo -e "$message1
$message2
$message3" | mail -s "Machine Up Too Long" $email

                else

                    # display message with cocoaDialog
                    cdIcon="<set your icon>"
                    cdText="Your computer has not been restarted in $num days.  Please restart ASAP.  Thank you."
                    bubble=`$CD bubble --title "$cdTitle" --no-timeout --text "$cdText" --icon-file $cdIcon`

            fi  

    fi
fi

exit 0