clean up
[monitor.git] / moncommands.py
1 import os
2
3 DEBUG= 0
4
5 class ExceptionTimeout(Exception): pass
6 COMMAND_TIMEOUT = 60
7 ssh_options = { 'StrictHostKeyChecking':'no', 
8                                 'BatchMode':'yes', 
9                                 'PasswordAuthentication':'no',
10                                 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
11 from select import select 
12 import subprocess
13 import signal
14
15 class Sopen(subprocess.Popen):
16         def kill(self, signal = signal.SIGTERM):
17                 os.kill(self.pid, signal)
18
19 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
20         lin, lout, lerr = select([stream], [], [], timeout)
21         if len(lin) == 0:
22                 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
23
24         return stream.read(count)
25
26 class CMD:
27         def __init__(self):
28                 pass
29
30         def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
31
32                 #print "CMD.run_noexcept(%s)" % cmd
33                 try:
34                         return CMD.run(self,cmd,timeout)
35                 except ExceptionTimeout:
36                         import traceback; print traceback.print_exc()
37                         return ("", "SCRIPTTIMEOUT")
38                         
39         def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
40                 (o,e) = self.run(cmd, timeout)
41                 self.output = o
42                 self.error = e
43                 if self.s.returncode is None:
44                         self.s.wait()
45                 return self.s.returncode
46
47         def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
48
49                 #print "CMD.run(%s)" % cmd
50                 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
51                 self.s = s
52                 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
53                 #print "calling select(%s)" % timeout
54                 lout, lin, lerr = select([f_out], [], [f_err], timeout)
55                 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
56                 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
57                         # Reached a timeout!  Nuke process so it does not hang.
58                         #print "KILLING"
59                         s.kill(signal.SIGKILL)
60                         raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
61                 else:
62                         #print "RETURNING"
63                         #print len(lin), len(lout), len(lerr)
64                         pass
65
66                 o_value = ""
67                 e_value = ""
68
69                 o_value = f_out.read()
70                 e_value = f_err.read()
71
72                 #print "striping output"
73                 o_value = o_value.strip()
74                 e_value = e_value.strip()
75
76                 #print "OUTPUT -%s-%s-" % (o_value, e_value)
77
78                 #print "closing files"
79                 f_out.close()
80                 f_in.close()
81                 f_err.close()
82                 try:
83                         #print "s.kill()"
84                         s.kill()
85                         #print "after s.kill()"
86                 except OSError:
87                         # no such process, due to it already exiting...
88                         pass
89
90                 #print o_value, e_value
91                 return (o_value, e_value)
92
93         def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
94
95                 #print "CMD.run(%s)" % " ".join(args)
96                 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
97                 self.s = s
98                 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
99                 lout, lin, lerr = select([f_out], [], [f_err], timeout)
100                 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
101                         # Reached a timeout!  Nuke process so it does not hang.
102                         s.kill(signal.SIGKILL)
103                         raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
104                 o_value = f_out.read()
105                 e_value = ""
106                 if o_value == "":       # An error has occured
107                         e_value = f_err.read()
108
109                 o_value = o_value.strip()
110                 e_value = e_value.strip()
111
112                 f_out.close()
113                 f_in.close()
114                 f_err.close()
115                 try:
116                         s.kill()
117                 except OSError:
118                         # no such process, due to it already exiting...
119                         pass
120
121                 return (o_value, e_value)
122
123
124 class SSH(CMD):
125         def __init__(self, user, host, port=22, options = ssh_options):
126                 self.options = options
127                 self.user = user
128                 self.host = host
129                 self.port = port
130                 return
131
132         def __options_to_str(self):
133                 options = ""
134                 for o,v in self.options.iteritems():
135                         options = options + "-o %s=%s " % (o,v)
136                 return options
137
138         def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
139                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
140                                                                         self.user, self.host, cmd)
141                 #print "SSH.run(%s)" % cmd
142                 return CMD.run(self, cmd, timeout)
143
144         def get_file(self, rmt_filename, local_filename=None):
145                 if local_filename == None:
146                         local_filename = "./"
147                 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(), 
148                                                                         self.user, self.host, 
149                                                                         rmt_filename, local_filename)
150                 # output :
151                 #       errors will be on stderr,
152                 #   success will have a blank stderr...
153                 return CMD.run_noexcept(self, cmd)
154
155         def run_noexcept(self, cmd):
156                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
157                                                                         self.user, self.host, cmd)
158                 #print "SSH.run_noexcept(%s)" % cmd
159                 return CMD.run_noexcept(self, cmd)
160
161         def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
162                 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(), 
163                                                                         self.user, self.host, cmd)
164                 #print "SSH.run_noexcept2(%s)" % cmd
165                 r = CMD.run_noexcept(self, cmd, timeout)
166
167                 # XXX: this may be resulting in deadlocks... not sure.
168                 #if self.s.returncode is None:
169                 #       #self.s.kill()
170                 #       self.s.kill(signal.SIGKILL)
171                 #       self.s.wait()
172                 #       self.ret = self.s.returncode
173                 self.ret = -1
174
175                 return r
176
177         def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
178                 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(), 
179                                                                         self.user, self.host, cmd)
180                 #print "SSH.system2(%s)" % cmd
181                 return CMD.system(self, cmd, timeout)
182
183         def runE(self, cmd):
184                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
185                                                                         self.user, self.host, cmd)
186                 if ( DEBUG == 1 ):
187                         print cmd,
188                 (f_in, f_out, f_err) = os.popen3(cmd)
189
190                 value = f_out.read()
191                 if value == "": # An error has occured
192                         value = f_err.read()
193                         value = value.strip()
194
195                 if ( DEBUG == 1 ):
196                         print " == %s" % value
197                 f_out.close()
198                 f_in.close()
199                 f_err.close()
200                 return value.strip()
201                 
202 import time
203 class MyTimer:
204         def __init__(self):
205                 self.start = time.time()
206
207         def end(self):
208                 self.end = time.time()
209                 t = self.end-self.start
210                 return t
211
212         def diff(self):
213                 self.end = time.time()
214                 t = self.end-self.start
215                 self.start = self.end
216                 return t