lemmon_tools.py
Go to the documentation of this file.
1 # SERVERS = ["lem.hep.caltech.edu", "ec2-52-10-107-248.us-west-2.compute.amazonaws.com", "52.10.107.248"]
2 SERVERS = ["lem.hep.caltech.edu"]
3 PORTS = range(1024, 1032)
4 ###########################
5 # machine information
7  status = {}
8  import socket, time
9  machine = socket.gethostname()
10  status["machine"] = machine
11  this_time = time.localtime()
12  time_str = time.strftime("%Y-%m-%d %H:%M:%S", this_time)
13  status["time"] = time_str
14  status["year"] = this_time.tm_year
15  status["month"] = this_time.tm_mon
16  status["day"] = this_time.tm_mday
17  status["hour"] = this_time.tm_hour
18  status["minute"] = this_time.tm_min
19  status["second"] = this_time.tm_sec
20  return status
21 ###########################
22 # run LEM status check
23 def runStatusCheck(MAX_ATTEMPTS=3, verbose=False):
24  status = runMachineCheck()
25  import urllib2
26  results = {}
27  for server in SERVERS:
28  for port in PORTS:
29  url = "http://%s:%i/status"%(server,port)
30  results[url] = {"status" :False,
31  "status_text" :False,
32  "exception" :False,
33  "exception_text":False}
34  if verbose: print "lem : determining status of %s"%url
35  attempts = 0
36  while (attempts < MAX_ATTEMPTS):
37  try:
38  response = urllib2.urlopen(url)
39  content = response.read()
40  if verbose: print "lem : ---\n%s\nlem : ---\n"%content
41  results[url]["status"] = "good"
42  results[url]["status_text"] = content
43  results[url]["exception"] = False
44  results[url]["exception_text"] = False
45  break
46  except urllib2.URLError as exception:
47  attempts += 1
48  if verbose:
49  print "lem : attempt[%i/%i] failed"%(attempts,MAX_ATTEMPTS)
50  print "lem : exception type: ",type(exception)
51  print "lem : ---\n%s\nlem : ---\n"%str(exception.reason)
52  results[url]["status"] = "bad"
53  results[url]["status_text"] = False
54  results[url]["exception"] = str(type(exception))
55  results[url]["exception_text"] = str(exception.reason)
56  status["results"] = results
57  return status
58 ###########################
59 # append a status to a file
60 def appendToFile(status, prefix="/nusoft/app/web/htdoc/nova/lem/"):
61  import os, json
62  output_name = "%s/lemmon.json"%(prefix)
63  if not os.path.exists(output_name):
64  print "tools: output file %s doesn't exist, making a fresh file"%output_name
65  statuses = {}
66  statuses["servers"] = SERVERS
67  statuses["ports"] = PORTS
68  statuses["machine"] = []
69  statuses["time"] = []
70  statuses["year"] = []
71  statuses["month"] = []
72  statuses["day"] = []
73  statuses["hour"] = []
74  statuses["minute"] = []
75  statuses["second"] = []
76  statuses["results"] = []
77  else:
78  print "tools: loading output file"
79  statuses = json.load(open(output_name,"r"))
80  print "tools: Appending to list of %i statuses"%len(statuses["time"])
81  statuses = appendStatus(statuses, status)
82  with open(output_name, "w") as outfile:
83  json.dump(statuses, outfile)
84  print "tools: written to %s"%output_name
85 ###########################
86 # append status to statuses
87 def appendStatus(history, new):
88  history["machine"].append( new["machine"])
89  history["time"].append( new["time"])
90  history["year"].append( new["year"])
91  history["month"].append( new["month"])
92  history["day"].append( new["day"])
93  history["hour"].append( new["hour"])
94  history["minute"].append( new["minute"])
95  history["second"].append( new["second"])
96  history["results"].append( new["results"])
97  return history
def appendStatus(history, new)
append status to statuses
Definition: lemmon_tools.py:87
void append()
Definition: append.C:24
def runMachineCheck()
machine information
Definition: lemmon_tools.py:6
def appendToFile(status, prefix="/nusoft/app/web/htdoc/nova/lem/")
append a status to a file
Definition: lemmon_tools.py:60
procfile open("FD_BRL_v0.txt")
def runStatusCheck(MAX_ATTEMPTS=3, verbose=False)
run LEM status check
Definition: lemmon_tools.py:23