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