#!/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:
# - Vincent Garonne, <vincent.garonne@cern.ch>, 2015

'''
Probe to check the min free space.
'''

import sys
import traceback

from rucio.core.rse import list_rses, get_rse_limits, set_rse_usage

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


if __name__ == "__main__":
    try:

        for rse in list_rses():
            limits = get_rse_limits(rse=rse['rse'], rse_id=rse['id'])
            min_free_space = limits.get('MinFreeSpace')
            if min_free_space is not None:
                # print rse['rse'], min_free_space
                set_rse_usage(rse=rse['rse'], source='min_free_space', used=min_free_space, free=None)
    except:
        print (traceback.format_exc())
        sys.exit(UNKNOWN)
    sys.exit(OK)
