nagios监控网卡流量

/proc/net/dev文件记录了网卡的进出数据,通过sleep一秒(1s可能暴力了点,可以根据需要自己改时间),将前后的两次数据相减得出流量,脚本如下

#!/usr/bin/env python
# _*_coding:utf-8_*_

"""
check interface traffic
"""

# $Id$#
__author__ = 'warshipJian'
__version__ = '$Revision: 0.1 $'

import sys
import math
import time
import fcntl
import struct
import socket


def convertbytes(data):
    return int(data) / math.pow(1024, 1)

def ifconfig_bytes():
    traffic_in = dict()
    traffic_out = dict()
    with open('/proc/net/dev') as f:
        for val in f:
            if 'lo' in val:
                continue
            if ':' not in val:
                continue

            _val = val.split(':')

            try:
                ifname = _val[0].strip(':').strip()
                netdata = _val[1].strip(':').strip().split()
                if(len(netdata)) < 8:
                    continue
                if_in = netdata[0]
                if_out = netdata[8]
            except ValueError:
                continue

            traffic_in[ifname] = int(if_in)
            traffic_out[ifname] = int(if_out)

    return traffic_in, traffic_out

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
        )[20:24])
    except IOError:
        return None

def ip_type(ip):
    f = struct.unpack('!I', socket.inet_pton(socket.AF_INET, ip))[0]
    private = (
        [2130706432, 4278190080],  # 127.0.0.0,   255.0.0.0   http://tools.ietf.org/html/rfc3330
        [3232235520, 4294901760],  # 192.168.0.0, 255.255.0.0 http://tools.ietf.org/html/rfc1918
        [2886729728, 4293918720],  # 172.16.0.0,  255.240.0.0 http://tools.ietf.org/html/rfc1918
        [167772160, 4278190080],  # 10.0.0.0,    255.0.0.0   http://tools.ietf.org/html/rfc1918
    )
    for net in private:
        if (f & net[1]) == net[0]:
            return True
    return False

def main():
    info = list()
    label = list()
    status = 0

    if_in, if_out = ifconfig_bytes()

    time.sleep(1)

    _if_in, _if_out = ifconfig_bytes()


    for val in if_in:
        ip = get_ip_address(val)
        if ip == None:
            continue
        receive = convertbytes(_if_in[val] - if_in[val])
        transmit = convertbytes(_if_out[val] - if_out[val])
        ptf = '%s receive:%.2fkB/s,transmit:%.2fkB/s' % (val, receive, transmit)
        labe = '%s_receive=%.2f;0;100 %s_transmit=%.2f;0;100' % (val, receive, val, transmit)
        info.append(ptf)
        label.append(labe)
        if(ip_type(ip)):
            if receive >= 50000 or transmit >= 50000:
                status = 2
            elif receive >= 30000 or transmit >= 30000:
                status = 1
        else:
            if receive >= 5000 or transmit >= 5000:
                status = 2
            elif receive >= 3000 or transmit >= 3000:
                status = 1

    print ' '.join(info) + '|' + ' '.join(label)
    sys.exit(status)

if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值