import sys
import string
import time
+from monitor import common
from monitor import database
from monitor.database.info.model import *
import getopt
print "blacklist.py --delete=<i>"
def main():
+ from monitor import parser as parsermodule
+ parser = parsermodule.getParser(['nodesets'])
- loginbase = False
+ 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" )
+
+ config = parsermodule.parse_args(parser)
+
+ 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
- try:
- longopts = ["delete=", "loginbase", "help"]
- (opts, argv) = getopt.getopt(sys.argv[1:], "d:lh", longopts)
- except getopt.GetoptError, err:
- print "Error: " + err.msg
- sys.exit(1)
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 ]
- for (opt, optval) in opts:
- if opt in ["-d", "--delete"]:
- i = optval
- bl = BlacklistRecord.get_by(hostname=i)
- bl.delete()
- elif opt in ["-l", "--loginbase"]:
- loginbase = True
- else:
- usage()
- sys.exit(0)
+ 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()
- i_cnt = 0
- if not loginbase:
- for i in hostnames:
- print i
- i_cnt += 1
- else:
- for i in loginbases:
- print i
- i_cnt += 1
-
+ 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:
+ objlist = hostnames_exp
- while 1:
- line = sys.stdin.readline()
- if not line:
- break
- line = line.strip()
- if line not in hostnames and line not in loginbases:
- if loginbase:
- bl = BlacklistRecord(loginbase=line)
+ for i in objlist:
+ if i[1] > datetime.now():
+ print i[0], i[1]
else:
- bl = BlacklistRecord(hostname=line)
- bl.flush()
- i_cnt += 1
-
+ print i[0]
+
session.flush()
- if loginbase:
- print "Total %d loginbases in blacklist" % (i_cnt)
- else:
- print "Total %d nodes in blacklist" % (i_cnt)
if __name__ == '__main__':
import os