Skip to content Skip to sidebar Skip to footer

Nagiosplugin: 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: if certificateMatchesServerHost

Solution 1:

With a help from my friend, I finally solved the problem.

About the first question, we can use the nagiosplugin.summary module to change the status line, something like this:

classSslConfiguration(nap.Resource):
    def__init__(self, host, port):
        self.host = host
        self.port = port

    defcheck(self):
            ...
        if hostname_validation.startswith('OK') and expire_in.days > 0and is_trusted == 'OK':
            final_score = protocol_score * 0.3 + key_score * 0.3 + cipher_score * 0.4else:
            final_score = 0return (hostname_validation, is_trusted, expire_in.days, final_score)

    defprobe(self):
        if self.check()[3] > 0:
            return [nap.Metric('sslscore', self.check()[3])]
        elifnot 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')]


classSslSummary(nap.Summary):
    def__init__(self, host, port):
        self.host = host
        self.port = port

    defstatus_line(self, results):
        ssl_configuration = SslConfiguration(self.host, self.port)
        if results['sslscore'].context.name == 'serverHostname':
            return"sslscore is 0 ({0})".format(ssl_configuration.check()[0])
        elif results['sslscore'].context.name == 'validationResult':
            return"sslscore is 0 ({0})".format(ssl_configuration.check()[1])
        elif results['sslscore'].context.name == 'expireInDays':
            return"sslscore is 0 (The certificate expired {0} days ago)".format(ssl_configuration.check()[2])

    defproblem(self, results):
        return self.status_line(results)

The second and third question can be solved by setting the verbose parameter of main() to zero:

@nap.guardeddefmain():
    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 up to 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 the output can help sysadmin to know what is going on:

check_ssl_configuration.py -H google.com
SSLCONFIGURATION OK - sslscore is87| sslscore=87.0;@65:80;@65

check_ssl_configuration.py -H 173.194.127.169
SSLCONFIGURATION CRITICAL - sslscore is0 (FAILED - Certificate does NOTmatch173.194.127.169) | sslscore=0;@65:80;@65

Post a Comment for "Nagiosplugin: How To Show Different Fmt_metric Based On The Value?"