mirror of
https://github.com/librenms/librenms-agent.git
synced 2024-05-09 09:54:52 +00:00
Cleanup some code (#355)
* Format with isort * Format with Black * Fix CRLF * Format with shellcheck * Fix some warning * Fix PHP style * Dont modifiy check_mk files * Fixes
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import yaml
|
||||
from os.path import isfile
|
||||
from time import time
|
||||
|
||||
import yaml
|
||||
|
||||
output = {}
|
||||
output['error'] = 0
|
||||
output['errorString'] = ""
|
||||
output['version'] = 1
|
||||
output["error"] = 0
|
||||
output["errorString"] = ""
|
||||
output["version"] = 1
|
||||
|
||||
CONFIGFILE = '/etc/snmp/puppet.json'
|
||||
CONFIGFILE = "/etc/snmp/puppet.json"
|
||||
# optional config file
|
||||
# {
|
||||
# "agent": {
|
||||
@@ -20,13 +20,15 @@ CONFIGFILE = '/etc/snmp/puppet.json'
|
||||
# }
|
||||
|
||||
|
||||
summary_files = ['/var/cache/puppet/state/last_run_summary.yaml',
|
||||
'/opt/puppetlabs/puppet/cache/state/last_run_summary.yaml']
|
||||
summary_files = [
|
||||
"/var/cache/puppet/state/last_run_summary.yaml",
|
||||
"/opt/puppetlabs/puppet/cache/state/last_run_summary.yaml",
|
||||
]
|
||||
|
||||
|
||||
def parse_yaml_file(filename):
|
||||
try:
|
||||
yaml_data = yaml.load(open(filename, 'r'))
|
||||
yaml_data = yaml.load(open(filename, "r"))
|
||||
msg = None
|
||||
except yaml.scanner.ScannerError as e:
|
||||
yaml_data = []
|
||||
@@ -42,7 +44,7 @@ def time_processing(data):
|
||||
new_data = {}
|
||||
|
||||
for k in data.keys():
|
||||
if k == 'last_run':
|
||||
if k == "last_run":
|
||||
# generate difference to last run (seconds)
|
||||
new_data[k] = round(time() - data[k])
|
||||
continue
|
||||
@@ -53,36 +55,36 @@ def time_processing(data):
|
||||
|
||||
def processing(data):
|
||||
new_data = {}
|
||||
for k in ['changes', 'events', 'resources', 'version']:
|
||||
for k in ["changes", "events", "resources", "version"]:
|
||||
new_data[k] = data[k]
|
||||
|
||||
new_data['time'] = time_processing(data['time'])
|
||||
new_data["time"] = time_processing(data["time"])
|
||||
|
||||
return new_data
|
||||
|
||||
|
||||
# extend last_run_summary_file list with optional custom file
|
||||
if isfile(CONFIGFILE):
|
||||
with open(CONFIGFILE, 'r') as json_file:
|
||||
with open(CONFIGFILE, "r") as json_file:
|
||||
try:
|
||||
configfile = json.load(json_file)
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
output['error'] = 1
|
||||
output['errorString'] = "Configfile Error: '%s'" % e
|
||||
output["error"] = 1
|
||||
output["errorString"] = "Configfile Error: '%s'" % e
|
||||
else:
|
||||
configfile = None
|
||||
|
||||
if not output['error'] and configfile:
|
||||
if not output["error"] and configfile:
|
||||
try:
|
||||
if 'agent' in configfile.keys():
|
||||
custom_summary_file = configfile['agent']['summary_file']
|
||||
if "agent" in configfile.keys():
|
||||
custom_summary_file = configfile["agent"]["summary_file"]
|
||||
summary_files.insert(0, custom_summary_file)
|
||||
except KeyError:
|
||||
output['error'] = 1
|
||||
output['errorString'] = "Configfile Error: '%s'" % e
|
||||
except KeyError as e:
|
||||
output["error"] = 1
|
||||
output["errorString"] = "Configfile Error: '%s'" % e
|
||||
|
||||
# search existing summary file from list
|
||||
if not output['error']:
|
||||
if not output["error"]:
|
||||
summary_file = None
|
||||
for sum_file in summary_files:
|
||||
if isfile(sum_file):
|
||||
@@ -90,17 +92,17 @@ if not output['error']:
|
||||
break
|
||||
|
||||
if not summary_file:
|
||||
output['error'] = 1
|
||||
output['errorString'] = "no puppet agent run summary file found"
|
||||
output["error"] = 1
|
||||
output["errorString"] = "no puppet agent run summary file found"
|
||||
|
||||
# open summary file
|
||||
if not output['error']:
|
||||
if not output["error"]:
|
||||
msg, data = parse_yaml_file(summary_file)
|
||||
|
||||
if msg:
|
||||
output['error'] = 1
|
||||
output['errorString'] = msg
|
||||
output["error"] = 1
|
||||
output["errorString"] = msg
|
||||
|
||||
output['data'] = processing(data)
|
||||
output["data"] = processing(data)
|
||||
|
||||
print (json.dumps(output))
|
||||
print(json.dumps(output))
|
||||
|
Reference in New Issue
Block a user