Skip to content

Commit

Permalink
Plugin bugfixes/improvements
Browse files Browse the repository at this point in the history
linux_platform not available anymore in python 3.8, replaced by distro.linux_distribution

calculate delta values in iostat plugin to improve data accuracy, added diskstats collumns for kernel 5.5+ and 4.18+
  • Loading branch information
vfuse committed Jun 23, 2020
1 parent 82e87a0 commit 65edf53
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 7 deletions.
79 changes: 76 additions & 3 deletions nixstatsagent/plugins/iostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import sys
import psutil
import plugins
import time
import pprint

def diskstats_parse(dev=None):
file_path = '/proc/diskstats'
Expand All @@ -20,6 +22,18 @@ def diskstats_parse(dev=None):
columns_disk = ['m', 'mm', 'dev', 'reads', 'rd_mrg', 'rd_sectors',
'ms_reading', 'writes', 'wr_mrg', 'wr_sectors',
'ms_writing', 'cur_ios', 'ms_doing_io', 'ms_weighted']
# For kernel 4.18+
columns_disk_418 = ['m', 'mm', 'dev', 'reads', 'rd_mrg', 'rd_sectors',
'ms_reading', 'writes', 'wr_mrg', 'wr_sectors',
'ms_writing', 'cur_ios', 'ms_doing_io', 'ms_weighted',
'discards', 'discards_merged', 'discarded_sectors',
'discarded_time']
# for kernel 5.5+
columns_disk_55 = ['m', 'mm', 'dev', 'reads', 'rd_mrg', 'rd_sectors',
'ms_reading', 'writes', 'wr_mrg', 'wr_sectors',
'ms_writing', 'cur_ios', 'ms_doing_io', 'ms_weighted',
'discards', 'discards_merged', 'discarded_sectors',
'discarded_time', 'flush', 'flush_time']

columns_partition = ['m', 'mm', 'dev', 'reads', 'rd_sectors', 'writes', 'wr_sectors']

Expand All @@ -28,7 +42,11 @@ def diskstats_parse(dev=None):
if line == '':
continue
split = line.split()
if len(split) == len(columns_disk):
if len(split) == len(columns_disk_55):
columns = columns_disk_55
elif len(split) == len(columns_disk_418):
columns = columns_disk_418
elif len(split) == len(columns_disk):
columns = columns_disk
elif len(split) == len(columns_partition):
columns = columns_partition
Expand Down Expand Up @@ -60,8 +78,28 @@ class Plugin(plugins.BasePlugin):
__name__ = 'iostat'

def run(self, *unused):
results = diskstats_parse()
if not results or results is False:
delta_keys = (
'reads',
'writes',
'wr_sectors',
'rd_sectors',
'ms_reading',
'rd_mrg',
'wr_mrg',
'ms_weighted',
'ms_doing_io',
'ms_writing',
'discarded_sectors',
'discarded_time',
'flush',
'flush_time',
'discards'
)
next_cache = {}
next_cache['ts'] = time.time()
prev_cache = self.get_agent_cache() # Get absolute values from previous check
disks = diskstats_parse()
if not disks or disks is False:
results = {}
try:
diskdata = psutil.disk_io_counters(perdisk=True)
Expand All @@ -72,6 +110,41 @@ def run(self, *unused):
results[device] = device_stats
except Exception as e:
results = e.message
else:
results = {}
for device, values in disks.items():
device_stats = {}
next_cache[device] = {}
next_cache[device]['ts'] = time.time()
try:
prev_cache[device]
except:
prev_cache[device] = {}
for key_value, value in values.items():
if key_value in delta_keys:
try:
device_stats[key_value] = self.absolute_to_per_second(key_value, value, prev_cache[device])
except:
pass
next_cache[device][key_value] = value
else:
device_stats[key_value] = value
try:
device_stats['avgrq-sz'] = (device_stats['wr_sectors']+device_stats['rd_sectors']) / (device_stats['reads']+device_stats['writes'])
except:
device_stats['avgrq-sz'] = 0
try:
device_stats['tps'] = device_stats['reads']+device_stats['writes']
except:
device_stats['tps'] = 0
try:
device_stats['usage'] = (100 * device_stats['ms_doing_io']) / (1000 * (next_cache['ts'] - prev_cache['ts']))
except:
device_stats['usage'] = 0

results[device] = device_stats

self.set_agent_cache(next_cache)
return results


Expand Down
11 changes: 8 additions & 3 deletions nixstatsagent/plugins/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import time
import psutil
import plugins

try:
import distro
except ImportError:
distro = None

def systemCommand(Command, newlines=True):
Output = ""
Error = ""
try:
# Output = subprocess.check_output(Command, stderr = subprocess.STDOUT, shell='True')
proc = Popen(Command.split(), stdout=PIPE)
Output = proc.communicate()[0]
except Exception:
Expand Down Expand Up @@ -81,7 +83,10 @@ def run(self, *unused):
cpu['count'] = 0
mem = psutil.virtual_memory()
if sys.platform == "linux" or sys.platform == "linux2":
systeminfo['os'] = str(' '.join(platform.linux_distribution()))
if distro is None:
systeminfo['os'] = str(' '.join(platform.linux_distribution()))
else:
systeminfo['os'] = str(' '.join(distro.linux_distribution(full_distribution_name=True)))
elif sys.platform == "darwin":
systeminfo['os'] = "Mac OS %s" % platform.mac_ver()[0]
cpu['brand'] = str(systemCommand('sysctl machdep.cpu.brand_string', False)[0]).split(': ')[1]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

readme = open(os.path.join(here, 'README.md')).read()
if sys.version.startswith('3.'):
install_requires = ['psutil', 'netifaces', 'configparser', 'future']
install_requires = ['psutil', 'netifaces', 'configparser', 'future', 'distro']
elif sys.version.startswith('2.6'):
install_requires = ['psutil', 'netifaces', 'configparser==3.5.0', 'future']
else:
Expand Down

0 comments on commit 65edf53

Please sign in to comment.