From 807f07eb467d98eaaf1ef28d6afe85f2e60b017e Mon Sep 17 00:00:00 2001 From: easylo Date: Wed, 7 Sep 2016 08:22:49 +0200 Subject: [PATCH] enable statuscake --- composer.json | 3 +- composer.lock | 34 +- src/includes/functions.inc.php | 2 +- .../Config/Controller/ConfigController.php | 1 + .../Controller/AbstractServerController.php | 2 + .../Server/Controller/ServerController.php | 6 +- src/psm/Util/Install/Installer.php | 373 +++++++++--------- .../Util/Server/Updater/StatusNotifier.php | 254 ++++++------ 8 files changed, 361 insertions(+), 314 deletions(-) diff --git a/composer.json b/composer.json index a1e5c180..2a813320 100755 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "php-pushover/php-pushover": "dev-master", "twig/twig": "1.*", "paragonie/random_compat" : "1.1.6", - "indigophp/hash-compat" : "1.1.0" + "indigophp/hash-compat" : "1.1.0", + "php-statuscake/php-statuscake": "0.2" }, "autoload": { "files": [ diff --git a/composer.lock b/composer.lock index 1126411a..74a51ca3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "8f27400edd82e99aa35998a3e01fc23e", - "content-hash": "3d1c36ee7e11634bc149bfc9a250e4ae", + "hash": "ee6fcd5efd7619a347226c9ea90caf32", + "content-hash": "96a691d2bd6f2b93d99df6599cd6c24e", "packages": [ { "name": "indigophp/hash-compat", @@ -174,6 +174,36 @@ }, "time": "2014-07-30 13:55:53" }, + { + "name": "php-statuscake/php-statuscake", + "version": "0.2", + "source": { + "type": "git", + "url": "https://github.com/easylo/php-statuscake.git", + "reference": "567e734404b77178527c510623431df7753bdb5e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/easylo/php-statuscake/zipball/567e734404b77178527c510623431df7753bdb5e", + "reference": "567e734404b77178527c510623431df7753bdb5e", + "shasum": "" + }, + "type": "library", + "autoload": { + "files": [ + "Statuscake.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "authors": [ + { + "name": "Laurent RICHARD", + "email": "easylo@gmail.com" + } + ], + "description": "PHP class for the statuscake.com project", + "time": "2016-09-06 22:55:34" + }, { "name": "phpmailer/phpmailer", "version": "v5.2.6", diff --git a/src/includes/functions.inc.php b/src/includes/functions.inc.php index 2805281b..c9b8fe5b 100644 --- a/src/includes/functions.inc.php +++ b/src/includes/functions.inc.php @@ -482,7 +482,7 @@ function psm_build_mail($from_name = null, $from_email = null) { /** * Prepare a new StatusCake util. * - * @return \Pushover + * @return \Statuscake */ function psm_build_statuscake() { $statuscake = new \StatusCake(); diff --git a/src/psm/Module/Config/Controller/ConfigController.php b/src/psm/Module/Config/Controller/ConfigController.php index 1f0c7796..9a3a763d 100644 --- a/src/psm/Module/Config/Controller/ConfigController.php +++ b/src/psm/Module/Config/Controller/ConfigController.php @@ -66,6 +66,7 @@ class ConfigController extends AbstractController { 'sms_gateway_password', 'sms_from', 'pushover_api_token', + 'statuscake_account_token', ); private $default_tab = 'general'; diff --git a/src/psm/Module/Server/Controller/AbstractServerController.php b/src/psm/Module/Server/Controller/AbstractServerController.php index 8f01f61d..c4c1054e 100644 --- a/src/psm/Module/Server/Controller/AbstractServerController.php +++ b/src/psm/Module/Server/Controller/AbstractServerController.php @@ -75,6 +75,8 @@ abstract class AbstractServerController extends AbstractController { `s`.`email`, `s`.`sms`, `s`.`pushover`, + `s`.`statuscake`, + `s`.`statuscake_test_id`, `s`.`warning_threshold`, `s`.`warning_threshold_counter`, `s`.`timeout`, diff --git a/src/psm/Module/Server/Controller/ServerController.php b/src/psm/Module/Server/Controller/ServerController.php index bb9bdb38..b36baae0 100644 --- a/src/psm/Module/Server/Controller/ServerController.php +++ b/src/psm/Module/Server/Controller/ServerController.php @@ -208,10 +208,12 @@ class ServerController extends AbstractServerController { 'edit_email_selected_' . $edit_server['email'] => 'selected="selected"', 'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"', 'edit_pushover_selected_' . $edit_server['pushover'] => 'selected="selected"', + 'edit_statuscake_selected_' . $edit_server['statuscake'] => 'selected="selected"', + 'edit_value_statuscake_test_id' => $edit_server['statuscake_test_id'], )); } - $notifications = array('email', 'sms', 'pushover'); + $notifications = array('email', 'sms', 'pushover','statuscake'); foreach($notifications as $notification) { if(psm_get_conf($notification . '_status') == 0) { $tpl_data['warning_' . $notification] = true; @@ -272,6 +274,8 @@ class ServerController extends AbstractServerController { 'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no', 'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no', 'pushover' => in_array($_POST['pushover'], array('yes', 'no')) ? $_POST['pushover'] : 'no', + 'statuscake' => in_array($_POST['statuscake'], array('yes', 'no')) ? $_POST['statuscake'] : 'no', + 'statuscake_test_id' => trim(strip_tags(psm_POST('statuscake_test_id', ''))), ); // make sure websites start with http:// if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') { diff --git a/src/psm/Util/Install/Installer.php b/src/psm/Util/Install/Installer.php index ad7d107e..e659128b 100644 --- a/src/psm/Util/Install/Installer.php +++ b/src/psm/Util/Install/Installer.php @@ -129,41 +129,42 @@ class Installer { $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "servers` (`ip`, `port`, `label`, `type`, `pattern`, `status`, `error`, `rtime`, `last_online`, `last_check`, `active`, `email`, `sms`, `pushover`) VALUES ('http://sourceforge.net/index.php', 80, 'SourceForge', 'website', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes'), ('smtp.gmail.com', 465, 'Gmail SMTP', 'service', '', 'on', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 'yes', 'yes', 'yes', 'yes')"; $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "users_servers` (`user_id`,`server_id`) VALUES (1, 1), (1, 2);"; $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUE - ('language', 'en_US'), - ('proxy', '0'), - ('proxy_url', ''), - ('proxy_user', ''), - ('proxy_password', ''), - ('email_status', '1'), - ('email_from_email', 'monitor@example.org'), - ('email_from_name', 'Server Monitor'), - ('email_smtp', ''), - ('email_smtp_host', ''), - ('email_smtp_port', ''), - ('email_smtp_security', ''), - ('email_smtp_username', ''), - ('email_smtp_password', ''), - ('sms_status', '0'), - ('sms_gateway', 'mollie'), - ('sms_gateway_username', 'username'), - ('sms_gateway_password', 'password'), - ('sms_from', '1234567890'), - ('pushover_status', '0'), - ('pushover_api_token', ''), - ('password_encrypt_key', '" . sha1(microtime()) . "'), - ('alert_type', 'status'), - ('log_status', '1'), - ('log_email', '1'), - ('log_sms', '1'), - ('log_pushover', '1'), - ('log_retention_period', '365'), - ('version', '" . PSM_VERSION . "'), - ('version_update_check', '" . PSM_VERSION . "'), - ('auto_refresh_servers', '0'), - ('show_update', '1'), - ('last_update_check', '0'), - ('cron_running', '0'), - ('cron_running_time', '0');"; + ('language', 'en_US'), + ('proxy', '0'), + ('proxy_url', ''), + ('proxy_user', ''), + ('proxy_password', ''), + ('email_status', '1'), + ('email_from_email', 'monitor@example.org'), + ('email_from_name', 'Server Monitor'), + ('email_smtp', ''), + ('email_smtp_host', ''), + ('email_smtp_port', ''), + ('email_smtp_security', ''), + ('email_smtp_username', ''), + ('email_smtp_password', ''), + ('sms_status', '0'), + ('sms_gateway', 'mollie'), + ('sms_gateway_username', 'username'), + ('sms_gateway_password', 'password'), + ('sms_from', '1234567890'), + ('pushover_status', '0'), + ('pushover_api_token', ''), + ('statuscake_account_token', ''), + ('password_encrypt_key', '" . sha1(microtime()) . "'), + ('alert_type', 'status'), + ('log_status', '1'), + ('log_email', '1'), + ('log_sms', '1'), + ('log_pushover', '1'), + ('log_retention_period', '365'), + ('version', '" . PSM_VERSION . "'), + ('version_update_check', '" . PSM_VERSION . "'), + ('auto_refresh_servers', '0'), + ('show_update', '1'), + ('last_update_check', '0'), + ('cron_running', '0'), + ('cron_running_time', '0');"; $this->execSQL($queries); } @@ -173,109 +174,111 @@ class Installer { protected function installTables() { $tables = array( PSM_DB_PREFIX . 'config' => "CREATE TABLE `" . PSM_DB_PREFIX . "config` ( - `key` varchar(255) NOT NULL, - `value` varchar(255) NOT NULL, - PRIMARY KEY (`key`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `key` varchar(255) NOT NULL, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`key`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'users' => "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "users` ( - `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `user_name` varchar(64) NOT NULL COMMENT 'user''s name, unique', - `password` varchar(255) NOT NULL COMMENT 'user''s password in salted and hashed format', - `password_reset_hash` char(40) DEFAULT NULL COMMENT 'user''s password reset code', - `password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request', - `rememberme_token` varchar(64) DEFAULT NULL COMMENT 'user''s remember-me cookie token', - `level` tinyint(2) unsigned NOT NULL DEFAULT '20', - `name` varchar(255) NOT NULL, - `mobile` varchar(15) NOT NULL, - `pushover_key` varchar(255) NOT NULL, - `pushover_device` varchar(255) NOT NULL, - `email` varchar(255) NOT NULL, - PRIMARY KEY (`user_id`), - UNIQUE KEY `unique_username` (`user_name`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `user_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `user_name` varchar(64) NOT NULL COMMENT 'user''s name, unique', + `password` varchar(255) NOT NULL COMMENT 'user''s password in salted and hashed format', + `password_reset_hash` char(40) DEFAULT NULL COMMENT 'user''s password reset code', + `password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request', + `rememberme_token` varchar(64) DEFAULT NULL COMMENT 'user''s remember-me cookie token', + `level` tinyint(2) unsigned NOT NULL DEFAULT '20', + `name` varchar(255) NOT NULL, + `mobile` varchar(15) NOT NULL, + `pushover_key` varchar(255) NOT NULL, + `pushover_device` varchar(255) NOT NULL, + `email` varchar(255) NOT NULL, + PRIMARY KEY (`user_id`), + UNIQUE KEY `unique_username` (`user_name`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'users_preferences' => "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "users_preferences` ( - `user_id` int(11) unsigned NOT NULL, - `key` varchar(255) NOT NULL, - `value` varchar(255) NOT NULL, - PRIMARY KEY (`user_id`, `key`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `user_id` int(11) unsigned NOT NULL, + `key` varchar(255) NOT NULL, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`user_id`, `key`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'users_servers' => "CREATE TABLE `" . PSM_DB_PREFIX . "users_servers` ( - `user_id` INT( 11 ) UNSIGNED NOT NULL , - `server_id` INT( 11 ) UNSIGNED NOT NULL , - PRIMARY KEY ( `user_id` , `server_id` ) - ) ENGINE = MYISAM ;", + `user_id` INT( 11 ) UNSIGNED NOT NULL , + `server_id` INT( 11 ) UNSIGNED NOT NULL , + PRIMARY KEY ( `user_id` , `server_id` ) + ) ENGINE = MYISAM ;", PSM_DB_PREFIX . 'log' => "CREATE TABLE `" . PSM_DB_PREFIX . "log` ( - `log_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `server_id` int(11) unsigned NOT NULL, - `type` enum('status','email','sms','pushover') NOT NULL, - `message` varchar(255) NOT NULL, - `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP, - PRIMARY KEY (`log_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", - PSM_DB_PREFIX . 'log_users' => "CREATE TABLE `" . PSM_DB_PREFIX . "log_users` ( - `log_id` int(11) UNSIGNED NOT NULL , - `user_id` int(11) UNSIGNED NOT NULL , - PRIMARY KEY (`log_id`, `user_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `log_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `server_id` int(11) unsigned NOT NULL, + `type` enum('status','email','sms','pushover') NOT NULL, + `message` varchar(255) NOT NULL, + `datetime` timestamp NOT NULL default CURRENT_TIMESTAMP, + PRIMARY KEY (`log_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + PSM_DB_PREFIX . 'log_users' => "CREATE TABLE `" . PSM_DB_PREFIX . "log_users` ( + `log_id` int(11) UNSIGNED NOT NULL , + `user_id` int(11) UNSIGNED NOT NULL , + PRIMARY KEY (`log_id`, `user_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'servers' => "CREATE TABLE `" . PSM_DB_PREFIX . "servers` ( - `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `ip` varchar(500) NOT NULL, - `port` int(5) unsigned NOT NULL, - `label` varchar(255) NOT NULL, - `type` enum('ping','service','website') NOT NULL default 'service', - `pattern` varchar(255) NOT NULL, - `header_name` varchar(255) NOT NULL default '', - `header_value` varchar(255) NOT NULL default '', - `status` enum('on','off') NOT NULL default 'on', - `error` varchar(255) NULL, - `rtime` FLOAT(9, 7) NULL, - `last_online` datetime NULL, - `last_check` datetime NULL, - `active` enum('yes','no') NOT NULL default 'yes', - `email` enum('yes','no') NOT NULL default 'yes', - `sms` enum('yes','no') NOT NULL default 'no', - `pushover` enum('yes','no') NOT NULL default 'yes', - `warning_threshold` mediumint(1) unsigned NOT NULL DEFAULT '1', - `warning_threshold_counter` mediumint(1) unsigned NOT NULL DEFAULT '0', - `timeout` smallint(1) unsigned NULL DEFAULT NULL, - `website_username` varchar(255) DEFAULT NULL, - `website_password` varchar(255) DEFAULT NULL, - PRIMARY KEY (`server_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `ip` varchar(500) NOT NULL, + `port` int(5) unsigned NOT NULL, + `label` varchar(255) NOT NULL, + `type` enum('ping','service','website') NOT NULL default 'service', + `pattern` varchar(255) NOT NULL, + `header_name` varchar(255) NOT NULL default '', + `header_value` varchar(255) NOT NULL default '', + `status` enum('on','off') NOT NULL default 'on', + `error` varchar(255) NULL, + `rtime` FLOAT(9, 7) NULL, + `last_online` datetime NULL, + `last_check` datetime NULL, + `active` enum('yes','no') NOT NULL default 'yes', + `email` enum('yes','no') NOT NULL default 'yes', + `sms` enum('yes','no') NOT NULL default 'no', + `pushover` enum('yes','no') NOT NULL default 'yes', + `statuscake` enum('yes','no') NOT NULL default 'yes', + `statuscake_test_id` varchar(50) NULL, + `warning_threshold` mediumint(1) unsigned NOT NULL DEFAULT '1', + `warning_threshold_counter` mediumint(1) unsigned NOT NULL DEFAULT '0', + `timeout` smallint(1) unsigned NULL DEFAULT NULL, + `website_username` varchar(255) DEFAULT NULL, + `website_password` varchar(255) DEFAULT NULL, + PRIMARY KEY (`server_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'servers_uptime' => "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "servers_uptime` ( - `servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `server_id` int(11) unsigned NOT NULL, - `date` datetime NOT NULL, - `status` tinyint(1) unsigned NOT NULL, - `latency` float(9,7) DEFAULT NULL, - PRIMARY KEY (`servers_uptime_id`), - KEY `server_id` (`server_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + `servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `server_id` int(11) unsigned NOT NULL, + `date` datetime NOT NULL, + `status` tinyint(1) unsigned NOT NULL, + `latency` float(9,7) DEFAULT NULL, + PRIMARY KEY (`servers_uptime_id`), + KEY `server_id` (`server_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", PSM_DB_PREFIX . 'servers_history' => "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "servers_history` ( - `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `server_id` int(11) unsigned NOT NULL, - `date` date NOT NULL, - `latency_min` float(9,7) NOT NULL, - `latency_avg` float(9,7) NOT NULL, - `latency_max` float(9,7) NOT NULL, - `checks_total` int(11) unsigned NOT NULL, - `checks_failed` int(11) unsigned NOT NULL, - PRIMARY KEY (`servers_history_id`), - UNIQUE KEY `server_id_date` (`server_id`,`date`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", - ); + `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `server_id` int(11) unsigned NOT NULL, + `date` date NOT NULL, + `latency_min` float(9,7) NOT NULL, + `latency_avg` float(9,7) NOT NULL, + `latency_max` float(9,7) NOT NULL, + `checks_total` int(11) unsigned NOT NULL, + `checks_failed` int(11) unsigned NOT NULL, + PRIMARY KEY (`servers_history_id`), + UNIQUE KEY `server_id_date` (`server_id`,`date`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;", + ); - foreach($tables as $name => $sql) { - $if_table_exists = $this->db->query("SHOW TABLES LIKE '{$name}'"); +foreach($tables as $name => $sql) { + $if_table_exists = $this->db->query("SHOW TABLES LIKE '{$name}'"); - if(!empty($if_table_exists)) { - $this->log('Table ' . $name . ' already exists in your database!'); - } else { - $this->execSQL($sql); - $this->log('Table ' . $name . ' added.'); - } - } + if(!empty($if_table_exists)) { + $this->log('Table ' . $name . ' already exists in your database!'); + } else { + $this->execSQL($sql); + $this->log('Table ' . $name . ' added.'); } +} +} /** * Populate the tables and perform upgrades if necessary @@ -352,44 +355,44 @@ class Installer { $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` CHANGE `user_id` `user_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` - ADD `user_name` varchar(64) COLLATE utf8_general_ci NOT NULL COMMENT 'user\'s name, unique' AFTER `user_id`, - ADD `password` varchar(255) COLLATE utf8_general_ci NOT NULL COMMENT 'user\'s password in salted and hashed format' AFTER `user_name`, - ADD `password_reset_hash` char(40) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\'s password reset code' AFTER `password`, - ADD `password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request' AFTER `password_reset_hash`, - ADD `rememberme_token` varchar(64) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\'s remember-me cookie token' AFTER `password_reset_timestamp`, - ADD `level` TINYINT( 2 ) UNSIGNED NOT NULL DEFAULT '20' AFTER `rememberme_token`;"; + ADD `user_name` varchar(64) COLLATE utf8_general_ci NOT NULL COMMENT 'user\'s name, unique' AFTER `user_id`, + ADD `password` varchar(255) COLLATE utf8_general_ci NOT NULL COMMENT 'user\'s password in salted and hashed format' AFTER `user_name`, + ADD `password_reset_hash` char(40) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\'s password reset code' AFTER `password`, + ADD `password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request' AFTER `password_reset_hash`, + ADD `rememberme_token` varchar(64) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\'s remember-me cookie token' AFTER `password_reset_timestamp`, + ADD `level` TINYINT( 2 ) UNSIGNED NOT NULL DEFAULT '20' AFTER `rememberme_token`;"; // make sure all current users are admins (previously we didnt have non-admins): $queries[] = "UPDATE `" . PSM_DB_PREFIX . "users` SET `user_name`=`email`, `level`=10;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "users` ADD UNIQUE `unique_username` ( `user_name` );"; $queries[] = "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "servers_uptime` ( - `servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `server_id` int(11) unsigned NOT NULL, - `date` datetime NOT NULL, - `status` tinyint(1) unsigned NOT NULL, - `latency` float(9,7) DEFAULT NULL, - PRIMARY KEY (`servers_uptime_id`), - KEY `server_id` (`server_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; + `servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `server_id` int(11) unsigned NOT NULL, + `date` datetime NOT NULL, + `status` tinyint(1) unsigned NOT NULL, + `latency` float(9,7) DEFAULT NULL, + PRIMARY KEY (`servers_uptime_id`), + KEY `server_id` (`server_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $queries[] = "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "servers_history` ( - `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `server_id` int(11) unsigned NOT NULL, - `date` date NOT NULL, - `latency_min` float(9,7) NOT NULL, - `latency_avg` float(9,7) NOT NULL, - `latency_max` float(9,7) NOT NULL, - `checks_total` int(11) unsigned NOT NULL, - `checks_failed` int(11) unsigned NOT NULL, - PRIMARY KEY (`servers_history_id`), - UNIQUE KEY `server_id_date` (`server_id`,`date`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; + `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `server_id` int(11) unsigned NOT NULL, + `date` date NOT NULL, + `latency_min` float(9,7) NOT NULL, + `latency_avg` float(9,7) NOT NULL, + `latency_max` float(9,7) NOT NULL, + `checks_total` int(11) unsigned NOT NULL, + `checks_failed` int(11) unsigned NOT NULL, + PRIMARY KEY (`servers_history_id`), + UNIQUE KEY `server_id_date` (`server_id`,`date`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $queries[] = "CREATE TABLE `" . PSM_DB_PREFIX . "users_servers` ( - `user_id` INT( 11 ) UNSIGNED NOT NULL , - `server_id` INT( 11 ) UNSIGNED NOT NULL , - PRIMARY KEY ( `user_id` , `server_id` ) - ) ENGINE = MYISAM ;"; + `user_id` INT( 11 ) UNSIGNED NOT NULL , + `server_id` INT( 11 ) UNSIGNED NOT NULL , + PRIMARY KEY ( `user_id` , `server_id` ) + ) ENGINE = MYISAM ;"; $this->execSQL($queries); // from 3.0 all user-server relations are in a separate table @@ -408,7 +411,7 @@ class Installer { $this->db->save(PSM_DB_PREFIX . 'users_servers', array( 'user_id' => $user['user_id'], 'server_id' => $id, - )); + )); } } $this->execSQL("ALTER TABLE `".PSM_DB_PREFIX."users` DROP `server_id`;"); @@ -433,11 +436,11 @@ class Installer { $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `timeout` smallint(1) unsigned NULL DEFAULT NULL;"; $queries[] = "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "users_preferences` ( - `user_id` int(11) unsigned NOT NULL, - `key` varchar(255) NOT NULL, - `value` varchar(255) NOT NULL, - PRIMARY KEY (`user_id`, `key`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; + `user_id` int(11) unsigned NOT NULL, + `key` varchar(255) NOT NULL, + `value` varchar(255) NOT NULL, + PRIMARY KEY (`user_id`, `key`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $this->execSQL($queries); } @@ -452,36 +455,36 @@ class Installer { $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` CHANGE `ip` `ip` VARCHAR(500) NOT NULL;"; $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `website_username` varchar(255) NULL, ADD `website_password` varchar(255) NULL AFTER `website_username`;"; $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUE - ('proxy', '0'), - ('proxy_url', ''), - ('proxy_user', ''), - ('proxy_password', '');"; + ('proxy', '0'), + ('proxy_url', ''), + ('proxy_user', ''), + ('proxy_password', '');"; $this->execSQL($queries); // Create log_users table - $this->execSQL("CREATE TABLE `" . PSM_DB_PREFIX . "log_users` ( - `log_id` int(11) UNSIGNED NOT NULL , - `user_id` int(11) UNSIGNED NOT NULL , - PRIMARY KEY (`log_id`, `user_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); + $this->execSQL("CREATE TABLE `" . PSM_DB_PREFIX . "log_users` ( + `log_id` int(11) UNSIGNED NOT NULL , + `user_id` int(11) UNSIGNED NOT NULL , + PRIMARY KEY (`log_id`, `user_id`) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"); // Migrate the data - $logs = $this->db->select(PSM_DB_PREFIX . 'log', null, array('log_id', 'user_id')); - foreach ($logs as $log) { + $logs = $this->db->select(PSM_DB_PREFIX . 'log', null, array('log_id', 'user_id')); + foreach ($logs as $log) { // Validation - if (empty($log['user_id']) || trim($log['user_id']) == '') { - continue; - } + if (empty($log['user_id']) || trim($log['user_id']) == '') { + continue; + } // Insert into new table - foreach (explode(',', $log['user_id']) as $user_id) { - psm_add_log_user($log['log_id'], $user_id); - } - } + foreach (explode(',', $log['user_id']) as $user_id) { + psm_add_log_user($log['log_id'], $user_id); + } + } // Drop old user_id('s) column - $this->execSQL("ALTER TABLE `" . PSM_DB_PREFIX . "log` DROP COLUMN `user_id`;"); + $this->execSQL("ALTER TABLE `" . PSM_DB_PREFIX . "log` DROP COLUMN `user_id`;"); } /** @@ -489,7 +492,13 @@ class Installer { */ protected function upgrade321() { $queries = array(); + + psm_update_conf('statuscake_account_token', ''); + $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `statuscake` enum('yes','no') NOT NULL default 'yes', ADD `statuscake_test_id` varchar(50) NULL AFTER `pushover`;"; + $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD COLUMN `header_name` VARCHAR(255) AFTER `pattern`, ADD COLUMN `header_value` VARCHAR(255) AFTER `header_name`"; + $this->execSQL($queries); + } } diff --git a/src/psm/Util/Server/Updater/StatusNotifier.php b/src/psm/Util/Server/Updater/StatusNotifier.php index b0a8f270..6c70a2d4 100644 --- a/src/psm/Util/Server/Updater/StatusNotifier.php +++ b/src/psm/Util/Server/Updater/StatusNotifier.php @@ -66,12 +66,6 @@ class StatusNotifier { */ protected $send_statuscake_account_id = null; - /** - * Test for sending StatusCake Notification - * @var string send_statuscake_test_id - */ - protected $send_statuscake_test_id = null; - /** * Save log records? * @var boolean $save_log @@ -108,8 +102,7 @@ class StatusNotifier { $this->send_emails = psm_get_conf('email_status'); $this->send_sms = psm_get_conf('sms_status'); $this->send_pushover = psm_get_conf('pushover_status'); - $this->send_statuscake_account_id = psm_get_conf('statuscake_account_id'); - $this->send_statuscake_test_id = psm_get_conf('statuscake_test_id'); + $this->send_statuscake_account_token = psm_get_conf('statuscake_account_token'); $this->save_logs = psm_get_conf('log_status'); } @@ -122,6 +115,7 @@ class StatusNotifier { * @return boolean */ public function notify($server_id, $status_old, $status_new) { + if(!$this->send_emails && !$this->send_sms && !$this->save_logs) { // seems like we have nothing to do. skip the rest return false; @@ -134,9 +128,9 @@ class StatusNotifier { // get server info from db $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array( 'server_id' => $server_id, - ), array( - 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'header_name', 'header_value', 'error', 'active', 'email', 'sms', 'pushover', - )); + ), array( + 'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'rtime', 'header_name', 'header_value', 'error', 'active', 'email', 'sms', 'pushover', 'statuscake','statuscake_test_id', + )); if(empty($this->server)) { return false; } @@ -146,71 +140,74 @@ class StatusNotifier { // check which type of alert the user wants switch(psm_get_conf('alert_type')) { case 'always': - if($status_new == false) { + if($status_new == false) { // server is offline. we are in error state. - $notify = true; - } - break; + $notify = true; + } + break; case 'offline': // only send a notification if the server goes down for the first time! - if($status_new == false && $status_old == true) { - $notify = true; - } - break; + if($status_new == false && $status_old == true) { + $notify = true; + } + break; case 'status': - if($status_new != $status_old) { + if($status_new != $status_old) { // status has been changed! - $notify = true; - } - break; - } - - if(!$notify) { - return false; - } - - // first add to log (we use the same text as the SMS message because its short..) - if($this->save_logs) { - psm_add_log( - $this->server_id, - 'status', - psm_parse_msg($status_new, 'sms', $this->server) - ); - } - - $users = $this->getUsers($this->server_id); - - if(empty($users)) { - return $notify; - } - - // check if email is enabled for this server - if($this->send_emails && $this->server['email'] == 'yes') { - // send email - $this->notifyByEmail($users); - } - - // check if sms is enabled for this server - if($this->send_sms && $this->server['sms'] == 'yes') { - // yay lets wake those nerds up! - $this->notifyByTxtMsg($users); - } - - // check if pushover is enabled for this server - if($this->send_pushover && $this->server['pushover'] == 'yes') { - // yay lets wake those nerds up! - $this->notifyByPushover($users); + $notify = true; + } + break; } // check if statuscake is enabled for this server - if( !is_null($this->send_statuscake_account_id) && !is_null($this->send_statuscake_test_id) && $this->server['send_statuscake'] == 'yes') { + if( !is_null($this->send_statuscake_account_token) && !empty($this->send_statuscake_account_token) + && $this->server['statuscake'] == 'yes' + && !is_null($this->server['statuscake_test_id']) && !empty($this->server['statuscake_test_id']) + ) { // yay lets wake those nerds up! - $this->notifyByStatusCake($users, $this->send_statuscake_test_id); - } + $this->notifyByStatusCake($users, $this->server['statuscake_test_id']); + } + if(!$notify) { + return false; + } + + // first add to log (we use the same text as the SMS message because its short..) + if($this->save_logs) { + psm_add_log( + $this->server_id, + 'status', + psm_parse_msg($status_new, 'sms', $this->server) + ); + } + + $users = $this->getUsers($this->server_id); + + if(empty($users)) { return $notify; } + // check if email is enabled for this server + if($this->send_emails && $this->server['email'] == 'yes') { + // send email + $this->notifyByEmail($users); + } + + // check if sms is enabled for this server + if($this->send_sms && $this->server['sms'] == 'yes') { + // yay lets wake those nerds up! + $this->notifyByTxtMsg($users); + } + + // check if pushover is enabled for this server + if($this->send_pushover && $this->server['pushover'] == 'yes') { + // yay lets wake those nerds up! + $this->notifyByPushover($users); + } + + return $notify; +} + /** * This functions performs the email notifications * @@ -227,33 +224,36 @@ class StatusNotifier { $mail->Body = utf8_decode($body); $mail->AltBody = str_replace('
', "\n", $body); - if(psm_get_conf('log_email')) { - $log_id = psm_add_log($this->server_id, 'email', $body); - } - - // go through empl - foreach ($users as $user) { - if(!empty($log_id)) { - psm_add_log_user($log_id, $user['user_id']); - } - - // we sent a seperate email to every single user. - $mail->AddAddress($user['email'], $user['name']); - $mail->Send(); - $mail->ClearAddresses(); - } - } - - protected function notifyByStatusCake($users, $send_statuscake_test_id) { - - $statuscake = psm_build_statuscake(); - $statuscake->setTestId($send_statuscake_test_id) - - // Log - if(psm_get_conf('log_pushover')) { - $log_id = psm_add_log($this->server_id, 'statuscake', $message); + if(psm_get_conf('log_email')) { + $log_id = psm_add_log($this->server_id, 'email', $body); } + // go through empl + foreach ($users as $user) { + if(!empty($log_id)) { + psm_add_log_user($log_id, $user['user_id']); + } + + // we sent a seperate email to every single user. + $mail->AddAddress($user['email'], $user['name']); + $mail->Send(); + $mail->ClearAddresses(); + } + } + + protected function notifyByStatusCake($users, $statuscake_test_id) { + + $statuscake = psm_build_statuscake(); + $statuscake->setTestId($statuscake_test_id); + $statuscake->setTime(11); + $statuscake->setStatusCode(200); + + // Log + //if(psm_get_conf('log_statuscake')) { + // $log_id = psm_add_log($this->server_id, 'statuscake', $message); + //} + + $statuscake->send(); } /** @@ -264,51 +264,51 @@ class StatusNotifier { */ protected function notifyByPushover($users) { // Remove users that have no pushover_key - foreach($users as $k => $user) { - if (trim($user['pushover_key']) == '') { - unset($users[$k]); - } - } + foreach($users as $k => $user) { + if (trim($user['pushover_key']) == '') { + unset($users[$k]); + } + } // Validation - if (empty($users)) { - return; - } + if (empty($users)) { + return; + } // Pushover - $message = psm_parse_msg($this->status_new, 'pushover_message', $this->server); - $pushover = psm_build_pushover(); - if($this->status_new === true) { - $pushover->setPriority(0); - } else { - $pushover->setPriority(2); + $message = psm_parse_msg($this->status_new, 'pushover_message', $this->server); + $pushover = psm_build_pushover(); + if($this->status_new === true) { + $pushover->setPriority(0); + } else { + $pushover->setPriority(2); $pushover->setRetry(300); //Used with Priority = 2; Pushover will resend the notification every 60 seconds until the user accepts. $pushover->setExpire(3600); //Used with Priority = 2; Pushover will resend the notification every 60 seconds for 3600 seconds. After that point, it stops sending notifications. } - $pushover->setTitle(psm_parse_msg($this->status_new, 'pushover_title', $this->server)); - $pushover->setMessage(str_replace('
', "\n", $message)); - $pushover->setUrl(psm_build_url()); - $pushover->setUrlTitle(psm_get_lang('system', 'title')); + $pushover->setTitle(psm_parse_msg($this->status_new, 'pushover_title', $this->server)); + $pushover->setMessage(str_replace('
', "\n", $message)); + $pushover->setUrl(psm_build_url()); + $pushover->setUrlTitle(psm_get_lang('system', 'title')); // Log if(psm_get_conf('log_pushover')) { - $log_id = psm_add_log($this->server_id, 'pushover', $message); - } + $log_id = psm_add_log($this->server_id, 'pushover', $message); + } - foreach($users as $user) { + foreach($users as $user) { // Log - if(!empty($log_id)) { - psm_add_log_user($log_id, $user['user_id']); - } + if(!empty($log_id)) { + psm_add_log_user($log_id, $user['user_id']); + } // Set recipient + send - $pushover->setUser($user['pushover_key']); - if($user['pushover_device'] != '') { - $pushover->setDevice($user['pushover_device']); - } - $pushover->send(); + $pushover->setUser($user['pushover_key']); + if($user['pushover_device'] != '') { + $pushover->setDevice($user['pushover_device']); + } + $pushover->send(); } - } + } /** * This functions performs the text message notifications @@ -322,19 +322,19 @@ class StatusNotifier { return false; } - $message = psm_parse_msg($this->status_new, 'sms', $this->server); + $message = psm_parse_msg($this->status_new, 'sms', $this->server); // Log - if(psm_get_conf('log_sms')) { - $log_id = psm_add_log($this->server_id, 'sms', $message); + if(psm_get_conf('log_sms')) { + $log_id = psm_add_log($this->server_id, 'sms', $message); } // add all users to the recipients list foreach ($users as $user) { // Log - if(!empty($log_id)) { - psm_add_log_user($log_id, $user['user_id']); - } + if(!empty($log_id)) { + psm_add_log_user($log_id, $user['user_id']); + } $sms->addRecipients($user['mobile']); } @@ -356,10 +356,10 @@ class StatusNotifier { SELECT `u`.`user_id`, `u`.`name`,`u`.`email`, `u`.`mobile`, `u`.`pushover_key`, `u`.`pushover_device` FROM `".PSM_DB_PREFIX."users` AS `u` JOIN `".PSM_DB_PREFIX."users_servers` AS `us` ON ( - `us`.`user_id`=`u`.`user_id` - AND `us`.`server_id` = {$server_id} + `us`.`user_id`=`u`.`user_id` + AND `us`.`server_id` = {$server_id} ) - "); + "); return $users; } }