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

# <<<solaris_mem>>>
# Memory: 256G real, 54G free, 257G swap in use, 86G swap free

# <<<solaris_mem>>>
# Memory: 512M phys mem, 353M free mem, 2000M total swap, 2000M free swap

# <<<solaris_mem>>>
# Memory: 10G phys mem, 1905M free mem, 8002M total swap, 8002M free swap

# <<<solaris_mem>>>
# Memory: 640M real, 10M free, 293M swap in use, 349M swap free

# <<<solaris_mem>>>
# Memory: 2048M real, 913M free, 723M swap in use, 2863M swap free

def parse_solaris_mem(info):
    # The 1.2.4 agent seems to create an empty section under some circumstances
    if not info:
        return

    values = []
    mem_tokens = " ".join(info[0][1:]).split(",")
    is_total_swap = False
    for token in mem_tokens:
        if "total swap" in token:
            is_total_swap = True
        values.append(solaris_mem_to_kbytes(token.split()[0]))

    # convert swap-in-use to swap-total, as expected by check_memory()
    if not is_total_swap:
        values[2] = values[2] + values[3]

    keys   = ['MemTotal', 'MemFree', 'SwapTotal', 'SwapFree']
    return dict(zip(keys, values))


def solaris_mem_to_kbytes(s):
    if s[-1] == 'G':
        return int(s[:-1]) * 1024 * 1024
    elif s[-1] == 'M':
        return int(s[:-1]) * 1024


def inventory_solaris_mem_used(parsed):
    return [(None, {})]


def check_solaris_mem_used(_no_item, params, parsed):
    return check_memory(params, parsed)


check_info['solaris_mem'] = {
    "parse_function"          : parse_solaris_mem,
    "check_function"          : check_solaris_mem_used,
    "inventory_function"      : inventory_solaris_mem_used,
    "service_description"     : "Memory used",
    "has_perfdata"            : True,
    "group"                   : "memory",
    "default_levels_variable" : "memory_default_levels",
    "includes"                : [ "mem.include" ],
}
