Beispiel #1
0
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))
Beispiel #2
0
from Radix import CharSet

parm = { 'fname=s' : 'a conf info data file, yaml format' }
options = OptConf( **parm )
opt = options.opt
args = options.args
conf = options.conf

if 'fname' not in opt: options.help()
alertr = RedisClient(db='alert', **conf['database'])
print "redis connect stat: %s" % alertr.ping()
pfile = opt['fname']
data = yaml.load(open(pfile))
groups = alertr.keys('group:*')
old_data = dict()
for group in groups:
    try:
        v = alertr.get(group)
    except:
        pass
    old_data[group] = v


for p in data:
    name = 'group:'+p.pop('name')
    old_data[name] = json.dumps(p)
    print "设置%s : %s\n" % (name, old_data[name])

for i, v in old_data.items():
    alertr.set(i, v)