Wednesday 21 September 2011

Notify users when their disk space is running low

Well, after my last post about listing problems to fix and make my job that much easier, I thought I'd look up a script I had a while back which notified users when their disk space was running low. It really did save a lot of time and effort, as users would know exactly what the problem was: That their disk space was running LOW!

I couldn't make the script on my own back then (and still couldn't now) so I put up a "Help me" topic on the Apple Discussions and low and behold, someone came to help, amazing!

Here's the code, although you might have to read the discussion to get the right syntax as it didn't post quite right:

--****BEGIN SCRIPT
idle
on idle
set message_ to "Stephen! Don't trouble the technician, delete some files!"
set MinAllowFS to 20
try
set bootDiskInfo to do shell script "diskutil list | grep 'stephen'"
set bootDiskID to last word of bootDiskInfo
set FSinfo to do shell script "diskutil info " & bootDiskID & " | grep 'Free Space'"
set ActFS to word 3 of FSinfo

--Pick one or more of the following responses by removing the comment ("")

--if ActFS < MinAllowFS then beep 3

--if ActFS < MinAllowFS then display dialog message_

if ActFS < MinAllowFS then say "[[rate 180]]" & message_
end try
return 10 --<< Here's where you set the repeat rate in seconds
end idle
--
****END SCRIPT


The message wasn't quite what I had in mind. I wanted to inform the students that their disk space was running low and might cause some problems. They should take action by moving files off their home space, and emptying the trash. Something more along the lines of:

set message_ to "Your home space is running low, this may cause applications to crash and even corrupt you files. Please move some files off your home drive and don't forget to Empty the Trash."

The MinAllowFS is the threshold for the message to kick in, currently set to 20MB

As I wanted it to run for any networked user that was logged in, I ended up using the command "df -k -m" instead of "diskutil list". This lists all volumes mounted and the amount of space left in MB (-m). This df command is more useful then the 'diskutil' command as it gives us the free space straight away. The specific line from this list contains the word "students" so this line would read:

set bootDiskInfo to do shell script "df -k -m | grep 'students'"

Word 4 of this is the amount of disk space left, in MB

set ActFS to word 4 of bootDiskInfo

So the whole script should read:

--****BEGIN SCRIPT
idle
on idle
set message_ to "Your home space is running low, this may cause applications to crash and even corrupt you files. Please move some files off your home drive and don't forget to Empty the Trash."
set MinAllowFS to 20
try
set bootDiskInfo to do shell script "df -k -m | grep 'students'"
set ActFS to word 4 of bootDiskInfo

--Pick one or more of the following responses by removing the comment ("")

--if ActFS < MinAllowFS then beep 3

if ActFS < MinAllowFS then display dialog message_

--if ActFS < MinAllowFS then say [[rate 180]] & message_


end try
return 300 --<< Here's where you set the repeat rate in seconds
end idle
--
****END SCRIPT



------------------ 28/09/2013 ----------------
I've just came back to this post and finally got around to implementing it. I ended up with a problem having two lines of output from the df command, because two lines contain the word 'student'. To solve this, I had to use the grep -v option, which is the inverse of the grep command, finding the lines without it in. So trigger is on the line I don't want so the command I used was:

df -k -m | grep 'students' | grep -v 'trigger'


Script is now:


--****BEGIN SCRIPT
idleon idleset message_ to "Your home space is running low, this may cause applications to crash and even corrupt your files. Please move some files off your home drive and don't forget to Empty the Trash."
set MinAllowFS to 10
tryset bootDiskInfo to do shell script "df -k -m | grep 'students' | grep -v 'trigger'"
set ActFS to word 6 of bootDiskInfo
--Pick one or more of the following responses by removing the comment ("")
--if ActFS < MinAllowFS then beep 3
if ActFS < MinAllowFS then display dialog message_
--if ActFS < MinAllowFS then say [[rate 180]] & message_
end tryreturn 300 --<< Here's where you set the repeat rate in seconds
end idle

Now I have written a few programs, I'm getting the hang of writing them properly. A few things I would like to adjust are:

  1. verify (assert!) that ActFS is set to an appropriate number (not a letter or symbol)
  2. check if it is a member of staff and not a student and look for "staff" instead
  3. feed back to the user how much disk space there is free, total disk quota.
  4. give user the option of running a utility which will help identify larger files and folders
  5. contact administrator for help (either by remote desktop or email)

No comments:

Post a Comment