Created
November 20, 2018 03:22
-
-
Save burnsie7/0c9469b5bf151c7dee7870844c9406c2 to your computer and use it in GitHub Desktop.
agent check example parsing the values from directory listing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pprint | |
import subprocess | |
import random | |
from checks import AgentCheck | |
# for logging | |
def log_parsed_output(output_list): | |
with open(writepath, mode) as f: | |
for i in output_list: | |
f.write(i+'\n') | |
# for metric parsing | |
def parse_output_to_metrics(output_list): | |
parsed_list = [] | |
try: | |
for i in output_list: | |
line_list = i.split(' ') | |
parsed_list.append(line_list) | |
except Exception as e: | |
print('error "{}" line: "{}"'.format(e, i)) | |
return parsed_list | |
def remove_spaces(line_list): | |
metric_list = [] | |
for x in range(0, len(line_list)): | |
if line_list[x]: | |
metric_list.append(line_list[x]) | |
return metric_list | |
class CheckValue(AgentCheck): | |
def check(self, instance): | |
## main | |
writepath = '/home/ubuntu/logfile.txt' | |
mode = 'a' if os.path.exists(writepath) else 'w' | |
output = subprocess.check_output(['ls', '-l']) | |
output_list = output.split('\n') | |
parsed_list = parse_output_to_metrics(output_list) | |
metrics = [] | |
for line_list in parsed_list: | |
metrics.append(remove_spaces(line_list)) | |
metric_list = [] | |
for metric in metrics: | |
try: | |
m = {} | |
tags = [] | |
m['metric'] = 'my_metric_name' | |
m['value'] = metric[4] | |
m['tags'] = ['user:{}'.format(l[3]), 'file:{}'.format(metric[-1])] | |
metric_list.append(m) | |
pprint.pprint(m) | |
except Exception as e: | |
print('error "{}" line: "{}"'.format(e, metric)) | |
for m in metric_list: | |
try: | |
self.gauge(m['metric'], m['value'], tags=m['tags']) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment