#!/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:
# - Mario Lassnig, <mario.lassnig@cern.ch>, 2013-2014

'''
Probe to check the queues of messages to submit by Hermes to the broker
'''

import sys

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

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

queue_sql = """SELECT COUNT(*) FROM atlas_rucio.messages"""

if __name__ == "__main__":
    try:
        session = get_session()
        result = session.execute(queue_sql).fetchall()
        print 'queues.messages %s' % result[0][0]
        monitor.record_gauge(stat='queues.messages', value=result[0][0])

        if result[0][0] > 100000:
            sys.exit(WARNING)
        elif result[0][0] > 1000000:
            sys.exit(CRITICAL)

    except Exception, e:
        sys.exit(UNKNOWN)
    sys.exit(OK)
