Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active July 7, 2016 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandout/db1ec004aa73e4262ef0d3cd3023a9e7 to your computer and use it in GitHub Desktop.
Save xandout/db1ec004aa73e4262ef0d3cd3023a9e7 to your computer and use it in GitHub Desktop.
#!/bin/python
"""
python report.py | sendmail user@domain.com
"""
import yum, platform
import os
from collections import namedtuple
DiskUsage = namedtuple('DiskUsage', 'total used free')
def disk_usage(path):
"""
https://stackoverflow.com/a/31856769
Return disk usage statistics about the given path.
Will return the namedtuple with attributes: 'total', 'used' and 'free',
which are the amount of total, used and free space, in bytes.
"""
st = os.statvfs(path)
free = st.f_bavail * st.f_frsize
total = st.f_blocks * st.f_frsize
used = (st.f_blocks - st.f_bfree) * st.f_frsize
return DiskUsage(total, used, free)
def GetHumanReadable(size,precision=2):
"""
https://stackoverflow.com/a/32009595
"""
suffixes=['B','KB','MB','GB','TB']
suffixIndex = 0
while size > 1024 and suffixIndex < 4:
suffixIndex += 1 #increment the index of the suffix
size = size/1024.0 #apply the division
return "%.*f%s"%(precision,size,suffixes[suffixIndex])
base = yum.YumBase()
base.setCacheDir()
package_list = base.doPackageLists(pkgnarrow='updates', patterns='', ignore_case=True)
hostname = platform.uname()[1]
du = disk_usage('/')
duh = DiskUsage(GetHumanReadable(du.total), GetHumanReadable(du.used), GetHumanReadable(du.free))
output = '''
---- Host : %s -----
Needs Updates To
-----------------------
%s
Disk Space
-----------------------
Free : %s
Used : %s
Total : %s
'''
pkgs = [x.name for x in package_list.updates]
st = '\n '.join(pkgs)
print(output % (hostname, st, duh.free, duh.used, duh.total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment