0

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
2
  • You mean you want to have access to the result of int(x.split()[-2][:-1])? Then just put that into a variable before you test, and re-use the variable. Commented May 1, 2017 at 16:48
  • @MartijnPieters, yes that's correct. that i tried but somehow i'm loosing focus how to correctly fit that into logic. Commented May 1, 2017 at 16:49

1 Answer 1

1

Just put the result of the int(x.split()[-2][:-1]) expression into a variable first:

for x in output[1:]:
    perc = int(x.split()[-2][:-1])
    if perc >= threshold:
        # ...

You may want to avoid re-inventing the wheel; the excellent psutil project supports reading disk statistics already:

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)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the guidance and another suggestion to move with psutil.. but i'm unable to use that psutil into my environment due some restrictions that's why using this method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.