#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.1 AA1
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.2 AA2
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.3 AA3
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.4 AA4
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.5 AA5
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.6 AA6
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.7 AA7
# .1.3.6.1.4.1.1718.3.2.3.1.2.1.1.8 AA8
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.1 TowerA_InfeedA_Outlet1
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.2 PRZ_FAB01_SW1_li
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.3 TowerA_InfeedA_Outlet3
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.4 TowerA_InfeedA_Outlet4
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.5 TowerA_InfeedA_Outlet5
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.6 1643_AMS_Telekom_Umsetz.
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.7 TowerA_InfeedA_Outlet7
# .1.3.6.1.4.1.1718.3.2.3.1.3.1.1.8 S2M_Optokoppler
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.1 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.2 1
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.3 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.4 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.5 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.6 1
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.7 0
# .1.3.6.1.4.1.1718.3.2.3.1.5.1.1.8 1

# parsed = {
#  'AA1 TowerA_InfeedA_1'        : 0,
#  'AA2 PRZ_FAB01_SW1_li'        : 1,
#  'AA3 TowerA_InfeedA_3'        : 0,
#  'AA4 TowerA_InfeedA_4'        : 0,
#  'AA5 TowerA_InfeedA_5'        : 0,
#  'AA6 1643_AMS_Telekom_Umsetz.': 1,
#  'AA7 TowerA_InfeedA_7'        : 0,
#  'AA8 S2M_Optokoppler'         : 1,
# }


def parse_sentry_pdu_outlets(info):
    parsed = {}
    for outlet_id, outlet_name, outlet_state_str in info:
        outlet_name = outlet_name.replace('Outlet','')
        outlet_id_name = "%s %s" % (outlet_id, outlet_name)
        parsed[outlet_id_name] = int(outlet_state_str)
    return parsed


def inventory_sentry_pdu_outlets(parsed):
    return [ (key, None) for key in parsed ]


def check_sentry_pdu_outlets(item, _no_params, parsed):
    outlet_states = {
        0: (0, "off"),
        1: (0, "on"),
        2: (1, "off wait"),
        3: (1, "on wait"),
        4: (2, "off error"),
        5: (2, "on error"),
        6: (2, "no comm"),
        7: (2, "reading"),
        8: (2, "off fuse"),
        9: (2, "on fuse"),
    }

    if item in parsed:
        outlet_state = parsed[item]

        if outlet_state in outlet_states:
            return outlet_states[outlet_state][0], \
                   "Status: %s" % outlet_states[outlet_state][1]
        else:
            return 3, "Unhandled state: %d" % outlet_state


check_info['sentry_pdu_outlets'] = {
    'parse_function'        : parse_sentry_pdu_outlets,
    'inventory_function'    : inventory_sentry_pdu_outlets,
    'check_function'        : check_sentry_pdu_outlets,
    'service_description'   : 'Outlet %s',
    'snmp_info'             : ( ".1.3.6.1.4.1.1718.3.2.3.1",  [ "2", "3", "5" ] ),
    'snmp_scan_function'    : lambda oid: oid(".1.3.6.1.2.1.1.2.0") == ".1.3.6.1.4.1.1718.3",
    'includes'              : [ 'elphase.include' ],
}
