Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_nmb_daemon(counter=0):
- #check status of server
- cmd = "/etc/init.d/nmb status"
- #send that command to CLI
- p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- #get the output
- stdout, stderr = p.communicate()
- #check for the error in executing the command
- if stderr:
- print("there was an error in executing ping command")
- return "cmd failed"
- #check the output
- pattern_running = r"\brunning\b"#regex for a running nmb service
- match = re.compile(pattern_running).search(stdout)
- if not match:
- #the service is down, attempt restart
- restart_cmd = "/etc/init.d/nmb start"
- subprocess.Popen(restart_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- #increament on the counter, use it as a check to avoid an infinite recursive loop
- counter += 1
- print counter
- if counter == 5:
- #we've tried five times restarting this service, it probably needs manual restating
- #inform the admins
- print "nmb daemon is down"
- return "nmb down"
- check_nmb_daemon(counter)
- else:
- print "nmb daemon is up"
- return "nmb up"
- response = check_nmb_daemon()
- print "response is %s" % response
- if response != "nmb up":
- #either the remote command failed
- if response == "cmd failed":
- email_msg = "command that checks for the nmb daemon failed to execute. check it out"
- else:
- email_msg = "i tried to restart the service 5 times, untill i gave up. do it manually"
- send_email(email_msg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement