#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Biodec 2017                            info@biodec.com |
# +------------------------------------------------------------------+
#

# Example of plugin output
#<<modbus>>
#"PrePCR fridge","Virtual probe fault alarm","False"
#"PrePCR fridge","Probe 1 fault alarm","False"
#"PrePCR fridge","Probe 2 fault alarm","False"
#"PrePCR fridge","Probe 3 fault alarm","False"

import json
import ConfigParser
import os, re

# the inventory function, checks the agent output and create a list of items
def inventory_modbus(info):
    # begin with empty inventory
    inventory = []
    for line in info:
        device, alarm, status = re.sub('"', '', line[0]), re.sub('"', '', line[1]), line[2]
        inventory.append(('%s %s' % ( re.sub(',', '', device), re.sub(',', '', alarm)), None)) 
    return inventory

# the check function, process the output
def check_modbus(item, params, info):
    for line in info:
        if str(line[2]) == '"False"':
            return (0, "OK")
        else:
            return (2, "CRIT")

# declare the check to Check_MK
check_info['modbus'] = {
    "check_function"          : check_modbus,
    "inventory_function"      : inventory_modbus,
    "service_description"     : "Modbus facts",
}
