#!/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
#
import datetime
import sys
from rucio.client import Client

OK, WARNING, CRITICAL, UNKNOWN = 0, 1, 2, 3
host = sys.argv[1]
c = Client()

for dict in c.get_rse_usage(host):
    if dict['source'] == 'rucio':
        if datetime.datetime.utcnow() - dict['updated_at'] < datetime.timedelta(hours=1):
            print 'Used space :', dict['used']
            sys.exit(OK)
        else:
            print 'Used space has not been refreshed for more than 1 hour. Last refresh on %s' % str(dict['updated_at'])
            sys.exit(WARNING)
