|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8; py-indent-offset: 4 -*- |
| 3 | +# +------------------------------------------------------------------+ |
| 4 | +# | ____ _ _ __ __ _ __ | |
| 5 | +# | / ___| |__ ___ ___| | __ | \/ | |/ / | |
| 6 | +# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / | |
| 7 | +# | | |___| | | | __/ (__| < | | | | . \ | |
| 8 | +# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ | |
| 9 | +# | | |
| 10 | +# | Copyright Mathias Kettner 2016 mk@mathias-kettner.de | |
| 11 | +# +------------------------------------------------------------------+ |
| 12 | +# |
| 13 | +# This file is part of Check_MK. |
| 14 | +# The official homepage is at http://mathias-kettner.de/check_mk. |
| 15 | +# |
| 16 | +# check_mk is free software; you can redistribute it and/or modify it |
| 17 | +# under the terms of the GNU General Public License as published by |
| 18 | +# the Free Software Foundation in version 2. check_mk is distributed |
| 19 | +# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with- |
| 20 | +# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 21 | +# PARTICULAR PURPOSE. See the GNU General Public License for more de- |
| 22 | +# ails. You should have received a copy of the GNU General Public |
| 23 | +# License along with GNU Make; see the file COPYING. If not, write |
| 24 | +# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 25 | +# Boston, MA 02110-1301 USA. |
| 26 | + |
| 27 | +# <<<php_fpm_pools>>> |
| 28 | +# www dynamic active_processes 1 |
| 29 | +# www dynamic accepted_conn 726 |
| 30 | +# www dynamic listen_queue 0 |
| 31 | +# www dynamic start_since 79602 |
| 32 | +# www dynamic idle_processes 1 |
| 33 | +# www dynamic start_time 1545257568 |
| 34 | +# www dynamic slow_requests 0 |
| 35 | +# www dynamic max_active_processes 2 |
| 36 | +# www dynamic max_children_reached 0 |
| 37 | +# www dynamic max_listen_queue 0 |
| 38 | +# www dynamic total_processes 2 |
| 39 | + |
| 40 | + |
| 41 | +from typing import Any, Mapping |
| 42 | +from .agent_based_api.v1.type_defs import CheckResult, DiscoveryResult, StringTable |
| 43 | + |
| 44 | +import time |
| 45 | + |
| 46 | +from cmk.base.plugins.agent_based.agent_based_api.v1 import ( |
| 47 | + check_levels, |
| 48 | + get_rate, |
| 49 | + get_value_store, |
| 50 | + GetRateError, |
| 51 | + register, |
| 52 | + render, |
| 53 | + Service, |
| 54 | +) |
| 55 | + |
| 56 | + |
| 57 | +Section = Mapping[str, Any] |
| 58 | + |
| 59 | + |
| 60 | +def parse_php_fpm_pools(string_table: StringTable) -> Section: |
| 61 | + data = {} |
| 62 | + for line in string_table: |
| 63 | + if len(line) != 4: |
| 64 | + continue # Skip unexpected lines |
| 65 | + pool_name, pm_type, metric, value = line |
| 66 | + item = '%s [%s]' % (pool_name, pm_type) |
| 67 | + if item not in data: |
| 68 | + data[item] = {} |
| 69 | + |
| 70 | + data[item][metric] = int(value) |
| 71 | + |
| 72 | + return data |
| 73 | + |
| 74 | + |
| 75 | +register.agent_section( |
| 76 | + name='php_fpm_pools', |
| 77 | + parse_function=parse_php_fpm_pools, |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +def discover_php_fpm_pools(section: Section) -> DiscoveryResult: |
| 82 | + for item in section.keys(): |
| 83 | + yield Service(item=item) |
| 84 | + |
| 85 | + |
| 86 | +def check_php_fpm_pools( |
| 87 | + item: str, |
| 88 | + params: Mapping[str, Any], |
| 89 | + section: Section, |
| 90 | +) -> CheckResult: |
| 91 | + if item not in section: |
| 92 | + return |
| 93 | + |
| 94 | + if params is None: |
| 95 | + params = {} |
| 96 | + |
| 97 | + data = dict(section[item]) |
| 98 | + |
| 99 | + lower_perfkeys = ['idle_processes'] |
| 100 | + upper_perfkeys = [ |
| 101 | + 'active_processes', 'max_active_processes', 'max_children_reached', |
| 102 | + 'slow_requests', 'listen_queue', 'max_listen_queue', |
| 103 | + ] |
| 104 | + perfkeys = lower_perfkeys + upper_perfkeys |
| 105 | + |
| 106 | + # Add some more values, derived from the raw ones... |
| 107 | + this_time = int(time.time()) |
| 108 | + for key in ['accepted_conn', 'max_children_reached', 'slow_requests']: |
| 109 | + try: |
| 110 | + per_sec = get_rate( |
| 111 | + get_value_store(), |
| 112 | + "php_fpm_status.%s" % key, |
| 113 | + this_time, |
| 114 | + data[key], |
| 115 | + raise_overflow=True |
| 116 | + ) |
| 117 | + except GetRateError: |
| 118 | + pass |
| 119 | + else: |
| 120 | + data['%s_per_sec' % key] = per_sec |
| 121 | + perfkeys.append('%s_per_sec' % key) |
| 122 | + |
| 123 | + for key in perfkeys: |
| 124 | + if key in lower_perfkeys: |
| 125 | + levels_lower = params.get(key) |
| 126 | + levels_upper = None |
| 127 | + else: |
| 128 | + levels_lower = None |
| 129 | + levels_upper = params.get(key) |
| 130 | + yield from check_levels( |
| 131 | + value=data[key], |
| 132 | + metric_name=key, |
| 133 | + levels_lower=levels_lower, |
| 134 | + levels_upper=levels_upper, |
| 135 | + label=key.replace('_', ' ').title(), |
| 136 | + render_func=( |
| 137 | + render.timespan if key == 'start_since' |
| 138 | + else (lambda x: "%0.2f/s" % x) if key.endswith("_per_sec") |
| 139 | + else (lambda x: "%d" % x) |
| 140 | + ), |
| 141 | + notice_only=key not in ( |
| 142 | + 'active_processes', |
| 143 | + 'idle_processes', |
| 144 | + 'listen_queue', |
| 145 | + 'start_since', |
| 146 | + 'accepted_conn_per_sec', |
| 147 | + ), |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +register.check_plugin( |
| 152 | + name='php_fpm_pools', |
| 153 | + service_name='PHP-FPM Pool %s Status', |
| 154 | + discovery_function=discover_php_fpm_pools, |
| 155 | + check_function=check_php_fpm_pools, |
| 156 | + check_ruleset_name='php_fpm_pools', |
| 157 | + check_default_parameters={}, |
| 158 | +) |
0 commit comments