Last active
May 7, 2018 09:02
-
-
Save sguter90/7c51d93665f4aad408754047bb1b810d to your computer and use it in GitHub Desktop.
ISPConfig Icingia2 HTTP Service Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ISPConfig Icingia2 HTTP Service Plugin for ISPConfig Version 3.1.2 and above. | |
* | |
* This class extends ISPConfig's vhost management with the functionality to manage icingia2 HTTP services. | |
* | |
* This Plugin requires Icinga2 (https://www.icinga.com/products/icinga-2/) | |
* The config files add a variable named "vars.http_vhosts" to an existing icinga2 host. | |
* With the matching icinga2 service configuration a icinga2 service will be added for each entry contained in "vars.http_vhosts". | |
* If "Rewrite to HTTPS" is enabled in the ISPConfig website configuration, a second check for certificates will be added. | |
* | |
* | |
* Setup: | |
* 1) Set icinga2_config_path to your prefered icinga2 config location | |
* | |
* 2) Set icinga2_restart_cmd to the correct value if your system is not using init.d | |
* | |
* 3) Create path set in icinga2_config_path | |
* | |
* 4) Create a host in icinga2 to which the generated website configs should get attached | |
* | |
* 5) Include icinga2_config_path in your host configuration. | |
* Example: In /etc/icinga2/conf.d/hosts/example.com.conf add following line at the end of your existing configuration: | |
* include "vhosts/*.conf" | |
* | |
* This will include all configurations sotred in the folder /etc/icinga2/conf.d/hosts/vhosts/ | |
* | |
* 6) Download "check_ssl_certificate"-Plugin and place it your plugin folder (https://exchange.nagios.org/directory/Plugins/Network-Protocols/HTTP/check_ssl_certificate/details) | |
* | |
* 7) Add the following configuration to your /etc/icinga2/conf.d/commands.conf to register ssl_certificate as CheckCommand in icinga2: | |
* object CheckCommand "ssl_certificate" { | |
* import "plugin-check-command" | |
* command = [PluginDir + "/check_ssl_certificate" ] | |
* arguments = { | |
* "-H" = "$http_address$" | |
* "-w" = "29" | |
* "-c" = "10" | |
* } | |
* } | |
* | |
* 8) Add the following configuration to your /etc/icinga2/conf.d/services.conf to add services: | |
* apply Service "http - " for (http_vhost => config in host.vars.http_vhosts) { | |
* import "generic-service" | |
* | |
* check_command = "http" | |
* | |
* vars += config | |
* } | |
* | |
* apply Service "ssl-cert - " for (http_vhost => config in host.vars.http_vhosts) { | |
* import "generic-service" | |
* | |
* check_command = "ssl_certificate" | |
* check_interval = 24h | |
* retry_interval = 6h | |
* | |
* vars += config | |
* | |
* assign where config.vars.http_ssl == "1" | |
* | |
* } | |
* | |
* 9) Place this file in your icinga2 "plugins-available" directory (/usr/local/ispconfig/server/plugins-available) | |
* | |
* 10) Activate plugin with a symlink: ln -s /usr/local/ispconfig/server/plugins-available/icingia2_http_service_plugin.inc.php /usr/local/ispconfig/server/plugins-enabled/icingia2_http_service_plugin.inc.php | |
* | |
* 11) Ensure File is readable by webserver | |
* | |
* | |
* Usage: | |
* Every time you add or edit a website in ISPConfig, a icinga2 config file for this website will be written. | |
* If you delete a website in ISPConfig, the icinga2 config file for this website will be deleted. | |
* After icinga2 configs are updated, the icinga2 system service will be restarted to reload config. | |
* | |
* | |
* @author Christoph Mueller <christoph@flying-lama.com> | |
*/ | |
class icingia2_http_service_plugin { | |
/** | |
* Stores the internal plugin name. | |
* | |
* @var string | |
*/ | |
private $plugin_name = 'icingia2_http_service_plugin'; | |
/** | |
* Stores the internal class name. | |
* | |
* Needs to be the same as $plugin_name. | |
* | |
* @var string | |
*/ | |
private $class_name = 'icingia2_http_service_plugin'; | |
/** | |
* Stores where icinga2 config files are stored | |
* | |
* Absolute path with trailing slash | |
* | |
* @var string | |
*/ | |
private $icinga2_config_path = '/etc/icinga2/conf.d/hosts/vhosts/'; | |
/** | |
* Stores icinga2 restart command | |
* | |
* For applying config changes | |
* | |
* @var string | |
*/ | |
private $icinga2_restart_cmd = '/etc/init.d/icinga2 restart 1>/dev/null 2>&1'; | |
/** | |
* Stores the current vhost action. | |
* | |
* When ISPConfig triggers the vhost event, it passes either create,update,delete etc. | |
* | |
* @see onLoad() | |
* | |
* @var string | |
*/ | |
private $action = ''; | |
/** | |
* ISPConfig onInstall hook. | |
* | |
* Called during ISPConfig installation to determine if a symlink shall be created. | |
* | |
* @return bool create symlink if true | |
*/ | |
public function onInstall() { | |
global $conf; | |
return $conf['services']['web'] == true; | |
} | |
/** | |
* ISPConfig onLoad hook. | |
* | |
* Register the plugin for some site related events. | |
*/ | |
public function onLoad() { | |
global $app; | |
$app->plugins->registerEvent('web_domain_insert', $this->plugin_name, 'insert'); | |
$app->plugins->registerEvent('web_domain_update', $this->plugin_name, 'update'); | |
$app->plugins->registerEvent('web_domain_delete', $this->plugin_name, 'delete'); | |
} | |
/** | |
* ISPConfig insert hook. | |
* | |
* Called every time a new site is created. | |
* | |
* @uses update() | |
* | |
* @param string $event_name the event/action name | |
* @param array $data the vhost data | |
*/ | |
public function insert($event_name, $data) { | |
global $app, $conf; | |
$this->action = 'insert'; | |
$this->update($event_name, $data); | |
} | |
/** | |
* ISPConfig update hook. | |
* | |
* Called every time a site gets updated from within ISPConfig. | |
* | |
* @see insert() | |
* @see delete() | |
* | |
* @param string $event_name the event/action name | |
* @param array $data the vhost data | |
*/ | |
public function update($event_name, $data) { | |
global $app, $conf; | |
if ($this->action != 'insert') { | |
$this->action = 'update'; | |
} | |
$app->uses('getconf'); | |
$app->uses('system'); | |
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web'); | |
$checkSSL = 0; | |
if($data['new']['rewrite_to_https'] == 'y') { | |
$checkSSL = 1; | |
} | |
$config = ' | |
vars.http_vhosts["'.$data['new']['domain'].'"] = { | |
http_address = "'.$data['new']['domain'].'" | |
vars.http_ssl = "'.$checkSSL.'" | |
}'; | |
file_put_contents($this->icinga2_config_path.'ispconfig_domain_id_'.$data['new']['domain_id'].'.conf', $config); | |
$this->_exec($this->icinga2_restart_cmd); | |
$this->action = ''; | |
} | |
/** | |
* ISPConfig delete hook. | |
* | |
* Called every time, a site get's removed. | |
* | |
* @uses update() | |
* | |
* @param string $event_name the event/action name | |
* @param array $data the vhost data | |
*/ | |
public function delete($event_name, $data) { | |
global $app, $conf; | |
$this->action = 'delete'; | |
$var_str = var_export($data, true); | |
unlink($this->icinga2_config_path.'ispconfig_domain_id_'.$data['old']['domain_id'].'.conf'); | |
$this->_exec($this->icinga2_restart_cmd); | |
} | |
/** | |
* ISPConfig internal debug function. | |
* | |
* Function for easier debugging. | |
* | |
* @param string $command executable command to debug | |
*/ | |
private function _exec($command) { | |
global $app; | |
$app->log('exec: '. $command, LOGLEVEL_DEBUG); | |
exec($command); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment