[Courses] [python] Lesson 8: Extras
Kay Nettle
pkn at cs.utexas.edu
Tue Aug 9 14:19:47 UTC 2011
Here is a python script that I wrote instead of a shell script. It
was just a one time use script, but it was fun to translate it into
python.
If the file was empty, that either meant I hadn't updated cups or
the host was down. This script fpings any host that had a 0 size
file (the file name was the hostname)
#!/usr/bin/python
import os, subprocess
for file in os.listdir("/tmp/z"):
if os.stat(file).st_size == 0:
proc = subprocess.Popen(["fping",file],stderr=subprocess.PIPE)
serr=proc.communicate()
I'm also working on a nagios plugin to check the ipmi log on our cluster/server
machines and submit a hardware request if there is something critical
in the log file. Nagios will log any entry. On heavily loaded systems,
this can take longer than the nagios timeout, so I'm going to have it call
another python script that will send mail to our hardware request system.
It's still in the testing phase, which is why it has odd paths.
exit3 = unknown state
exit2 = critical state
exit1 = warning state
#!/usr/bin/python3
from datadiff import diff
import os, sys, shutil, subprocess
directory="/tmp"
log_file=directory + "/output"
new_log_file=log_file + '.new'
def myopen(open_it):
try:
file = open(open_it, "w")
except IOError as error:
print (error)
sys.exit(3)
return(file)
if not os.path.isdir(directory):
print ("No %s directory" % (directory))
sys.exit(3)
if not os.path.isfile(log_file):
log_it = myopen(log_file)
subprocess.call(["ipmitool", "delloem", "sel", "list"], stdout=log_it)
log_it.close()
print ("Had to create state file")
sys.exit(3)
log_it = myopen(new_log_file)
subprocess.call(["ipmitool", "delloem", "sel", "list"], stdout=log_it)
log_it.close()
orig = open(log_file).read()
new = open(new_log_file).read()
out = diff(orig, new, context=0)
if not out:
sys.exit(0)
if out.find('Critical') >=0:
severity='Critical'
else:
severity='Warning'
lines=out.splitlines(1)
msg=""
for i in lines:
if (i.find('---') >= 0) | (i.find('+++') >=0) | (i.find('@@') >= 0) :
continue
i = i.strip('+')
msg += i
print (i.rstrip('\n'))
shutil.copy(new_log_file,log_file)
if severity == 'Critical':
subprocess.Popen(['/var/local/pkn/python/foo.py', msg])
sys.exit(2)
else:
sys.exit(1)
More information about the Courses
mailing list