|
1 | | -#!/usr/bin/env python |
2 | | -# Copyright European Organization for Nuclear Research (CERN) 2013 |
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright European Organization for Nuclear Research (CERN) since 2012 |
3 | 3 | # |
4 | 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -# You may not use this file except in compliance with the License. |
6 | | -# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
7 | 7 | # |
8 | | -# Authors: |
9 | | -# - Martin Barisits, <martin.barisits@cern.ch>, 2014 |
10 | | -# - Eric Vaandering, <ewv@fnal.gov>, 2019-2021 |
11 | | -# - Thomas Beermann, <thomas.beermann@cern.ch>, 2019 |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
12 | 15 |
|
13 | 16 | """ |
14 | 17 | Probe to check the backlog of stuck rules. |
15 | 18 | """ |
16 | | -from __future__ import print_function |
17 | 19 |
|
18 | 20 | import sys |
19 | 21 | import traceback |
| 22 | +from sqlalchemy.sql import and_, func, null, or_, select |
20 | 23 |
|
21 | | -from prometheus_client import CollectorRegistry, Gauge, push_to_gateway |
22 | | -from rucio.common.config import config_get |
23 | | -from rucio.db.sqla.session import BASE, get_session |
| 24 | +from rucio.db.sqla import models |
| 25 | +from rucio.db.sqla.session import get_session |
24 | 26 |
|
25 | | -from utils.common import probe_metrics |
| 27 | +from utils.common import PrometheusPusher |
26 | 28 |
|
27 | 29 | # Exit statuses |
28 | 30 | OK, WARNING, CRITICAL, UNKNOWN = 0, 1, 2, 3 |
29 | 31 |
|
30 | | -if BASE.metadata.schema: |
31 | | - schema = BASE.metadata.schema + '.' |
32 | | -else: |
33 | | - schema = '' |
34 | | - |
35 | | -PROM_SERVERS = config_get('monitor', 'prometheus_servers', raise_exception=False, default='') |
36 | | -if PROM_SERVERS != '': |
37 | | - PROM_SERVERS = PROM_SERVERS.split(',') |
38 | | - |
39 | 32 | if __name__ == "__main__": |
40 | 33 | try: |
41 | | - registry = CollectorRegistry() |
42 | 34 | session = get_session() |
43 | | - sql = 'SELECT COUNT(1) FROM {schema}RULES where state=\'S\' and (error !=\'MissingSourceReplica\' or error IS NULL)'.format( |
44 | | - schema=schema) |
45 | | - result = session.execute(sql).fetchone()[0] |
46 | | - probe_metrics.gauge(name='judge.stuck_rules_without_missing_source_replica').set(result) |
47 | | - Gauge('judge_stuck_rules_without_missing_source_replica', '', registry=registry).set(result) |
| 35 | + without_missing_replica_statement = select( |
| 36 | + func.count() |
| 37 | + ).select_from( |
| 38 | + models.ReplicationRule |
| 39 | + ).where( |
| 40 | + and_( |
| 41 | + models.ReplicationRule.state == "S", |
| 42 | + or_( |
| 43 | + models.ReplicationRule.error != "MissingSourceReplica", |
| 44 | + models.ReplicationRule.error == null() |
| 45 | + ) |
| 46 | + ) |
| 47 | + ) |
48 | 48 |
|
49 | | - sql = 'SELECT COUNT(1) FROM {schema}RULES where state=\'S\' and error =\'MissingSourceReplica\''.format( |
50 | | - schema=schema) |
51 | | - result = session.execute(sql).fetchone()[0] |
52 | | - probe_metrics.gauge(name='judge.stuck_rules_with_missing_source_replica').set(result) |
53 | | - Gauge('judge_stuck_rules_with_missing_source_replica', '', registry=registry).set(result) |
| 49 | + with_missing_replica_statement = select( |
| 50 | + func.count() |
| 51 | + ).select_from( |
| 52 | + models.ReplicationRule |
| 53 | + ).where( |
| 54 | + and_( |
| 55 | + models.ReplicationRule.state == "S", |
| 56 | + models.ReplicationRule.error == "MissingSourceReplica" |
| 57 | + ) |
| 58 | + ) |
| 59 | + queries = { |
| 60 | + "without_missing_source_replica": without_missing_replica_statement, |
| 61 | + "with_missing_source_replica": with_missing_replica_statement |
| 62 | + } |
54 | 63 |
|
55 | | - if len(PROM_SERVERS): |
56 | | - for server in PROM_SERVERS: |
57 | | - try: |
58 | | - push_to_gateway(server.strip(), job='check_stuck_rules', registry=registry) |
59 | | - except: |
60 | | - continue |
61 | | - except: |
| 64 | + with PrometheusPusher() as manager: |
| 65 | + for source_status, statement in queries.items(): |
| 66 | + result = session.execute(statement).scalar_one() |
| 67 | + (manager.gauge( |
| 68 | + "stuck_rules.{source_status}", |
| 69 | + documentation="Backlog of stuck rules") |
| 70 | + .labels(source_status=source_status) |
| 71 | + .set(result)) |
| 72 | + except Exception: |
62 | 73 | print(traceback.format_exc()) |
63 | 74 | sys.exit(UNKNOWN) |
64 | 75 | sys.exit(OK) |
0 commit comments