Warning: After consulting with some of my friends, I’ve concluded that Nagios is a much better solution, so this article is now obsolete. Read it only if you don’t want to install Nagios on your server.
Now that I have some server that I need to administer and look after, I’m starting to notice potential issues that until several months ago were just somebody’s else’s problems and I didn’t cared much for them. One of which is to monitor the server’s free space, since it’s better to know when your server’s storage capacity is running low. Saves me some money on the headache pills. Well, knowing a scripting language like python comes in handy in times like these, because it helped me to put together a simple script that warns me whenever one or more hard-drives are over 85% full.
Here it is:
#!/usr/bin/python import commands WARNING_PERCENTAGE = 85 def main(): global WARNING_PERCENTAGE warnings = [] status, output = commands.getstatusoutput( """df -k | awk '{if ($6 !~ /cdrom/ && NR != 1) print $1 " " ($5 + 0) }'""" ) for line in output.splitlines(): hdd, used = line.split( ' ' ) if int( used ) >= WARNING_PERCENTAGE: warnings.append( line ) if len( warnings ) is not 0: send_warning( warnings ) def send_warning( warnings ): # here you should send e-mails, smses and so on print warnings if __name__ == '__main__': main()
Since it uses df and awk, it’s obvious that it won’t run on Windows. But who need Windows anyway…
PS: do I have to mention that this script should be ran as a cron job?