0

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()   
a ekandem
  • 99
  • 1
  • 10
  • Can you post more code - especially related to how you actually make the call - using os.system or subprocess...? – vikramls Nov 07 '14 at 23:45

1 Answers1

1

Instead of

[os.environ['BB'], os.environ['BBDISP'], "status arin,flier,com.store-backup, color, date_now, msg, alert"] 

you should run command from python like:

os.system(
    ' '.join([os.environ['BB'], os.environ['BBDISP'], '"status arin,flier,com.store-backup,', ','.join([color, str(date_now), msg, alert,'"'])])
)
Andrej
  • 7,474
  • 1
  • 19
  • 21
  • This is looking really promising Andrej, there is an unmatched double quote and I have no idea the logic on this so I don't know where the other one should go or if the first one should be dropped. – a ekandem Nov 08 '14 at 00:27
  • getting following error: [arin@db11 ~]$ python store_tester.py File "store_tester.py", line 54 )) ^ SyntaxError: invalid syntax – a ekandem Nov 08 '14 at 00:37
  • Tried different things but not sure where issue really is. ^ under 2nd ) – a ekandem Nov 08 '14 at 00:38
  • @aekandem Missed square bracket. If the command doesn't work try to print what you have inside of os.system and run from console. – Andrej Nov 08 '14 at 00:44