#!/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.


# No faults:
# Agent section is empty

# From https://docs.oracle.com/cd/E23824_01/html/821-1451/glisy.html
# One fault:
# --------------- ------------------------------------  -------------- ---------
# TIME            EVENT-ID                              MSG-ID         SEVERITY
# --------------- ------------------------------------  -------------- ---------
# Aug 24 17:56:03 7b83c87c-78f6-6a8e-fa2b-d0cf16834049  SUN4V-8001-8H  Minor
#
# Host        : bur419-61
# Platform    : SUNW,T5440        Chassis_id  : BEL07524BN
# Product_sn  : BEL07524BN
#
# Fault class : fault.cpu.ultraSPARC-T2plus.ireg
# Affects     : cpu:///cpuid=0/serial=1F95806CD1421929
#                   faulted and taken out of service
# FRU         : "MB/CPU0" (hc://:product-id=SUNW,T5440:server-id=bur419-61:\
#               serial=3529:part=541255304/motherboard=0/cpuboard=0)
#                   faulty
# Serial ID.  : 3529
#               1F95806CD1421929
#
# Description : The number of integer register errors associated with this thread
#               has exceeded acceptable levels.
#
# Response    : The fault manager will attempt to remove the affected thread from
#               service.
#
# Impact      : System performance may be affected.
#
# Action      : Use 'fmadm faulty' to provide a more detailed view of this event.
#               Please refer to the associated reference document at
#               http://sun.com/msg/SUN4V-8001-8H for the latest service
#               procedures and policies regarding this diagnosis.

# Multiple faults:
# --------------- ------------------------------------  -------------- -------
# TIME            EVENT-ID                              MSG-ID         SEVERITY
# --------------- ------------------------------------  -------------- -------
# Sep 21 10:01:36 d482f935-5c8f-e9ab-9f25-d0aaafec1e6c  PCIEX-8000-5Y  Major
#
# Fault class  : fault.io.pci.device-invreq
# Affects      : dev:///pci@0,0/pci1022,7458@11/pci1000,3060@0
#                dev:///pci@0,0/pci1022,7458@11/pci1000,3060@1
#                 ok and in service
#                dev:///pci@0,0/pci1022,7458@11/pci1000,3060@2
#                dev:///pci@0,0/pci1022,7458@11/pci1000,3060@3
#                  faulty and taken out of service
# FRU          : "SLOT 2" (hc://.../pciexrc=3/pciexbus=4/pciexdev=0)
#                  repair attempted
#                "SLOT 3" (hc://.../pciexrc=3/pciexbus=4/pciexdev=1)
#                  acquitted
#                "SLOT 4" (hc://.../pciexrc=3/pciexbus=4/pciexdev=2)
#                  not present
#                "SLOT 5" (hc://.../pciexrc=3/pciexbus=4/pciexdev=3)
#                  faulty
#
#  Description  : The transmitting device sent an invalid request.
#
#  Response     : One or more device instances may be disabled
#
#  Impact       : Possible loss of services provided by the device instances
#                 associated with this fault
#
#  Action       : Use 'fmadm faulty' to provide a more detailed view of this event.
#                 Please refer to the associated reference document at
#                 http://sun.com/msg/PCIEX-8000-5Y for the latest service
#                 procedures and policies regarding this diagnosis.


def inventory_solaris_fmadm(info):
    return [ (None, None) ]


def check_solaris_fmadm(_no_item, params, info):
    if not info:
        return 0, "No faults detected"

    map_state = {
        "minor"    : (1, "minor"),
        "major"    : (2, "major"),
        "critical" : (2, "critical"),
    }

    infotext = ""
    state, state_readable = map_state.get(info[3][-1].split(" ")[-1].lower(), (3, "unknown"))
    for line in info[4:]:
        if line[0].strip().lower() == "fault class":
            infotext += "Status: %s, Name: %s" % (state_readable, line[1].strip())

    return state, infotext


check_info['solaris_fmadm'] = {
    'inventory_function'        : inventory_solaris_fmadm,
    'check_function'            : check_solaris_fmadm,
    'service_description'       : 'FMD Status',
}
