#!/usr/bin/python


# Author: Simon Zeyer
# E-Mail: simon@zeyersch.de
# URL: 
# License: GPLv2


# default parameters
nc_groupquota_default_levels = (90, 95)

# the inventory function
def inventory_nc_groupquota(info):
    # loop over all output lines of the agent
    for line in info:
        arr_ncgq_vars = line[0].split(";")
        yield arr_ncgq_vars[0] + "", "nc_groupquota_default_levels"

# the check function (dummy)
def check_nc_groupquota(item, params, info):

    # print(item, params, info)
    group = None
    
    for line in info:
        if line[0].split(";")[0] == item:
            group = line[0].split(";")
            #print(group)


    #if line_count is 0, no backup file exists --> error!
    if not group:
        yield 2, "group not found!"
        return

    perfdata = [
        ( "quota", int(group[2])/1024/1024, int(group[1])/1024/1024/100*params[0], int(group[1])/1024/1024/100*params[1] ),
    ]
    #check counter
    if 100*int(group[2])/int(group[1]) >= params[1]:
        yield 2, "quota using {} exceeted crit level of {}%".format(int(100*int(group[2])/int(group[1])), params[1]), perfdata
        return
    if 100*int(group[2])/int(group[1]) >= params[0]:
        yield 1, "quota using {} exceeted warn level of {}%".format(int(100*int(group[2])/int(group[1])), params[0]), perfdata
        return
    if 100*int(group[2])/int(group[1]) < params[0]:
        yield 0, "quota using {}%".format(int(100*int(group[2])/int(group[1]))), perfdata
        return

    yield 3, "error occured in check plugin."
    return

# declare the check to Check_MK
check_info["nc_groupquota"] = {
    'check_function':            check_nc_groupquota,
    'inventory_function':        inventory_nc_groupquota,
    'service_description':       'NC GroupQuota %s',
    'group':                     'filesystem',
    'has_perfdata':              True,
}
