#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2015             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
# Written by comNET GmbH, Ringo Hartmann
#
# 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.

# <<<appdynamics_web_container:sep(124)>>>
# Hans|http-8180|Error Count:0|Busy Threads:0|Current Threads In Pool:0|Request Count:0|Maximum Threads:200
# Hans|jk-8109|Error Count:0|Request Count:2

def inventory_appdynamics_web_container(info):
    for line in info:
        yield ' '.join(line[0:2]), {}

def check_appdynamics_web_container(item, params, info):
    for line in info:
        if item == ' '.join(line[0:2]):
            values = {}
            for metric in line[2:]:
                name, value = metric.split(':')
                values[name] = int(value)

            error_count     = values.get('Error Count', None)
            request_count   = values.get('Request Count', None)

            current_threads = values.get('Current Threads In Pool', None)
            busy_threads    = values.get('Busy Threads', None)
            max_threads     = values.get('Maximum Threads', None)

            if type(params) == tuple:
                warn, crit = params
            else:
                warn, crit = (None, None)

            if current_threads != None:
                state = 0
                if crit and current_threads >= crit:
                    state = 2
                elif warn and current_threads >= warn:
                    state = 1

                thread_levels_label = ''
                if state > 0:
                    thread_levels_label = ' (warn/crit at %d/%d)' % (warn, crit)

                if max_threads != None:
                    perfdata = [('current_threads', current_threads, warn, crit, 0, max_threads)]
                    threads_percent = (100.0 * current_threads / max(1, max_threads))
                    max_info = ' of %d (%.2f%%)' % (max_threads, threads_percent)
                else:
                    perfdata = [('current_threads', current_threads, warn, crit)]
                    max_info = ''
                yield state, 'Current threads: %d%s%s' % (current_threads, max_info, thread_levels_label), perfdata

                if busy_threads != None:
                    perfdata = [('busy_threads', busy_threads)]
                    yield 0, 'Busy threads: %d' % busy_threads, perfdata

            now = time.time()

            if error_count != None:
                rate_id = 'appdynamics_web_container.%s.error' % (item.lower().replace(' ', '_'))
                error_rate = get_rate(rate_id, now, error_count)
                perfdata = [('error_rate', error_rate)]
                yield 0, 'Errors: %.2f/sec' % error_rate, perfdata

            if request_count != None:
                rate_id = 'appdynamics_web_container.%s.request' % (item.lower().replace(' ', '_'))
                request_rate = get_rate(rate_id, now, request_count)
                perfdata = [('request_rate', request_rate)]
                yield 0, 'Requests: %.2f/sec' % request_rate, perfdata

check_info['appdynamics_web_container'] = {
  'inventory_function'  : inventory_appdynamics_web_container,
  'check_function'      : check_appdynamics_web_container,
  'service_description' : 'AppDynamics Web Container %s',
  'has_perfdata'        : True,
  'group'               : 'jvm_threads',
}
