-2

how to modify this script to alert me whenever block_usage_pct >90%

from __future__ import with_statement
import contextlib
import os
import sys 
print "Filesystem\tMounted on\tUse%\tIUse%"
with contextlib.closing(open('/etc/mtab')) as fp: 
  for m in fp: 
    fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = m.split()
    if fs_spec.startswith('/'):
      r = os.statvfs(fs_file)
      block_usage_pct = 100.0 - (float(r.f_bavail) / float(r.f_blocks) * 100)
      inode_usage_pct = 100.0 - (float(r.f_favail) / float(r.f_files) * 100)

when i try this it says synatx invalid if (float(block_usage_pct)) >10 print "%s\t%s\t\t%d%%\t%d%%" % (fs_spec, fs_file, block_usage_pct, inode_usage_pct)

cad
  • 337
  • 2
  • 15
  • Stack Overflow is no code writing service. Post your code and tell what the specific problem is. – klutt Jul 10 '17 at 16:32

1 Answers1

0

This answer is a good reference on how to invoke df -h using Python, from there you can process the output and apply whatever rules you want to apply.

Tague Griffith
  • 3,963
  • 2
  • 20
  • 24
  • from __future__ import with_statement import contextlib import os import sys print "Filesystem\tMounted on\tUse%\tIUse%" with contextlib.closing(open('/etc/mtab')) as fp: for m in fp: fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = m.split() if fs_spec.startswith('/'): r = os.statvfs(fs_file) block_usage_pct = 100.0 - (float(r.f_bavail) / float(r.f_blocks) * 100) print "%s\t%s\t\t%d%%\t%d%%" % (fs_spec, fs_file, block_usage_pct, inode_usage_pct) i want to setup alert whenever block_usage_pct >90. – cad Jul 10 '17 at 19:12