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

j4p_performance_mem_default_levels = ( 1000, 2000 ) # MB

def j4p_performance_parse(info):
    parsed = {}
    for instance, var, value in info:
	entry = parsed.get(instance, {})
	parsed[instance] = entry
	entry[var] = value
    return parsed

def inventory_j4p_performance_mem(checktype, info):
    parsed = j4p_performance_parse(info)
    return [ (k, "j4p_performance_mem_default_levels") for k in parsed ]

def check_j4p_performance_mem(item, params, info):
    warn, crit = params
    parsed = j4p_performance_parse(info)
    if item not in parsed:
	return (3, "UNKNOWN - data not found in agent output")
    d = parsed[item]
    mb = 1024 * 1024.0
    heap = saveint(d["HeapMemoryUsage"]) / mb
    non_heap = saveint(d["NonHeapMemoryUsage"]) / mb
    total = heap + non_heap
    perfdata = [ ("heap",    heap,     warn, crit),
		 ("nonheap", non_heap, warn, crit) ]
    infotext = "%.0f MB total (%.0f heap, %.0f MB non-heap), levels at %.0f/%.0f" % (total, heap, non_heap, warn, crit)
    if total >= crit:
	return (2, "CRIT - " + infotext, perfdata)
    elif total >= warn:
	return (1, "WARN - " + infotext, perfdata)
    else:
	return (0, "OK - " + infotext, perfdata)


check_info["j4p_performance.mem"] = ( check_j4p_performance_mem, "JMX %s Memory", 1, inventory_j4p_performance_mem )

