#!/usr/bin/env python
# Copyright European Organization for Nuclear Research (CERN) 2013
#
# 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
#
# Authors:
# - Martin Barisits, <martin.barisits@cern.ch>, 2014

'''
Probe to check the backlog of stuck rules.
'''
import sys
import traceback

from rucio.core import monitor
from rucio.db.session import get_session

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

if __name__ == "__main__":
    try:
        session = get_session()

        result = session.execute('SELECT COUNT(1) FROM ATLAS_RUCIO.RULES where state=\'S\' and error !=\'MissingSourceReplica\'').fetchone()[0]
        monitor.record_gauge(stat='judge.stuck_rules_without_missing_source_replica', value=result)

        result = session.execute('SELECT COUNT(1) FROM ATLAS_RUCIO.RULES where state=\'S\' and error =\'MissingSourceReplica\'').fetchone()[0]
        monitor.record_gauge(stat='judge.stuck_rules_with_missing_source_replica', value=result)
    except:
        print traceback.format_exc()
        sys.exit(UNKNOWN)
    sys.exit(OK)
