Process Monitoring Script Write a python script to check a list of services and
ID: 3858870 • Letter: P
Question
Process Monitoring Script Write a python script to check a list of services and determine if they are running or not. A report should be generated detailing the information gathered. The list of services should be obtained via a text file. $ cat services .txt httpd, mysqld, vsftpd, sshd $ ./scriptl.py services.txt Service Report onThu Mar 7 9:20:59 PDT 2015 ____________________________ Checking the following services: [OK] httpd [DOWN] mysqld [DOWN] vsftpd [OK] sshd The services must be read from a text file specified as a command line argument. This file should contain comma separated values. a. https://www.tutorialspoint.com/python3/python_command_line_arguments.htm Ensure that the -'s match the length of the string above. a. Get the length of the "Service Report on hellip" string and use print('Tilde' * length). Make sure you have spaces after [Ok] and [DowN] so that everything lines up. Add terminal colors so OK is green and DOWN is red. a. http://misc flogisoft.com/bash/tip_colors_and_formattingExplanation / Answer
import subprocess
import sys
import time
class bcolors:
HEADER = '[95m'
OKBLUE = '[94m'
OKGREEN = '[92m'
WARNING = '[93m'
FAIL = '[91m'
ENDC = '[0m'
BOLD = '[1m'
UNDERLINE = '[4m'
filename=sys.argv[1]
f= open(filename,"r")
contents =f.read().strip()
print(contents.split(','))
processes = contents.split(',')
output = subprocess.check_output(['ps', '-A'])
print("Service Report on "+time.asctime( time.localtime(time.time()) ))
print("*******************************************")
print("Checking the following services:")
print()
for process in processes:
if bytes(process, 'utf-8') in output:
print(bcolors.OKGREEN+"[OK]"+bcolors.ENDC+" "+process)
else:
print(bcolors.FAIL+"[DOWN]"+bcolors.ENDC +" "+process)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.