added some new cr
[monitor.git] / reboot.py
1 #!/usr/bin/python
2 #
3 # Reboot specified nodes
4 #
5
6 import getpass, getopt
7 import os, sys
8 import xml, xmlrpclib
9 import errno, time, traceback
10 import urllib2
11 import threading, popen2
12 import array, struct
13 #from socket import *
14 import socket
15 import plc
16
17 plc_lock = threading.Lock()
18
19 # Use our versions of telnetlib and pyssh
20 sys.path.insert(0, os.path.dirname(sys.argv[0]))
21 import telnetlib
22 sys.path.insert(0, os.path.dirname(sys.argv[0]) + "/pyssh")    
23 import pyssh
24
25 # Timeouts in seconds
26 TELNET_TIMEOUT = 30
27
28 # Event class ID from pcu events
29 #NODE_POWER_CONTROL = 3
30
31 # Monitor user ID
32 #MONITOR_USER_ID = 11142
33
34 import logging
35 logger = logging.getLogger("monitor")
36 verbose = 1
37 #dryrun = 0;
38
39 class ExceptionNotFound(Exception): pass
40 class ExceptionPassword(Exception): pass
41 class ExceptionTimeout(Exception): pass
42 class ExceptionPrompt(Exception): pass
43 class ExceptionPort(Exception): pass
44
45 def telnet_answer(telnet, expected, buffer):
46         global verbose
47
48         output = telnet.read_until(expected, TELNET_TIMEOUT)
49         #if verbose:
50         #       logger.debug(output)
51         if output.find(expected) == -1:
52                 raise ExceptionNotFound, "'%s' not found" % expected
53         else:
54                 telnet.write(buffer + "\r\n")
55
56
57 # PCU has model, host, preferred-port, user, passwd, 
58
59 class PCUExpect:
60         def __init__(self, protocol, verbose, dryrun):
61                 self.verbose = verbose
62                 self.protocol = protocol
63                 self.dryrun = dryrun
64
65         def telnet_answer(telnet, expected, buffer):
66                 global verbose
67
68                 output = telnet.read_until(expected, TELNET_TIMEOUT)
69                 #if verbose:
70                 #       logger.debug(output)
71                 if output.find(expected) == -1:
72                         raise ExceptionNotFound, "'%s' not found" % expected
73                 else:
74                         telnet.write(buffer + "\r\n")
75         
76         def _run(self, host, user, passwd, node_port, protocols):
77                 self.run()
78
79         def run(self):
80                 pass
81                 
82         
83
84 def ipal_reboot(ip, password, port, dryrun):
85         global verbose
86         global plc_lock
87
88
89         telnet = None
90
91         try:
92                 #plc_lock.acquire()
93                 #print "lock acquired"
94
95                 #try:
96                         #telnet = telnetlib.Telnet(ip) # , timeout=TELNET_TIMEOUT)
97                 telnet = telnetlib.Telnet(ip, timeout=TELNET_TIMEOUT)
98                 #except:
99                 #       import traceback
100                 #       traceback.print_exc()
101
102
103                 telnet.set_debuglevel(verbose)
104
105                 # XXX Some iPals require you to hit Enter a few times first
106                 telnet_answer(telnet, "Password >", "\r\n\r\n")
107
108                 # Login
109                 telnet_answer(telnet, "Password >", password)
110
111                 # XXX Some iPals require you to hit Enter a few times first
112                 telnet.write("\r\n\r\n")
113
114                 # P# - Pulse relay
115                 if not dryrun:
116                         telnet_answer(telnet, "Enter >", "P%d" % port)
117
118                 telnet.read_until("Enter >", TELNET_TIMEOUT)
119
120                 # Close
121                 telnet.close()
122
123                 #print "lock released"
124                 #plc_lock.release()
125                 return 0
126
127         except EOFError, err:
128                 if verbose:
129                         logger.debug("ipal_reboot: EOF")
130                         logger.debug(err)
131                 telnet.close()
132                 import traceback
133                 traceback.print_exc()
134                 #print "lock released"
135                 #plc_lock.release()
136                 return errno.ECONNRESET
137         except socket.error, err:
138                 logger.debug("ipal_reboot: Socket Error")
139                 logger.debug(err)
140                 import traceback
141                 traceback.print_exc()
142
143                 return errno.ETIMEDOUT
144                 
145         except Exception, err:
146                 if verbose:
147                         logger.debug("ipal_reboot: Exception")
148                         logger.debug(err)
149                 if telnet:
150                         telnet.close()
151                 import traceback
152                 traceback.print_exc()
153                 #print "lock released"
154                 #plc_lock.release()
155                 return  "ipal error"
156
157
158 def apc_reboot(ip, username, password, port, protocol, dryrun):
159         global verbose
160
161         transport = None
162
163         try:
164                 #if "ssh" in protocol:
165                 if "22" in protocol and protocol['22'] == "open":
166                         transport = pyssh.Ssh(username, ip)
167                         transport.open()
168                         # Login
169                         telnet_answer(transport, "password:", password)
170                 #elif "telnet" in protocol:
171                 elif "23" in protocol and protocol['23'] == "open":
172                         transport = telnetlib.Telnet(ip, timeout=TELNET_TIMEOUT)
173                         #transport = telnetlib.Telnet(ip)
174                         transport.set_debuglevel(verbose)
175                         # Login
176                         telnet_answer(transport, "User Name", username)
177                         telnet_answer(transport, "Password", password)
178                 else:
179                         logger.debug("Unknown protocol %s" %protocol)
180                         raise "Closed protocol ports!"
181
182
183                 # 1- Device Manager
184                 # 2- Network
185                 # 3- System
186                 # 4- Logout
187
188                 # 1- Device Manager
189                 telnet_answer(transport, "\r\n> ", "1")
190
191                 # 1- Phase Monitor/Configuration
192                 # 2- Outlet Restriction Configuration
193                 # 3- Outlet Control/Config
194                 # 4- Power Supply Status
195
196                 # 3- Outlet Control/Config
197                 telnet_answer(transport, "\r\n> ", "3")
198
199                 # 1- Outlet 1
200                 # 2- Outlet 2
201                 # ...
202
203                 # n- Outlet n
204                 telnet_answer(transport, "\r\n> ", str(port))
205                 
206                 # 1- Control Outlet
207                 # 2- Configure Outlet
208
209                 # 1- Control Outlet
210                 telnet_answer(transport, "\r\n> ", "1")
211
212                 # 1- Immediate On                         
213                 # 2- Immediate Off                       
214                 # 3- Immediate Reboot             
215                 # 4- Delayed On                         
216                 # 5- Delayed Off                           
217                 # 6- Delayed Reboot                     
218                 # 7- Cancel                                     
219
220                 # 3- Immediate Reboot             
221                 telnet_answer(transport, "\r\n> ", "3")
222
223                 if not dryrun:
224                         telnet_answer(transport, 
225                                 "Enter 'YES' to continue or <ENTER> to cancel", "YES\r\n")
226                         telnet_answer(transport, 
227                                 "Press <ENTER> to continue...", "")
228
229                 # Close
230                 transport.close()
231                 return 0
232
233         except EOFError, err:
234                 if verbose:
235                         logger.debug(err)
236                 if transport:
237                         transport.close()
238                 return errno.ECONNRESET
239         except socket.error, err:
240                 if verbose:
241                         logger.debug(err)
242                 return errno.ETIMEDOUT
243
244         except Exception, err:
245                 import traceback
246                 traceback.print_exc()
247                 if verbose:
248                         logger.debug(err)
249                 if transport:
250                         transport.close()
251                 return "apc error: check password"
252
253 def drac_reboot(ip, username, password, dryrun):
254         global verbose
255         ssh = None
256         try:
257                 ssh = pyssh.Ssh(username, ip)
258                 ssh.set_debuglevel(verbose)
259                 ssh.open()
260                 # Login
261                 print "password"
262                 telnet_answer(ssh, "password:", password)
263
264                 # Testing Reboot ?
265                 print "reset or power"
266                 if dryrun:
267                         telnet_answer(ssh, "[%s]#" % username, "getsysinfo")
268                 else:
269                         # Reset this machine
270                         telnet_answer(ssh, "[%s]#" % username, "serveraction powercycle")
271
272                 print "exit"
273                 telnet_answer(ssh, "[%s]#" % username, "exit")
274
275                 # Close
276                 print "close"
277                 output = ssh.close()
278                 return 0
279
280         except socket.error, err:
281                 print "exception"
282                 import traceback
283                 traceback.print_exc()
284                 if verbose:
285                         logger.debug(err)
286                 if ssh:
287                         output = ssh.close()
288                         if verbose:
289                                 logger.debug(err)
290                 return errno.ETIMEDOUT
291         except Exception, err:
292                 print "exception"
293                 import traceback
294                 traceback.print_exc()
295                 if verbose:
296                         logger.debug(err)
297                 if ssh:
298                         output = ssh.close()
299                         if verbose:
300                                 logger.debug(err)
301                 return "drac error: check password"
302
303 def ilo_reboot(ip, username, password, dryrun):
304         global verbose
305
306         ssh = None
307
308         try:
309                 ssh = pyssh.Ssh(username, ip)
310                 ssh.set_debuglevel(verbose)
311                 ssh.open()
312                 # Login
313                 print "password"
314                 telnet_answer(ssh, "password:", password)
315
316                 # User:vici logged-in to ILOUSE701N7N4.CS.Princeton.EDU(128.112.154.171)
317                 # iLO Advanced 1.26 at 10:01:40 Nov 17 2006
318                 # Server Name: USE701N7N400
319                 # Server Power: On
320                 # 
321                 # </>hpiLO-> 
322                 print "cd system1"
323                 telnet_answer(ssh, "</>hpiLO->", "cd system1")
324
325                 # Reboot Outlet  N        (Y/N)?
326                 print "reset or power"
327                 if dryrun:
328                         telnet_answer(ssh, "</system1>hpiLO->", "POWER")
329                 else:
330                         # Reset this machine
331                         telnet_answer(ssh, "</system1>hpiLO->", "reset")
332
333                 print "exit"
334                 telnet_answer(ssh, "</system1>hpiLO->", "exit")
335
336                 # Close
337                 print "close"
338                 output = ssh.close()
339                 return 0
340
341         except socket.error, err:
342                 print "exception"
343                 import traceback
344                 traceback.print_exc()
345                 if verbose:
346                         logger.debug(err)
347                 if ssh:
348                         output = ssh.close()
349                         if verbose:
350                                 logger.debug(err)
351                 return errno.ETIMEDOUT
352         except Exception, err:
353                 print "exception"
354                 import traceback
355                 traceback.print_exc()
356                 if verbose:
357                         logger.debug(err)
358                 if ssh:
359                         output = ssh.close()
360                         if verbose:
361                                 logger.debug(err)
362                 return "ilo error: check password"
363
364 def baytech_reboot(ip, username, password, port, dryrun):
365         global verbose
366
367         ssh = None
368
369         #verbose = 1 
370         try:
371                 ssh = pyssh.Ssh(username, ip)
372                 ssh.set_debuglevel(verbose)
373                 ssh.open()
374
375                 # Login
376                 telnet_answer(ssh, "password:", password)
377
378                 # PL1 comm output  (2 ,1).........1
379                 # PL2 comm output  (2 ,2).........2
380                 # PL3 comm output  (2 ,3).........3
381                 # no machine       (2 ,4).........4
382                 # Control Outlets  (5 ,1).........5
383                 # Logout..........................T
384
385                 # Control Outlets  (5 ,1).........5
386                 telnet_answer(ssh, "Enter Request :", "5")
387
388                 # Reboot N
389                 try:
390                         telnet_answer(ssh, "DS-RPC>", "Reboot %d" % port)
391                 except ExceptionNotFound, msg:
392                         # one machine is configured to ask for a username,
393                         # even after login...
394                         print "msg: %s" % msg
395                         ssh.write(username + "\r\n")
396                         telnet_answer(ssh, "DS-RPC>", "Reboot %d" % port)
397                         
398
399                 # Reboot Outlet  N        (Y/N)?
400                 if dryrun:
401                         telnet_answer(ssh, "(Y/N)?", "N")
402                 else:
403                         telnet_answer(ssh, "(Y/N)?", "Y")
404                 telnet_answer(ssh, "DS-RPC>", "")
405
406                 # Close
407                 output = ssh.close()
408                 return 0
409
410         except socket.error, err:
411                 print "exception"
412                 import traceback
413                 traceback.print_exc()
414                 if verbose:
415                         logger.debug(err)
416                 if ssh:
417                         output = ssh.close()
418                         if verbose:
419                                 logger.debug(err)
420                 return errno.ETIMEDOUT
421         except Exception, err:
422                 print "exception"
423                 import traceback
424                 traceback.print_exc()
425                 if verbose:
426                         logger.debug(err)
427                 if ssh:
428                         output = ssh.close()
429                         if verbose:
430                                 logger.debug(err)
431                 return "baytech error: check password"
432
433 ### rebooting european BlackBox PSE boxes
434 # Thierry Parmentelat - May 11 2005
435 # tested on 4-ports models known as PSE505-FR
436 # uses http to POST a data 'P<port>=r'
437 # relies on basic authentication within http1.0
438 # first curl-based script was
439 # curl --http1.0 --basic --user <username>:<password> --data P<port>=r \
440 #       http://<hostname>:<http_port>/cmd.html && echo OK
441
442 def bbpse_reboot (pcu_ip,username,password,port_in_pcu,http_port, dryrun):
443
444         global verbose
445
446         url = "http://%s:%d/cmd.html" % (pcu_ip,http_port)
447         data= "P%d=r" % port_in_pcu
448         if verbose:
449                 logger.debug("POSTing '%s' on %s" % (data,url))
450
451         authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
452         uri = "%s:%d" % (pcu_ip,http_port)
453         authinfo.add_password (None, uri, username, password)
454         authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
455
456         opener = urllib2.build_opener(authhandler)
457         urllib2.install_opener(opener)
458
459         if (dryrun):
460                 return 0
461
462         try:
463                 f = urllib2.urlopen(url,data)
464
465                 r= f.read()
466                 if verbose:
467                         logger.debug(r)
468                 return 0
469
470         except urllib2.URLError,err:
471                 logger.info('Could not open http connection', err)
472                 return "bbpse error"
473
474 ### rebooting x10toggle based systems addressed by port
475 # Marc E. Fiuczynski - May 31 2005
476 # tested on 4-ports models known as PSE505-FR
477 # uses ssh and password to login to an account
478 # that will cause the system to be powercycled.
479
480 def x10toggle_reboot(ip, username, password, port, dryrun):
481         global verbose
482
483         ssh = None
484         try:
485                 ssh = pyssh.Ssh(username, ip)
486                 ssh.open()
487
488                 # Login
489                 telnet_answer(ssh, "password:", password)
490
491                 if not dryrun:
492                         # Reboot
493                         telnet_answer(ssh, "x10toggle>", "A%d" % port)
494
495                 # Close
496                 output = ssh.close()
497                 if verbose:
498                         logger.debug(output)
499                 return 0
500
501         except Exception, err:
502                 if verbose:
503                         logger.debug(err)
504                 if ssh:
505                         output = ssh.close()
506                         if verbose:
507                                 logger.debug(output)
508                 return errno.ETIMEDOUT
509
510 ### rebooting Dell systems via RAC card
511 # Marc E. Fiuczynski - June 01 2005
512 # tested with David Lowenthal's itchy/scratchy nodes at UGA
513 #
514
515 def runcmd(command, args, username, password, timeout = None):
516
517         result = [None]
518         result_ready = threading.Condition()
519
520         def set_result(x):
521
522                 result_ready.acquire()
523                 try:
524                         result[0] = x
525                 finally:
526                         result_ready.notify()
527                         result_ready.release()
528
529         def do_command(command, username, password):
530
531                 try:
532                         # Popen4 is a popen-type class that combines stdout and stderr
533                         p = popen2.Popen4(command)
534
535                         # read all output data
536                         p.tochild.write("%s\n" % username)
537                         p.tochild.write("%s\n" % password)
538                         p.tochild.close()
539                         data = p.fromchild.read()
540
541                         while True:
542                                 # might get interrupted by a signal in poll() or waitpid()
543                                 try:
544                                         retval = p.wait()
545                                         set_result((retval, data))
546                                         break
547                                 except OSError, ex:
548                                         if ex.errno == errno.EINTR:
549                                                 continue
550                                         raise ex
551                 except Exception, ex:
552                         set_result(ex)
553
554         if args:
555                 command = " ".join([command] + args)
556
557         worker = threading.Thread(target = do_command, args = (command, username, password, ))
558         worker.setDaemon(True)
559         result_ready.acquire()
560         worker.start()
561         result_ready.wait(timeout)
562         try:
563                 if result == [None]:
564                         raise Exception, "command timed-out: '%s'" % command
565         finally:
566                 result_ready.release()
567         result = result[0]
568
569         if isinstance(result, Exception):
570                 raise result
571         else:
572                 (retval, data) = result
573                 if os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == 0:
574                         return data
575                 else:
576                         out = "system command ('%s') " % command
577                         if os.WIFEXITED(retval):
578                                 out += "failed, rc = %d" % os.WEXITSTATUS(retval)
579                         else:
580                                 out += "killed by signal %d" % os.WTERMSIG(retval)
581                         if data:
582                                 out += "; output follows:\n" + data
583                         raise Exception, out
584
585 def racadm_reboot(ip, username, password, port, dryrun):
586         global verbose
587
588         try:
589                 cmd = "/usr/sbin/racadm"
590                 os.stat(cmd)
591                 if not dryrun:
592                         output = runcmd(cmd, ["-r %s -i serveraction powercycle" % ip],
593                                 username, password)
594                 else:
595                         output = "dryrun of racadm command"
596
597                 logger.debug("runcmd returned without output %s" % output)
598                 if verbose:
599                         logger.debug(output)
600                 return 0
601
602         except Exception, err:
603                 logger.debug("runcmd raised exception %s" % err)
604                 if verbose:
605                         logger.debug(err)
606                 return errno.ETIMEDOUT
607
608 def pcu_name(pcu):
609         if pcu['hostname'] is not None and pcu['hostname'] is not "":
610                 return pcu['hostname']
611         elif pcu['ip'] is not None and pcu['ip'] is not "":
612                 return pcu['ip']
613         else:
614                 return None
615
616 def get_pcu_values(pcu_id):
617         # TODO: obviously, this shouldn't be loaded each time...
618         import soltesz
619         fb =soltesz.dbLoad("findbadpcus")
620
621         try:
622                 values = fb['nodes']["id_%s" % pcu_id]['values']
623         except:
624                 values = None
625
626         return values
627
628 def reboot_new(nodename, continue_probe, dryrun):
629
630         pcu = plc.getpcu(nodename)
631         if not pcu:
632                 return False
633
634         values = get_pcu_values(pcu['pcu_id'])
635         if values == None:
636                 return False
637         
638         # Try the PCU first
639         logger.debug("Trying PCU %s %s" % (pcu['hostname'], pcu['model']))
640
641         # DataProbe iPal (many sites)
642         if  continue_probe and values['model'].find("Dataprobe IP-41x/IP-81x") >= 0:
643                 if values['portstatus']['23'] == "open":
644                         rb_ret = ipal_reboot(pcu_name(values),
645                                                                         values['password'],
646                                                                         pcu[nodename],
647                                                                         dryrun)
648                 else:
649                         rb_ret = "Unsupported_Port"
650                         
651
652         # APC Masterswitch (Berkeley)
653         elif continue_probe and values['model'].find("APC AP79xx/Masterswitch") >= 0:
654                 if  values['portstatus']['22'] == "open" or \
655                         values['portstatus']['23'] == "open":
656                         rb_ret = apc_reboot(pcu_name(values),
657                                                                         values['username'],
658                                                                         values['password'], 
659                                                                         pcu[nodename],
660                                                                         values['portstatus'], 
661                                                                         dryrun)
662                 else:
663                         rb_ret = "Unsupported_Port"
664         # BayTech DS4-RPC
665         elif continue_probe and values['model'].find("Baytech DS4-RPC") >= 0:
666                 if values['portstatus']['22'] == "open":
667                         rb_ret = baytech_reboot(pcu_name(values),
668                                                                            values['username'],
669                                                                            values['password'], 
670                                                                            pcu[nodename],
671                                                                            dryrun)
672                 else:
673                         rb_ret = "Unsupported_Port"
674                         
675
676         # iLO
677         elif continue_probe and values['model'].find("HP iLO") >= 0:
678                 if values['portstatus']['22'] == "open":
679                         rb_ret = ilo_reboot(pcu_name(values),
680                                                                            values['username'],
681                                                                            values['password'], 
682                                                                            dryrun)
683                 else:
684                         rb_ret = "Unsupported_Port"
685                         
686         # DRAC ssh
687         elif continue_probe and values['model'].find("Dell RAC") >= 0:
688                 if values['portstatus']['22'] == "open":
689                         rb_ret = drac_reboot(pcu_name(values),
690                                                                            values['username'],
691                                                                            values['password'], 
692                                                                            dryrun)
693                 else:
694                         rb_ret = "Unsupported_Port"
695                         
696
697         # BlackBox PSExxx-xx (e.g. PSE505-FR)
698         elif continue_probe and \
699                 (values['model'].find("BlackBox PS5xx") >= 0 or
700                  values['model'].find("ePowerSwitch 1/4/8x") >=0 ):
701                 if values['portstatus']['80'] == "open":
702                         rb_ret = bbpse_reboot(pcu_name(values),
703                                                         values['username'], 
704                                                         values['password'], 
705                                                         pcu[nodename],
706                                                         80,
707                                                         dryrun)
708                 else:
709                         rb_ret = "Unsupported_PCU"
710                         
711         # x10toggle
712         elif    continue_probe and values['protocol'] == "ssh" and \
713                         values['model'] == "x10toggle":
714                 rb_ret = x10toggle_reboot(pcu_name(values),
715                                                                                 values['username'],
716                                                                                 values['password'], 
717                                                                                 pcu[nodename],
718                                                                                 dryrun)
719         # ????
720         elif continue_probe and values['protocol'] == "racadm" and \
721                         values['model'] == "RAC":
722                 rb_ret = racadm_reboot(pcu_name(values),
723                                                                           values['username'],
724                                                                           values['password'],
725                                                                           pcu[nodename],
726                                                                           dryrun)
727         elif continue_probe:
728                 rb_ret = "Unsupported_PCU"
729
730         elif continue_probe == False:
731                 if 'portstatus' in values:
732                         rb_ret = "NetDown"
733                 else:
734                         rb_ret = "Not_Run"
735         else:
736                 rb_ret = -1
737         
738         if rb_ret != 0:
739                 return False
740         else:
741                 return True
742
743
744 # Returns true if rebooted via PCU
745 def reboot(nodename, dryrun):
746         pcu = plc.getpcu(nodename)
747         if not pcu:
748                 plc.nodePOD(nodename)
749                 return False
750         # Try the PCU first
751         logger.debug("Trying PCU %s %s" % (pcu['hostname'], pcu['model']))
752
753         # APC Masterswitch (Berkeley)
754         if pcu['model'] == "APC Masterswitch":
755                 err = apc_reboot(pcu['ip'], pcu['username'],pcu['password'], 
756                                 pcu[nodename], pcu['protocol'], dryrun)
757
758         # DataProbe iPal (many sites)
759         elif pcu['protocol'] == "telnet" and pcu['model'].find("IP-4") >= 0:
760                 err = ipal_reboot(pcu['ip'],pcu['password'], pcu[nodename], dryrun)
761
762         # BayTech DS4-RPC
763         elif pcu['protocol'] == "ssh" and \
764         (pcu['model'].find("Baytech") >= 0 or pcu['model'].find("DS4") >= 0):
765                 err = baytech_reboot(pcu['ip'], pcu['username'],pcu['password'], pcu[nodename], dryrun)
766
767         # BlackBox PSExxx-xx (e.g. PSE505-FR)
768         elif pcu['protocol'] == "http" and (pcu['model'] == "bbpse"):
769                 err = bbpse_reboot(pcu['ip'], pcu['username'], pcu['password'], pcu[nodename],80, dryrun)
770
771         # x10toggle
772         elif pcu['protocol'] == "ssh" and (pcu['model'] == "x10toggle"):
773                 err = x10toggle_reboot(pcu['ip'], pcu['username'],pcu['password'], pcu[nodename], dryrun)
774
775         # 
776         elif pcu['protocol'] == "racadm" and (pcu['model'] == "RAC"):
777                 err = racadm_reboot(pcu['ip'], pcu['username'],pcu['password'], pcu_[nodename], dryrun)
778
779         # Unknown or unsupported
780         else:
781                 err = errno.EPROTONOSUPPORT
782                 return False
783         return True 
784
785 #def get_suggested(suggestion_id,db):
786 #
787 #       sql= """select node_id,pcu_id from nodes where suggestion = %d """\
788 #                       % (suggestion_id)
789 #       try:
790 #               nodes = db.query(sql).dictresult()
791 #       except pg.ProgrammingError, err:
792 #               print( "Database error for query: %s\n%s" % (sql,err) )
793 #               sys.exit(1)
794 #       return nodes
795
796 #def get_pcu_info(node_id,pcu_id,db):
797 #       sql= """select port_number from pcu_ports where node_id = %d and pcu_id = %d """\
798 #                       % (node_id,pcu_id)
799 #       try:
800 #          port_number = db.query(sql).dictresult()
801 #       except pg.ProgrammingError, err:
802 #               print( "Database error for query: %s\n%s" % (sql,err) )
803 #               sys.exit(1)
804 #       
805 #       sql= """select * from pcu where pcu_id = %d """\
806 #                       % (pcu_id)
807 #       try:
808 #               pcu = db.query(sql).dictresult()
809 #       except pg.ProgrammingError, err:
810 #               print( "Database error for query: %s\n%s" % (sql,err) )
811 #               sys.exit(1)
812 #
813 #       result = {'node_id':node_id,'pcu_id':pcu_id,'port_number':port_number[0]['port_number'], 
814 #                         'ip':pcu[0]['ip'],'username':pcu[0]['username'],'password':pcu[0]['password'],\
815 #                         'model':pcu[0]['model'],'protocol':pcu[0]['protocol'],'hostname':pcu[0]['hostname']}
816 #
817 #       return result
818
819 #def add_plc_event(node_id,err,db):
820 #       site_id = plc_db_utils.get_site_from_node_id(node_id,db)
821 #       message = "PCU reboot by monitor-msgs@planet-lab.org: %s" % os.strerror(err)
822 #
823 #       sql = """insert into events (event_class_id,message,person_id,node_id,site_id) values """\
824 #                 """(%d,'%s',%d,%d,%d)""" % (NODE_POWER_CONTROL,message,MONITOR_USER_ID,node_id,site_id)
825 #       print sql
826 #
827 #       try:
828 #               db.query(sql)
829 #       except pg.ProgrammingError, err:
830 #               print( "Database error for: %s\n%s" % (sql,err) )
831 #               sys.exit(1)
832
833
834 def main():
835         logger.setLevel(logging.DEBUG)
836         ch = logging.StreamHandler()
837         ch.setLevel(logging.DEBUG)
838         formatter = logging.Formatter('LOGGER - %(message)s')
839         ch.setFormatter(formatter)
840         logger.addHandler(ch)
841
842
843         try:
844                 reboot("planetlab2.cs.uchicago.edu")
845                 reboot("alice.cs.princeton.edu")
846         except Exception, err:
847                 print err
848         # used later for pretty printing
849 #       pp = pprint.PrettyPrinter(indent=2)
850
851 #       user = "Monitor"
852 #       password = None
853
854 #       plc_db = plc_dbs.open_plc_db_write()
855 #       mon_db = plc_dbs.open_mon_db()
856
857         # 5 = needs script reboot - fix this later
858 #       nodes = get_suggested(5,mon_db)
859
860 #       for row in nodes:
861                 
862 #               pcu = get_pcu_info(row['node_id'],row['pcu_id'],plc_db)
863 #               add_plc_event(row['node_id'],err,plc_db)
864
865 if __name__ == '__main__':
866         import plc
867         logger = logging.getLogger("monitor")
868         main()