我创建了以下脚本,该脚本提供了为Linux系统定义的文件系统用法。我已经设置了阈值来检查状态,如果它超过了它的简单说FS超过90%并且工作正常。因此,无论是在90或100的阈值之后,它只是简单地说it's more than 90%
。
现在我唯一期待把它从if条件中得到的实际值放在一边,不知怎的,我现在无法得到它。
所以,试着去了解How to get the value of if condition and store in a variable in python
。
感谢您对此脚本或脚本的任何更改提供任何帮助。
import subprocess
import socket
threshold = 90
hst_name = (socket.gethostname())
def fs_function(usage):
return_val = None
try:
return_val = subprocess.Popen(['df', '-Ph', usage], stdout=subprocess.PIPE)
except IndexError:
print "Mount point not found."
return return_val
def show_result(output, mount_name):
if len(output) > 0:
for x in output[1:]:
if int(x.split()[-2][:-1]) >= threshold:
print "Service Status: Filesystem For " + mount_name + " is not normal & it's more than " + str(threshold) + "% on the host",hst_name
else:
print "Service Status: Filesystem For " + mount_name + " is normal on the host",hst_name
def fs_main():
rootfs = fs_function("/")
varfs = fs_function("/var")
tmPfs = fs_function("/tmp")
output = rootfs.communicate()[0].strip().split("\n")
show_result(output, "root (/)")
output = varfs.communicate()[0].strip().split("\n")
show_result(output, "Var (/var)")
output = tmPfs.communicate()[0].strip().split("\n")
show_result(output, "tmp (/tmp)")
fs_main()
上面的脚本给出了如下输出:
Service Status: Filesystem For root (/) is not normal & it's more than 90% on the host noi-karn
Service Status: Filesystem For Var (/var) is normal on the host noi-karn
Service Status: Filesystem For tmp (/tmp) is normal on the host noi-karn
答案 0 :(得分:1)
只需将import psutil
status_normal = "normal"
status_abnormal = "not normal & it's more than {}%".format(threshold)
for partition in psutil.disk_partitions():
usage = psutil.disk_usage(partition.mountpoint)
status = status_abnormal if usage.percent > threshold else status_normal
print "Filesystem For {} is {} on the host {}".format(
partition.mountpoint, status, hst_name)
表达式的结果放入变量中:
x/ (1024.0 * 1024.0 * 1024.0)
你可能想避免重新发明轮子;优秀的psutil
project支持读取磁盘统计信息:
x/ (1024D * 1024D * 1024D)