monicazhang 发表于 2016-1-7 11:56:39

nagios如何监控windows 网卡流量

本帖最后由 monicazhang 于 2016-1-7 15:47 编辑

来自:网络

script
还是先上代码,注释以后再补吧,
反人类的WMI模块。。。。。。。。。。。。
?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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") nagios培训
         
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():                   nagios实施    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         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          nagios配置    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__': nagios安装    printr()



想做Nagios, Zabbix,Cacti,iTop各种交流的,可以进入开源监控工具Nagios交流QQ群号 :476809427
页: [1]
查看完整版本: nagios如何监控windows 网卡流量