专栏首页python3python监控系统资源

python监控系统资源

监控网卡流量

#!/usr/bin/python

import re

import os

#get SNMP-MIB2 of the devices

def getAllitems(host,oid):

        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]

        return sn1

                                                                                          

#get network device

def getDevices(host):

        device_mib = getAllitems(host,'RFC1213-MIB::ifDescr')

        device_list = []

        for item in device_mib:

                if re.search('eth',item):

                        device_list.append(item.split(':')[3].strip())

        return device_list

                                                                                          

#get network date

def getDate(host,oid):

        date_mib = getAllitems(host,oid)[1:]

        date = []

        for item in date_mib:

                byte = float(item.split(':')[3].strip())

                date.append(str(round(byte/1024,2)) + ' KB')

        return date

                                                                                          

if __name__ == '__main__':

        hosts = ['192.168.30.111','192.168.30.112']

        for host in hosts:

                device_list = getDevices(host)

                                                                                          

                inside = getDate(host,'IF-MIB::ifInOctets')

                outside = getDate(host,'IF-MIB::ifOutOctets')

                                                                                          

                print '==========' + host + '=========='

                for i in range(len(inside)):

                        print '%s : RX: %-15s   TX: %s ' % (device_list[i], inside[i], outside[i])

                print

监控内存(swap)使用率

#!/usr/bin/python

import os

def getAllitems(host, oid):

        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]

        return sn1

                                                                            

def getSwapTotal(host):

        swap_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalSwap.0')[0].split(' ')[3]

        return swap_total

                                                                            

def getSwapUsed(host):

        swap_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailSwap.0')[0].split(' ')[3]

        swap_total = getSwapTotal(host)

        swap_used = str(round(((float(swap_total)-float(swap_avail))/float(swap_total))*100 ,2)) + '%'

        return swap_used

                                                                            

def getMemTotal(host):

        mem_total = getAllitems(host, 'UCD-SNMP-MIB::memTotalReal.0')[0].split(' ')[3]

        return mem_total

                                                                            

def getMemUsed(host):

        mem_total = getMemTotal(host)

        mem_avail = getAllitems(host, 'UCD-SNMP-MIB::memAvailReal.0')[0].split(' ')[3]

        mem_used = str(round(((float(mem_total)-float(mem_avail))/float(mem_total))*100 ,2)) + '%'

        return mem_used

                                                                            

if __name__ == '__main__':

        hosts = ['192.168.30.111','192.168.30.112']

        print "Monitoring Memory Usage"

        for host in hosts:

                mem_used = getMemUsed(host)

                swap_used = getSwapUsed(host)

                print '==========' + host + '=========='

                print 'Mem_Used = %-15s   Swap_Used = %-15s' % (mem_used, swap_used)

                print

监控系统负载

#!/usr/bin/python

import os

def getAllitems(host, oid):

        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')

        return sn1

                                                                     

def getload(host,loid):

        load_oids = '1.3.6.1.4.1.2021.10.1.3.' + str(loid)

        return getAllitems(host,load_oids)[0].split(':')[3]

                                                                     

if __name__ == '__main__':

        hosts = ['192.168.30.111','192.168.30.112']

        #check_system_load

        print '==============System Load=============='

        for host in hosts:

                load1 = getload(host, 1)

                load10 = getload(host, 2)

                load15 = getload(host, 3)

                print '%s load(1min): %s ,load(10min): %s ,load(15min): %s' % (host,load1,load10,load15)

监控CPU

#!/usr/bin/python

import os

def getAllitems(host, oid):

        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid + '|grep Raw|grep Cpu|grep -v Kernel').read().split('\n')[:-1]

        return sn1

                                                                

def getDate(host):

        items = getAllitems(host, '.1.3.6.1.4.1.2021.11')

                                                                

        date = []

        rate = []

        cpu_total = 0

        #us = us+ni, sy = sy + irq + sirq

        for item in items:

                float_item = float(item.split(' ')[3])

                cpu_total += float_item

                if item == items[0]:

                        date.append(float(item.split(' ')[3]) + float(items[1].split(' ')[3]))

                elif item == item[2]:

                        date.append(float(item.split(' ')[3] + items[5].split(' ')[3] + items[6].split(' ')[3]))

                else:

                        date.append(float_item)

                                                                

        #calculate cpu usage percentage

        for item in date:

                rate.append((item/cpu_total)*100)

                                                                

        mean = ['%us','%ni','%sy','%id','%wa','%cpu_irq','%cpu_sIRQ']

                                                                

        #calculate cpu usage percentage

        result = map(None,rate,mean)

        return result

                                                                

if __name__ == '__main__':

        hosts = ['192.168.30.111','192.168.30.112']

        for host in hosts:

                print '==========' + host + '=========='

                result = getDate(host)

                print 'Cpu(s)',

                #print result

                for i in range(5):

                        print ' %.2f%s' % (result[i][0],result[i][1]),

                print

                print

监控磁盘

#!/usr/bin/python

import re

import os

def getAllitems(host,oid):

        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]

        return sn1

                                                             

def getDate(source,newitem):

        for item in source[5:]:

                newitem.append(item.split(':')[3].strip())

        return newitem

                                                             

def getRealDate(item1,item2,listname):

        for i in range(len(item1)):

                listname.append(int(item1[i])*int(item2[i])/1024)

        return listname

                                                             

def caculateDiskUsedRate(host):

        hrStorageDescr = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageDescr')

        hrStorageUsed = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageUsed')

        hrStorageSize = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageSize')

        hrStorageAllocationUnits = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits')

                                                             

        disk_list = []

        hrsused = []

        hrsize = []

        hrsaunits = []

                                                             

        #get disk_list

        for item in hrStorageDescr:

                if re.search('/',item):

                        disk_list.append(item.split(':')[3])

        #print disk_list      

                                                             

        getDate(hrStorageUsed,hrsused)

        getDate(hrStorageSize,hrsize)

        #print getDate(hrStorageAllocationUnits,hrsaunits)

                                                             

        #get hrstorageAllocationUnits

        for item in hrStorageAllocationUnits[5:]:

                hrsaunits.append(item.split(':')[3].strip().split(' ')[0])

        #caculate the result

        #disk_used = hrStorageUsed * hrStorageAllocationUnits /1024 (KB)

        disk_used = []

        total_size = []

        disk_used = getRealDate(hrsused,hrsaunits,disk_used)

        total_size = getRealDate(hrsize,hrsaunits,total_size)

                                                             

        diskused_rate = []

        for i in range(len(disk_used)):

                diskused_rate.append(str(round((float(disk_used[i])/float(total_size[i])*100), 2)) + '%')

                                                             

        return diskused_rate,disk_list

                                                             

if __name__ == '__main__':

        hosts = ['192.168.30.111','192.168.30.112']

        for host in hosts:

                result = caculateDiskUsedRate(host)

                diskused_rate = result[0]

                partition = result[1]

                print "==========",host,'=========='

                for i in range(len(diskused_rate)):

                        print '%-20s used: %s' % (partition[i],diskused_rate[i])

                print

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Python——献给那些对Python面

        面向对象,看似不难。有的同学学过之后,还是不知道如何去使用它。有时候编写代码,写着写着就遇到坑了,比如写着写着就连你自己也在怀疑到底是不是面向对象编程了...

    py3study
  • Python开发爆破工具

    实际去安装一些FTP软件比较困难,我这里就用Metasploitable Linux

    py3study
  • python中获取某月的天数

    >>(datetime.datetime(2010.3) - datetime.datetime(2010.2)).days

    py3study
  • Java HTTP Host 头攻击原理以及如何防御

    很对微信群里的网友,我都称他们为老铁,从来不称兄道弟的,因为我觉得不真实。不向某东,说一套做一套。

    业余草
  • 大数据和云计算技术周报(第61期)

    大数据” 三个字其实是个marketing语言,从技术角度看,包含范围很广,计算、存储、网络都涉及,知识点广、学习难度高。

    大数据和云计算技术
  • Jenkins运行恢复的Job出错

    今天由于误操作,删除了一个Job,在我重新恢复这个Job之后,重新运行的时候报如下错误

    kongxx
  • Python 之 获取Host Esxi

    py3study
  • 大数据系列之数据质量浅探

    数据质量管理(Data Quality Management),是指对数据从计划、获取、存储、共享、维护、应用、消亡生命周期的每个阶段里可能引发的各类数据质量...

    苦叶子
  • 【转】flash builder优化方案

    要使用fb进行开发的时候,经常会遇到内存不足,控制台需要退出等问题,还有就是打包生成发布文件的时候,会遇到XX问题而失败…

    meteoric
  • OD常用断点

    用户2135432

扫码关注云+社区

领取腾讯云代金券