Skip to content

Commit

Permalink
Plugin fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vfuse committed Oct 15, 2018
1 parent d28e3ce commit f06ca12
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 25 deletions.
2 changes: 1 addition & 1 deletion nixstatsagent/nixstatsagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import urllib2


__version__ = '1.1.44'
__version__ = '1.1.45'

__FILEABSDIRNAME__ = os.path.dirname(os.path.abspath(__file__))

Expand Down
120 changes: 120 additions & 0 deletions nixstatsagent/plugins/elasticsearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
import time
import plugins
import json
import collections


class Plugin(plugins.BasePlugin):
__name__ = 'elasticsearch'

def run(self, config):
'''
experimental monitoring plugin for elasticsearch
Add to /etc/nixstats.ini:
[elasticsearch]
enabled = yes
status_page_url = http://127.0.0.1:9200/_stats
'''

def ascii_encode_dict(data):
ascii_encode = lambda x: x.encode('ascii') if isinstance(x, unicode) else x
return dict(map(ascii_encode, pair) for pair in data.items())

results = dict()
next_cache = dict()
request = urllib2.Request(config.get('elasticsearch', 'status_page_url'))
raw_response = urllib2.urlopen(request)
next_cache['ts'] = time.time()
prev_cache = self.get_agent_cache() # Get absolute values from previous check
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
try:
j = flatten(json.loads(raw_response.read(), object_hook=ascii_encode_dict)['_all']['total'])
except Exception:
return False


delta_keys = (
'get_time_in_millis',
'indexing_index_time_in_millis',
'flush_total_time_in_millis',
'indexing_delete_time_in_millis',
'indexing_index_time_in_millis',
'indexing_throttle_time_in_millis',
'merges_total_stopped_time_in_millis',
'merges_total_throttled_time_in_millis',
'merges_total_time_in_millis',
'recovery_throttle_time_in_millis',
'refresh_total_time_in_millis',
'search_fetch_time_in_millis',
'search_query_time_in_millis',
'search_scroll_time_in_millis',
'search_suggest_time_in_millis',
'warmer_total_time_in_millis',
'docs_count',
'docs_deleted',
'flush_total',
'get_exists_total',
'get_missing_total',
'get_total',
'indexing_delete_total',
'indexing_index_total',
'indexing_noop_update_total',
'merges_total',
'merges_total_docs',
'merges_total_auto_throttle_in_bytes',
'query_cache_cache_count',
'query_cache_cache_size',
'query_cache_evictions',
'query_cache_hit_count',
'query_cache_miss_count',
'query_cache_total_count',
'refresh_total',
'request_cache_hit_count',
'request_cache_miss_count',
'search_fetch_total',
'search_open_contexts',
'search_query_total',
'search_scroll_total',
'search_suggest_total',
'segments_count',
'segments_max_unsafe_auto_id_timestamp',
'warmer_total',
'get_exists_time_in_millis',
'get_missing_time_in_millis'
)

data = {}
constructors = [str, float]
for key, value in j.items():
key = key.lower().strip()
for c in constructors:
try:
value = c(value)
except ValueError:
pass
if key in delta_keys and type(value) is not str:
j[key] = self.absolute_to_per_second(key, float(value), prev_cache)
data[key] = float(value)
else:
pass

data['ts'] = time.time()
# Cache absolute values for next check calculations
self.set_agent_cache(data)

return j


if __name__ == '__main__':
Plugin().execute()
33 changes: 33 additions & 0 deletions nixstatsagent/plugins/gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import plugins
import sys

class Plugin(plugins.BasePlugin):
__name__ = 'gpu'

def run(self, *unused):
'''
expirimental plugin used to collect GPU load from OpenHardWareMonitor (Windows)
'''
data = {}

if sys.platform == "win32":
try:
import wmi
except:
return 'wmi module not installed.'
try:
w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
for sensor in temperature_infos:
if sensor.SensorType==u'Load' and sensor.Name==u'GPU Core':
data[sensor.Parent.replace('/','-').strip('-')] = sensor.Value
except:
return 'Could not fetch GPU Load data from OpenHardwareMonitor.'

return data


if __name__ == '__main__':
Plugin().execute()
11 changes: 5 additions & 6 deletions nixstatsagent/plugins/litespeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import re
import urllib2
import base64
import requests

class Plugin(plugins.BasePlugin):
__name__ = 'litespeed'

'''
This plugin needs requests (pip install requests)
Litespeed monitoring plugin. Add the following section to /etc/nixstats.ini
[litespeed]
enabled=yes
host=localhost
Expand All @@ -26,12 +28,9 @@ def run(self, config):
results = {}
data = False
prev_cache = self.get_agent_cache() # Get absolute values from previous check
request = urllib2.Request("http://%s:%s/status?rpt=summary" % (config.get('litespeed', 'host'),config.get('litespeed', 'port')))
base64string = base64.b64encode('%s:%s' % (config.get('litespeed', 'username'), config.get('litespeed', 'password')))
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request).read()
response = requests.get("http://%s:%s/status?rpt=summary" % (config.get('litespeed', 'host'),config.get('litespeed', 'port')), auth=(config.get('litespeed', 'username'), config.get('litespeed', 'password')), verify=False)

for line in response.split('\n'):
for line in response.text.split('\n'):
test = re.search('REQ_RATE \[(.*)\]', line)
if test is not None and test.group(1):
data = True
Expand Down
2 changes: 2 additions & 0 deletions nixstatsagent/plugins/temp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
import plugins
import psutil
import sys

class Plugin(plugins.BasePlugin):
__name__ = 'temp'
Expand All @@ -18,6 +19,7 @@ def run(self, *unused):
import wmi
except:
return 'wmi module not installed.'

try:
w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
Expand Down
38 changes: 20 additions & 18 deletions nixstatsagent/plugins/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def run(self, config):
for key, value in values.items():
deltas[key] = {}
for subkey, subvalue in value.items():
deltas[key][subkey] = self.absolute_to_per_second('%s_%s' % (key, subkey), float(subvalue), prev_cache)
last_value['%s_%s' % (key, subkey)] = float(value[subkey])
if subkey == 'mem_bytes' or subkey == 'soft_limit_bytes' or subkey == 'min_guarantee_bytes' or subkey == 'hard_limit_bytes':
deltas[key][subkey] = value[subkey]
else:
deltas[key][subkey] = self.absolute_to_per_second('%s_%s' % (key, subkey), float(subvalue), prev_cache)
last_value['%s_%s' % (key, subkey)] = float(value[subkey])
last_value['ts'] = time.time()
self.set_agent_cache(last_value)
return results
return deltas

def canon(self, name):
return re.sub(r"[^a-zA-Z0-9_]", "_", name)
Expand Down Expand Up @@ -89,11 +92,10 @@ def fetch_values(self, uri):
conn = libvirt.openReadOnly(uri)
ids = conn.listDomainsID()
results = {}
processors = float(conn.getInfo()[2])
data = {}
for id in ids:
data['net_rx'] = 0
data['net_tx'] = 0
data = {}
data['net_rx_bytes'] = 0
data['net_tx_bytes'] = 0
try:
dom = conn.lookupByID(id)
name = dom.name()
Expand All @@ -106,26 +108,26 @@ def fetch_values(self, uri):
for iface in ifaces:
try:
stats = dom.interfaceStats(iface)
data['net_rx'] += stats[0]
data['net_tx'] += stats[4]
data['net_rx_bytes'] += stats[0]
data['net_tx_bytes'] += stats[4]
except:
print >>sys.stderr, "Cannot get ifstats for '%s' on '%s'" % (iface, name)

cputime = float(dom.info()[4])
cputime_percentage = 1.0e-7 * cputime / processors
cputime_percentage = 1.0e-7 * cputime
data['cpu'] = cputime_percentage

maxmem, mem = dom.info()[1:3]
mem *= 1024
maxmem *= 1024
data['mem'] = mem
data['mem_bytes'] = mem
memtune = self.get_memtune(dom)
data['min_guarantee'] = memtune['min_guarantee'] * 1024
data['hard_limit'] = memtune['hard_limit'] * 1024
data['soft_limit'] = memtune['soft_limit'] * 1024
data['min_guarantee_bytes'] = memtune['min_guarantee'] * 1024
data['hard_limit_bytes'] = memtune['hard_limit'] * 1024
data['soft_limit_bytes'] = memtune['soft_limit'] * 1024

data['disk_rd'] = 0
data['disk_wr'] = 0
data['disk_rd_bytes'] = 0
data['disk_wr_bytes'] = 0
data['disk_wr_req'] = 0
data['disk_rd_req'] = 0
try:
Expand All @@ -140,8 +142,8 @@ def fetch_values(self, uri):
for disk in disks:
try:
rd_req, rd_bytes, wr_req, wr_bytes, errs = dom.blockStats(disk)
data['disk_rd'] += rd_bytes
data['disk_wr'] += wr_bytes
data['disk_rd_bytes'] += rd_bytes
data['disk_wr_bytes'] += wr_bytes
data['disk_rd_req'] += rd_req
data['disk_wr_req'] += wr_req
except TypeError:
Expand Down

0 comments on commit f06ca12

Please sign in to comment.