7 from monitor import common
8 from monitor import database
9 from monitor.database.info.model import *
13 print "blacklist.py --delete=<i>"
16 from monitor import parser as parsermodule
17 parser = parsermodule.getParser(['nodesets'])
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" )
31 config = parsermodule.parse_args(parser)
33 l_nodes = common.get_nodeset(config)
34 if config.site is None:
36 if config.loginbase: loginbase=True
39 print "Blacklisting site:", config.site
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 ]
51 print "Blacklisting nodes: ", l_nodes
53 if host not in hostnames:
54 print "adding to blacklist %s" % host
55 bl = BlacklistRecord(hostname=host, expires=int(config.expires))
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))
66 print "Deleting nodes: %s" % l_nodes
68 bl = BlacklistRecord.get_by(hostname=h)
71 print "Deleting site: %s" % config.site
72 bl = BlacklistRecord.get_by(loginbase=config.site)
75 # default option is to list
77 objlist = loginbases_exp
79 objlist = hostnames_exp
83 print i[0], i[1], "<-- expired"
84 elif i[1] > datetime.now():
91 if __name__ == '__main__':
95 #except Exception, error:
96 # print "Exception %s" % error