nagios 监控windows 网卡流量

script

还是先上代码,注释以后再补吧,

反人类的WMI模块。。。。。。。。。。。。

from __future__ import division
import time,wmi,win32com
from optparse import OptionParser

parser = OptionParser(usage="%prog -w <warning threshold> -c <critical threshold> -p ",version="%prog ")
        
parser.add_option("-w", "--warning",action="store", type="string", dest="warn_threshold", help="Warning threshold in percentage")
        
parser.add_option("-c", "--critical",action="store", type="string", dest="crit_threshold", help="Critical threshold in percentage")

parser.add_option("-p", "--partion",action="store", type="string", dest="partion", help="Partion Name")
       
(options, args) = parser.parse_args()

def get_net():  
    c = wmi.WMI ()  
    com = win32com.client.Dispatch("WbemScripting.SWbemRefresher")  
    obj = win32com.client.GetObject("winmgmts:\\root\cimv2")  
    items = com.AddEnum(obj, "Win32_PerfRawData_Tcpip_NetworkInterface").objectSet  
  
    data_dict = {}  
    interfaces = []  
    for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1):  
#        print interface.IPAddress[0]  
        interfaces.append(interface.Description)  
  
    net_bytes_in = 0  
    net_bytes_out = 0  
    net_pkts_in = 0  
    net_pkts_out = 0  
      
    com.Refresh()  
    for item in items:  
        if item.Name in interfaces:  
            #print 'Name:%s -> B_in:%s, B_out:%s, P_in:%s, P_out:%s' %(item.Name, item.BytesReceivedPerSec, item.BytesSentPerSec, item.PacketsReceivedPerSec, item.PacketsSentPerSec)  
            net_bytes_in += long(item.BytesReceivedPerSec)  
            net_bytes_out += long(item.BytesSentPerSec)  
            net_pkts_in += long(item.PacketsReceivedPerSec)  
            net_pkts_out += long(item.PacketsSentPerSec)  
  
    time.sleep(1)  
  
    net_bytes_in_cur = 0  
    net_bytes_out_cur = 0  
      
    com.Refresh()  
    for item in items:  
        if item.Name in interfaces:  
            net_bytes_in = long(item.BytesReceivedPerSec) - net_bytes_in  
            net_bytes_in_cur += long(item.BytesReceivedPerSec)  
            net_bytes_out = long(item.BytesSentPerSec) - net_bytes_out  
            net_bytes_out_cur += long(item.BytesSentPerSec)  
            net_pkts_in = long(item.PacketsReceivedPerSec) - net_pkts_in  
            net_pkts_out = long(item.PacketsSentPerSec) - net_pkts_out  
  
    data_dict['net_bytes_in'] = {'volume':net_bytes_in, 'unit':'B/s'}  
    data_dict['net_bytes_in_sum'] = {'volume':net_bytes_in_cur, 'unit':'B'}  
    data_dict['net_bytes_out'] = {'volume':net_bytes_out, 'unit':'B/s'}  
    data_dict['net_bytes_out_sum'] = {'volume':net_bytes_out_cur, 'unit':'B'}  
    data_dict['net_pkts_in'] = {'volume':net_pkts_in, 'unit':'p/s'}  
    data_dict['net_pkts_out'] = {'volume':net_pkts_out, 'unit':'p/s'}  
  
    return data_dict

def printr():
    data = get_net()
    traffic_out = int(data['net_bytes_out']['volume'])/1024
    traffic_in = int(data['net_bytes_in']['volume'])/1024
    package_out = int(data['net_pkts_out']['volume'])
    package_in = int(data['net_pkts_in']['volume'])
    print 'NET traffic_out: %d KB/s traffic_in: %d KB/s package_out: %d p/s package_in: %d p/s | traffic_out=%dKB traffic_in=%dKB package_out=%d package_in=%d' %(traffic_out,traffic_in,package_out,package_in,traffic_out,traffic_in,package_out,package_in)

if __name__ == '__main__':
    printr()


你可能感兴趣的:(nagios 监控windows 网卡流量)