码迷,mamicode.com
首页 > 其他好文 > 详细

自定义粒度,snmp流量监控,业务突发可视化

时间:2021-07-05 16:57:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cout   mmu   argv   使用   usr   ESS   nmp   pytho   --   

现在主流监控软件和云平台提供的流量监控,监控粒度最小只能设置为1分钟,无法准确定位故障,特别是瞬时突发较大的业务

对比python的snmp库还是更喜欢用subprocess调用snmpwalk命令,脚本如下,前端可以使用脚本采集到的数据进行绘图

 1 #! /usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 import time,subprocess,re,signal,os,sys
 4 class traffic:  
 5     def __init__(self,ip,port,community,interval):
 6         cmd_index = snmpwalk -v 2c -c %s %s  ifDescr | grep -w %s%(community,ip,port) 
 7         portindex = subprocess.Popen(cmd_index,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
 8         ret = re.findall(ifDescr.\d+,portindex.decode(utf8))[0]
 9         portMIB = int(re.findall(\d+,ret)[0])
10         self.cmd_in=snmpwalk -v 2c -c %s %s ifHCInOctets.%s%(community,ip,portMIB) 
11         self.cmd_out=snmpwalk -v 2c -c %s %s ifHCOutOctets.%s%(community,ip,portMIB) 
12         self.interval = interval
13     def form(self,data):
14         ret = re.findall(Counter64:.*,data.decode(utf8))[0]
15         ret = ret.split(:)[1].strip()
16         ret = round(int(ret)*8/1024/1024,2)
17         return ret
18     def snmp(self):
19         traffic_in =  subprocess.Popen(self.cmd_in,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
20         traffic_out = subprocess.Popen(self.cmd_out,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]
21         traffic_in = self.form(traffic_in)
22         traffic_out = self.form(traffic_out)
23         return(traffic_in,traffic_out)
24     def gettraffic(self):
25         befo_in,befo_out = self.snmp()
26         time.sleep(self.interval)
27         after_in,after_out = self.snmp()
28         return ({in:round((after_in-befo_in)/self.interval,2),out:round((after_out-befo_out)/self.interval,2)})
29 def signal_handler(*args, **kwargs):
30     try:
31         if count!=0:
32             print(----------------------------end-----------------------------)
33             print(               min          max          avg)
34             print(incoming    %sM        %sM        %sM%(traffic_in_min,traffic_in_max,round(traffic_in_sum/count,2)))
35             print(outgoing    %sM        %sM        %sM%(traffic_out_min,traffic_out_max,round(traffic_out_sum/count,2)))
36     except:
37         os._exit(0)
38     os._exit(0)
39 if __name__  == "__main__":
40     signal.signal(signal.SIGINT, signal_handler)
41     traffic_in_max = 0
42     traffic_out_max = 0
43     traffic_in_min = 999999999999
44     traffic_out_min = 999999999999
45     traffic_in_sum = 0
46     traffic_out_sum = 0
47     count = 0
48     try:
49         host = sys.argv[1]
50         port = sys.argv[2]
51         community = sys.argv[3]
52         interval = int(sys.argv[4])
53         mx =traffic(host,port,community,interval)
54         print(-------------%s %s秒粒度流量监控------------------%(host,interval))
55         while True:
56             ret = mx.gettraffic()
57             traffic_in = ret[in]
58             traffic_out = ret[out]
59             traffic_in_max = max(traffic_in,traffic_in_max)
60             traffic_out_max = max(traffic_out,traffic_out_max)
61             traffic_in_min = min(traffic_in,traffic_in_min)
62             traffic_out_min = min(traffic_out,traffic_out_min)
63             traffic_in_sum += traffic_in
64             traffic_out_sum += traffic_out
65             count+=1
66             print(traffic_in: %s Mbps%traffic_in,traffic_out: %s Mbps%traffic_out)
67     except:
68         print(usage:)
69         print(    <host> <port> <community> <interval>)
70         print(options:)
71         print(    host: <dest_ip>)
72         print(    portMIB <dest port>)
73         print(    community: <community>)
74         print(    interval: <get data interval>)
75         print(examples:)
76         print(    ./traffic.py 10.0.0.1 ge-0/0/46 public 5)

效果如下

 1 $ ./traffic.py
 2 usage:
 3     <host> <port> <community> <interval>
 4 options:
 5     host: <dest_ip>
 6     port <dest port>
 7     community: <community>
 8     interval: <get data interval>
 9 examples:
10     ./traffic.py 10.0.0.1 ge-0/0/46 public 5
11 
12 
13 $ ./traffic.py 10.0.0.1 ge-0/0/46 public 5
14 -------------10.0.0.1 5秒粒度流量监控------------------
15 traffic_in: 7.65 Mbps traffic_out: 42.16 Mbps
16 traffic_in: 14.98 Mbps traffic_out: 51.32 Mbps
17 traffic_in: 13.96 Mbps traffic_out: 51.99 Mbps
18 traffic_in: 8.18 Mbps traffic_out: 44.42 Mbps
19 traffic_in: 8.99 Mbps traffic_out: 53.03 Mbps
20 traffic_in: 17.66 Mbps traffic_out: 49.03 Mbps
21 traffic_in: 47.14 Mbps traffic_out: 49.27 Mbps
22 traffic_in: 71.05 Mbps traffic_out: 50.45 Mbps
23 traffic_in: 72.73 Mbps traffic_out: 48.16 Mbps
24 ^C----------------------------end-----------------------------
25                min          max          avg
26 incoming    7.65M        72.73M        29.15M
27 outgoing    42.16M        53.03M        48.87M

 

自定义粒度,snmp流量监控,业务突发可视化

标签:cout   mmu   argv   使用   usr   ESS   nmp   pytho   --   

原文地址:https://www.cnblogs.com/darkchen/p/14949401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!