I have a very elementary python script (no class or functions defined) which runs fine. I need to send data to hobbit monitor.
In bash I would send it like this, $BB
and $$BBDISP
set in the environment path by hobbit program, variables $MACHINE
and $TEST
the host and test name, $COLOR
determined by the script and displayed by hobbit, and MSG
which holds the actual data:
$BB $BBDISP "status $MACHINE.$TEST $COLOR `date` $MSG"
For Python I have this but it's not working:
[os.environ['BB'], os.environ['BBDISP'], "status arin,fliers,com.store-backup, color, date_now, msg, alert"]
Any idea how the Python syntax needs to be adjusted?
Here is the script:
#!/usr/bin/python26
# import needed modules, using sys, os, re for os level use, glob for pattern matching, and time for log related timestamping
import sys
import os
import subprocess
import glob
import time
import re
import datetime
#BB = ("/home/hobbit/bin/bb")
date_now = datetime.datetime.now()
#only run if backup log exists:
if os.path.exists("/mnt/backup/full"):
now = time.time()
log_mostrc = max(glob.glob('/mnt/backup/full/*.log'), key=os.path.getctime)
c_time = time.ctime(os.path.getctime(log_mostrc))
#check if backup was successful
if "innobackupex: completed OK!" in open(log_mostrc).read():
msg = "innobackupex: completed OK!"
else:
msg = "backup may not have been successful. Please investigate."
sys.exit()
#time measures
file_epoch = os.path.getctime(log_mostrc)
hours_recent = (now - (26*60*60)) # 26 hours ago
hours_old = (now - (52*60*60)) # 52 hours ago
sec_ago = now - file_epoch
hours_ago = int(sec_ago) / 3660
#test age of files
if file_epoch < hours_old:
alert = "backup created over 52 hours ago! Now %d hours old! Please investigate!" % hours_ago
color = 'red'
elif file_epoch < hours_recent:
alert = "backup created over 26 hours ago! Now %d hours old! Please investigate." % hours_ago
color = 'yellow'
else:
alert = "backup created about %d hours ago. Still fresh." % hours_ago
color = 'green'
# print msg
# print color, log_mostrc, c_time, alert
[os.environ['BB'], os.environ['BBDISP'], "status arin,flier,com.store-backup, color, date_now, msg, alert"]
else:
sys.exit()