Skip to content

Commit 3c38ccf

Browse files
committed
Common: Rewrite check_stuck_rules
* change gauge to PrometheusPusher * update to sqla2.0 * sort import * change header * Change except to except Exception
1 parent a7c0556 commit 3c38ccf

1 file changed

Lines changed: 51 additions & 40 deletions

File tree

common/check_stuck_rules

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,75 @@
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
33
#
44
# 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
77
#
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.
1215

1316
"""
1417
Probe to check the backlog of stuck rules.
1518
"""
16-
from __future__ import print_function
1719

1820
import sys
1921
import traceback
22+
from sqlalchemy.sql import and_, func, null, or_, select
2023

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
2426

25-
from utils.common import probe_metrics
27+
from utils.common import PrometheusPusher
2628

2729
# Exit statuses
2830
OK, WARNING, CRITICAL, UNKNOWN = 0, 1, 2, 3
2931

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-
3932
if __name__ == "__main__":
4033
try:
41-
registry = CollectorRegistry()
4234
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+
)
4848

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+
}
5463

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:
6273
print(traceback.format_exc())
6374
sys.exit(UNKNOWN)
6475
sys.exit(OK)

0 commit comments

Comments
 (0)