liip / monitor-bundle
Liip Monitor Bundle
Requires
- php: ^7.3 || ^8.0
- laminas/laminas-diagnostics: ^1.9
- symfony/framework-bundle: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- doctrine/doctrine-migrations-bundle: ^2.0 || ^3.0 || ^7.0
- doctrine/migrations: ^2.0 || ^3.0
- doctrine/persistence: ^1.3.3 || ^2.0 || ^3.0
- enlightn/security-checker: ^1.11
- friendsofphp/php-cs-fixer: ^3.4
- guzzlehttp/guzzle: ^5.3.2 || ^6.3.3 || ^7.0.1
- matthiasnoback/symfony-dependency-injection-test: ^4.3 || ^5.0
- phpunit/phpunit: ^9.0
- swiftmailer/swiftmailer: ^5.4 || ^6.1 || ^7.0
- symfony/asset: ^5.4 || ^6.0 || ^7.0
- symfony/browser-kit: ^5.4 || ^6.0 || ^7.0
- symfony/expression-language: ^5.4 || ^6.0 || ^7.0
- symfony/finder: ^5.4 || ^6.0 || ^7.0
- symfony/mailer: ^5.4 || ^6.0 || ^7.0
- symfony/messenger: ^5.4 ||^6.0 || ^7.0
- symfony/templating: ^5.4 || ^6.0 || ^7.0
- symfony/twig-bundle: ^5.4 || ^6.0 || ^7.0
Suggests
- symfony/expression-language: To use the Expression check
- 3.x-dev
- dev-master / 2.x-dev
- 2.23.0
- 2.22.0
- 2.21.0
- 2.20.0
- 2.19.0
- 2.18.0
- v2.17.1
- 2.17.0
- 2.16.1
- 2.16.0
- 2.15.0
- 2.14.1
- 2.14.0
- 2.13.0
- 2.12.3
- 2.12.2
- 2.12.1
- 2.12.0
- 2.11.2
- 2.11.1
- 2.11.0
- 2.10.0
- 2.9.1
- 2.9.0
- 2.8.0
- 2.7.8
- 2.7.7
- 2.7.6
- 2.7.5
- 2.7.4
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.x-dev
- 2.6.7
- 2.6.6
- 2.6.5
- 2.6.4
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-RC1
- 1.0.x-dev
- 1.0.1
- 1.0.0
- 0.4.0
- dev-2_7_fixes
This package is auto-updated.
Last update: 2024-08-24 14:29:04 UTC
README
此包提供了一种运行一系列与应用程序相关的健康检查的方法。此包范围内的健康检查不仅限于执行简单的像对服务器进行 ping 操作以查看其是否存活等操作。例如,一个Memcache服务器可能活着并且没有在Nagios中显示任何错误,但你可能无法从你的PHP应用程序中访问它。每个健康检查都应该实现一些应用程序逻辑,以确保它们始终正常工作。另一个用途可以是测试特定的需求,例如PHP扩展的可用性。
此包的另一个设计目标是能够使用与您的应用程序相同的配置和环境来执行检查。这样,您可以确保如果健康检查运行成功,则您的应用程序也应该可以正常工作。
因此,每个健康检查都将是一个实现 CheckInterface::check
方法的类,该方法必须返回一个 CheckResult
对象。该方法内部发生的事情由检查开发者决定。
健康检查定义为Symfony服务,并且必须使用 liip_monitor.check
标签标记,以便由 健康检查运行器 识别。这为应用程序和包开发者提供了很大的灵活性,当他们想添加自己的检查时。
检查可以通过命令行使用Symfony命令或通过提供JSON格式结果的REST API运行。
以下是网络界面
安装
使用composer安装
composer require liip/monitor-bundle
然后,在AppKernel.php
文件中注册该包
public function registerBundles() { $bundles = array( // ... new Liip\MonitorBundle\LiipMonitorBundle(), // ... ); return $bundles; }
如果您想启用由包提供的REST API,请在routing.yml
中添加以下内容
_monitor: resource: "@LiipMonitorBundle/Resources/config/routing.xml" prefix: /monitor/health
然后,在配置中启用控制器
liip_monitor: enable_controller: true
最后,不要忘记将包资源安装到您的Web根目录中
./app/console assets:install web --symlink --relative
启用内置健康检查
要启用内置健康检查,请将它们添加到您的config.yml
中
liip_monitor: checks: php_extensions: [apc, xdebug]
添加健康检查
有关创建自定义检查的说明,请参阅编写自定义检查
实现类之后,就是时候将检查服务注册到我们的服务容器中
services: monitor.check.php_extensions: class: Acme\HelloBundle\Check\PhpExtensionsCheck arguments: - [ xhprof, apc, memcache ] tags: - { name: liip_monitor.check, alias: php_extensions }
其中重要的是要记住用liip_monitor.check
标签标记您的服务。这样做后,检查运行器将能够找到您的检查。请注意,检查可以位于您的包中或您的应用程序特定代码中。位置不重要,只要服务正确标记即可。别名是可选的,然后将定义在运行健康检查时使用的id
,否则在这种情况下必须使用完整的服务id。
如果您的应用程序的服务定义使用autoconfigure
来发现服务,则实现Laminas\Diagnostics\Check\CheckInterface
的类将被自动标记。
可用的内置健康检查
有关所有内置检查及其配置的列表,请参阅下面的“完整默认配置”。
运行检查
运行健康检查有两种方式:通过使用CLI或通过使用由包提供的REST API。让我们看看CLI可用的命令有哪些
列出检查
$ ./app/console monitor:list
monitor.check.jackrabbit
monitor.check.redis
monitor.check.memcache
monitor.check.php_extensions
运行所有检查
$ ./app/console monitor:health
Jackrabbit Health Check: OK
Redis Health Check: OK
Memcache Health Check: KO - No configuration set for session.save_path
PHP Extensions Health Check: OK
运行单个检查
要运行单个检查,您需要向health
命令提供检查id
$ ./app/console monitor:health monitor.check.php_extensions
PHP Extensions Health Check: OK
将健康检查作为composer post-install/update脚本运行
要将健康检查作为composer的安装后或更新后脚本运行,只需将Liip\\MonitorBundle\\Composer\\ScriptHandler::checkHealth
ScriptHandler添加到composer.json
中的post-install-cmd / post-update-cmd
命令部分。
"scripts": { "post-install-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile", "Liip\\MonitorBundle\\Composer\\ScriptHandler::checkHealth" ], "post-update-cmd": [ "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets", "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile", "Liip\\MonitorBundle\\Composer\\ScriptHandler::checkHealth" ] },
添加额外的报告器
默认有两个报告器:用于REST API的ArrayReporter
和用于CLI命令的ConsoleReporter
。您可以添加额外的报告器供这两个使用。
首先,定义一个额外的报告器服务并使用liip_monitor.additional_reporter
标记它
my_reporter: class: My\Reporter tags: - { name: liip_monitor.additional_reporter, alias: my_reporter }
要使用CLI运行额外的报告器,为每个报告器添加--reporter=...
选项
./app/console monitor:health --reporter=my_reporter
要使用REST API运行此报告器,添加一个reporters
查询参数
/monitor/health?reporters[]=my_reporter
您可以使用以下命令列出可用的报告器
bin/console monitor:list --reporters
分组检查
可以将不同环境(例如,应用服务器、cron运行程序等)的健康检查分组。如果未指定不同的分组,所有健康检查都属于default
组。
为内置检查定义组
要为内置健康检查定义组,请将以下分组提示添加到您的config.yml
liip_monitor: default_group: default checks: groups: default: # checks you may want to execute by default php_extensions: [apc, xdebug] cron: # checks you may want to execute only on cron servers php_extensions: [redis]
这创建了两个组,default
和cron
,每个组都有自己的检查。
为标记的服务定义组
要为标记的服务定义组,请将group
属性添加到相应的标记中
services: monitor.check.php_extensions: class: Acme\HelloBundle\Check\PhpExtensionsCheck arguments: - [ xhprof, apc, memcache ] tags: - { name: liip_monitor.check, alias: php_extensions, group: cron } - { name: liip_monitor.check, alias: php_extensions, group: app_server }
autoconfigure
将检查放入默认组。您必须将autoconfigure: false
添加到服务定义中,以更改组
services: Acme\HelloBundle\Check\PhpExtensionsCheck: autoconfigure: false tags: - { name: liip_monitor.check, group: app_server }
指定CLI命令的组
两个CLI命令都有一个--group=...
选项。如果没有给出,则使用默认组。
bin/console monitor:list --group=app_server bin/console monitor:health --group=app_server
两个命令monitor:list
和monitor:health
都有一个--all
选项,用于列出或运行所有注册组的检查。此外,monitor:list
还有一个--groups
选项,用于列出所有注册的组。
完整默认配置
liip_monitor: enable_controller: false view_template: null failure_status_code: 502 mailer: enabled: false recipient: ~ # Required sender: ~ # Required subject: ~ # Required send_on_warning: true default_group: default checks: # Grouping checks groups: # Prototype name: # Validate that a named extension or a collection of extensions is available php_extensions: [] # Example: apc, xdebug # Pairs of a PHP setting and an expected value php_flags: # Example: session.use_only_cookies: false # Prototype setting: ~ # Pairs of a version and a comparison operator php_version: # Example: 5.4.15: >= # Prototype version: ~ # Process name/pid or an array of process names/pids process_running: ~ # Example: [apache, foo] # Validate that a given path (or a collection of paths) is a dir and is readable readable_directory: [] # Example: ["%kernel.cache_dir%"] # Validate that a given path (or a collection of paths) is a dir and is writable writable_directory: [] # Example: ["%kernel.cache_dir%"] # Validate that a class or a collection of classes is available class_exists: [] # Example: ["Lua", "My\Fancy\Class"] # Benchmark CPU performance and return failure if it is below the given ratio cpu_performance: ~ # Example: 1.0 # This is the power of an EC2 micro instance # Checks to see if the disk usage is below warning/critical percent thresholds disk_usage: warning: 70 critical: 90 path: '%kernel.cache_dir%' # Checks Symfony2 requirements file symfony_requirements: file: '%kernel.root_dir%/SymfonyRequirements.php' # Checks to see if the OpCache memory usage is below warning/critical thresholds opcache_memory: warning: 70 critical: 90 # Checks to see if the APC memory usage is below warning/critical thresholds apc_memory: warning: 70 critical: 90 # Checks to see if the APC fragmentation is below warning/critical thresholds apc_fragmentation: warning: 70 critical: 90 # Connection name or an array of connection names doctrine_dbal: null # Example: [default, crm] # Checks to see if migrations from specified configuration file are applied doctrine_migrations: # Examples: application_migrations: configuration_file: %kernel.root_dir%/Resources/config/migrations.yml connection: default migrations_with_doctrine_bundle: connection: default migrations_with_doctrine_bundle_v2: default # Prototype name: # Absolute path to doctrine migrations configuration configuration_file: ~ # Connection name from doctrine DBAL configuration connection: ~ # Required # Connection name or an array of connection names doctrine_mongodb: null # Example: [default, crm] # Check if MemCache extension is loaded and given server is reachable memcache: # Prototype name: host: localhost port: 11211 # Validate that a Redis service is running redis: # Prototype name: host: localhost port: 6379 password: null # or dsn: redis://#:6379 # Attempt connection to given HTTP host and (optionally) check status code and page content http_service: # Prototype name: host: localhost port: 80 path: / status_code: 200 content: null # Attempt connection using Guzzle to given HTTP host and (optionally) check status code and page content guzzle_http_service: # Prototype name: url: localhost headers: [] options: [] status_code: 200 content: null method: GET body: null # Validate that a RabbitMQ service is running rabbit_mq: # Prototype name: host: localhost port: 5672 user: guest password: guest vhost: / # or dsn: amqp://guest:guest@localhost:5672/%2F # Checks the version of this app against the latest stable release symfony_version: ~ # Checks if error pages have been customized for given error codes custom_error_pages: # The status codes that should be customized error_codes: [] # Required # The directory where your custom error page twig templates are located. Keep as "%kernel.project_dir%" to use default location. path: '%kernel.project_dir%' # Checks installed composer dependencies against the SensioLabs Security Advisory database security_advisory: lock_file: '%kernel.root_dir%/../composer.lock' # Validate that a stream wrapper or collection of stream wrappers exists stream_wrapper_exists: [] # Example: ['zlib', 'bzip2', 'zip'] # Find and validate INI files file_ini: [] # Example: ['path/to/my.ini'] # Find and validate JSON files file_json: [] # Example: ['path/to/my.json'] # Find and validate XML files file_xml: [] # Example: ['path/to/my.xml'] # Find and validate YAML files file_yaml: [] # Example: ['path/to/my.yml'] # PDO connections to check for connection pdo_connections: # Prototype name: dsn: null username: null password: null timeout: 1 # Checks that fail/warn when given expression is false (expressions are evaluated with symfony/expression-language) expressions: # Example: opcache: label: OPcache warning_expression: ini('opcache.revalidate_freq') > 0 critical_expression: ini('opcache.enable') warning_message: OPcache not optimized for production critical_message: OPcache not enabled # Prototype alias: label: ~ # Required warning_expression: null # Example: ini('apc.stat') == 0 critical_expression: null # Example: ini('short_open_tag') == 1 warning_message: null critical_message: null # Validate that a messenger transport does not contain more than warning/critical messages # Transport must implement MessageCountAwareInterface messenger_transports: name: # name of transport critical_threshold: 10 # required warning_threshold: null # optional: warning level service: null # defaults to messenger.transport.name
REST API文档
有关REST API的文档,请参阅:[http://myproject.org/monitor/health/](http://myproject.org/monitor/health/)。不要忘记在您的routing.xml
文件中添加bundle路由。
Nagios集成
您可以在Resources/scripts目录中找到用Perl和Python编写的简单Nagios检查。
Perl版本
这取决于CPAN上的可用的perl模块,包括Getopt::Std、WWW::Mechanize和JSON
将脚本复制到Nagios的脚本目录中,并创建一个如下所示的命令
define command{
command_name check_symfony_health
command_line $USER1$/check_symfony2.pl -H $HOSTNAME$
}
使用主机名标志(-H)运行命令将检查"http://$HOSTNAME$/monitor/health/run"。您还可以使用地址标志(-A)检查指定的URL
command_line $USER1$/check_symfony2.pl -A https://mysite.org/monitor/health/run
插件可以使用身份验证使用,使用用户名(-u)和密码(-p)标志
command_line $USER1$/check_symfony2.p1 -H $HOSTNAME$ -u username -p password
您还可以使用标准标志指定检查的警告(-w)和关键(-c)级别
command_line $USER1$/check_symfony2.pl -H $HOSTNAME$ -w 1 -c 2
除了-A和-H之外,可以组合任何标志。-u和-p标志应始终一起使用。
Python版本
Python版本取决于nagiosplugin库< 1.0.0。
将脚本复制到Nagios的脚本目录中,并创建一个如下所示的命令
define command{
command_name check_symfony_health
command_line $USER1$/check_symfony2.py -w 0 -c 0 -u https://$HOSTNAME$
}
要使用带有HTTP基本身份验证的插件,将命令更改为
command_line $USER1$/check_symfony2.py -w 0 -c 0 -u https://$HOSTNAME$ -a username:password
将检查连接到Nagios中的主机
添加一个服务
define service{
hostgroup_name Symfony2
service_description Symfony2 health check
check_command check_symfony_health
use generic-service
}
并创建一个连接到Symfony2主机组的宿主
define host{
use web-host
host_name www.myhost.com
address 8.8.8.4
hostgroups Symfony2
}
并将您的宿主放入Symfony2主机组中。