1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-21 18:41:03 +00:00

Do proper error/exception handling

* Ensure plugin exits with non-zero status
* Fix formatting of informative output
This commit is contained in:
Sandro 2022-06-07 22:05:42 +02:00
parent dbc8e511a2
commit 9b4b5cd9b8

View file

@ -100,14 +100,15 @@ import sys
try: try:
from nftables import Nftables from nftables import Nftables
from nftables import json from nftables import json
except: except Exception as err:
raise RuntimeError("Unable to load nftables module.") print("Unable to load nftables module.")
sys.exit() sys.exit(err)
try: try:
from pymunin import MuninPlugin, MuninGraph, muninMain from pymunin import MuninPlugin, MuninGraph, muninMain
except: except Exception as err:
raise RuntimeError("Unable to load PyMunin module.") print("Unable to load PyMunin module.")
sys.exit() sys.exit(err)
def _find_objects(ruleset, type): def _find_objects(ruleset, type):
@ -119,11 +120,11 @@ def nft_cmd(nftlib, cmd):
rc, output, error = nftlib.cmd(cmd) rc, output, error = nftlib.cmd(cmd)
if rc != 0: if rc != 0:
# do proper error handling here, exceptions etc # do proper error handling here, exceptions etc
raise RuntimeError("ERROR: running cmd nft {}".format(cmd)) raise RuntimeError("Error running cmd 'nft {}'".format(cmd))
if len(output) == 0: if len(output) == 0:
# more error control # more error control
raise RuntimeError("ERROR: no output from libnftables") raise ValueError("ERROR: no output from libnftables")
# transform the libnftables JSON output into generic python data structures # transform the libnftables JSON output into generic python data structures
ruleset = json.loads(output)["nftables"] ruleset = json.loads(output)["nftables"]
@ -186,23 +187,21 @@ class MuninNftCountersPlugin(MuninPlugin):
try: try:
counters = getCounters() counters = getCounters()
if len(counters) <= 0: if len(counters) <= 0:
print(""" print("# No counters in nftables. Try adding some first.",
# No counters in nftables. Try adding some first. "# See 'munin-doc %s' for more information." % self.plugin_name,
# See 'munin-doc %s' for more information. sep="\n")
""" % self.plugin_name) sys.exit(1)
sys.exit() except Exception as err:
except: print("# Plugin needs to be run as root since nftables can only be",
print(""" "# run as root.",
# Plugin needs to be run as root since nftables can only be "#",
# run as root. "# Use the following setting in the configuration file",
# "# to enable root privileges:",
# Use the following setting in the configuration file "#",
# to enable root privileges: "# [%s]" % self.plugin_name,
# "# user root",
# [%s] sep="\n")
# user root sys.exit(err)
""" % self.plugin_name)
sys.exit()
count_only = self.envGet("count_only") count_only = self.envGet("count_only")