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


# Some relevant OIDs are
# sysUpTime: 1.3.6.1.4.1.11.2.3.1.1.1
# sysUsers: 1.3.6.1.4.1.11.2.3.1.1.2
# sysAvgJobs1: 1.3.6.1.4.1.11.2.3.1.1.3
# sysAvgJobs5: 1.3.6.1.4.1.11.2.3.1.1.4
# sysAvgJobs15: 1.3.6.1.4.1.11.2.3.1.1.5
# sysMaxProcess: 1.3.6.1.4.1.11.2.3.1.1.6
# sysFreeMemory: 1.3.6.1.4.1.11.2.3.1.1.7
# sysPhysMemory: 1.3.6.1.4.1.11.2.3.1.1.8
# sysMaxUserMemory: 1.3.6.1.4.1.11.2.3.1.1.9
# sysSwapConfig: 1.3.6.1.4.1.11.2.3.1.1.10
# sysEnabledSwap: 1.3.6.1.4.1.11.2.3.1.1.11
# sysFreeSwap: 1.3.6.1.4.1.11.2.3.1.1.12
# sysUserCPU: 1.3.6.1.4.1.11.2.3.1.1.13
# sysSysCPU: 1.3.6.1.4.1.11.2.3.1.1.14
# sysIdleCPU: 1.3.6.1.4.1.11.2.3.1.1.15
# sysNiceCPU: 1.3.6.1.4.1.11.2.3.1.1.16

# Example walk:
# .1.3.6.1.4.1.11.2.3.1.1.1.0  215207600
# .1.3.6.1.4.1.11.2.3.1.1.10.0  33357824
# .1.3.6.1.4.1.11.2.3.1.1.11.0  33357824
# .1.3.6.1.4.1.11.2.3.1.1.12.0  29350932
# .1.3.6.1.4.1.11.2.3.1.1.13.0  52129100
# .1.3.6.1.4.1.11.2.3.1.1.14.0  23331438
# .1.3.6.1.4.1.11.2.3.1.1.15.0  123137168
# .1.3.6.1.4.1.11.2.3.1.1.16.0  10



def inventory_hpux_snmp_cpu(info):
    if len(info) > 0:
        return [(None, None)]

def check_hpux_snmp_cpu(item, _no_params, info):
    parts = dict(info)
    this_time = time.time()
    total_rate = 0
    rates = []
    for what, oid in [
        ( "user",   "13.0" ),
        ( "system", "14.0" ),
        ( "idle",   "15.0" ),
        ( "nice",   "16.0" )]:
        value = int(parts[oid])
        rate = get_rate("snmp_cpu_util.%s" % what, this_time, value)
        total_rate += rate
        rates.append(rate)

    if total_rate == 0:
        raise MKCounterWrapped("No counter counted. Time has ceased to flow.")

    perfdata = []
    infos = []
    for what, rate in zip(["user", "system", "idle", "nice"], rates):
        part = rate / total_rate
        perc = part * 100
        perfdata.append((what, perc, None, None, 0, 100))
        infos.append("%s: %.0f%%" % (what, perc))

    return (0, ", ".join(infos), perfdata)


check_info['hpux_snmp_cs.cpu'] = (check_hpux_snmp_cpu, "CPU utilization", 1, inventory_hpux_snmp_cpu)

snmp_info['hpux_snmp_cs'] = ( ".1.3.6.1.4.1.11.2.3.1", [ OID_END, 1 ])

snmp_scan_functions['hpux_snmp_cs.cpu'] = \
    lambda oid: oid(".1.3.6.1.2.1.1.1.0").startswith("HP-UX")
