From 9021fd35ca30b3b863c66b4cc11ac10edc8abb48 Mon Sep 17 00:00:00 2001 From: voetberg Date: Mon, 5 Aug 2024 14:13:23 -0500 Subject: [PATCH] Common: Rewrite check_updated_dids * Rewrite query into proper sqla2.0 * change gauge for prometheus pusher * clean out comments * sort imports * Update header * change except to except Exception --- common/check_updated_dids | 64 ++++++++++++++------------------------- 1 file changed, 23 insertions(+), 41 deletions(-) diff --git a/common/check_updated_dids b/common/check_updated_dids index 756aad8a..92b5ddf0 100755 --- a/common/check_updated_dids +++ b/common/check_updated_dids @@ -1,65 +1,47 @@ -#!/usr/bin/env python -# Copyright European Organization for Nuclear Research (CERN) 2013 +#!/usr/bin/env python3 +# Copyright European Organization for Nuclear Research (CERN) since 2012 # # Licensed under the Apache License, Version 2.0 (the "License"); -# You may not use this file except in compliance with the License. -# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # -# Authors: -# - Vincent Garonne, , 2013 -# - Thomas Beermann, , 2019 -# - Eric Vaandering , 2020-2021 +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. """ Probe to check the backlog of updated dids. """ -from __future__ import print_function import sys import traceback +from sqlalchemy import func, select -from prometheus_client import CollectorRegistry, Gauge, push_to_gateway -from rucio.common.config import config_get from rucio.db.sqla import models from rucio.db.sqla.session import get_session -from rucio.db.sqla.util import get_count -from utils.common import probe_metrics + +from utils.common import PrometheusPusher # Exit statuses OK, WARNING, CRITICAL, UNKNOWN = 0, 1, 2, 3 -PROM_SERVERS = config_get('monitor', 'prometheus_servers', raise_exception=False, default='') -if PROM_SERVERS != '': - PROM_SERVERS = PROM_SERVERS.split(',') - if __name__ == "__main__": try: - registry = CollectorRegistry() session = get_session() - query = session.query(models.UpdatedDID) - result = get_count(query) - probe_metrics.gauge(name='judge.updated_dids').set(result) - Gauge('judge_updated_dids', '', registry=registry).set(result) - - if len(PROM_SERVERS): - for server in PROM_SERVERS: - try: - push_to_gateway(server.strip(), job='check_updated_dids', registry=registry) - except: - continue - - # created_at, count, max, min, avg, stdev = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 - # result = session.execute('select * from atlas_rucio.concurency_stats where created_at > sysdate - 1/1440') - # for row in result: - # created_at, count, max, min, avg, stdev = row - # monitor.record_gauge(stat='judge.updated_dids_per_min.count', value=count or 0) - # monitor.record_gauge(stat='judge.updated_dids_per_min.max', value=max or 0) - # monitor.record_gauge(stat='judge.updated_dids_per_min.min', value=min or 0) - # monitor.record_gauge(stat='judge.updated_dids_per_min.avg', value=avg or 0) - # monitor.record_gauge(stat='judge.updated_dids_per_min.stdev', value=stdev or 0) - # print created_at, count, max, min, avg, stdev - except: + statement = select( + func.count() + ).select_from( + models.UpdatedDID + ) + result = session.execute(statement).scalar_one() + with PrometheusPusher() as manager: + manager.gauge("judge.updated_dids").set(result) + except Exception: print(traceback.format_exc()) sys.exit(UNKNOWN) sys.exit(OK)