randomhost / icinga
提供Icinga检查和通知命令。
Requires
- php: >=5.3.0
Requires (Dev)
- behat/behat: ~2.0
- phpspec/phpspec: ~2.4
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-22 02:56:01 UTC
README
randomhost/icinga
本包为Icinga监控系统提供检查和通知命令。
用法
基础类
抽象的Base类实现了以下公共方法,这些方法对检查和通知类都是可用的。
-
Base::getShortOptions()
返回可用的短选项。返回值应作为PHP内置getopt()函数的第一个参数传递,并用于设置检查类接受的命令行参数。 -
Base::getLongOptions()
返回可用的长选项。返回值应作为PHP内置getopt()函数的第二个参数传递,并用于设置检查类接受的命令行参数。Base类预定义了长选项--help,该选项触发内置的帮助方法Base::displayHelp()。 -
Base::getMessage()
返回插件输出。返回值应输出到stdout,并定义传递给Icinga的状态消息。 -
Base::getCode()
返回返回码。返回值应传递给PHP内置的exit()函数,并定义传递给Icinga的状态码。 -
Base::setOptions($options)
此方法接受由PHP内置getopt()函数返回的解析后的命令行参数。
检查和通知类不应直接扩展此类。它们应根据需要相应地扩展相应的基类Check\Base或Notification\Base。
检查插件
使用此包构建的检查插件的基本方法可能如下所示
<?php namespace randomhost\Icinga\Check; require_once '/path/to/vendor/autoload.php'; $check = new ExampleService(); $check->setOptions( getopt( $check->getShortOptions(), $check->getLongOptions() ) ); $check->run(); echo $check->getMessage(); exit($check->getCode());
这将实例化示例服务的检查类并运行为此服务定义的检查。正在检查的内容取决于单个检查实现。
Check\Base类
抽象的Check\Base类为扩展子类提供常用方法。它除了提供公共的Base类方法外,还实现了一个公共方法。
Check\Base::run()
负责验证命令行参数,显示帮助输出并执行主要的检查插件逻辑。
所有检查类都应该扩展此类。
实现检查类
要创建检查类,只需扩展Check\Base类并实现一个受保护的方法check()。
<?php namespace randomhost\Icinga\Check; class ExampleService extends Base implements Check { protected function check() { // main check logic goes here $this->setMessage('Everything is fine'); $this->setCode(self::STATE_OK); } }
如果您的检查需要命令行参数,您可以在检查类的构造函数中定义这些参数。这也是放置在缺少必需参数时显示的帮助输出的正确位置。
<?php namespace randomhost\Icinga\Check; class ExampleService extends Base implements Check { public function __construct() { $this->setLongOptions( array( 'host:', 'port:', 'user:', 'password:', 'warningThreshold:', 'criticalThreshold:' ) ); $this->setRequiredOptions( array( 'host', 'port', 'user', 'password', 'warningThreshold', 'criticalThreshold' ) ); $this->setHelp(' Icinga plugin for checking the example service. --host Example service IP address or hostname --port Example service port --user Example service user --password Example service password --warningThreshold Threshold to trigger the WARNING state --criticalThreshold Threshold to trigger the CRITICAL state '); } protected function check() { $options = $this->getOptions(); // main check logic goes here } }
通知插件
使用此包构建的通知插件的基本方法可能如下所示
<?php namespace randomhost\Icinga\Notification; require_once '/path/to/vendor/autoload.php'; $notification = new ExampleNotification(); $notification->setOptions( getopt( $notification->getShortOptions(), $notification->getLongOptions() ) ); $notification->run(); echo $notification->getMessage(); exit($notification->getCode());
这将实例化示例通知插件的类并运行为此插件定义的逻辑。发送的通知类型取决于单个通知类实现。
Notification\Base类
抽象的Notification\Base类为扩展子类提供常用方法。它除了提供公共的Base类方法外,还实现了一个公共方法。
Notification\Base::run()
负责验证命令行参数,显示帮助输出并执行主要的通知插件逻辑。
所有通知类都应该扩展此类。
实现通知类
要创建通知类,只需扩展Notification\Base类并实现一个受保护的方法send()。
<?php namespace randomhost\Icinga\Notification; class ExampleNotification extends Base implements Notification { protected function send() { // main notification logic goes here $this->setMessage('Notification sent'); $this->setCode(self::STATE_OK); } }
如果你的通知类需要命令行参数,你可以在通知类的构造函数中定义这些参数。这也是放置显示所需参数缺失时的帮助输出的正确位置。
<?php namespace randomhost\Icinga\Notification; class ExampleNotification extends Base implements Notification { public function __construct() { $this->setLongOptions( array( 'type:', 'service:', 'host:', 'address:', 'state:', 'time:', 'output:', 'phonenumber:', ) ); $this->setRequiredOptions( array( 'type', 'service', 'host', 'address', 'state', 'time', 'output', 'phonenumber', ) ); $this->setHelp(' Icinga plugin for sending notifications via the example notification provider. --type Notification type --service Service name --host Host name --address Host address --state Service state --time Notification time --output Check plugin output --phonenumber User phone number '); } protected function send() { $options = $this->getOptions(); // main notification logic goes here } }
许可
请参阅LICENSE.txt以获取完整的许可详情。