added --node option throughout calls
[monitor.git] / blacklist.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7 from monitor import database
8 from monitor.database.info.model import *
9 import getopt
10
11 def usage():
12         print "blacklist.py --delete=<i>" 
13
14 def main():
15
16         try:
17                 longopts = ["delete=", "help"]
18                 (opts, argv) = getopt.getopt(sys.argv[1:], "d:h", longopts)
19         except getopt.GetoptError, err:
20                 print "Error: " + err.msg
21                 sys.exit(1)
22
23         blacklist = BlacklistRecord.query.all()
24         hostnames = [ h.hostname for h in blacklist ]
25
26         for (opt, optval) in opts:
27                 if opt in ["-d", "--delete"]:
28                         i = optval
29                         bl = BlacklistRecord.get_by(hostname=i)
30                         bl.delete()
31                 else:
32                         usage()
33                         sys.exit(0)
34
35         i_cnt = 0
36         for i in blacklist:
37                 print i.hostname
38                 i_cnt += 1
39
40
41         while 1:
42                 line = sys.stdin.readline()
43                 if not line:
44                         break
45                 line = line.strip()
46                 if line not in hostnames:
47                         bl = BlacklistRecord(hostname=line)
48                         bl.flush()
49                         i_cnt += 1
50
51         session.flush()
52         print "Total %d nodes in blacklist" % (i_cnt)
53         
54 if __name__ == '__main__':
55         import os
56         #try:
57         main()
58         #except Exception, error:
59         #       print "Exception %s" % error
60         #       sys.exit(0)