← Back to team overview

bootstack-charmers team mailing list archive

Re: [Merge] ~guoqiao/charm-juju-lint:dev into charm-juju-lint:master

 

Added really small comment.

Diff comments:

> diff --git a/scripts/plugins/check_juju_lint_results.py b/scripts/plugins/check_juju_lint_results.py
> new file mode 100755
> index 0000000..8f963b2
> --- /dev/null
> +++ b/scripts/plugins/check_juju_lint_results.py
> @@ -0,0 +1,104 @@
> +#!/usr/bin/env python3
> +
> +"""
> +# check_juju_lint_results.py
> +
> +Nagios plugin script to check juju lint results.
> +
> +Copyright (C) 2020 Canonical Ltd.
> +Copyright (C) 2020 Joe Guo <joe.guo@xxxxxxxxxxxxx>
> +
> +This program is free software: you can redistribute it and/or modify it under
> +the terms of the GNU General Public License version 3, as published by the Free
> +Software Foundation.
> +
> +This program is distributed in the hope that it will be useful, but WITHOUT ANY
> +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
> +PARTICULAR PURPOSE.  See the GNU General Public License for more details.
> +
> +You should have received a copy of the GNU General Public License along with
> +this program.  If not, see <https://www.gnu.org/licenses/>.
> +"""
> +
> +import argparse
> +import sys
> +import os
> +from time import time
> +from os.path import join, isfile
> +
> +VAR_LIB = "/var/lib/juju-lint"
> +LINT_RESULTS_FILE = join(VAR_LIB, 'lint-results.txt')
> +
> +NAGIOS_STATUS_OK = 0
> +NAGIOS_STATUS_WARNING = 1
> +NAGIOS_STATUS_CRITICAL = 2
> +NAGIOS_STATUS_UNKNOWN = 3
> +
> +NAGIOS_STATUS = {
> +    NAGIOS_STATUS_OK: 'OK',
> +    NAGIOS_STATUS_WARNING: 'WARNING',
> +    NAGIOS_STATUS_CRITICAL: 'CRITICAL',
> +    NAGIOS_STATUS_UNKNOWN: 'UNKNOWN',
> +}
> +
> +
> +def nagios_exit(status, message):
> +    assert status in NAGIOS_STATUS, "Invalid Nagios status code"
> +    output = '{}: {}'.format(NAGIOS_STATUS[status], message)
> +    print(output)  # nagios requires print to stdout, no stderr
> +    sys.exit(status)
> +
> +
> +def main():
> +    parser = argparse.ArgumentParser(
> +        description='check juju lint results',
> +        # show default in help
> +        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
> +
> +    parser.add_argument(
> +        '-f', '--lint-results-file',
> +        dest='lint_results_file',
> +        default=LINT_RESULTS_FILE,
> +        help='juju lint results file to check')
> +
> +    parser.add_argument(
> +        '-a', '--lint-results-max-age',
> +        dest='lint_results_max_age',
> +        type=int, default=60,
> +        help='juju lint results file max age in minutes, 0 to ignore')
> +
> +    args = parser.parse_args()
> +    lint_results_file = args.lint_results_file
> +
> +    # raise CRITICAL alert if lint results file missing
> +    # which implies cron job may stop working
> +    if not isfile(lint_results_file):
> +        message = 'lint results file not found: {}'.format(lint_results_file)
> +        nagios_exit(NAGIOS_STATUS_CRITICAL, message)
> +
> +    max_age = args.lint_results_max_age
> +    if max_age > 0:  # if <= 0, skip

Unnecessary comment on this line.

> +        stat = os.stat(lint_results_file)
> +        age_sec = int(time() - stat.st_mtime)
> +        max_age_sec = max_age * 60
> +        if age_sec > max_age_sec:
> +            message = 'lint results file {} is older than max age {} mins'.format(
> +                lint_results_file, max_age)
> +            nagios_exit(NAGIOS_STATUS_CRITICAL, message)
> +
> +    with open(lint_results_file) as txt:
> +        lint_results = txt.read()
> +
> +    if 'ERROR' in lint_results or 'CRITICAL' in lint_results:
> +        message = 'juju-lint errors\n{}'.format(lint_results)
> +        nagios_exit(NAGIOS_STATUS_CRITICAL, message)
> +    elif 'WARNING' in lint_results:
> +        message = 'juju-lint warnings\n{}'.format(lint_results)
> +        nagios_exit(NAGIOS_STATUS_WARNING, message)
> +    else:
> +        message = 'juju lint is happy\n{}'.format(lint_results)
> +        nagios_exit(NAGIOS_STATUS_OK, message)
> +
> +
> +if __name__ == "__main__":
> +    main()


-- 
https://code.launchpad.net/~guoqiao/charm-juju-lint/+git/charm-juju-lint/+merge/377959
Your team Canonical BootStack Charmers is subscribed to branch charm-juju-lint:master.


References