示例#1
0
文件: black.py 项目: qinguoan/rambo
class BlackManager(object):
    def __init__(self,**conf):
        self.blackr  = RedisClient(db='black', **conf)
        self.ranger = MagicShow(**conf)

    def addbl(self,data):
        expire = data['expire']
        hosts = self.ranger.load(data['range']).show()
        if hosts == 'all':
            key = 'black_%s_%s' % (data['policy'],host)
            self.blackr.set(key,'black',ex=expire)
        else:
            for host in hosts:
                key = 'black_%s_%s' % (data['policy'],host)
                self.blackr.set(key,'black',ex=expire)

    def delbl(self,data):
        print data
        policy = data['policy']
        hosts = self.ranger.load(data['range']).show()
        for host in hosts:
            if policy != 'all':
                key = 'black_%s_%s' % (policy,host)
                self.blackr.delete(key)
            else:
                keys = self.blackr.keys('black_*_%s' % host)
                for key in keys:
                    self.blackr.delete(key)

    def check(self,host,policy=None):
        keys = list()
        key = 'black_all_%s' % (host)
        keys.append(key)
        if policy:
            keys.append('black_%s_%s' % (policy,host))
            keys.append('black_%s_all' % (policy))
        for key in keys:
            if self.blackr.exists(key):
                return True
        return False

    def show(self):
        keys = self.blackr.keys('black*')
        for k in keys:
            try:
                typer,policy,host = k.split('_')
                ttl = self.blackr.pttl(k)
            except:
                print "=================", k
            else:
                print "策略:%s, 主机:%s, 剩余时间:%s 秒" % (policy,host,int(ttl/1000))
示例#2
0
文件: black.py 项目: qinguoan/rambo
 def __init__(self,**conf):
     self.blackr  = RedisClient(db='black', **conf)
     self.ranger = MagicShow(**conf)