#!/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:
# - Cedric Serfon, <cedric.serfon@cern.ch>, 2014

'''
Probe to check the queues of the transfer service
'''

import sys
from rucio.common.exception import ServiceUnavailable
from rucio.rse import rsemanager

scheme = 'https'

print sys.argv
site = sys.argv[1]
# Exit statuses recognized by Nagios
UNKNOWN, OK, WARNING, CRITICAL = -1, 0, 1, 2

rse_settings = rsemanager.get_rse_info(site)
dict = rsemanager.select_protocol(rse_settings, operation='write', scheme=scheme)
basepath = '%s://%s:%s%s' % (dict['scheme'], dict['hostname'], dict['port'], dict['prefix'])
print basepath
p = rsemanager.create_protocol(rse_settings, operation='write', scheme='https')
try:
    p.connect()
except ServiceUnavailable, e:
    sys.exit(CRITICAL)

if not p.exists(basepath):
    sys.exit(CRITICAL)
p.close()

sys.exit(OK)
