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

# Example output from agent:
# <<<winperf_mem>>>
# 1440580801.44 4 3579545
# 24 5203433 counter
# 20 643575808 large_rawcount
# 22 291414016 large_rawcount
# 26 6212497408 large_rawcount
# 28 17614 counter
# 30 1012830 counter
# 32 3988745 counter
# 34 456907 counter
# 36 2011463 counter             -----> Pages Counter
# 818 2007095 counter
# 38 141612 counter
# 44 4368 counter
# 52 35241984 large_rawcount
# 54 6586368 large_rawcount
# 46 273 counter
# 56 41033 rawcount
# 60 33522 rawcount
# 674 3545 rawcount
# 814 114274304 large_rawcount
# 816 155688960 large_rawcount
# 62 35016704 large_rawcount
# 64 1003520 large_rawcount
# 66 131072 large_rawcount
# 68 2777088 large_rawcount
# 70 1777664 large_rawcount
# 72 77348864 large_rawcount
# 1402 71146 raw_fraction
# 1402 1516723 raw_base
# 1376 628492 large_rawcount
# 1378 613 large_rawcount


def inventory_winperf_mem(info):
    if len(info) > 1:
        return [ (None, {}) ]

def check_winperf_mem(_unused, params, info):
    init_line = info[0]
    this_time = float(init_line[0])

    lines = iter(info)
    try:
        while True:
            line = lines.next()
            if line[0] == "36":
                page_counter = int(line[1])
                break
    except StopIteration:
        pass

    pages_per_sec = get_rate(None, this_time, page_counter)
    state = 0
    if "pages_per_second" in params:
        warn, crit = params["pages_per_second"]
        if pages_per_sec >= crit:
            state = 2
        elif pages_per_sec >= warn:
            state = 1

    yield state, "Pages/s: %d" % pages_per_sec, [("mem_pages_rate", pages_per_sec)]

check_info["winperf_mem"] = {
    'check_function':          check_winperf_mem,
    'inventory_function':      inventory_winperf_mem,
    'service_description':     'Memory Pages',
    'group':                   'mem_pages',
    'has_perfdata':            True
}
