Python 写的几个监控脚本(CPU,内存,网卡流量,负载,磁盘空间)

释放双眼,带上耳机,听听看~!

    刚刚学python,根据需求自己动手写了几个监控系统性能的脚本,原理比较简单,都是通过SNMP协议获取系统信息,再进行相应的计算和格式化,最后输出结果。

    以下的脚本都是通过os.popen模块调用snmpwalk 命令获取到的MIB 信息,原本想用py-snmp,不过介于网上资源太少,而且官网的例子看不太懂,只能用这种方法替代。后期再尝试用py-snmp来做这些事情。由于这些脚本都是调用snmpwalk命令来获取信息,所以被监控的机器上需要支持snmp,可以执行

# yum install -y net-snmp*

下面是几个脚本的代码,第一次写,写的不够规范,也不够简洁,还请大家多指教。

监控网卡流量


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
1#!/usr/bin/python
2import re
3import os
4#get SNMP-MIB2 of the devices
5def getAllitems(host,oid):
6        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
7        return sn1
8                                                                                        
9#get network device
10def getDevices(host):
11        device_mib = getAllitems(host,'RFC1213-MIB::ifDescr')
12        device_list = []
13        for item in device_mib:
14                if re.search('eth',item):
15                        device_list.append(item.split(':')[3].strip())
16        return device_list
17                                                                                        
18#get network date
19def getDate(host,oid):
20        date_mib = getAllitems(host,oid)[1:]
21        date = []
22        for item in date_mib:
23                byte = float(item.split(':')[3].strip())
24                date.append(str(round(byte/1024,2)) + ' KB')
25        return date
26                                                                                        
27if __name__ == '__main__':
28        hosts = ['192.168.30.111','192.168.30.112']
29        for host in hosts:
30                device_list = getDevices(host)
31                                                                                        
32                inside = getDate(host,'IF-MIB::ifInOctets')
33                outside = getDate(host,'IF-MIB::ifOutOctets')
34                                                                                        
35                print '==========' + host + '=========='
36                for i in range(len(inside)):
37                        print '%s : RX: %-15s   TX: %s ' % (device_list[i], inside[i], outside[i])
38                print
39

监控内存(swap)使用率


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
1#!/usr/bin/python
2import os
3def getAllitems(host, oid):
4        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
5        return sn1
6                                                                          
7def getSwapTotal(host):
8        swap_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalSwap.0')[0].split(' ')[3]
9        return swap_total
10                                                                          
11def getSwapUsed(host):
12        swap_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailSwap.0')[0].split(' ')[3]
13        swap_total = getSwapTotal(host)
14        swap_used = str(round(((float(swap_total)-float(swap_avail))/float(swap_total))*100 ,2)) + '%'
15        return swap_used
16                                                                          
17def getMemTotal(host):
18        mem_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalReal.0')[0].split(' ')[3]
19        return mem_total
20                                                                          
21def getMemUsed(host):
22        mem_total = getMemTotal(host)
23        mem_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailReal.0')[0].split(' ')[3]
24        mem_used = str(round(((float(mem_total)-float(mem_avail))/float(mem_total))*100 ,2)) + '%'
25        return mem_used
26                                                                          
27if __name__ == '__main__':
28        hosts = ['192.168.30.111','192.168.30.112']
29        print "Monitoring Memory Usage"
30        for host in hosts:
31                mem_used = getMemUsed(host)
32                swap_used = getSwapUsed(host)
33                print '==========' + host + '=========='
34                print 'Mem_Used = %-15s   Swap_Used = %-15s' % (mem_used, swap_used)
35                print
36

监控系统负载


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1#!/usr/bin/python
2import os
3def getAllitems(host, oid):
4        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')
5        return sn1
6                                                                    
7def getload(host,loid):
8        load_oids = '1.3.6.1.4.1.2021.10.1.3.' + str(loid)
9        return getAllitems(host,load_oids)[0].split(':')[3]
10                                                                    
11if __name__ == '__main__':
12        hosts = ['192.168.30.111','192.168.30.112']
13        #check_system_load
14        print '==============System Load=============='
15        for host in hosts:
16                load1 = getload(host, 1)
17                load10 = getload(host, 2)
18                load15 = getload(host, 3)
19                print '%s load(1min): %s ,load(10min): %s ,load(15min): %s' % (host,load1,load10,load15)
20

监控CPU


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
1#!/usr/bin/python
2import os
3def getAllitems(host, oid):
4        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid + '|grep Raw|grep Cpu|grep -v Kernel').read().split('\n')[:-1]
5        return sn1
6                                                              
7def getDate(host):
8        items = getAllitems(host, '.1.3.6.1.4.1.2021.11')
9                                                              
10        date = []
11        rate = []
12        cpu_total = 0
13        #us = us+ni, sy = sy + irq + sirq
14        for item in items:
15                float_item = float(item.split(' ')[3])
16                cpu_total += float_item
17                if item == items[0]:
18                        date.append(float(item.split(' ')[3]) + float(items[1].split(' ')[3]))
19                elif item == item[2]:
20                        date.append(float(item.split(' ')[3] + items[5].split(' ')[3] + items[6].split(' ')[3]))
21                else:
22                        date.append(float_item)
23                                                              
24        #calculate cpu usage percentage
25        for item in date:
26                rate.append((item/cpu_total)*100)
27                                                              
28        mean = ['%us','%ni','%sy','%id','%wa','%cpu_irq','%cpu_sIRQ']
29                                                              
30        #calculate cpu usage percentage
31        result = map(None,rate,mean)
32        return result
33                                                              
34if __name__ == '__main__':
35        hosts = ['192.168.30.111','192.168.30.112']
36        for host in hosts:
37                print '==========' + host + '=========='
38                result = getDate(host)
39                print 'Cpu(s)',
40                #print result
41                for i in range(5):
42                        print ' %.2f%s' % (result[i][0],result[i][1]),
43                print
44                print
45

监控磁盘


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
1#!/usr/bin/python
2import re
3import os
4def getAllitems(host,oid):
5        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
6        return sn1
7                                                            
8def getDate(source,newitem):
9        for item in source[5:]:
10                newitem.append(item.split(':')[3].strip())
11        return newitem
12                                                            
13def getRealDate(item1,item2,listname):
14        for i in range(len(item1)):
15                listname.append(int(item1[i])*int(item2[i])/1024)
16        return listname
17                                                            
18def caculateDiskUsedRate(host):
19        hrStorageDescr = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageDescr')
20        hrStorageUsed = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageUsed')
21        hrStorageSize = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageSize')
22        hrStorageAllocationUnits = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits')
23                                                            
24        disk_list = []
25        hrsused = []
26        hrsize = []
27        hrsaunits = []
28                                                            
29        #get disk_list
30        for item in hrStorageDescr:
31                if re.search('/',item):
32                        disk_list.append(item.split(':')[3])
33        #print disk_list      
34                                                            
35        getDate(hrStorageUsed,hrsused)
36        getDate(hrStorageSize,hrsize)
37        #print getDate(hrStorageAllocationUnits,hrsaunits)
38                                                            
39        #get hrstorageAllocationUnits
40        for item in hrStorageAllocationUnits[5:]:
41                hrsaunits.append(item.split(':')[3].strip().split(' ')[0])
42        #caculate the result
43        #disk_used = hrStorageUsed * hrStorageAllocationUnits /1024 (KB)
44        disk_used = []
45        total_size = []
46        disk_used = getRealDate(hrsused,hrsaunits,disk_used)
47        total_size = getRealDate(hrsize,hrsaunits,total_size)
48                                                            
49        diskused_rate = []
50        for i in range(len(disk_used)):
51                diskused_rate.append(str(round((float(disk_used[i])/float(total_size[i])*100), 2)) + '%')
52                                                            
53        return diskused_rate,disk_list
54                                                            
55if __name__ == '__main__':
56        hosts = ['192.168.30.111','192.168.30.112']
57        for host in hosts:
58                result = caculateDiskUsedRate(host)
59                diskused_rate = result[0]
60                partition = result[1]
61                print "==========",host,'=========='
62                for i in range(len(diskused_rate)):
63                        print '%-20s used: %s' % (partition[i],diskused_rate[i])
64                print
65

执行结果

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

Bootstrap 间隔 (Spacing)

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索