From b820b80140b7bd3183f3f06e90db48e55157add3 Mon Sep 17 00:00:00 2001 From: Xavier Naveira Date: Mon, 25 May 2015 11:17:26 +0200 Subject: [PATCH 1/2] Added grain for osdisk stats --- grains/osdisk.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 grains/osdisk.py diff --git a/grains/osdisk.py b/grains/osdisk.py new file mode 100644 index 0000000..0d9ba24 --- /dev/null +++ b/grains/osdisk.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +import os + +def get_osdisk_stats(): +""" + Calculates and resturns the disk used, available and total capacity + in gigabytes. Calculations code from: + http://www.stealthcopter.com/blog/2009/09/python-diskspace/ +""" + grains = {} + grains['osdisk'] = {} + disk = os.statvfs("/") + + capacity = disk.f_bsize * disk.f_blocks + available = disk.f_bsize * disk.f_bavail + used = disk.f_bsize * (disk.f_blocks - disk.f_bavail) + + # print information in bytes + #print used, available, capacity + # print information in Kilobytes + #print used/1024, available/1024, capacity/1024 + # print information in Megabytes + #print used/1.048576e6, available/1.048576e6, capacity/1.048576e6 + # print information in Gigabytes + #print used/1.073741824e9, available/1.073741824e9, capacity/1.073741824e9 + + grains['osdisk']['used'] = int(round(used/1.073741824e9)) + grains['osdisk']['available'] = int(round(available/1.073741824e9)) + grains['osdisk']['capacity'] = int(round(capacity/1.073741824e9)) + + return grains From b9fb3df56378c10719edd644f60a680045ae6411 Mon Sep 17 00:00:00 2001 From: Xavier Naveira Date: Mon, 25 May 2015 11:33:05 +0200 Subject: [PATCH 2/2] Corrected typo, indentation and removed trailing spaces --- grains/osdisk.py | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/grains/osdisk.py b/grains/osdisk.py index 0d9ba24..4471f80 100644 --- a/grains/osdisk.py +++ b/grains/osdisk.py @@ -1,31 +1,31 @@ #!/usr/bin/env python -import os - +import os + def get_osdisk_stats(): -""" - Calculates and resturns the disk used, available and total capacity - in gigabytes. Calculations code from: - http://www.stealthcopter.com/blog/2009/09/python-diskspace/ -""" - grains = {} - grains['osdisk'] = {} - disk = os.statvfs("/") - - capacity = disk.f_bsize * disk.f_blocks - available = disk.f_bsize * disk.f_bavail - used = disk.f_bsize * (disk.f_blocks - disk.f_bavail) - - # print information in bytes - #print used, available, capacity - # print information in Kilobytes - #print used/1024, available/1024, capacity/1024 - # print information in Megabytes - #print used/1.048576e6, available/1.048576e6, capacity/1.048576e6 - # print information in Gigabytes - #print used/1.073741824e9, available/1.073741824e9, capacity/1.073741824e9 + ''' + Calculates and returns the disk used, available and total capacity + in gigabytes. Calculations code from: + http://www.stealthcopter.com/blog/2009/09/python-diskspace/ + ''' + grains = {} + grains['osdisk'] = {} + disk = os.statvfs("/") + + capacity = disk.f_bsize * disk.f_blocks + available = disk.f_bsize * disk.f_bavail + used = disk.f_bsize * (disk.f_blocks - disk.f_bavail) + + # print information in bytes + #print used, available, capacity + # print information in Kilobytes + #print used/1024, available/1024, capacity/1024 + # print information in Megabytes + #print used/1.048576e6, available/1.048576e6, capacity/1.048576e6 + # print information in Gigabytes + #print used/1.073741824e9, available/1.073741824e9, capacity/1.073741824e9 - grains['osdisk']['used'] = int(round(used/1.073741824e9)) - grains['osdisk']['available'] = int(round(available/1.073741824e9)) - grains['osdisk']['capacity'] = int(round(capacity/1.073741824e9)) + grains['osdisk']['used'] = int(round(used/1.073741824e9)) + grains['osdisk']['available'] = int(round(available/1.073741824e9)) + grains['osdisk']['capacity'] = int(round(capacity/1.073741824e9)) - return grains + return grains