4484a339a5ab9be35ec116d21609f624b91dcd78
[mom.git] / nm_mom.py
1 #!/usr/bin/python
2
3 # Restarts NM if NM does not respond to periodic probes.
4 #
5 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
7 #
8 # $Id: $
9
10
11 import syslog
12 import os
13 import sys
14 import getopt
15 import time
16 import pickle
17
18 import socket
19 import xmlrpclib
20 import bwlimit
21
22 from sets import Set
23
24 # Utility functions
25 from pl_mom import *
26
27
28 def restartNM():
29         try:
30                 pid = os.fork()
31                 if pid == 0:
32                         bwlimit.run("/etc/init.d/pl_nm stop")
33                         try:
34                                 nmpid = bwlimit.run("/bin/ps -e -o pid,cmd | grep nm.py")[0].split(" ")[0]
35                                 os.kill(int(nmpid), 9)
36                                 time.sleep(3)
37                                 print "NM  %s forcefully killed." %nmpid
38                         except:
39                                 print "NM %s killed." %nmpid
40                         bwlimit.run("/etc/init.d/pl_nm restart")
41                         print "NM restarted."
42                         os._exit(0)
43                 else:
44                         os.waitpid(pid,0)
45         except Exception, err:
46                 print "Warning: Exception received while restarting NM:  %s" % err
47
48
49 def main():
50         syslog.openlog("nm_mom")
51         sys.stdout = sys.stderr = Logger()
52         
53         nm = NM()
54         last_maxrate = 0 
55         try:
56                 # Query something.  If it doesnt come back...
57                 vals = nm.query("pl_netflow", [('nm_net_max_rate', last_maxrate)])
58
59         except Exception, err:
60                 print "Warning: Exception received while querying NM:", err
61                 restartNM()
62
63 if __name__ == '__main__':
64     main()