pythonwindos磁盘监视器(snmp),这两个脚本如何合并成一个脚本?

2024-05-29 05:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须使用主动监控,我需要监控到windos的整个硬盘,通过硬盘盘符的方式传输参数。如果我实现了我想要的,你需要这三个剧本。谁能帮我改进下一个吗? 我想要一个脚本来完成这两个脚本的工作。如何合并?在

1:代码Cdisk

import sys
import os
import commands
warning = sys.argv[2]
critical = sys.argv[3]
ip =sys.argv[5]
#val = sys.argv[1]
StorageAllocationUnitsC="1.3.6.1.2.1.25.2.3.1.4.1"#last4.1=Cdisk,4.2=Ddisk....
StorageSizeC="1.3.6.1.2.1.25.2.3.1.5.1"#last5.1=Cdisk,5.2=Ddisk....
StorageUsedC="1.3.6.1.2.1.25.2.3.1.6.1"#last6.1=Cdisk,5.2=Ddisk....

StorageAll = "snmpwalk -v 2c -c public %s %s" % (ip,StorageAllocationUnitsC)
(r_c,r_e) = commands.getstatusoutput(StorageAll)
if r_c != 0:
    print "C - snmpwalk is Error."
else:
    StorageAllC = r_e.split("INTEGER: ")[-1][:4]

StorageSize = "snmpwalk -v 2c -c public %s %s" % (ip,StorageSizeC)
(r_c,r_e) = commands.getstatusoutput(StorageSize)
if r_c != 0:
     print "C - snmpwalk is Error."
else:
     StorageSize = r_e.split("INTEGER: ")[1]

StorageUsed = "snmpwalk -v 2c -c public %s %s" % (ip,StorageUsedC)
(r_c,r_e) = commands.getstatusoutput(StorageUsed)
if r_c !=0:
    print "C - snmpwalk is not value."
else:
    StorageUsed =  r_e.split("INTEGER: ")[1]



diskall = int(StorageAllC)*int(StorageSize)/1024/1024/1024
diskusd = int(StorageAllC)*int(StorageUsed)/1024/1024/1024
diskrate = round(float(diskusd)/float(diskall)*100,2)

if int(sys.argv[2]) > int(diskrate):
    print "Critical - pls check Cdiskrate %s" % diskrate + "%"
    ret = 2
elif int (sys.argv[3]) > int(diskrate):
    print "Warning - pls check Cdiskrate %s" % diskrate + "%"
    ret = 1
else:
    print "OK - good! %s" % diskrate + "%"
    ret =0
sys.exit(ret)

2:代码Ddisk

^{pr2}$

Tags: ipifsyselsecommandsintprintret
1条回答
网友
1楼 · 发布于 2024-05-29 05:26:36

你真正需要做的就是组合一些变量。有更好的方法来构建snmp查询,但这超出了这个问题的范围。在

import sys
import os
import commands

warning = sys.argv[2]
critical = sys.argv[3]
ip =sys.argv[5]
#val = sys.argv[1]

StorageAllocationUnitsC ="1.3.6.1.2.1.25.2.3.1.4.1"
StorageAllocationUnitsD ="1.3.6.1.2.1.25.2.3.1.4.2"
StorageSizeC="1.3.6.1.2.1.25.2.3.1.5.1"
StorageSizeD="1.3.6.1.2.1.25.2.3.1.5.2"
StorageUsedC="1.3.6.1.2.1.25.2.3.1.6.1"
StorageUsedD="1.3.6.1.2.1.25.2.3.1.6.2"

results = dict()

for oid,descr in [(StorageAllocationUnitsC,"C_StorageUnits"), 
    (StorageAllocationUnitsD,"D_StorageUnits")]:
    StorageAll = "snmpwalk -v 2c -c public %s %s" % (ip,oid)
    (r_c,r_e) = commands.getstatusoutput(StorageAll)
    if r_c != 0:
        print "%s - snmpwalk is Error." % descr
    else:
        results[descr] = r_e.split("INTEGER: ")[-1][:4]

for oid,descr in [(StorageSizeC, "C_StorageSize"), 
    (StorageSizeD, "D_StorageSize"), (StorageUsedC, "C_StorageUsed"), 
    (StorageUsedD, "D_StorageUsed")]:
    Storage = "snmpwalk -v 2c -c public %s %s" % (ip,oid)
    (r_c,r_e) = commands.getstatusoutput(Storage)
    if r_c != 0:
         print "%s - snmpwalk is Error." % descr
    else:
         results[descr] = r_e.split("INTEGER: ")[1]

所有结果都存储在名为results的python字典中

相关问题 更多 >

    热门问题