Advertisement
oquidave

check status of nmb daemon

Sep 19th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1.  
  2. def check_nmb_daemon(counter=0):
  3.   #check status of server
  4.   cmd = "/etc/init.d/nmb status"
  5.  
  6.   #send that command to CLI
  7.   p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  8.   #get the output
  9.   stdout, stderr = p.communicate()
  10.   #check for the error in executing the command
  11.   if stderr:
  12.       print("there was an error in executing ping command")
  13.       return "cmd failed"
  14.   #check the output
  15.   pattern_running = r"\brunning\b"#regex for a running nmb service
  16.   match = re.compile(pattern_running).search(stdout)
  17.   if not match:
  18.     #the service is down, attempt restart
  19.     restart_cmd = "/etc/init.d/nmb start"
  20.     subprocess.Popen(restart_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  21.     #increament on the counter, use it as a check to avoid an infinite recursive loop
  22.     counter += 1
  23.     print counter
  24.     if counter == 5:
  25.       #we've tried five times restarting this service, it probably needs manual restating
  26.       #inform the admins
  27.       print "nmb daemon is down"
  28.       return "nmb down"
  29.     check_nmb_daemon(counter)
  30.   else:
  31.     print "nmb daemon is up"
  32.     return "nmb up"
  33.  
  34. response  = check_nmb_daemon()
  35. print "response is %s" % response
  36. if response != "nmb up":
  37.   #either the remote command failed
  38.   if response == "cmd failed":
  39.      email_msg = "command that checks for the nmb daemon failed to execute. check it out"
  40.   else:
  41.     email_msg = "i tried to restart the service 5 times, untill i gave up. do it manually"
  42.   send_email(email_msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement