cytopia / check_php
Nagios 或 Icinga 插件,用于检查 PHP 启动错误。
0.8
2018-11-04 08:07 UTC
README
check_php 是一个符合 POSIX 规范的 Nagios 插件,它可以检查 PHP 启动错误(-s
)、缺少的 PHP 模块(-m
)、php.ini 中配置错误的指令(-c
)以及可用的 PHP 更新(-u
)。此插件支持性能数据(错误和警告计数随时间的变化)和长输出(所有问题的详细情况)。
Nagios 配置 | Icinga2 配置 | 使用方法 | 示例 | 许可证 | 贡献者 | 惊人之处
需求
特性
- 检查 PHP 启动错误
- 检查 PHP 更新(包括次要版本和补丁级别)
- 检查缺少的 PHP 模块
- 检查黑名单中的 PHP 模块,如果它们被编译进去,则抛出错误或警告
- 检查预期的 php.ini 配置指令(例如:date.timezone 必须是 "Europe/Berlin",等等)
- 每个检查都可以指定其自己的严重性(警告或错误)
动机
如果你需要管理许多已安装 PHP 的服务器,你可以使用此插件来确保所有服务器或服务器组使用相同的配置、相同的编译模块,并且始终保持最新状态。
1. Nagios 配置
1.1 命令定义
为了在远程服务器上检查 PHP,您需要使用 check_by_ssh
。
name: check_by_ssh_php command: $USER1$/check_by_ssh -H $HOSTADDRESS$ -t 60 -l "$USER17$" -C "$USER22$/check_php $ARG1$"
1.2 服务定义
在上面的命令定义中,只有一个参数变量分配给了 check_php
:$ARG1
。因此,您可以在服务定义中将所有必需的参数分配给这个单一变量。
check command: check_by_ssh_php
$ARG1$: -s e -u w -m curl e -m gettext e -m openssl e -m json e
2. Icinga2 配置
2.1 命令定义
/* * Check PHP health */ object CheckCommand "php" { import "plugin-check-command" command = [ PluginDir + "/check_php" ] arguments = { "-s" = { value = "$php_startup$" description = "Check for PHP startup errors and display warning or error if any exists. Allowed values are 'w' for warnings and 'e' for critical errors." } "-u" = { value = "$php_updates$" description = "Check for updated PHP version online. Allowed values are 'w' for warnings and 'e' for critical errors. This check requires wget, curl or fetch." } "-p" = { value = "$php_binary$" description = "Optional path to PHP binary. This argument allows to define a certain PHP binary to be checked. If none is defined, the default PHP version will be used." } "-d" = { value = "$php_delimiter$" description = "Delimiter for multi-value arguments." } "-m" = { value = "$php_modules$" description = "Check for required modules." repeat_key = true } "-b" = { value = "$php_blacklist$" description = "Check for blacklisted modules." repeat_key = true } "-c" = { value = "$php_config$" description = "Check PHP setting directives that diverge from the given default value." repeat_key = true } "-v" = { set_if = "$php_verbose$" description = "Enable verbose mode." } } }
2.2 服务定义示例(使用 apply)
/* * PHP Health */ apply Service "php-" for (php => config in host.vars.php) { check_command = "php" // Assuming your PHP setup doesn't change too often, we don't // bother to check twice a day only. check_interval = 12h display_name = "PHP " + php notes = "Checks currently installed PHP " + php + " health." // Service variables from php definition. vars += config vars.php_delimiter = "|" if ( config.php_modules ) { vars.php_modules = [] for (key => value in config.php_modules) { vars.php_modules += [ key + vars.php_delimiter + value ] } } if ( config.php_blacklist ) { vars.php_blacklist = [] for (key => value in config.php_blacklist) { vars.php_blacklist += [ key + vars.php_delimiter + value ] } } if ( config.php_config ) { vars.php_config = [] for (key => value in config.php_config) { vars.php_config += [ key + vars.php_delimiter + value["default"] + vars.php_delimiter + value["severity"] ] } } // Application rules. assign where host.name = NodeName && host.vars.php }
2.3 主机对象定义
object Host "node.example.com" { // Other settings. vars.php[ "7.1" ] = { php_binary = "/opt/php/7.1/bin/php" php_updates = "w" php_startup = "e" php_modules = { intl = "w" mbstring = "w" soap = "w" apcu = "w" memcached = "w" geoip = "w" mongodb = "w" imagick = "w" redis = "w" openssl = "w" xml = "w" json = "w" curl = "w" } php_blacklist = { mcrypt = "w" } php_config = { "date.timezone" = { "default" = "Europe/Berlin" "severity" = "w" } } } }
3. 使用方法
每个参数都允许您指定应触发哪种严重性(<w|e>
),其中 w
触发警告,e
触发错误。可以多次使用的参数(-m
和 -c
)当然可以使用不同的严重性。所有严重性都将汇总,最高严重性(错误 > 警告)将决定最终状态。
Usage: check_php [-s <w|e>] [-u <w|e>] [-m <module> <w|e>] [-b <module> <w|e> [-c <conf> <val> <w|e>] [-p <path>] [-d <delimiter>] [-v] check_php -h check_php -V Nagios plugin that will check if PHP exists, for PHP startup errors, missing modules, misconfigured directives and available updates. -s <w|e> [single] Check for PHP startup errors and display nagios warning or error if any exists. Warning: -s w Error: -s e -u <w|e> [single] Check for updated PHP version online. (requires wget, curl or fetch) Will only check for patch updates and will not notify if your current version PHP 5.5 and there is already PHP 5.6 out there. -m <module> <w|e> [multiple] Require compiled PHP module and display nagios warning/error if the module was not compiled against PHP. Use multiple times to check against multiple modules. Example: -m "mysql" w -m "mysqli" e -b <module> <w|e> [multiple] Check PHP for modules that should not be compiled in and display nagios warning/error if the module is compiled against PHP. Use multiple times to check for multiple blacklisted modules. Example: -b "imagick" w -b "tidy" e -c <conf> <val> <w|e> [multiple] Check for misconfigured directives in php.ini and display nagios warning/error if the configuration does not match. Use multiple times to check against multiple configurations. Example: -c "date.timezone" "Europe/Berlin" e -p <path> [optional] Define the path to the PHP binary that shall be used. If no value is given, the current user's default PHP version will be checked. Example: -p "/usr/bin/php" -d <delimiter> [optional] Delimiter used to concatenate arguments of the above options that require multiple values. Example: -d "|" -m "mysql|w" -b "mcrypt|w" -c "date.timezone|Europe/Berlin|e" -v Be verbose (Show PHP Version and Zend Engine Version) -h Display help -V Display version
4. 示例
针对首选时区和编译模块 mysql
进行检查
$ check_php -c "date.timezone" "Europe/Berlin" e -m mysql e [ERR] PHP 5.6.16 has errors: Missing module(s) | 'OK'=0;;;; 'Errors'=1;;;; 'Warnings'=0;;;; 'Unknown'=0;;;; [CRITICAL] Module: "mysql" not available [OK] Config "date.timezone" = "Europe/Berlin"
检查 PHP 启动错误
$ check_php -s w [WARN] PHP 5.6.16 has warning: Startup errors | 'OK'=0;;;; 'Errors'=0;;;; 'Warnings'=1;;;; 'Unknown'=0;;;; [WARNING] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/Cellar/php56/5.6.14/lib/php/extensions/no-debug-non-zts-20131226/test' - dlopen(/usr/local/Cellar/php56/5.6.14/lib/php/extensions/no-debug-non-zts-20131226/test, 9): image not found in Unknown on line 0
组合多个模块检查
$ check_php -m mysql e -m mysqli w -m mbstring w [OK] PHP 5.6.16 is healthy | 'OK'=1;;;; 'Errors'=0;;;; 'Warnings'=0;;;; 'Unknown'=0;;;; [OK] Module: "mysql" available [OK] Module: "mysqli" available [OK] Module: "mbstring" available
检查 PHP 更新(正常)
$ check_php -u e [OK] PHP 5.6.16 is healthy | 'OK'=1;;;; 'Errors'=0;;;; 'Warnings'=0;;;; 'Unknown'=0;;;; [OK] No PHP startup errors [OK] PHP Version 5.6.14 up to date.
检查 PHP 更新(有可用更新)
$ check_php -u e [ERR] PHP 5.6.13 has errors: Updates available | 'OK'=0;;;; 'Errors'=1;;;; 'Warnings'=-;;;; 'Unknown'=0;;;; [OK] No PHP startup errors [CRITICAL] PHP Version 5.6.13 too old. Latest: 5.6.14.
检查 PHP 更新(能够区分 PHP 5.4、5.5 和 5.6)
$ check_php -u e [ERR] PHP 5.5.1 has errors: Updates available | 'OK'=0;;;; 'Errors'=1;;;; 'Warnings'=0;;;; 'Unknown'=0;;;; [OK] No PHP startup errors [CRITICAL] PHP Version 5.5.1 too old. Latest: 5.5.30.
组合了大量的选项
$ check_php -s w -m mysql e -m mbstring e -m xml e -c date.timezone 'Europe/Berlin' e -c session.cookie_secure "On" e -u e -v [ERR] PHP 5.6.14 has errors: Wrong config | 'OK'=0;;;; 'Errors'=1;;;; 'Warnings'=0;;;; 'Unknown'=0;;;; [OK] No PHP startup errors [OK] PHP Version 5.6.14 up to date. [OK] Module: "mysql" available [OK] Module: "mbstring" available [OK] Module: "xml" available [OK] Config "date.timezone" = "Europe/Berlin" [CRITICAL] Config "session.cookie_secure" = "Off", excpected: "On" PHP 5.6.14 Zend Engine v2.6.0