#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |          _           _           _       _   __   _______        |
# |       __| |_  ___ __| |__  _ __ | |__   / | /  \ |__ / _ \       |
# |      / _| ' \/ -_) _| / / | '  \| / /   | || () | |_ \_, /       |
# |      \__|_||_\___\__|_\_\_|_|_|_|_\_\   |_(_)__(_)___//_/        |
# |                                            check_mk 1.0.39       |
# |                                                                  |
# | Copyright Mathias Kettner 2009                mk@mathias-kettner |
# +------------------------------------------------------------------+
# 
# This file is part of check_mk 1.0.39.
# 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.

# vms_memused_default_levels = (50.0, 80.0)

vms_memused_default_levels         = (150.0, 200.0)

def inventory_vms_mem(checkname, info):
    for line in info:
        if line[0] == "MEM":
            return [(None, "", "vms_memused_default_levels")]
    return []


# MEM 524288.00 20106.00 157124.00 26635.00 107394.00 168.00 7.00 9.00 3.00 0.00 0.00 8192.00 79.52
def check_vms_mem(item, params, info):
    warn, crit = params
    try:
      for line in info:
        if line[0].startswith("MEM"):
            physical_size_in_pages  = (float(line[1]))  # Gesamtspeicher
            vms_alloc_pages         = (float(line[2]))  # reserviert fuer Kernel
            virtual_io_cache_pages  = (float(line[3]))  
            modified_list_pages     = (float(line[4]))  # dirty pages
            free_list_size_pages    = (float(line[5]))  # ist so eine Art Cache, letztendlich
            number_of_processes     = (float(line[6]))
            interactive_processes   = (float(line[7]))
            network_processes       = (float(line[8]))
            batch_processes         = (float(line[9]))
            proc_in_swapped_persec  = (float(line[10]))
            proc_out_swapped_persec = (float(line[11]))
            pagesize                = (float(line[12]))
            memory_used_perc        = (float(line[13]))

            mem_total_mb = float(physical_size_in_pages * pagesize) / (1024*1024)
            mem_used_mb  = mem_total_mb * memory_used_perc / 100.0
            warn_mb = warn * mem_total_mb / 100
            crit_mb = crit * mem_total_mb / 100
            perfdata = [ ('ramused', "%.2fMB" % mem_used_mb, warn_mb, crit_mb, 0, mem_total_mb) ]

            used_txt = "%.1f%% of RAM (%.2f of %.2f GB) used by processes" % (memory_used_perc, mem_used_mb/1024, mem_total_mb/1024)
            if memory_used_perc >= crit:
                return (2, 'CRIT - %s (critical at %.1f%%)' % (used_txt, crit), perfdata)
            elif memory_used_perc >= warn:
                return (1, 'WARN - %s (warning at %.1f%%)' % (used_txt, warn), perfdata)
            else:
                return (0, 'OK - %s' % used_txt, perfdata)

    except 1:
        return (3, "UNKNOWN - invalid output from plugin")

check_info['vms_sys.mem'] =  (check_vms_mem, "Memory used", 1,  inventory_vms_mem)

# <<<vms_sys>>>
#      sum  us   sy   wa   sy   sy   sy
# CPU0 3.30 1.18 0.99 0.10 0.68 0.32 0.46
# CPU1 6.32 3.08 1.62 0.16 1.25  0.32 0.46
# CPU2 6.77 3.00 2.37 0.24 0.75  0.32 0.46
# CPU3 12.22 5.11 4.35 0.19 1.27  0.32 0.46
# MEM 524288.00 20106.00 157124.00 26635.00 107394.00 168.00 7.00 9.00 3.00 0.00 0.00 8192.00 79.52
# PAG 12.35 2.03 0.00 2.03 10.32
# GIO 18.56 284.52 0.10 12.64 0.00 1.15 1765.00
# XQP 2.23 0.02 0.07 0.00 0.15 0.00 0.00 0.00
# LCK 19624.00 22.16 37.58 22.18 6.66 0.40 0.00 0.00 0.00
def inventory_vms_cpu_utilization(checkname, info):
    for line in info:
        if line[0].startswith("CPU"):
            return [(None, "", None)]
    return []

def check_vms_cpu_utilization(item, param, info):
    # Es sieht so aus, als sind die %-Werte, die wir vom
    # Agenten bekommen, bereits durch die Anzahl der CPUs
    # geteilt, so dass das Maximum der Summe 100% ist und
    # nicht 100% mal CPU-Anzahl. Eigentlich sollte der Agent
    # besser die Werte so ausgeben, dass jede einzelne CPU
    # fuer sich 100% erreichen kann. 
    # Wir gleichen das aktuell hier im Check aus. Der Agent
    # sollte dahingehend aber ueberprueft werden.
    us = 0.0
    sy = 0.0
    wa = 0.0
    count = 0
    for line in info:
        if line[0].startswith("CPU"):
            us += float(line[2])
            sy += float(line[3]) + float(line[5]) + float(line[6]) + float(line[7])
            wa += float(line[4])
            count += 1
    if count == 0:
        return (3, "UNKNOWN - incomplete data from host")

    if count == 1:
	cputext = "1 CPU"
    else:
	cputext = "%d CPUS" % count

    user_perc   = us / count
    system_perc = sy / count  
    wait_perc   = wa / count

    perfdata = [ 
          ( "user",   "%.3f" % user_perc ), 
          ( "system", "%.3f" % system_perc ), 
          ( "wait",   "%.3f" % wait_perc ) ]
    return (0, "OK - user: %2.0f%%, system: %2.0f%%, wait: %2.0f%% (%s)" 
	% (user_perc, system_perc, wait_perc, cputext), perfdata)
    
check_info['vms_sys.util'] =  (check_vms_cpu_utilization, "CPU utilization", 1,  inventory_vms_cpu_utilization)
