#
# Common statistics
#

# command, identifier,    description,                  min, max, warn, crit, value
# auto     callpOpenFiles CALLP-Memory-peak-usage-in-kB 0    2048 1500  1900  52

# the inventory function
def inventory_omg_callp(info):
    for line in info:
        try:
            command, identifier, description, min, max, warn, crit, value = line
            if command == "auto":
               yield description.replace("-"," "), None
        except:
            continue

# the check function
def check_omg_callp(item, params, info):
   for line in info:
       try:
           command, identifier, description, min, max, warn, crit, value = line
           if description.replace("-"," ") == item:
              perfdata = [ ( item, value, warn, crit, min, max ) ]
              if int(value) > int(crit):
                  return 2, value, perfdata
              if int(value) > int(warn):
                  return 1, value, perfdata
              else:
                  return 0, value, perfdata
       except:
           continue

check_info["officemaster_gate.callpstate"] = {
    'check_function':            check_omg_callp,
    'inventory_function':        inventory_omg_callp,
    'service_description':       'OM %s',
    'has_perfdata':              True,
}

#
# Interface statistics
#
channel_usage_default_values = (0, 30, 25, 30)


# the inventory function
def inventory_omg_interface(info):
   for line in info:
       try:
           interface, l1state, l2state, calls, type, channels = line
           if interface.startswith("ILID") and (not l1state == "L1:OFF"):
              yield type + " " + interface, None
       except:
         continue

# the check function
def check_omg_interface(item, params, info):
   for line in info:
       try:
         interface, l1state, l2state, calls, type, channels = line
         index = type + " " + interface
         if index == item:
            min = "0"
            max = int(channels.split(":")[1])

            if channels == "1":
               warn = "2"
               crit = "2"
            elif channels == "2":
               warn = "3"
               crit = "3"
            else:
               warn = "27"
               crit = "31"

            active_calls = int(calls.split(":")[1])
            perfdata = [ ( "active calls", active_calls, warn, crit, min, max ) ]
            if l1state == "L1:UP" and l2state == "L2:ESTABLISHED" and active_calls > crit:
               return 2, "%s %s %s Active calls:%d" % (interface, l1state,l2state,active_calls), perfdata
            if l1state == "L1:UP" and l2state == "L2:ESTABLISHED" and active_calls > warn:
               return 1, "%s %s %s Active calls:%d" % (interface, l1state,l2state,active_calls), perfdata
            if l1state == "L1:UP" and l2state == "L2:ESTABLISHED":
               return 0, "%s %s %s Active calls:%d" % (interface, l1state,l2state,active_calls), perfdata
            if l1state == "L1:UP" and (l2state == "L2:OFF" or l2state == "L2:IDLE"):
               return 1, "%s Cable connected but no protocol established %s %s" % (interface, l1state,l2state), perfdata
            if l1state == "L1:DOWN":
               return 2, "%s State %s" % (interface, l1state), perfdata
            if l1state == "L1:OFF":
               return 3, "%s channel is disabled in gateway configuration" % (interface)
            else:
               return 3, "%s Unkown state %s" % (interface, line)
       except:
         continue

# declare the check to Check_MK
check_info["officemaster_gate.interface"] = {
    'check_function':            check_omg_interface,
    'inventory_function':        inventory_omg_interface,
    'service_description':       'OM Interface %s',
    'has_perfdata':              True,
}
