X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=blacklist.py;h=3ff769f0e4f9caa28445bea32e6ce91b668f5952;hb=ce9101bfa7720c131a982220f2905e4439b324fc;hp=c96dc89641d48ea841d204f6fe800b0fab2b9841;hpb=6496f5b4a0220e4055fee76c97f92293f9559117;p=monitor.git diff --git a/blacklist.py b/blacklist.py index c96dc89..3ff769f 100755 --- a/blacklist.py +++ b/blacklist.py @@ -4,47 +4,86 @@ import os import sys import string import time -import database -import plc +from monitor import common +from monitor import database +from monitor.database.info.model import * import getopt def usage(): print "blacklist.py --delete=" def main(): + from monitor import parser as parsermodule + parser = parsermodule.getParser(['nodesets']) - try: - longopts = ["delete=", "help"] - (opts, argv) = getopt.getopt(sys.argv[1:], "d:h", longopts) - except getopt.GetoptError, err: - print "Error: " + err.msg - sys.exit(1) + parser.set_defaults( expires=0, delete=False, add=False, list=True, loginbase=False) + parser.add_option("", "--expires", dest="expires", + help="Set expiration time for blacklisted objects (in seconds)" ) + parser.add_option("", "--delete", dest="delete", action="store_true", + help="Remove objects from blacklist" ) + parser.add_option("", "--list", dest="list", action="store_true", + help="List objects in blacklist" ) + parser.add_option("", "--add", dest="add", action="store_true", + help="List objects in blacklist" ) + parser.add_option("", "--loginbase", dest="loginbase", action="store_true", + help="List objects in blacklist" ) - l_blacklist = database.if_cached_else(1, "l_blacklist", lambda : []) + config = parsermodule.parse_args(parser) - for (opt, optval) in opts: - if opt in ["-d", "--delete"]: - i = int(optval) - del l_blacklist[i] + l_nodes = common.get_nodeset(config) + if config.site is None: + loginbase = False + if config.loginbase: loginbase=True + else: + loginbase = True + print "Blacklisting site:", config.site + + + hostnames_q = BlacklistRecord.getHostnameBlacklist() + loginbases_q = BlacklistRecord.getLoginbaseBlacklist() + hostnames = [ h.hostname for h in hostnames_q ] + loginbases = [ h.loginbase for h in loginbases_q ] + hostnames_exp = [ (h.hostname,h.date_created+timedelta(0,h.expires)) for h in hostnames_q ] + loginbases_exp = [ (h.loginbase,h.date_created+timedelta(0,h.expires)) for h in loginbases_q ] + + if config.add: + print "Blacklisting nodes: ", l_nodes + for host in l_nodes: + if host not in hostnames: + print "adding to blacklist %s" % host + bl = BlacklistRecord(hostname=host, expires=int(config.expires)) + bl.flush() + + if loginbase: + print "Blacklisting site: ", config.site + if config.site not in loginbases: + print "adding to blacklist %s" % config.site + bl = BlacklistRecord(loginbase=config.site, expires=int(config.expires)) + bl.flush() + + elif config.delete: + print "Deleting nodes: %s" % l_nodes + for h in l_nodes: + bl = BlacklistRecord.get_by(hostname=h) + if bl: bl.delete() + if config.site: + print "Deleting site: %s" % config.site + bl = BlacklistRecord.get_by(loginbase=config.site) + if bl: bl.delete() + else: + # default option is to list + if loginbase: + objlist = loginbases_exp else: - usage() - sys.exit(0) - - i_cnt = 0 - for i in l_blacklist: - print i_cnt, " ", i - i_cnt += 1 - - while 1: - line = sys.stdin.readline() - if not line: - break - line = line.strip() - if not line in l_blacklist: - l_blacklist.append(line) - - print "Total %d nodes in blacklist" % (len(l_blacklist)) - database.dbDump("l_blacklist") + objlist = hostnames_exp + + for i in objlist: + if i[1] > datetime.now(): + print i[0], i[1] + else: + print i[0] + + session.flush() if __name__ == '__main__': import os