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

# Example output from agent:
# <<<prtconf:sep(58):persist(1404743142)>>>
# System Model: IBM,8231-E2D
# Machine Serial Number: 06AAB2T
# Processor Type: PowerPC_POWER7
# Processor Implementation Mode: POWER 7
# Processor Version: PV_7_Compat
# Number Of Processors: 8
# Processor Clock Speed: 4284 MHz
# CPU Type: 64-bit
# Kernel Type: 64-bit
# LPAR Info: 1 wiaix001
# Memory Size: 257792 MB
# Good Memory Size: 257792 MB
# Platform Firmware level: AL770_076
# Firmware Version: IBM,AL770_076
# Console Login: enable
# Auto Restart: true
# Full Core: false

# Note: this is only the header. Much more stuff follows, but is currently
# not being parsed.


def parse_prtconf(info):
    parsed = {}

    for line in info:
        if not line:
            continue
        if line[0].startswith("========="):
            break  # ignore the rest of the output currently
        if len(line) == 2:
            k, v = line
            parsed[k] = v.strip()

    return parsed


def _split_vendor(string):
    if string.upper().startswith("IBM"):
        return "IBM", string[3:].lstrip('., -/')
    return "", string


def inv_prtconf(info, inventory_tree):
    parsed = parse_prtconf(info)

    cpu_dict = inventory_tree.get_dict("hardware.cpu.")
    sys_dict = inventory_tree.get_dict("hardware.system.")
    mem_dict = inventory_tree.get_dict("hardware.memory.")
    fmw_dict = inventory_tree.get_dict("software.firmware.")

    cpu_type = parsed.get("CPU Type")
    if cpu_type is not None:
        cpu_dict["arch"] = "ppc64" if cpu_type == "64-bit" else "ppc"

    kernel_type = parsed.get("Kernel Type")
    if kernel_type is not None:
        os_dict = inventory_tree.get_dict("software.os.")
        os_dict["arch"] = "ppc64" if kernel_type == "64-bit" else "ppc"

    proc_type = parsed.get("Processor Type")
    if proc_type is not None:
        cpu_dict["model"] = proc_type

    proc_impl_mode = parsed.get("Processor Implementation Mode")
    if proc_impl_mode is not None:
        cpu_dict["implementation_mode"] = proc_impl_mode

    max_speed = parsed.get("Processor Clock Speed")
    if max_speed is not None:
        cpu_dict["max_speed"] = float(max_speed.split()[0]) * 1000 * 1000

    num_cpu = parsed.get("Number Of Processors")
    if num_cpu is not None:
        cpu_dict.setdefault("cpus", int(num_cpu))

    fw_version = parsed.get("Firmware Version")
    if fw_version is not None:
        vendor, fmw_dict["version"] = _split_vendor(fw_version)
        if vendor:
            fmw_dict["vendor"] = vendor

    fw_platform_level = parsed.get("Platform Firmware level")
    if fw_platform_level is not None:
        fmw_dict["platform_level"] = fw_platform_level

    serial = parsed.get("Machine Serial Number")
    if serial is not None:
        sys_dict["serial"] = serial

    model = parsed.get("System Model")
    if model is not None:
        manufacturer, sys_dict["product"] = _split_vendor(model)
        if manufacturer:
            sys_dict["manufacturer"] = manufacturer

    ram = parsed.get("Memory Size")
    if ram is not None:
        mem_dict["total_ram_usable"] = int(ram.split()[0]) * 1024 * 1024

    swap = parsed.get("Total Paging Space")
    if swap is not None:
        mem_dict["total_swap"] = int(swap.replace("MB", "")) * 1024 * 1024


inv_info['prtconf'] = {
    "inv_function": inv_prtconf,
}
