예제 #1
0
def zbxsend(connectnum):
    zbx_host = getiniConfig.getConfig('/tools/config/nsconfig',
                                      'monitorlinenumber.ini', 'zabbixConfig',
                                      'zbx_host')
    zbx_port = int(
        getiniConfig.getConfig('/tools/config/nsconfig',
                               'monitorlinenumber.ini', 'zabbixConfig',
                               'zbx_port'))
    zbx_host = eval(zbx_host)
    innerIP = str(get_ip_address('eth1'))
    innerIPPart = '.'.join(innerIP.split('.')[0:-1])
    zbx_ip = ''
    for host in zbx_host:
        for inIP in host['innerIP']:
            inIP = '.'.join(inIP.split('.')[0:-1])
            if innerIPPart == inIP:
                zbx_ip = host['zbx_ip']
                continue
    if not zbx_ip:
        zbx_ip = "127.0.0.1"
    zbxdata = []
    zbxdata.append({
        "host": str(get_ip_address('eth1')),
        "key": "monitor_linenumber.status",
        "value": int(connectnum)
    })
    zbxsender = zabbix_sender(zbx_ip, zbx_port)
    zbxsender.send(zbxdata, datarecv_print)

    getiniConfig.write_config('/home/config', 'monitorNewlinenumber.ini',
                              'lastNumConfig', 'lastLineNum', str(connectnum))
예제 #2
0
파일: line.py 프로젝트: levvli/lvan-script
def line(socketconfig='/tools/config/nsconfig/collect.ini:line',
         port='2888[0-6]'):

    try:
        logName = 'lineNum' + str(
            time.strftime('%Y%m%d', time.localtime(time.time())))
        log = getLogger(logName)
        area, group, oid, ctime = gethostinfo()
        connectnum = getconnectnum(port)
        connectnum_json = {
            'zone': area,
            'group': group,
            'time': ctime,
            'num': connectnum,
            'oid': oid
        }
        lastLineNum = getiniConfig.getConfig('/tools/config/nsconfig',
                                             'monitorlinenumber.ini',
                                             'lastNumConfig', 'lastLineNum')
        if str(lastLineNum) == '0':
            getiniConfig.write_config('/tools/config/nsconfig',
                                      'monitorlinenumber.ini', 'lastNumConfig',
                                      'lastLineNum', str(connectnum))
        else:
            zbxsend(connectnum)
        log.info(str(connectnum_json))
    except Exception, e:
        log.error("monitor line error: %s" % str(e))
        try:
            connectnum = getconnectnum(port)
            zbxsend(connectnum)
        except Exception, e:
            log.error("monitor line error: %s" % str(e))
예제 #3
0
def line(socketconfig='/tools/config/nsconfig/collectNew.ini:line',port='2888[0-6]'):

    try:
        connectnum_json = ''
        connectnum = '1'
        logName = 'lineNum'+str(time.strftime('%Y%m%d',time.localtime(time.time())))
        log = getLogger(logName)
        area,group,oid,ctime = gethostinfo()
        connectnum = getconnectnum(port)
        headJson = ''
        senderNodeId,logTime,limitNum,headJson = getNodeIdAndType('line','')
        if senderNodeId:
            connectnum_json = headJson+'''{"zone":"%s","group":"%s","time":"%s","num":"%s","oid":"%s"}}'''
            connectnum_json = connectnum_json %(
               str(senderNodeId),str(logTime),str(limitNum),'p1-line','1',str(area),str(group),str(ctime),str(connectnum),str(oid)
            )
        #print connectnum_json,'  dddd'
        # connectnum_json = {'zone':area,'group':group,'time':ctime,'num':connectnum,'oid':oid}
        lastLineNum = getiniConfig.getConfig('/home/config','monitorNewlinenumber.ini','lastNumConfig','lastLineNum')
        if str(lastLineNum) == '0':
            getiniConfig.write_config('/home/config','monitorNewlinenumber.ini','lastNumConfig','lastLineNum',str(connectnum))
        else:
            zbxsend(connectnum)
        log.info('Newinfo :'+str(connectnum_json))
    except Exception,e:
        print str(e)
        log.error("monitor lineNew error: %s, onlineNum is %s"%(str(e),str(connectnum)))
        try:
            connectnum = getconnectnum(port)
            zbxsend(connectnum)
        except Exception,e:
            log.error("monitor lineNew error1: %s"%str(e))
예제 #4
0
def send_zabbix(zbxdata=[]):

    zbx_host = getiniConfig.getConfig('/tools/config/nsconfig',
                                      'monitorlinenumber.ini', 'zabbixConfig',
                                      'zbx_host')
    zbx_port = int(
        getiniConfig.getConfig('/tools/config/nsconfig',
                               'monitorlinenumber.ini', 'zabbixConfig',
                               'zbx_port'))
    zbx_hosts = eval(zbx_host)
    innerIP = str(get_ip_address('eth1'))
    innerIPPart = '.'.join(innerIP.split('.')[0:-1])
    zbx_ip = ''
    for host in zbx_hosts:
        for inIP in host['innerIP']:
            inIP = '.'.join(inIP.split('.')[0:-1])
            if innerIPPart == inIP:
                zbx_ip = host['zbx_ip']
                continue
    if not zbx_ip:
        zbx_ip = "127.0.0.1"
    zbxsender = zabbix_sender(zbx_ip, zbx_port)
    zbxsender.send(zbxdata, datarecv_print)
예제 #5
0
def line(port='2888[0-6]'):
    try:
        connectnum = getconnectnum(port)
        endtime = datetime.datetime.now()
        lastLineNum = getiniConfig.getConfig('/home/config',
                                             'monitoronline.ini',
                                             'lastNumConfig', 'lastLineNum')
        if not int(lastLineNum):
            getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                      'lastNumConfig', 'lastLineNum',
                                      str(connectnum))
            getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                      'lastNumConfig', 'starttime',
                                      str(endtime))
            getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                      'lastNumConfig', 'deltatime1', '0')
            getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                      'lastNumConfig', 'deltatime2', '0')
        else:
            starttime_str = getiniConfig.getConfig('/home/config',
                                                   'monitoronline.ini',
                                                   'lastNumConfig',
                                                   'starttime')
            starttime = datetime.datetime(
                *time.strptime(starttime_str[:19], "%Y-%m-%d %H:%M:%S")[:6])
            if int(connectnum) - int(lastLineNum) > 500:
                print '在线人数突然上升大于500人'
                time_delta = endtime - starttime
                if int(time_delta.seconds) < 300:  #时间差小于5分钟,每次发送
                    print '5分钟内间隔一分钟报警!'
                    zbxsend(1)
                elif int(time_delta.seconds) < 1800:  #时间差大于5分钟小于30分钟,每5分钟发送报警
                    deltatime = getiniConfig.getConfig('/home/config',
                                                       'monitoronline.ini',
                                                       'lastNumConfig',
                                                       'deltatime1')
                    if deltatime != '0':
                        starttime = datetime.datetime(*time.strptime(
                            deltatime[:19], "%Y-%m-%d %H:%M:%S")[:6])
                        time_delta = endtime - starttime
                        if int(time_delta.seconds) > 300:
                            print '时间差大于5分钟小于30分钟,每5分钟发送报警'
                            zbxsend(1)
                            getiniConfig.write_config('/home/config',
                                                      'monitoronline.ini',
                                                      'lastNumConfig',
                                                      'deltatime1',
                                                      str(endtime))
                        else:
                            print '时间差大于5分钟小于30分钟,没有超过5分钟不发送报警'
                            zbxsend(0)
                    else:
                        print '时间差大于5分钟小于30分钟,第一次报警'
                        zbxsend(1)
                        getiniConfig.write_config('/home/config',
                                                  'monitoronline.ini',
                                                  'lastNumConfig',
                                                  'deltatime1', str(endtime))
                elif int(
                        time_delta.seconds) < 3600:  #时间差大于30分钟小于60分钟,每10分钟发送报警
                    deltatime = getiniConfig.getConfig('/home/config',
                                                       'monitoronline.ini',
                                                       'lastNumConfig',
                                                       'deltatime2')
                    if deltatime:
                        starttime = datetime.datetime(*time.strptime(
                            deltatime[:19], "%Y-%m-%d %H:%M:%S")[:6])
                        time_delta = endtime - starttime
                        if int(time_delta.seconds) > 600:
                            print '时间差大于30分钟小于60分钟,每10分钟发送报警'
                            zbxsend(1)
                            getiniConfig.write_config('/home/config',
                                                      'monitoronline.ini',
                                                      'lastNumConfig',
                                                      'deltatime2',
                                                      str(endtime))
                        else:
                            print '时间差大于30分钟小于60分钟,没有超过10分钟不发送报警'
                            zbxsend(0)
                    else:
                        print '时间差大于30分钟小于60分钟,第一次报警'
                        zbxsend(1)
                        getiniConfig.write_config('/home/config',
                                                  'monitoronline.ini',
                                                  'lastNumConfig',
                                                  'deltatime2', str(endtime))
                else:  #时间差大于60分钟,不报警
                    print '时间差大于60分钟,不报警'
                    zbxsend(0)
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'lastLineNum',
                                          str(connectnum))

            elif abs(int(connectnum) - int(lastLineNum)) > 0:
                print '人数监控正常'
                zbxsend(0)
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'lastLineNum',
                                          str(connectnum))
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'starttime',
                                          str(endtime))
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'deltatime1', '0')
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'deltatime2', '0')
                return

            if int(connectnum) - int(lastLineNum) < -200:
                print '在线人数下降大于200人'
                time_delta = endtime - starttime
                if int(time_delta.seconds) < 300:  #时间差小于5分钟,每次发送
                    print '5分钟内间隔一分钟报警!'
                    zbxsend(2)
                elif int(time_delta.seconds) < 1800:  #时间差大于5分钟小于30分钟,每5分钟发送报警
                    deltatime = getiniConfig.getConfig('/home/config',
                                                       'monitoronline.ini',
                                                       'lastNumConfig',
                                                       'deltatime1')
                    if deltatime != '0':
                        starttime = datetime.datetime(*time.strptime(
                            deltatime[:19], "%Y-%m-%d %H:%M:%S")[:6])
                        time_delta = endtime - starttime
                        if int(time_delta.seconds) > 300:
                            print '时间差大于5分钟小于30分钟,每5分钟发送报警'
                            zbxsend(2)
                            getiniConfig.write_config('/home/config',
                                                      'monitoronline.ini',
                                                      'lastNumConfig',
                                                      'deltatime1',
                                                      str(endtime))
                        else:
                            print '时间差大于5分钟小于30分钟,没有超过5分钟不发送报警'
                            zbxsend(0)
                    else:
                        print '时间差大于5分钟小于30分钟,第一次报警'
                        zbxsend(2)
                        getiniConfig.write_config('/home/config',
                                                  'monitoronline.ini',
                                                  'lastNumConfig',
                                                  'deltatime1', str(endtime))
                elif int(
                        time_delta.seconds) < 3600:  #时间差大于30分钟小于60分钟,每10分钟发送报警
                    deltatime = getiniConfig.getConfig('/home/config',
                                                       'monitoronline.ini',
                                                       'lastNumConfig',
                                                       'deltatime2')
                    if deltatime:
                        starttime = datetime.datetime(*time.strptime(
                            deltatime[:19], "%Y-%m-%d %H:%M:%S")[:6])
                        time_delta = endtime - starttime
                        if int(time_delta.seconds) > 600:
                            print '时间差大于30分钟小于60分钟,每10分钟发送报警'
                            zbxsend(2)
                            getiniConfig.write_config('/home/config',
                                                      'monitoronline.ini',
                                                      'lastNumConfig',
                                                      'deltatime2',
                                                      str(endtime))
                        else:
                            print '时间差大于30分钟小于60分钟,没有超过10分钟不发送报警'
                            zbxsend(0)
                    else:
                        print '时间差大于30分钟小于60分钟,第一次报警'
                        zbxsend(2)
                        getiniConfig.write_config('/home/config',
                                                  'monitoronline.ini',
                                                  'lastNumConfig',
                                                  'deltatime2', str(endtime))
                else:  #时间差大于60分钟,不报警
                    print '时间差大于60分钟,不报警'
                    zbxsend(0)
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'lastLineNum',
                                          str(connectnum))

            elif int(connectnum) - int(lastLineNum) <= 0:
                print '人数监控正常'
                zbxsend(0)
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'lastLineNum',
                                          str(connectnum))
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'starttime',
                                          str(endtime))
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'deltatime1', '0')
                getiniConfig.write_config('/home/config', 'monitoronline.ini',
                                          'lastNumConfig', 'deltatime2', '0')

    except Exception, e:
        print str(e)