Improve human readable output

This change introduces more readable and useful output by adding the
certificate expiration to the human readable string. The response time
is now formatted too.
This commit is contained in:
Jan Dittberner 2015-12-29 22:17:59 +01:00
parent 7b1d482043
commit 11a16bb362
2 changed files with 35 additions and 8 deletions

View file

@ -1,5 +1,10 @@
# change log
## version 0.2 yyyy-mm-dd
* improve human readable output by returning both response time as well as
certificate expiry
## version 0.1.2 2015-02-11
* first icinga exchange release

View file

@ -31,7 +31,7 @@ from defusedxml.sax import make_parser
import nagiosplugin
__author__ = "Jan Dittberner"
__version__ = "0.1.2"
__version__ = "0.2.dev1"
NS_IETF_XMPP_SASL = 'urn:ietf:params:xml:ns:xmpp-sasl'
@ -418,6 +418,7 @@ class Xmpp(nagiosplugin.Resource):
except socket.gaierror as e:
self.state = nagiosplugin.Critical
self.cause = str(e)
_LOG.debug("got an gaierror %s", e)
return nagiosplugin.Metric("time", "unknown")
except XmppException as e:
self.state = nagiosplugin.Critical
@ -455,23 +456,32 @@ class DaysValidContext(nagiosplugin.Context):
Context for checking the certificate expiry date.
"""
fmt_hint = "less than {value} days"
def __init__(
self, name, warndays=0, critdays=0,
fmt_metric='certificate expires in {value} days'
fmt_metric='certificate valid for {value} days'
):
super(DaysValidContext, self).__init__(name, fmt_metric=fmt_metric)
self.warning = nagiosplugin.Range('@%d:' % warndays)
self.critical = nagiosplugin.Range('@%d:' % critdays)
self.warndays = warndays
self.critdays = critdays
def evaluate(self, metric, resource):
if resource.checkcerts and metric.value is not None:
hint = self.describe(metric)
if self.critical.match(metric.value):
return nagiosplugin.Result(nagiosplugin.Critical, hint, metric)
return nagiosplugin.Result(
nagiosplugin.Critical,
hint=self.fmt_hint.format(value=self.critdays),
metric=metric)
if self.warning.match(metric.value):
return nagiosplugin.Result(nagiosplugin.Warn, hint, metric)
return nagiosplugin.Result(nagiosplugin.Ok, hint, metric)
return nagiosplugin.Result(
nagiosplugin.Warn,
hint=self.fmt_hint.format(value=self.warndays),
metric=metric)
return nagiosplugin.Result(
nagiosplugin.Ok, "", metric)
return nagiosplugin.Result(nagiosplugin.Ok)
def performance(self, metric, resource):
@ -480,6 +490,16 @@ class DaysValidContext(nagiosplugin.Context):
return None
class XmppSummary(nagiosplugin.Summary):
"""
Summary instance that outputs all metrics if the check results are ok.
"""
def ok(self, results):
return ", ".join([str(res) for res in results])
@nagiosplugin.guarded
def main():
"""
@ -551,8 +571,10 @@ def main():
]
check = nagiosplugin.Check(
Xmpp(**kwargs),
XmppContext('time', warning, critical),
DaysValidContext('daysleft', warndays, critdays)
XmppContext(
'time', warning, critical, fmt_metric="request took {value}{uom}"),
DaysValidContext('daysleft', warndays, critdays),
XmppSummary(),
)
check.main(verbose=verbose, timeout=0)