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

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 saveint(hrsize) > 1: # some device have zero (broken) values
                return [(None, "memused_default_levels")]

def check_hr_mem(_no_item, params, info):
    # This check does not yet support averaging. We need to
    # convert it to mem.include
    if type(params) == dict:
        params = params["levels"]

    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
	    # We use only the first entry of each type. We have
	    # seen devices (pfSense), that have lots of additional
            # entries that are not useful.
	    if hrtype not in usage and hrdescr != "Virtual memory":
	        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" ]:
            hr_mem = saveint(hrused)
            if hr_mem < 0: # some devices report negative used cache values...
                hr_mem = saveint(hrsize)
            cached_mb += hr_mem * 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 and totalram_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 * totalram_mb),
                        int(crit/100.0 * totalram_mb), 0, totalvirt_mb))
            if totalused_perc >= crit:
                return (2, '%s, critical at %.1f%%' % (infotext, crit), perfdata)
            elif totalused_perc >= warn:
                return (1, '%s, warning at %.1f%%' % (infotext, warn), perfdata)
            else:
                return (0, '%s' % infotext, perfdata)
        else:
            perfdata.append(('memused', str(totalused_mb)+'MB', warn, crit, 0, totalram_mb))
            if totalused_mb >= crit:
                return (2, '%s, critical at %.2f GB' % (infotext, crit / 1024.0), perfdata)
            elif totalused_mb >= warn:
                return (1, '%s, warning at %.2f GB' % (infotext, warn / 1024.0), perfdata)
            else:
                return (0, '%s' % infotext, perfdata)

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

check_info["hr_mem"] = {
    'check_function':          check_hr_mem,
    'inventory_function':      inventory_hr_mem,
    'service_description':     'Memory used',
    'has_perfdata':            True,
    'snmp_info':               ('.1.3.6.1.2.1.25.2.3.1', [
                                    2, # hrStorageType
                                    3, # hrStorageDescr
                                    4, # hrStorageAllocationUnits
                                    5, # hrStorageSize
                                    6, # hrStorageUsed
                                ]),
    # HOST-RESOURCES-MIB::hrSystemUptime.0
    'snmp_scan_function':      lambda oid: \
         not not oid('.1.3.6.1.2.1.25.1.1.0'),
    'group':                   'memory',
}
