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

# We use the following OIDs:


# PowerNet-MIB::upsAdvTestDiagnosticsResults   .1.3.6.1.4.1.318.1.1.1.7.2.3
# upsAdvTestDiagnosticsResults OBJECT-TYPE
#         SYNTAX INTEGER {
#                 ok(1),
#                 failed(2),
#                 invalidTest(3),
#                 testInProgress(4)
#         }
#         ACCESS read-only
#         STATUS mandatory
#         DESCRIPTION
#                 "The results of the last UPS diagnostics test performed."
#         ::= { upsAdvTest 3 }

# PowerNet-MIB::upsAdvTestLastDiagnosticsDate  .1.3.6.1.4.1.318.1.1.1.7.2.4
# upsAdvTestLastDiagnosticsDate OBJECT-TYPE
#         SYNTAX DisplayString
#         ACCESS read-only
#         STATUS mandatory
#         DESCRIPTION
#                 "The date the last UPS diagnostics test was performed in
#                  mm/dd/yy format."
#         ::= { upsAdvTest 4 }
#

import datetime

ups_test_default = ( 0, 0 )

def check_apc_test(item, params, info):
    warn, crit = params
    if not info:
        return 3, "Data Missing"
    line = info[0]
    DiagnosticsResults = saveint(line[0])
    LastDiagnosticsDate = line[1]

    month, day, year = map(saveint, LastDiagnosticsDate.split('/'))
    last_diag_date = datetime.date( year, month, day)
    today = datetime.date.today()
    diag_diff = today - last_diag_date
    dcrit =  datetime.timedelta(days=crit)
    dwarn =  datetime.timedelta(days=warn)

    diagnostic_status_text = { 1:"OK", 2:"failed", 3:"invalid", 4:"in progress" }

    state = 0
    diag_label = ""
    if DiagnosticsResults == 2:
        state = 2
        diag_label = "(!!)"
    elif DiagnosticsResults == 3:
        state = 1
        diag_label = "(!)"

    time_label = ""
    if crit != 0 and diag_diff >= dcrit:
        state = 2
        time_label = "(!!)"
    elif warn != 0 and diag_diff >= dwarn:
        state = max(state, 1)
        time_label = "(!)"

    ResultsDetail = "Result of self test: %s%s, Date of last test: %s%s" \
        % (diagnostic_status_text.get(DiagnosticsResults), diag_label, LastDiagnosticsDate, time_label)

    return (state, ResultsDetail )

def inventory_apc_test(info):
    if info:
        return [(None, "ups_test_default")]


check_info['apc_symmetra_test'] = {
  "inventory_function"  : inventory_apc_test,
  "check_function"      : check_apc_test,
  "service_description" : "Self Test",
  "has_perfdata"        : False,
  "group"               : "ups_test",
  "snmp_scan_function"  : lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.318.1.3"),
  "snmp_info"           : (".1.3.6.1.4.1.318.1.1.1.7.2", [ 3,4 ] )
}
