#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2010             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-
# ails.  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.

# Author: Lars Michelsen <lm@mathias-kettner.de>

# Relevant SNMP OIDs:
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.1.1 1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.1.2 2
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.1.3 3
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.1.4 4
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.2.1 SNMPv2-SMI::enterprises.11.2.3.7.8.3.2
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.2.2 SNMPv2-SMI::enterprises.11.2.3.7.8.3.1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.2.3 SNMPv2-SMI::enterprises.11.2.3.7.8.3.1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.2.4 SNMPv2-SMI::enterprises.11.2.3.7.8.3.3
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.3.1 1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.3.2 1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.3.3 1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.3.4 1
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.4.1 4
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.4.2 4
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.4.3 5
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.4.4 4
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.5.1 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.5.2 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.5.3 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.5.4 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.6.1 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.6.2 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.6.3 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.6.4 0
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.7.1 "Fan Sensor"
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.7.2 "Power Supply Sensor"
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.7.3 "Redundant Power Supply Sensor"
# .1.3.6.1.4.1.11.2.14.11.1.2.6.1.7.4 "Over-temperature Sensor"

# Status codes:
# 1 => unknown,
# 2 => bad,
# 3 => warning
# 4 => good,
# 5 => notPresent

# GENERAL MAPS:

hp_procurve_status_map = { '1': 'unknown', '2': 'bad', '3': 'warning', '4': 'good', '5': 'notPresent' }
hp_procurve_status2nagios_map = { 'unknown': 3, 'bad': 2, 'warning': 1, 'good': 0, 'notPresent': 1 }

def get_hp_procurve_sensor_type(type_input):
    type = ''
    if type_input.endswith('11.2.3.7.8.3.1'):
        type = 'PSU'
    elif type_input.endswith('11.2.3.7.8.3.2'):
        type = 'FAN'
    elif type_input.endswith('11.2.3.7.8.3.3'):
        type = 'Temp'
    elif type_input.endswith('11.2.3.7.8.3.4'):
        type = 'FutureSlot'
    return type

def inventory_hp_procurve_sensors(checkname, info):
    inventory = []
    for line in info:
        if len(line) == 4 and hp_procurve_status_map[line[2]] != 'notPresent':
            inventory.append((line[0], None))
    return inventory

def check_hp_procurve_sensors(item, _not_used, info):
    for line in info:
        if line[0] == item:
            procurve_status = hp_procurve_status_map[line[2]]
            status = hp_procurve_status2nagios_map[procurve_status]

            return (status, '%s - Condition of %s "%s" is %s' %
                      (nagios_state_names[status], get_hp_procurve_sensor_type(line[1]), line[3], procurve_status))
    return (3, "UNKNOWN - item not found in snmp data")


check_info['hp_procurve_sensors'] = (check_hp_procurve_sensors, "Sensor %s", 0,  inventory_hp_procurve_sensors)
snmp_info['hp_procurve_sensors']  = ( ".1.3.6.1.4.1.11.2.14.11.1.2.6.1", [ "1", "2", "4", "7" ] )
snmp_scan_functions['hp_procurve_sensors'] = \
    lambda oid: ".11.2.3.7.11" in oid(".1.3.6.1.2.1.1.2.0")
