nagiosplugin
nagiosplugin copied to clipboard
output formatting guide
Original report by Christian Kauhaus (Bitbucket: ckauhaus, GitHub: ckauhaus).
show how to:
- provide custom fmt_metric
- subclass Context and override describe()
- subclass Summary
Original comment by Christian Kauhaus (Bitbucket: ckauhaus, GitHub: ckauhaus).
Quan Tong Anh:
How to show different fmt_metric based on the value?
I'm writing a Nagios plugin that use xml output from sslyze to calculate SSL score based on Qualys Server Rating Guide.
Here're my code:
#!python
if certificateMatchesServerHostname == 'True' and expire_in.days > 0 and validationResult == 'ok':
...
final_score = protocol_score * 0.3 + key_score * 0.3 + cipher_score * 0.4
return [nap.Metric('sslscore', final_score, min=0, max=100)]
elif certificateMatchesServerHostname != 'True':
return [nap.Metric('serverHostname', hostnameValidation[0].attrib['serverHostname'])]
elif expire_in.days <= 0:
return [nap.Metric('expireInDays', expire_in.days)]
elif validationResult != 'ok':
return [nap.Metric('validationResult', validationResult)]
@nap.guarded
def main():
check = nap.Check(
SslConfiguration(),
nap.ScalarContext('sslscore', nap.Range('@65:80'), nap.Range('@0:65')),
nap.ScalarContext('serverHostname', fmt_metric='The certificate does not match the host name {value}'),
nap.ScalarContext('expireInDays', nap.Range('@:0'), fmt_metric='The certificate expired {value} days ago'),
nap.ScalarContext('validationResult', fmt_metric='This server\'s certificate is not trusted: {value}'))
check.main(timeout=60)
The reason I have to use multiple ScalarContext is I would like to show different fmt_metric if there is a problem with SSL certificate: does not match, expired, not trust, ...
With the above code, the output looks something like this:
SSLCONFIGURATION CRITICAL - The certificate does not match the host name a.b.c.d (outside range 0:) critical: The certificate does not match the host name a.b.c.d (outside range 0:) | serverHostname=a.b.c.d
What I really want to display is:
SSLCONFIGURATION CRITICAL - final_score is 0 (The certificate does not match the host name a.b.c.d) | sslscore=0;@65:80;@65;0;100
So, I have some questions:
-
How can I display different
fmt_metricbased on the sslscore value, in only one contextsslscore? -
How to remove the redundant line (2nd)?
critical: The certificate does not match the host name a.b.c.d (outside range 0:)
-
How to move the metric (3rd line) to at the end of the first line?