python - nagiosplugin: how to show different fmt_metric based on the value? -


i'm writing nagios plugin use xml output sslyze calculate ssl score based on qualys server rating guide.

here're code:

            if certificatematchesserverhostname == 'true' , expire_in.days > 0 , 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 not match 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 not trusted: {value}'))     check.main(timeout=60) 

the reason have use multiple scalarcontext show different fmt_metric if there problem ssl certificate: not match, expired, not trust, ...

with above code, output looks this:

sslconfiguration critical - certificate not match host name a.b.c.d (outside range 0:) critical: certificate not match host name a.b.c.d (outside range 0:) | serverhostname=a.b.c.d 

what want display is:

sslconfiguration critical - final_score 0 (the certificate not match host name a.b.c.d) | sslscore=0;@65:80;@65;0;100 

so, have questions:

  1. how can display different fmt_metric based on sslscore value, in 1 context sslscore?

  2. how remove redundant line (2nd)?

    critical: certificate not match host name a.b.c.d (outside range 0:) 
  3. how move metric (3rd line) @ end of first line?

with friend, solved problem.

about first question, can use nagiosplugin.summary module change status line, this:

class sslconfiguration(nap.resource):     def __init__(self, host, port):         self.host = host         self.port = port      def check(self):             ...         if hostname_validation.startswith('ok') , expire_in.days > 0 , is_trusted == 'ok':             final_score = protocol_score * 0.3 + key_score * 0.3 + cipher_score * 0.4         else:             final_score = 0         return (hostname_validation, is_trusted, expire_in.days, final_score)      def probe(self):         if self.check()[3] > 0:             return [nap.metric('sslscore', self.check()[3])]         elif not self.check()[0].startswith('ok'):             return [nap.metric('sslscore', 0, context='serverhostname')]         elif self.check()[1] != 'ok':             return [nap.metric('sslscore', 0, context='validationresult')]         elif self.check()[2] <= 0:             return [nap.metric('sslscore', 0, context='expireindays')]   class sslsummary(nap.summary):     def __init__(self, host, port):         self.host = host         self.port = port      def status_line(self, results):         ssl_configuration = sslconfiguration(self.host, self.port)         if results['sslscore'].context.name == 'serverhostname':             return "sslscore 0 ({0})".format(ssl_configuration.check()[0])         elif results['sslscore'].context.name == 'validationresult':             return "sslscore 0 ({0})".format(ssl_configuration.check()[1])         elif results['sslscore'].context.name == 'expireindays':             return "sslscore 0 (the certificate expired {0} days ago)".format(ssl_configuration.check()[2])      def problem(self, results):         return self.status_line(results) 

the second , third question can solved setting verbose parameter of main() zero:

@nap.guarded def main():     parser = argparse.argumentparser()     parser.add_argument('-h', '--host', type=str, required=true)     parser.add_argument('-p', '--port', type=int, default=443)     parser.add_argument('-v', '--verbose', action='count', default=0, help="increase output verbosity (use 3 times)")     parser.add_argument('-t', '--timeout', type=int, default=60)     args = parser.parse_args()      check = nap.check(         sslconfiguration(args.host, args.port),         nap.scalarcontext('sslscore', nap.range('@65:80'), nap.range('@0:65')),         nap.scalarcontext('serverhostname', nap.range('@65:80'), nap.range('@0:65')),         nap.scalarcontext('validationresult', nap.range('@65:80'), nap.range('@0:65')),         nap.scalarcontext('expireindays', nap.range('@65:80'), nap.range('@0:65')),         sslsummary(args.host, args.port))     check.main(args.verbose, args.timeout) 

now output can sysadmin know going on:

check_ssl_configuration.py -h google.com sslconfiguration ok - sslscore 87 | sslscore=87.0;@65:80;@65  check_ssl_configuration.py -h 173.194.127.169 sslconfiguration critical - sslscore 0 (failed - certificate not match 173.194.127.169) | sslscore=0;@65:80;@65 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -