I have created below script which gives the File-system usage as defined for the Linux systems. I have set the threshold value to check the status, if it goes above its simply says FS is more than 90% and that's working fine. So, whatever comes after threshold either 90 or 100 its simply says it's more than 90%.
Now only thing i'm looking forward to put the actual value which it gets from the if condition, somehow i'm unable to get that at this point.
So, trying to know,How to get the value of if condition and store in a variable in python.
appreciate any help on this or any changes on the script.
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()
The above script gives the ouput like below:
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
int(x.split()[-2][:-1])? Then just put that into a variable before you test, and re-use the variable.