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)) for h in hostnames_q ]
47 loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires)) for h in loginbases_q ]
50 print "Blacklisting nodes: ", l_nodes
52 if host not in hostnames:
53 print "adding to blacklist %s" % host
54 bl = BlacklistRecord(hostname=host, expires=int(config.expires))
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))
65 print "Deleting nodes: %s" % l_nodes
67 bl = BlacklistRecord.get_by(hostname=h)
70 print "Deleting site: %s" % config.site
71 bl = BlacklistRecord.get_by(loginbase=config.site)
74 # default option is to list
76 objlist = loginbases_exp
78 objlist = hostnames_exp
81 if i[1] > datetime.now():
88 if __name__ == '__main__':
92 #except Exception, error:
93 # print "Exception %s" % error