Package invocations :: Package monitoring :: Module Monitor
[hide private]

Source Code for Module invocations.monitoring.Monitor

  1  #! /usr/bin/env python 
  2  #------------------------------------------------------------------------------ 
  3  #$Id: Monitor.py 6501 2009-12-11 14:50:40Z EckhardSutorius $ 
  4  """ 
  5     Monitors system load and usage. 
  6   
  7     @author: E.T.W. Sutorius 
  8     @org:    WFAU, IfA, University of Edinburgh 
  9   
 10     @newfield contributors: Contributors, Contributors (Alphabetical Order) 
 11     @contributors: J. Bryant 
 12  """ 
 13  #------------------------------------------------------------------------------ 
 14  import getopt 
 15  import os 
 16  from   random import randint 
 17  import sys 
 18  from   time   import sleep 
 19   
 20  from   wsatools.Logger      import Logger 
 21  import wsatools.SystemConstants as sysc 
 22  import wsatools.Utilities       as utils 
 23  #------------------------------------------------------------------------------ 
 24   
25 -def usage():
26 print("""Usage: 27 Monitor.py [-l/--load][-p/--ps][-t/--trace] [size] 28 [-l/--load] invokes 'top' to get the load average 29 [-p/--ps] invokes 'ps' 30 [-t/--trace] invokes 'traceroute' with [size] (def: 1024) 31 [-w/--writes] writes to the screen called without any of l/p/t invokes all 32 """)
33 34 #------------------------------------------------------------------------------ 35
36 -def traceroute(connection, packetsize):
37 """traceroute -l -z 1000 apm7.ast.cam.ac.uk 1024 38 """ 39 Logger.addMessage("running traceroute to " + connection.split('@')[1], 40 alwaysLog=False) 41 42 cmd = "/khufu/scos/sandbox/WSA/src/curation/invocations/monitoring/trac.sh" 43 44 return os.popen(cmd + " %s" % packetsize).readlines()
45 46 #------------------------------------------------------------------------------ 47
48 -def get_ps(connection):
49 """ps -e -o pid,user,pcpu,etime,comm --sort pcpu 50 """ 51 Logger.addMessage("running ps at " + connection, alwaysLog=False) 52 53 cmd = "ssh %s 'ps -e -o user,pcpu,etime,comm --sort pcpu'" % connection 54 55 return os.popen(cmd).readlines()
56 57 #------------------------------------------------------------------------------ 58
59 -def get_load(connection):
60 """uptime 61 """ 62 Logger.addMessage("running uptime at" + connection, alwaysLog=False) 63 64 return os.popen("ssh %s 'uptime'" % connection).readline()[1:]
65 66 #------------------------------------------------------------------------------ 67 # Entry point for script. 68 69 # Allow module to be imported as well as executed from the command line 70 if __name__ == "__main__": 71 Logger.isVerbose = False 72 73 # read the arguments 74 try: 75 opts, args = getopt.getopt(sys.argv[1:], "lptw", 76 ["load","ps","trace","write"]) 77 if len(args) > 1: 78 raise getopt.GetoptError 79 except getopt.GetoptError: 80 # print help information and exit: 81 print("getopt Error: " + repr(sys.argv)) 82 usage() 83 exit() 84 85 packetsize = (args[0] if len(args) == 1 else 1024) 86 lflag = any(o in ("-l", "--load") for o, a in opts) 87 pflag = any(o in ("-p", "--ps") for o, a in opts) 88 tflag = any(o in ("-t", "--trace") for o, a in opts) 89 allflag = not any((lflag, pflag, tflag)) 90 screenflag = any(o in ("-w", "--write") for o, a in opts) 91 92 timestamp = utils.makeMssqlTimeStamp() 93 logFile = os.path.join(sysc.logFilePath(), "monitor", 94 "monitor_" + timestamp[:timestamp.find(':')]+".log") 95 96 connectto = sysc.scp_user() + "@" + sysc.scp_server() 97 98 toff = randint(0,2700) 99 sleep(toff) 100 101 # traceroute 102 if tflag or allflag: 103 trace = traceroute(connectto, packetsize) 104 105 # ps 106 if pflag or allflag: 107 ps = get_ps(connectto) 108 109 # load 110 if lflag or allflag: 111 load = get_load(connectto) 112 113 # log 114 if not screenflag: 115 fobj = open(logFile, mode='a') 116 fobj.write("# "+utils.makeMssqlTimeStamp()+"\n") 117 if tflag or allflag: 118 fobj.write("# traceroute\n") 119 for line in trace: 120 fobj.write(line) 121 if pflag or allflag: 122 fobj.write("# ps\n") 123 for line in ps: 124 fobj.write(line) 125 if lflag or allflag: 126 fobj.write("# uptime\n") 127 for line in load: 128 fobj.write(line) 129 fobj.close() 130 else: 131 if tflag or allflag: 132 for line in trace: 133 print line[:-1] 134 if pflag or allflag: 135 for line in ps: 136 print line[:-1] 137 if lflag or allflag: 138 for line in load: 139 print line[:-1] 140 141 #------------------------------------------------------------------------------ 142 # Change log: 143 # 144 # 7-Apr-2006, ETWS: Original version. 145 # 7-Apr-2006, JB: Cosmetic changes to code and output. 146 # 10-Apr-2006, JB: Removed "import mx.DateTime as mxt" as it was not 147 # used and causing an error 148 # 9-May-2006, RSC: Simplified return type of system constant logFilePath() 149 # 30-Jun-2006 JB: Editted to work as a cron job (hard-coded paths etc.) 150