Skip to content

Commit

Permalink
[Issue #20] and [Issue #21]
Browse files Browse the repository at this point in the history
  • Loading branch information
thenodon committed Oct 30, 2021
1 parent 09699cc commit 1d06c7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ The monitor-exporter adds a number of labels to each metric:
- **hostname** - is the `host_name` in Monitor
- **service** - is the `service_description` in Monitor
- **downtime** - if the host or service is currently in a downtime period - true/false. If the host is in downtime its
services are also in downtime.
services are also in downtime. **Attention, downtime is only support if monitor-export is running in cache mode.**
- **address** - the hosts real address
- **acknowledged** - is applicable if a host or service is in warning or critical and have been acknowledged by operations -
0/1 where 1 is acknowledged.
Expand Down
19 changes: 15 additions & 4 deletions monitor_exporter/monitorconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,30 @@ def __init__(self, config=None):
if 'all_custom_vars' in config[MonitorConfig.config_entry]:
self.allow_all_custom_vars = bool(config[MonitorConfig.config_entry]['all_custom_vars'])

# Collect service data for a single host
self.url_query_service_data = self.host + \
'/api/filter/query?query=[services]%20host.name="{}' \
'"&columns=host.name,description,perf_data,check_command,state' \
'"&columns=host.name,description,perf_data,check_command,state,' \
'downtimes,acknowledged,is_flapping' \
'&limit=' + self.default_limit

# Collect host data for a single host
self.url_get_host = self.host + \
'/api/filter/query?query=[hosts]%20name="{}' \
'"&columns=address,custom_variables,perf_data,check_command,state'
'"&columns=address,custom_variables,perf_data,check_command,state,' \
'downtimes,acknowledged,is_flapping'

# Collect service data for all services - used in cache mode
self.url_query_all_service_data = self.host + \
'/api/filter/{}?query=[services]%20all' \
'&columns=host.name,description,perf_data,check_command,state,' \
'downtimes,acknowledged'
'downtimes,acknowledged,is_flapping'

# # Collect host data for all hosts - used in cache mode
self.url_query_all_host = self.host + \
'/api/filter/{}?query=[hosts]%20all' \
'&columns=name,address,custom_variables,perf_data,check_command,state,' \
'downtimes,acknowledged'
'downtimes,acknowledged,is_flapping'

self.url_downtime = self.host + \
'/api/filter/{}?query=[downtimes]%20all' \
Expand Down Expand Up @@ -229,6 +235,11 @@ async def get_cache_host_data(self, host_name):
return {}

def collect_cache(self, ttl: int = 300):
"""
Collect Monitor data for all objects and store in cache
:param ttl:
:return:
"""
try:
# get downtime
now = int(time.time())
Expand Down
9 changes: 6 additions & 3 deletions monitor_exporter/perfdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
HOSTNAME = 'hostname'
SERVICE = 'service'

check_command_regex = re.compile(r'^.+?[^!\n]+')

class Perfdata:

Expand Down Expand Up @@ -59,19 +60,19 @@ async def get_perfdata(self):
if 'perf_data' in host_data:
host_perf_data = host_data['perf_data']
if 'check_command' in host_data:
host_check_command = host_data['check_command']
host_check_command = check_command_regex.search(host_data['check_command']).group()

host_custom_vars_labels = self.prometheus_labels(custom_vars)

check_command_regex = re.compile(r'^.+?[^!\n]+')

# Host state
labels = {HOSTNAME: self.query_hostname}
labels.update(host_custom_vars_labels)

# Additional labels for downtime,
if 'downtime' in host_data:
labels['downtime'] = str(host_data['downtime']).lower()
if 'is_flapping' in host_data:
labels['flapping'] = str(host_data['is_flapping'])
if 'address' in host_data:
labels['address'] = host_data['address']
if 'acknowledged' in host_data:
Expand Down Expand Up @@ -113,6 +114,8 @@ async def get_perfdata(self):
labels['downtime'] = str(item['downtime']).lower()
if 'address' in host_data:
labels['address'] = host_data['address']
if 'is_flapping' in item:
labels['flapping'] = str(item['is_flapping'])
if 'acknowledged' in item:
labels['acknowledged'] = str(item['acknowledged'])

Expand Down

0 comments on commit 1d06c7f

Please sign in to comment.