最近项目需求,需要有坏盘告警。
查了下有个rpm包:gsmartcontrol使用其smartctl命令可以获取是否坏盘。故代码如下:
from resource_monitoring import psutil
import subprocess
pdisks = psutil.disk_partitions()#获取分区
disks = dict()
badnum = 0
totalnum = len(pdisks)
for pdisk in pdisks:
print pdisk.device#获取盘符
cmd = "smartctl -H "+pdisk.device +" | grep 'self-assessment'"
print cmd
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
content = p.stdout.read()
print p.stdout.read()
if "PASSED" in content:
num = num +1
print num
except:
print "error"
print num
if num >0 :
print "the sum of baddisk is " + num;
else:
print "all disks is good"