Advertisement
sheldonalman

Check Swap and Restart Service

Mar 12th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. '''
  2. ######################
  3. checkswap.py
  4. Author: Sheldon Alman
  5. Date : Nov. 21 2013
  6. ######################
  7. This script was specifically written as a workaround for the Samba memory leak issue.  The script checks the amount of swap space available and restarts the Samba process if the amount of swap available is less than the specified value.
  8. ######################
  9. Revision History:
  10. v0.1 - Nov 21 2013 - Initial Release
  11. ######################
  12. '''
  13. import subprocess
  14. import re
  15. import syslog
  16.  
  17. def write_syslog(level,message):
  18.     '''This function writes the message passed to it at the corresponding log level to syslog'''
  19.     try:
  20.         syslog.syslog(level, message)
  21.         return True
  22.     except Exception, e:
  23.         print "Cannot write to syslog ",e
  24.         return False
  25.  
  26. def main():
  27.     service = 'smb'
  28.     total = subprocess.Popen(['/bin/grep','SwapTotal','/proc/meminfo'],stdout=subprocess.PIPE) #Total amount of swap space
  29.     free = subprocess.Popen(['/bin/grep','SwapFree','/proc/meminfo'],stdout=subprocess.PIPE) #Amount of free swap space
  30.     ps_aux = subprocess.Popen(['ps','aux'],stdout=subprocess.PIPE) #Executes the 'ps aux ' command and stores it's results
  31.     grep_service = subprocess.Popen(['grep',service],stdin=ps_aux.stdout, stdout=subprocess.PIPE) #Executes the 'grep smb' command and takes stdout of 'ps aux' as it's input
  32.     wc_service = subprocess.Popen(['wc','-l'],stdin=grep_service.stdout, stdout=subprocess.PIPE)
  33.     wc_service_string = str(eval(str(wc_service.communicate()[0])) - 1)
  34.     ps_aux.stdout.close()
  35.  
  36.     #Removes all other characters except for digits and converts the results to floating point numbers
  37.     free_swap = float(re.findall(r'\d+', str(free.communicate()))[0])
  38.     total_swap = float(re.findall(r'\d+', str(total.communicate()))[0])
  39.     max_swap_usage = 75.0 #Max swap usage before service restart
  40.    
  41.     '''If the amount of free swap space is less than the specified number, write to syslog and restart the smb process.'''
  42.     if (free_swap / total_swap * 100) < max_swap_usage:
  43.         write_syslog(syslog.LOG_WARNING, "Swap usage is above %s%%.  Restarting %s service" % (max_swap_usage, service))
  44.         write_syslog(syslog.LOG_WARNING, "Number of %s processes running : %s" % (service,wc_service_string))
  45.         try:
  46.             subprocess.check_call('/etc/init.d/%s restart' % (service), shell=True)
  47.         except Exception, e:
  48.             print "Could not execute ", e
  49.             write_syslog(syslog.LOG_WARNING, "Could not restart %s service. Error: %s" % (service, e))
  50.     else:
  51.         write_syslog(syslog.LOG_INFO, "Swap usage is less than %s%%.  Restart of %s not required." % (max_swap_usage,service))
  52.  
  53. if __name__ == "__main__":
  54.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement