clearer names for actions, and infer actions better
[monitor.git] / commands / blacklist.py
1 #!/usr/bin/python
2
3 import os
4 import sys
5 import string
6 import time
7 from monitor import common
8 from monitor import database
9 from monitor.database.info.model import *
10 import getopt
11
12 def usage():
13         print "blacklist.py --delete=<i>" 
14
15 def main():
16         from monitor import parser as parsermodule
17         parser = parsermodule.getParser(['nodesets'])
18
19         parser.set_defaults( expires=0, delete=False, add=False, list=True, loginbase=False)
20         parser.add_option("", "--expires", dest="expires", 
21                                                 help="Set expiration time for blacklisted objects (in seconds)" )
22         parser.add_option("", "--delete", dest="delete", action="store_true",
23                                                 help="Remove objects from blacklist" )
24         parser.add_option("", "--list", dest="list", action="store_true",
25                                                 help="List objects in blacklist" )
26         parser.add_option("", "--add", dest="add", action="store_true",
27                                                 help="List objects in blacklist" )
28         parser.add_option("", "--loginbase", dest="loginbase", action="store_true",
29                                                 help="List objects in blacklist" )
30
31         config = parsermodule.parse_args(parser)
32
33         l_nodes = common.get_nodeset(config)
34         if config.site is None:
35                 loginbase = False
36                 if config.loginbase: loginbase=True
37         else:
38                 loginbase = True
39                 print "Blacklisting site:", config.site
40
41
42         hostnames_q = BlacklistRecord.getHostnameBlacklist()
43         loginbases_q = BlacklistRecord.getLoginbaseBlacklist()
44         hostnames  = [ h.hostname for h in hostnames_q ]
45         loginbases = [ h.loginbase for h in loginbases_q ]
46         hostnames_exp  = [ (h.hostname,h.date_created+timedelta(0,h.expires)) for h in hostnames_q ]
47         loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires)) for h in loginbases_q ]
48
49         if config.add:
50                 print "Blacklisting nodes: ", l_nodes
51                 for host in l_nodes:
52                         if host not in hostnames:
53                                 print "adding to blacklist %s" % host
54                                 bl = BlacklistRecord(hostname=host, expires=int(config.expires))
55                                 bl.flush()
56
57                 if loginbase:
58                         print "Blacklisting site: ", config.site
59                         if config.site not in loginbases:
60                                 print "adding to blacklist %s" % config.site
61                                 bl = BlacklistRecord(loginbase=config.site, expires=int(config.expires))
62                                 bl.flush()
63
64         elif config.delete:
65                 print "Deleting nodes: %s" % l_nodes
66                 for h in l_nodes:
67                         bl = BlacklistRecord.get_by(hostname=h)
68                         if bl: bl.delete()
69                 if config.site:
70                         print "Deleting site: %s" % config.site
71                         bl = BlacklistRecord.get_by(loginbase=config.site)
72                         if bl: bl.delete()
73         else:
74                 # default option is to list
75                 if loginbase:
76                         objlist = loginbases_exp
77                 else:
78                         objlist = hostnames_exp
79
80                 for i in objlist:
81                         if i[1] > datetime.now():
82                                 print i[0], i[1]
83                         else:
84                                 print i[0]
85                         
86         session.flush()
87         
88 if __name__ == '__main__':
89         import os
90         #try:
91         main()
92         #except Exception, error:
93         #       print "Exception %s" % error
94         #       sys.exit(0)