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

memused_default_levels = (150.0, 200.0)

# Memory information is - together with filesystems - in
# hrStorage. We need the entries of the types hrStorageVirtualMemory
# and hrStorageRam
def inventory_hr_mem(info):
    # Do we find at least one entry concerning memory?
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        if hrtype in [ ".1.3.6.1.2.1.25.2.1.2",
                       ".1.3.6.1.2.1.25.2.1.3" ]:
            if int(hrsize) > 0: # some device have zero (broken) values
                return [(None, "memused_default_levels")]

def check_hr_mem(item, params, info):
    usage = {}
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        if hrtype in [ ".1.3.6.1.2.1.25.2.1.2",
                       ".1.3.6.1.2.1.25.2.1.3" ]:
            size = saveint(hrsize) * saveint(hrunits) / 1048576.0
            used = saveint(hrused) * saveint(hrunits) / 1048576.0
            usage[hrtype] = (size, used)

    # Account for cached memory (this works at least for systems using
    # the UCD snmpd (such as Linux based applicances)
    cached_mb = 0
    for hrtype, hrdescr, hrunits, hrsize, hrused in info:
        if hrdescr in [ "Cached memory", "Memory buffers" ]:
            cached_mb += saveint(hrused) * saveint(hrunits) / 1048576.0

    totalram_mb,  ramused_mb   = usage.get(".1.3.6.1.2.1.25.2.1.2", (0,0))
    ramused_mb -= cached_mb
    totalvirt_mb, virtused_mb  = usage.get(".1.3.6.1.2.1.25.2.1.3", (0,0))
    totalmem_mb,  totalused_mb = totalram_mb + totalvirt_mb, ramused_mb + virtused_mb
    if totalmem_mb > 0:
        totalused_perc = 100 * totalused_mb / totalram_mb

        perfdata = [
            ('ramused', str(ramused_mb) + 'MB', None, None, 0, totalram_mb),
            ('swapused', str(virtused_mb) + 'MB', None, None, 0, totalvirt_mb) ]

        infotext = "%.2f GB used (%.2f GB RAM + %.2f GB SWAP, this is %.1f%% of %.2f GB RAM)" % \
               (totalused_mb / 1024.0, ramused_mb / 1024, virtused_mb / 1024, totalused_perc, totalram_mb / 1024.0)

        warn, crit = params
        if type(warn) == float:
            perfdata.append(('memused', str(totalused_mb)+'MB', int(warn/100.0 * totalmem_mb),
                        int(crit/100.0 * totalmem_mb), 0, totalvirt_mb))
            if totalused_perc >= crit:
                return (2, 'CRIT - %s, critical at %.1f%%' % (infotext, crit), perfdata)
            elif totalused_perc >= warn:
                return (1, 'WARN - %s, warning at %.1f%%' % (infotext, warn), perfdata)
            else:
                return (0, 'OK - %s' % infotext, perfdata)
        else:
            perfdata.append(('memused', str(totalused_mb)+'MB', warn, crit, 0, totalvirt_mb))
            if totalused_mb >= crit:
                return (2, 'CRIT - %s, critical at %.2f GB' % (infotext, crit / 1024.0), perfdata)
            elif totalused_mb >= warn:
                return (1, 'WARN - %s, warning at %.2f GB' % (infotext, warn / 1024.0), perfdata)
            else:
                return (0, 'OK - %s' % infotext, perfdata)

    return (3, "Invalid information. Total memory is empty.")

check_info["hr_mem"] = (check_hr_mem, "Memory used", 1, inventory_hr_mem)
snmp_info["hr_mem"] = ( ".1.3.6.1.2.1.25.2.3.1", [
    2, # hrStorageType
    3, # hrStorageDescr
    4, # hrStorageAllocationUnits
    5, # hrStorageSize
    6, # hrStorageUsed
] )
snmp_scan_functions["hr_mem"] = lambda oid: \
        not not oid('.1.3.6.1.2.1.25.1.1.0') # HOST-RESOURCES-MIB::hrSystemUptime.0
