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

# 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 inv_prtconf(info):
    confdict = {}
    for line in info:
        if len(line) == 2:
            varname = line[0]
            value = line[1].strip()
            confdict[varname] = value

        if line[0].startswith("========="):
            break # ignore the rest of the output currently

    for varname, value in confdict.items():
        if varname == "CPU Type":
            if value == "64-bit":
                arch = "ppc64"
            else:
                arch = "ppc"
            inv_tree("hardware.cpu.")["arch"] = arch

        elif varname == "Kernel Type":
            if value == "64-bit":
                arch = "ppc64"
            else:
                arch = "ppc"
            inv_tree("software.os.")["arch"] = arch

        elif varname == "Processor Type":
            inv_tree("hardware.cpu.")["model"] = value

        elif varname == "Processor Clock Speed":
            inv_tree("hardware.cpu.")["max_speed"] = float(value.split()[0]) * 1000 * 1000

        elif varname == "Number Of Processors":
            inv_tree("hardware.cpu.")["cpus"] = int(value)

        elif varname == "Firmware Version":
            inv_tree("hardware.bios.")["version"] = value
            if value.startswith("IBM"):
                inv_tree("hardware.bios.")["vendor"] = "IBM"
                inv_tree("hardware.system.")["manufacturer"] = "IBM"

        elif varname == "Machine Serial Number":
            inv_tree("hardware.system.")["serial"] = value

        elif varname == "System Model":
            inv_tree("hardware.system.")["product"] = value

        elif varname == "Memory Size":
            inv_tree("hardware.memory.")["total_ram_usable"] = int(value.split()[0]) * 1024 * 1024

        elif varname == "Total Paging Space":
            inv_tree("hardware.memory.")["total_swap"] = int(value.replace("MB", "")) * 1024 * 1024


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