I've posted a version of this script before but did some updates and wanted to re-post. This script will grab the uptime of a machine, and if past a set minimum number of days, will use cocoaDialog to bug the user to restart. If the machine is up past a set maximum number of days, the script will bug them with cocoaDialog and will start sending emails to them.
The script uses the JSS API (thanks to @brysontyrrell for his article on the JSS API) to grab the user's real name and email address to send them a message.
You can find the script on Github here: https://github.com/stevewood-tx/CasperScripts-Public/tree/master/checkUpTime
The link to Bryson's article: http://bryson3gps.wordpress.com/2014/03/30/the-jss-rest-api-for-everyone/
And here's the script:
#!/bin/sh
# Name: checkUpTime.sh
# Date: 19 Aug 2014
# Author: Steve Wood (swood@integer.com)
# Purpose: look for machines that have not been restarted in X number of days.
# Requirements: cocoaDialog on the local machine
#
# How To Use: create a policy in your JSS with this script set to run once every day.
## Global Variables and Stuff
logPath='/path/to/store/log/files' ### <--- enter a path to where you store log files locally
if [[ ! -d "$logPath" ]]; then
mkdir $logPath
fi
set -xv; exec 1> $logPath/checkUpTime.txt 2>&1
version=1.0
CD="/path/to/cocoaDialog.app/Contents/MacOS/cocoaDialog" ### <--- path to where you store cocoDialog on local machine
NC='/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action'
jssURL='https://YOUR.JSSSERVER.COM:8443' ### <--- enter your JSS URL
apiUser="<APIREADUSER>" ### <--- enter your API user
apiPass="<APIREADUSERPASS>" ### <--- enter your API user password
serNum=$(ioreg -l | grep IOPlatformSerialNumber | awk '{print $4}'| sed 's/"//g')
cdTitle="Machine Needs A Restart"
loggedInUser=`/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }'`
## set minDays - we start bugging users at this level with just a dialog box
minDays=7
## set maxDays - after we reach maxDays we bug with dialog box AND email
maxDays=15
## Grab user info ##
### Thanks to Bryson Tyrrell (@bryson3Gps) for the code to parse
info=$(curl -s -k -u $apiUser:$apiPass $jssURL/JSSResource/computers/match/$serNum)
email=$(echo $info | /usr/bin/awk -F'<email>|</email>' '{print $2}')
realName=$(echo $info | /usr/bin/awk -F'<realname>|</realname>' '{print $2}')
#### MAIN CODE ####
days=`uptime | awk '{ print $4 }' | sed 's/,//g'` # grabs the word "days" if it is there
num=`uptime | awk '{ print $3 }'` # grabs the number of hours or days in the uptime command
## set the body of the email message
message1="Dear $realName"
message1b="Your computer has now been up for $num days. It is important for you to restart your machine on a regular"
message2="basis to help it run more efficiently and to apply updates and patches that are deployed during the login or logout"
message3="process."
message3a="Please restart your machine ASAP. If you do not restart, you will continue to get this email and the pop-up"
message4="dialog box daily until you do."
message5="FROM THE IT STAFF" ### <--- change this to whomever you want
## now the logic
if [ $loggedInUser != "root" ]; then
if [ $days = "days" ]; then
if [ $num -gt $minDays ]; then
if [ $num -gt $maxDays ]; then
cdIcon="/private/var/inte/icons/redX.icns"
cdText="Your computer has not been restarted in more than $maxDays days. Please restart ASAP. Thank you."
bubble=`$CD bubble --title "$cdTitle" --no-timeout --text "$cdText" --icon-file $cdIcon`
if [ $email != "" ]; then
echo "$message1
$message1b
$message2
$message3
$message3a
$message4
$message5" | mail -s "URGENT: Restart Your Machine" $email
fi
else
cdIcon="/private/var/inte/icons/ProblemReporter.icns"
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
fi
exit 0