#!/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:
# - Thomas Beermann, <thomas.beermann@cern.ch>, 2014

'''
Probe to check if an ActiveMQ queue is above a certain threshold.
'''

from sys import argv, exit

from requests import get

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

if __name__ == "__main__":
    broker = argv[1]
    destination = argv[2]
    threshold = int(argv[3])
    url = """http://%s:61004/j4p/read/org.apache.activemq:type=Broker,brokerName=atlas_%s,destinationType=Queue,destinationName=%s/QueueSize""" % (broker, broker, destination)

    r = get(url)

    if r.status_code != 200:
        exit(UNKNOWN)

    current = r.json()['value']
    if current > (2 * threshold):
        exit(CRITICAL)
    elif current > threshold:
        exit(WARNING)

    exit(OK)
