Beispiel #1
0
def fnvhash(string):
    ret = 97
    for i in string:
        ret = ret ^ ord(i) * 13
    return ret


def insertMonData(d_in):
    try:
        data = json.loads(d_in)
        print data
        dTime = int(data['Time'])
        hostIndex = monTables[fnvhash(data['Host']) % len(monTables)]
        sql = "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % \
            (hostIndex, data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], dTime)
        ret = c.execute(sql)
    except mysql.IntegrityError:
        pass


if __name__ == '__main__':

    def logic(d_in):
        insertMonData(d_in)
        #        print d_in
        return ("OK")

    saverD = nbNet('0.0.0.0', 50136, logic)
    saverD.run()
Beispiel #2
0
            print("\t%s normal" % key)


    def run(self):
        #self.print_info()
        for item in self.conf.iterkeys():
            print("\n-- in run()")
            self.process(item)


if __name__ == '__main__':

    def get_conf():
        '''read conf from file'''
        with file(conf_name) as fr:
            fr_json = json.load(fr)
            #print(json.dumps(fr_json, indent=4))
            if fr_json is None:
                print("conf is none, exit")
                sys.exit(1)
            return fr_json

    def logic(d_in):
        f = filter(d_in, get_conf())
        f.run()
        return("OK")

    saverD = nbNet('0.0.0.0', 50762, logic)
    saverD.run()

Beispiel #3
0
    #print data
    alarminfo = conf.ff_conf[0]
    aMU = int(conf.ff_conf[0][2])
    nowMU = int(data['MemUsage'])
    to_addr = conf.ff_conf[0][-1]
    print to_addr
    if nowMU > aMU:
        subject_header = 'Subject: %s alarm memusage %s!' % (host, nowMU)
        content = 'please check %s ' % alarminfo
        mail.MailAlarm(to_addr, subject_header, content)

def MonterLA(d_in):
    data = json.loads(d_in)
    alarminfo = conf.ff_conf[1]
    aLA = int(conf.ff_conf[1][2])
    nowLA = data['LoadAvg']
    to_addr = conf.ff_conf[0][-1]
    if nowLA > aLA:
        subject_header = 'Subject: %s alarm loadavg %s!' % (host, nowLA)
        content = 'please check %s ' % alarminfo
        mail.MailAlarm(to_addr, subject_header, content)


if __name__ == '__main__':
    def logic(d_in):
        MonterMem(d_in)
        MonterLA(d_in)
        return("OK")
    ffD = nbNet('0.0.0.0', 9652, logic)
    ffD.run()
Beispiel #4
0
        hostHash = hashlib.md5()
        hostHash.update(data['Host'])
        hostIndex = monTables[ord(hostHash.digest()[-1:]) % len(monTables)]
        print hostIndex
        print data['Time']
        sql = "insert into `%s` (host,mem_free,mem_usage,mem_total,load_avg,time) values('%s','%d','%d','%d','%s','%d')" % (hostIndex,data['Host'],data['MemFree'],data['MemUsage'],data['MemTotal'],data['LoadAvg'],dTime)
        print sql
        ret = c.execute(sql)
        print "ok"
    except mysql.IntegrityError:
        pass

if __name__ == '__main__':
    def logic(d_in):
        insertMonData(d_in)
#    def logic(d_in):
#        try:
#            data = json.loads(d_in)
#            print data
#            sql = "insert into stat(host,mem_free,mem_usage,mem_total,load_avg,time) values('%s','%d','%d','%d','%s','%d')" % (data['Host'],data['MemFree'],data['MemUsage'],data['MemTotal'],data['LoadAvg'],int(data['Time']))
#            ret = c.execute(sql)
#        except mysql.IntegrityError:
#            pass
#        print d_in
        return("OK")

    saverD = nbNet('0.0.0.0', 54444, logic)
    saverD.run()


Beispiel #5
0
# 发送给服务端data是发送的数据
def sendsaver(saver_l, data, sock_l):
    return sendData_mh(saver_l, data, sock_l)


#发送给监控端
def sendff(ff_l, data, sock_l):
    return sendData_mh(ff_l, data, sock_l)


if __name__ == '__main__':
    # 监听地址和端口
    addr = trans_conf['addr']
    port = int(trans_conf['port'])

    # 处理程序
    def logic(data):
        print data
        #ff_ret = sendff(ff_l, data, ff_sock_l)
        #saver_ret = sendsaver(saver_l, data, saver_sock_l)
        # 一般情况只判断监控收到就可以了
        #if ff_ret:
        return ("OK")
        #else:
        #    return("ER")

    # 启动服务
    sock = bind_socket(addr, port)
    transD = nbNet(sock, logic)
    transD.run()
Beispiel #6
0
    """
    #print conf.ff_conf
    #print d_in
    mon_data = json.loads(d_in)
    for rule in conf.ff_conf:
        monKey, operator, value, alarmRecv = rule
        monName = monKey + operator + str(value)
        eval_function = str(mon_data[monKey]) + operator + str(value)
        ff_result = eval(eval_function)
        if ff_result:
            alarmStatus[monName] = True
            print "Alarm", eval_function, alarmRecv
        else:
            if (alarmStatus.get(monName, False)):
                alarmStatus[monName] = False
                print "Recover", eval_function, alarmRecv
                



if __name__ == '__main__':
    def logic(d_in):
        ff(d_in)
#        print d_in
        return("OK")

    ffD = nbNet('0.0.0.0', 50002, logic)
    ffD.run()


Beispiel #7
0
#!/usr/bin/env python
# coding=utf-8

import sys, os 
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet

if __name__ == '__main__':
    def logic(d_in):
        return(d_in[::-1])

    reverseD = nbNet('0.0.0.0', 9079, logic)
    reverseD.run()


Beispiel #8
0
                j[ud] = data[ud]
        ud_data = json.dumps(j)
        sql = (
            "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`,`user_define`) VALUES('%s', '%d', '%d', '%d', '%s', '%d','%s')"
            % (
                hostIndex,
                data["Host"],
                data["MemFree"],
                data["MemUsage"],
                data["MemTotal"],
                data["LoadAvg"],
                dTime,
                ud_data,
            )
        )
        c = db.execute(sql)
        ## 把UD_开头的监控项数据json插入到user_define数据表中
    except mysql.IntegrityError:
        pass


if __name__ == "__main__":

    def logic(d_in):
        insertMonData(d_in)
        #        print d_in
        return "OK"

    saverD = nbNet("0.0.0.0", 50001, logic)
    saverD.run()
Beispiel #9
0
saver_sock_l = [None]

# 发送给服务端data是发送的数据
def sendsaver(saver_l, data, sock_l):
    return sendData_mh(saver_l, data, sock_l)

#发送给监控端
def sendff(ff_l, data, sock_l):
    return sendData_mh(ff_l, data, sock_l)

if __name__ == '__main__':
    # 监听地址和端口
    addr = trans_conf['addr']
    port = int(trans_conf['port'])
    
    # 处理程序
    def logic(data):
        print data
        #ff_ret = sendff(ff_l, data, ff_sock_l)
        #saver_ret = sendsaver(saver_l, data, saver_sock_l)
        # 一般情况只判断监控收到就可以了
        #if ff_ret:
        return("OK")
        #else:
        #    return("ER")
    
    # 启动服务
    sock = bind_socket(addr, port)
    transD = nbNet(sock, logic)
    transD.run()
Beispiel #10
0
        data = json.loads(d_in)
        print data
    except ValueError:
        return []
    # for m in conf.ff_conf:
    #     items.append(m[0])
    datalist=list(data.keys())
    for m in conf.ff_conf:
        if m[0] in datalist:
            if handleData(data[m[0]],m[2],m[1]):
                for user in m[3:]:
                    recivers=recivers+","+user
                sm = SendMail()
                sm.send(email_theme.format(url_name=data['host']),sender,
                    recivers,show_msg.format(host=data['host'],api=m[0])
                    )
    return "OK"

if __name__ == '__main__':
    # if 'MemUsage' in conf.ff_conf[0]:
    #     print "OK"

    def logic(d_in):
        getResult(d_in)
    #print d_in
    #return("OK")

    saverD = nbNet('0.0.0.0', 50252, logic)
    saverD.run()

	
Beispiel #11
0
        data = json.loads(d_in)
        print data
    except ValueError:
        return []
    # for m in conf.ff_conf:
    #     items.append(m[0])
    datalist = list(data.keys())
    for m in conf.ff_conf:
        if m[0] in datalist:
            if handleData(data[m[0]], m[2], m[1]):
                for user in m[3:]:
                    recivers = recivers + "," + user
                sm = SendMail()
                sm.send(email_theme.format(url_name=data['host']), sender,
                        recivers, show_msg.format(host=data['host'], api=m[0]))
    return "OK"


if __name__ == '__main__':
    # if 'MemUsage' in conf.ff_conf[0]:
    #     print "OK"

    def logic(d_in):
        getResult(d_in)

    #print d_in
    #return("OK")

    saverD = nbNet('0.0.0.0', 50252, logic)
    saverD.run()
Beispiel #12
0
                           self.conf[key][2])
        else:
            print("\t%s normal" % key)

    def run(self):
        #self.print_info()
        for item in self.conf.iterkeys():
            print("\n-- in run()")
            self.process(item)


if __name__ == '__main__':

    def get_conf():
        '''read conf from file'''
        with file(conf_name) as fr:
            fr_json = json.load(fr)
            #print(json.dumps(fr_json, indent=4))
            if fr_json is None:
                print("conf is none, exit")
                sys.exit(1)
            return fr_json

    def logic(d_in):
        f = filter(d_in, get_conf())
        f.run()
        return ("OK")

    saverD = nbNet('0.0.0.0', 50762, logic)
    saverD.run()
Beispiel #13
0
#!/usr/bin/env python
# coding=utf-8

import sys, os 
import json
import hashlib

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet,sendData_mh
from ff import handle

sock_l = [None]
host_l = ['10.16.48.81:50001']

def transf(sock_l,host_l,data):
    return sendData_mh(sock_l,host_l,data)



if __name__ == '__main__':
    def logic(d_in):
        print d_in
        main(json.loads(d_in))
        transf(sock_l,host_l,d_in)
        return "OK"

    saverD = nbNet('0.0.0.0', 12888, logic)
    saverD.run()
Beispiel #14
0
import hashlib

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet, sendData_mh

saver_l = ["localhost:50136", "127.0.0.1:50136"]
ff_l = ["localhost:50138", "127.0.0.1:50138"]

saver_sock_l = [None]
ff_sock_l = [None]

def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)

def sendFf(d_in, ff_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)

if __name__ == '__main__':
    def logic(d_in):
        sendFf(d_in, ff_l)
        ret = sendSaver(d_in, saver_l)
        if ret:
            return("OK")
        else:
            return("ER")

    transD = nbNet('0.0.0.0', 50137, logic)
    transD.run()


Beispiel #15
0
#!/usr/bin/env python
# coding=utf-8

import sys, os
import json
import hashlib

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet, sendData_mh
from ff import handle

sock_l = [None]
host_l = ['10.16.48.81:50001']


def transf(sock_l, host_l, data):
    return sendData_mh(sock_l, host_l, data)


if __name__ == '__main__':

    def logic(d_in):
        print d_in
        main(json.loads(d_in))
        transf(sock_l, host_l, d_in)
        return "OK"

    saverD = nbNet('0.0.0.0', 12888, logic)
    saverD.run()
Beispiel #16
0
from simpleNet.nbNetFramework import sendData_mh, nbNet

#saver_l = ['baidu.com:88','localhost:50001']
saver_l = ['localhost:50001']  #,'baidu.com:88']
ff_l = ['localhost:50002']
saver_sock_l = [None]
ff_sock_l = [None]


def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)


def sendFf(d_in, saver_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)


if __name__ == "__main__":

    def logic(d_in):
        ret_ff = sendFf(d_in, ff_l)
        ret_ss = sendSaver(d_in, saver_l)
        if ret_ss == "OK":
            print "SendSave OK"
            return ("OK")
        else:
            return ("SendSave Error")

    transD = nbNet('0.0.0.0', 50000, logic)
    transD.run()
Beispiel #17
0
# coding=utf-8

import sys, os 
import json
import hashlib
#import MySQLdb as mysql

sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet
import mailf


def insertMonData(d_in):
    data = json.loads(d_in)
        #data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], dTime
    if int(data['MemUsage']) > 1900 :
        mailf.mailf('%s MemUsage >1900'%data['Host'],'%s MemUsage is %s '% (data['Host'],data['MemUsage']))
    if   float(data['LoadAvg']) > 0.1 :
        mailf.mailf('LoadAvg is % '% data['LoadAvg'],'LoadAvg too high')        

if __name__ == '__main__':
    def logic(d_in):
        insertMonData(d_in)
#        print d_in
        return("OK")

    saverD = nbNet('0.0.0.0', 50002, logic)
    saverD.run()


Beispiel #18
0
save_l = ["localhost:9650", "127.0.0.1:9650"]
ff_l = ["localhost:9652", "127.0.0.1:9652"]

saver_sock_l = [None]
ff_sock_l = [None]

def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)


def sendFf(d_in, saver_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)


if __name__ == '__main__':
    def logic(d_in):
        sendFf(d_in, ff_l)
        ret = sendSaver(d_in, save_l)
        if ret:
            return("OK")
        else:
            return("ER")


        return("OK")

    transD = nbNet('0.0.0.0', 9651, logic)
    transD.run()


Beispiel #19
0
port = int(ctrl_conf['port'])
def_timeout = ctrl_conf['timeout']

#处理程序
def logic(data):
    dec_data = decrypt(data)
    data = json.loads(dec_data)
    if "cmd" in data:
        send_data = get_iphostname()
        cmd = data['cmd']
        timeout =  data.get("timeout", def_timeout)
        command = Command(cmd)
        recode, output, error = command.run(int(timeout))
        if not recode:
            print recode
            # 如国命令执行成功
            send_data['result'] = output
        else:
            print recode
            # 如国命令执行失败
            send_data['result'] = error
        send_data['cmd'] = cmd
        enc_data = json.dumps(send_data)
        return encrypt(enc_data)
    else:
        return encrypt("input error")
    
sock = bind_socket(addr, port)
saverD = nbNet(sock, logic)
saverD.run()
Beispiel #20
0
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet, sendData_mh

saver_l = ["localhost:50001", "127.0.0.1:50001"]
ff_l = ["localhost:50002", "127.0.0.1:50002"]

saver_sock_l = [None]
ff_sock_l = [None]

def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)

def sendFf(d_in, ff_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)

if __name__ == '__main__':
    def logic(d_in):
        #ret = sendFf(d_in, ff_l)
        ret = sendSaver(d_in, saver_l)
        print ret
        if ret:
            return("OK")
        else:
            return("ER")

    transD = nbNet('0.0.0.0', 50000, logic)
    transD.run()


Beispiel #21
0
def insertMonData(data):
    '''数据写入数据库'''
    # 将发过来json数据转换成python格式
    data = json.loads(data)
    # 写入时间
    dTime = int(data['Time'])
    # 将主机的主机名使用fnv哈希,然后通过取余数,算出写入哪个分表中
    hostIndex = monTables[fnvhash(data['Host']) % len(monTables)]
    sql = (
        "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`)"
        "VALUES('%s', '%d', '%d', '%d', '%s', '%d')" %
        (hostIndex, data['Host'], data['MemFree'], data['MemUsage'],
         data['MemTotal'], data['LoadAvg'], dTime))
    result = db.execute(sql)


if __name__ == '__main__':
    # 监听地址和端口
    addr = saver_conf['addr']
    port = int(saver_conf['port'])

    # 处理程序
    def logic(data):
        insertMonData(data)
        return ("OK")

    sock = bind_socket(addr, port)
    saverD = nbNet(sock, logic)
    saverD.run()
Beispiel #22
0
#!/usr/bin/env python
# coding=utf-8

import sys, os 
sys.path.insert(1, os.path.join(sys.path[0], '..'))
from simpleNet.nbNetFramework import nbNet

if __name__ == '__main__':
    def logic(d_in):
        return(d_in[::-1])

    reverseD = nbNet('0.0.0.0', 9076, logic)
    reverseD.run()


Beispiel #23
0
def fnvhash(string):
    ret = 97
    for i in string:
        ret = ret ^ ord(i) * 13
    return ret

def insertMonData(d_in):
    try:
        data = json.loads(d_in)
        print data
        dTime = int(data['Time'])
        hostIndex = monTables[fnvhash(data['Host']) % len(monTables)]
        sql = "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % \
            (hostIndex, data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], dTime)
        ret = c.execute(sql)
    except mysql.IntegrityError:
        pass
    

if __name__ == '__main__':
    def logic(d_in):
        insertMonData(d_in)
#        print d_in
        return("OK")

    saverD = nbNet('0.0.0.0', 50001, logic)
    saverD.run()


Beispiel #24
0
        toMail(mail)
        return False

def toMail(mail):
    print "alarm %s" % mail


def ff(data, ff_conf):
    data = json.loads(data)
    th_array = []
    for conf in ff_conf:
        t = threading.Thread(target=compare, args=(data, conf,))
        th_array.append(t)
    for t in th_array:
        t.start()
    return True

if __name__ == '__main__':
    def logic(da_in):
        ret = ff(da_in, ff_conf)
        if ret:
            return "OK"
        else:
            return "ER"

    ffD = nbNet('0.0.0.0', 50000, logic)
    ffD.run()



Beispiel #25
0
def fnvhash(string):
    ret = 97
    for i in string:
        ret = ret ^ ord(i) * 13
    return ret

def insertMonData(d_in):
    try:
        data = json.loads(d_in)
        print data
        dTime = int(data['Time'])
        hostIndex = monTables[fnvhash(data['Host']) % len(monTables)]
        sql = "INSERT INTO `%s` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % \
            (hostIndex, data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], dTime)
        ret = c.execute(sql)
    except mysql.IntegrityError:
        pass
    

if __name__ == '__main__':
    def logic(d_in):
        insertMonData(d_in)
#        print d_in
        return("OK")

    saverD = nbNet('0.0.0.0', 50136, logic)
    saverD.run()


Beispiel #26
0
save_l = ["localhost:9650", "127.0.0.1:9650"]
ff_l = ["localhost:9652", "127.0.0.1:9652"]

saver_sock_l = [None]
ff_sock_l = [None]


def sendSaver(d_in, saver_l):
    return sendData_mh(saver_sock_l, saver_l, d_in)


def sendFf(d_in, saver_l):
    return sendData_mh(ff_sock_l, ff_l, d_in)


if __name__ == '__main__':

    def logic(d_in):
        sendFf(d_in, ff_l)
        ret = sendSaver(d_in, save_l)
        if ret:
            return ("OK")
        else:
            return ("ER")

        return ("OK")

    transD = nbNet('0.0.0.0', 9651, logic)
    transD.run()