Check OSX partitions and email results

I wrote a small Python script which gets a list of partitions and checks each one to make sure the partition is okay, and for the script to email the results.

#!/usr/bin/env python

import os
import string
import smtplib

from socket import gethostname

smtpserver = 'localhost'
smtpfrom = gethostname() + '@localhost'
smtpto = ['##recipient email##']

driveshandle = os.popen('/bin/df -h | grep disk')
drives = driveshandle.read()
driveshandle.close()
drives = drives.split('\n')

for drive in drives:

     diskinfo = ''
     if drive.find('dev') > -1:
          partition = drive.split(' ')
          print 'Drive ' +partition[0]
          fsck = os.popen('/sbin/fsck_hfs -fy ' + partition[0])
          diskinfo = fsck.read()
          fsck.close()
          smtpsubject = 'File System Checks for ' + gethostname() + ', drive ' + drive
          smtp = smtplib.SMTP(smtpserver)
          message = """\
From: %s
To: %s
Subject: %s

%s
""" % (smtpfrom, ", ".join(smtpto), smtpsubject, diskinfo)
          smtp.sendmail(smtpfrom, smtpto, message)
          smtp.quit()

print 'completed'

Comments are closed.