#!/usr/bin/env python
#
# bb-template.py
#
# Author: Ian Prowell (iprowell@sapient.com)
#
# This is a Python that makes sure that a list of files have been updated
# in a given time period and also makes sure that a list of files has not
# been updated in a given period.  It is based on the Python template
# written by Chris Read, which is available at www.deadcat.net
#

# Modules needed for the template
import sys
import os
import socket
import time
import string
from stat import *

# Put other imports here
#import bob
#from bob import *

# A list of logs that should be updated if feeds are working
success_log = ["/usr/local/audit/auditfile.log" , \
	"/usr/local/apps/debug/debugfile.log" ]

success_yellow = 15
success_red = 30

# A list of logs that should not be updated if feeds are working
error_log = [ "/usr/local/error/errorfile.log" ]

error_yellow = 30
error_red = 15

# Setup a function to calculate the number of seconds since the last
# file update
def seconds_since_changed(file_name):
	value = time.time() - os.stat(file_name)[ST_CTIME]
	return value


#
# Ye Olde Main function. 
#
def main(argv):

# BBPROG SHOULD JUST CONTAIN THE NAME OF THIS FILE
# USEFUL WHEN YOU GET ENVIRONMENT DUMPS TO LOCATE
# THE OFFENDING SCRIPT...

   os.environ['BBPROG'] = 'bb-feeds.py'

# TEST NAME: THIS WILL BECOME A COLUMN ON THE DISPLAY
# IT SHOULD BE AS SHORT AS POSSIBLE TO SAVE SPACE...
# NOTE YOU CAN ALSO CREATE A HELP FILE FOR YOUR TEST
# WHICH SHOULD BE PUT IN www/help/$TEST.html.  IT WILL
# BE LINKED INTO THE DISPLAY AUTOMATICALLY.

   TEST = 'feeds'
   os.environ['TEST'] = TEST

#
# Grab the machine name
#

   MACHINE = socket.gethostname() + ".sapient.com"
   MACHINE = string.replace(MACHINE, '.', ',')

#
# Format date as per the 'date' command...
#

   DATE = time.strftime("%a %b %d %H:%M:%S %Z %Y", time.localtime(time.time()))

#
# Do the test thing. Results are:
#  green: 	Fine
#  yellow:	Little Prob
#  red:		Big Prob
#
# NOTE: In your testing, don't forget to take care of _ALL_ exceptions that
#       may be generated. A nasty exception will wack you into the purple.
# 
# Default is green...
#
 
   error_message = ""
   COLOR = 'green'

# Make sure the success logs have been updated
   for file in success_log[:]:
	minutes_since_update = seconds_since_changed(file) / 60
	if ( minutes_since_update > success_yellow ) :
		if COLOR == 'green' :
			COLOR = 'yellow'
		error_message = error_message + file + " has not been updated in"
 		error_message = error_message + " " + `minutes_since_update`
		error_message = error_message + " minutes" + '\n'
		if ( minutes_since_update > success_red ) :
			COLOR = 'red'
# Make sure that the error logs have not been written to
   for file in error_log[:]:
	minutes_since_update = seconds_since_changed(file) / 60
	if ( minutes_since_update < error_yellow ) :
		if COLOR == 'green' :
			COLOR = 'yellow'
		error_message = error_message + file + " was updated"
		error_message = error_message + " " + `minutes_since_update`
		error_message = error_message + " minutes ago" + '\n'
		if ( minutes_since_update < error_red ) :
			COLOR = 'red'
 
#
# Build the base return string 
#
   if COLOR == 'green' :
   	LINE = "status " + MACHINE + "." + TEST + " " + COLOR + " " + DATE + " feeds tested OK on " + MACHINE
   else :
	LINE = "status " + MACHINE + "." + TEST + " " + COLOR + " " + DATE + " feeds did not test OK on " + MACHINE
#
# Add the merry story of what has happened to the end of the base,
# depending on the COLOR
#

   LINE = LINE + '\n' + error_message

#
# Build the command we need to execute to tell Big Brother what's happening...
#

   cmd = os.environ['BB'] + ' ' + os.environ['BBDISP'] + ' "' + LINE + '"'

#
# Run it all...
#

   os.system(cmd)



#
# Little bit at the end to run it all...
#
if __name__ == '__main__':
   sys.exit(main(sys.argv))
