From e9e9481eca64fc41fff58fac8e32ed9a5d67187a Mon Sep 17 00:00:00 2001 From: Greg Dubicki Date: Mon, 21 Feb 2022 20:17:01 +0000 Subject: [PATCH] Failures --- puppetboard/views/failures.py | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/puppetboard/views/failures.py b/puppetboard/views/failures.py index e69de29b..d37321aa 100644 --- a/puppetboard/views/failures.py +++ b/puppetboard/views/failures.py @@ -0,0 +1,61 @@ +from flask import Response, stream_with_context +from pypuppetdb.QueryBuilder import AndOperator, EqualsOperator + +from puppetboard.core import get_app, get_puppetdb, environments, stream_template +from puppetboard.utils import check_env, yield_or_stop + +app = get_app() +puppetdb = get_puppetdb() + + +@app.route('/failures', defaults={'env': app.config['DEFAULT_ENVIRONMENT']}) +@app.route('//failures') +def failures(env): + + nodes_query = AndOperator() + nodes_query.add(EqualsOperator('latest_report_status', 'failed')) + + envs = environments() + check_env(env, envs) + if env != '*': + nodes_query.add(EqualsOperator("catalog_environment", env)) + + nodes = puppetdb.nodes( + query=nodes_query, + with_status=True, + with_event_numbers=False, + ) + + failures = [] + + for node in yield_or_stop(nodes): + + report_query = AndOperator() + report_query.add(EqualsOperator('hash', node.latest_report_hash)) + + reports = puppetdb.reports( + query=report_query, + ) + + latest_failed_report = next(reports) + + for log in latest_failed_report.logs: + if log['level'] not in ['info', 'notice', 'warning']: + if log['source'] != 'Facter': + source = log['source'] + message = log['message'] + break + + failure = { + 'certname': node.name, + 'timestamp': node.report_timestamp, + 'source': source, + 'message': message, + } + failures.append(failure) + + return Response(stream_with_context( + stream_template('failures.html', + failures=failures, + envs=envs, + current_env=env)))