Supervisor service status check

#########################################################################################################################
#Usage: python <script_Name.py> –To_Mailid <Mail to whom> –Sender <From_Mail> –Passwd_Enc <Base64_encrypted Passwd>
########################################################################################################################

import subprocess,socket,os,sys
import smtplib,argparse
import base64
import sys

parser = argparse.ArgumentParser(description=”Service_check”)
parser.add_argument(‘–To_Mailid’, type=str, help=”Enter mail id to send mail”, required=True)
parser.add_argument(‘–Sender’, type=str, help=”Enter the username of sender mail id”, required=True)
parser.add_argument(‘–Passwd_Enc’, type=str, help=”Enter Base64 encrypted password”, required=True)

args = parser.parse_args()
inputs = {“To_Mailid”: args.To_Mailid, “Sender”: args.Sender, “Passwd_Enc”: args.Passwd_Enc}

Hostname=socket.gethostname()

def Send_mail(sub,msg):

sendto = args.To_Mailid
user= args.Sender
password = base64.b64decode(args.Passwd_Enc)
smtpsrv = “smtp.office365.com”
smtpserver = smtplib.SMTP(smtpsrv,587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(user, password)
msgbody = message = ‘Subject: {}\n\n{}’.format(sub, msg)
smtpserver.sendmail(user, sendto, msgbody)
smtpserver.close()
def supervisor_main():
suppid=”ps -ef | grep -w supervisor | grep -v grep | wc -l”
status = subprocess.check_output(suppid, shell=True)
if status > 0:
supervisor_services()
else:
command=”service supervisor start && sleep2″
subprocess.check_output(“command”, shell=True)
subj=”%s supervisor status” % Hostname
msg=”Hi All,\n\nsupervisor has been started on %s. \n\nThanks\nCorestack Team” % Hostname
Send_mail(subj,msg)
def supervisor_services():
services = []
Running_services = []
command=”supervisorctl status | awk ‘{print $1}’ 2>>/dev/null”
services=subprocess.check_output(command,shell=True)
List_Services = services.split(‘\n’)
while(“” in List_Services):
List_Services.remove(“”)

for each_service in List_Services:
service_command=”supervisorctl status |grep -w %s | awk {‘print $2′}” % each_service
service_status = subprocess.check_output(service_command, shell=True)
if str(service_status.strip()) != “RUNNING”:
Running_services.append(each_service)
subj=”Services which are all not running on %s” % Hostname
msg=”Hi All,\n\nFollowing services are not running on %s \n%s. \n\nThanks\nCorestack Team” % (Hostname,’,’.join(Running_services))
Send_mail(subj,msg)

supervisor_main()

Leave a comment