34745c830ffe2ff84c79f5517684436455e4a167
[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 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),h.date_created+timedelta(0,h.expires) < datetime.now() and h.expires != 0) for h in hostnames_q ]
47         #loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires)) for h in loginbases_q ]
48         loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires),h.date_created+timedelta(0,h.expires) < datetime.now() and h.expires != 0) for h in loginbases_q ]
49
50         if config.add:
51                 print "Blacklisting nodes: ", l_nodes
52                 for host in l_nodes:
53                         if host not in hostnames:
54                                 print "adding to blacklist %s" % host
55                                 bl = BlacklistRecord(hostname=host, expires=int(config.expires))
56                                 bl.flush()
57
58                 if loginbase:
59                         print "Blacklisting site: ", config.site
60                         if config.site not in loginbases:
61                                 print "adding to blacklist %s" % config.site
62                                 bl = BlacklistRecord(loginbase=config.site, expires=int(config.expires))
63                                 bl.flush()
64
65         elif config.delete:
66                 print "Deleting nodes: %s" % l_nodes
67                 for h in l_nodes:
68                         bl = BlacklistRecord.get_by(hostname=h)
69                         if bl: bl.delete()
70                 if config.site:
71                         print "Deleting site: %s" % config.site
72                         bl = BlacklistRecord.get_by(loginbase=config.site)
73                         if bl: bl.delete()
74         else:
75                 # default option is to list
76                 if loginbase:
77                         objlist = loginbases_exp
78                 else:
79                         objlist = hostnames_exp
80
81                 for i in objlist:
82                         if i[2]:
83                                 print i[0], i[1], "<-- expired"
84                         elif i[1] > datetime.now():
85                                 print i[0], i[1]
86                         else:
87                                 print i[0]
88                         
89         session.flush()
90         
91 if __name__ == '__main__':
92         import os
93         #try:
94         main()
95         #except Exception, error:
96         #       print "Exception %s" % error
97         #       sys.exit(0)