Add support for "site online when pattern not found" (#579)

Choose to mark a website offline of online is the pattern is (not) found.
This commit is contained in:
Danial 2018-05-24 22:49:26 +12:00 committed by Tim
parent c4e6a61361
commit 82d01b07f7
6 changed files with 31 additions and 6 deletions

View File

@ -73,6 +73,8 @@ $sm_lang = array(
'minutes' => 'minutes',
'second' => 'second',
'seconds' => 'seconds',
'online' => 'online',
'offline' => 'offline',
),
'menu' => array(
'config' => 'Config',
@ -164,7 +166,9 @@ $sm_lang = array(
'type_service' => 'Service',
'type_ping' => 'Ping',
'pattern' => 'Search string/pattern',
'pattern_description' => 'If this pattern is not found on the website, the server will be marked offline. Regular expressions are allowed.',
'pattern_description' => 'If this pattern is not found on the website, the server will be marked online/offline. Regular expressions are allowed.',
'pattern_online' => 'Pattern indicates website is',
'pattern_online_description' => 'Online: If this pattern is not found on the website, the server will be marked online. Offline: If this pattern is not found on the website, the server will be marked offline.',
'header' => 'Header',
'header_name_description' => 'Header name (case-sensitive)',
'header_value_description' => 'Header value. Regular expressions are allowed.',

View File

@ -64,6 +64,7 @@ abstract class AbstractServerController extends AbstractController {
`s`.`type`,
`s`.`label`,
`s`.`pattern`,
`s`.`pattern_online`,
`s`.`header_name`,
`s`.`header_value`,
`s`.`status`,

View File

@ -199,6 +199,7 @@ class ServerController extends AbstractServerController {
'edit_value_timeout' => $edit_server['timeout'],
'default_value_timeout' => PSM_CURL_TIMEOUT,
'edit_value_pattern' => $edit_server['pattern'],
'edit_pattern_selected_' . $edit_server['pattern_online'] => 'selected="selected"',
'edit_value_header_name' => $edit_server['header_name'],
'edit_value_header_value' => $edit_server['header_value'],
'edit_value_warning_threshold' => $edit_server['warning_threshold'],
@ -267,6 +268,7 @@ class ServerController extends AbstractServerController {
'port' => intval(psm_POST('port', 0)),
'type' => psm_POST('type', ''),
'pattern' => psm_POST('pattern', ''),
'pattern_online' => in_array($_POST['pattern_online'], array('yes', 'no')) ? $_POST['pattern_online'] : 'yes',
'header_name' => psm_POST('header_name', ''),
'header_value' => psm_POST('header_value', ''),
'rtime' => psm_POST('rtime', '0.0000000'),
@ -471,6 +473,8 @@ class ServerController extends AbstractServerController {
'label_ping' => psm_get_lang('servers', 'type_ping'),
'label_pattern' => psm_get_lang('servers', 'pattern'),
'label_pattern_description' => psm_get_lang('servers', 'pattern_description'),
'label_pattern_online' => psm_get_lang('servers', 'pattern_online'),
'label_pattern_online_description' => psm_get_lang('servers', 'pattern_online_description'),
'label_header' => psm_get_lang('servers', 'header'),
'label_header_name_description' => psm_get_lang('servers', 'header_name_description'),
'label_header_value_description' => psm_get_lang('servers', 'header_value_description'),
@ -496,6 +500,8 @@ class ServerController extends AbstractServerController {
'label_yes' => psm_get_lang('system', 'yes'),
'label_no' => psm_get_lang('system', 'no'),
'label_add_new' => psm_get_lang('system', 'add_new'),
'label_online' => psm_get_lang('system', 'online'),
'label_offline' => psm_get_lang('system', 'offline'),
);
}

View File

@ -126,7 +126,7 @@ class Installer {
$this->log('Populating database...');
$queries = array();
$queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "servers` (`ip`, `port`, `label`, `type`, `pattern`, `status`, `rtime`, `active`, `email`, `sms`, `pushover`, `telegram`) VALUES ('http://sourceforge.net/index.php', 80, 'SourceForge', 'website', '', 'on', '0.0000000', 'yes', 'yes', 'yes', 'yes', 'yes'), ('smtp.gmail.com', 465, 'Gmail SMTP', 'service', '', 'on', '0.0000000', 'yes', 'yes', 'yes', 'yes', 'yes')";
$queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "servers` (`ip`, `port`, `label`, `type`, `pattern`, `pattern_online`, `status`, `rtime`, `active`, `email`, `sms`, `pushover`, `telegram`) VALUES ('http://sourceforge.net/index.php', 80, 'SourceForge', 'website', '', 'yes','on', '0.0000000', 'yes', 'yes', 'yes', 'yes', 'yes'), ('smtp.gmail.com', 465, 'Gmail SMTP', 'service', '', 'yes', 'on', '0.0000000', 'yes', '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'),
@ -228,6 +228,7 @@ class Installer {
`label` varchar(255) NOT NULL,
`type` enum('ping','service','website') NOT NULL default 'service',
`pattern` varchar(255) NOT NULL,
`pattern_online` enum('yes','no') NOT NULL default 'yes',
`header_name` varchar(255) NOT NULL default '',
`header_value` varchar(255) NOT NULL default '',
`status` enum('on','off') NOT NULL default 'on',
@ -533,6 +534,7 @@ class Installer {
protected function upgrade330() {
$queries = array();
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD COLUMN `last_offline` DATETIME NULL AFTER `last_online`, ADD COLUMN `last_offline_duration` varchar(255) NULL AFTER `last_offline`;";
$queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `pattern_online` ENUM( 'yes','no' ) NOT NULL DEFAULT 'yes' AFTER `pattern`;";
$this->execSQL($queries);
if(psm_get_conf('sms_gateway') == 'mollie'){
psm_update_conf('sms_gateway', 'messagebird');

View File

@ -82,7 +82,7 @@ class StatusUpdater {
$this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array(
'server_id' => $server_id,
), array(
'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'header_name', 'header_value', 'status', 'active', 'warning_threshold',
'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'pattern_online', 'header_name', 'header_value', 'status', 'active', 'warning_threshold',
'warning_threshold_counter', 'timeout', 'website_username', 'website_password', 'last_offline'
));
if(empty($this->server)) {
@ -260,9 +260,12 @@ class StatusUpdater {
//Okay, the HTTP status is good : 2xx or 3xx. Now we have to test the pattern if it's set up
if($this->server['pattern'] != '') {
// Check to see if the pattern was found.
if(!preg_match("/{$this->server['pattern']}/i", $curl_result)) {
$this->error = 'TEXT ERROR : Pattern not found.';
// Check to see if the body should not contain specified pattern
// Check to see if the pattern was [not] found.
if(($this->server['pattern_online'] == 'yes') == !preg_match("/{$this->server['pattern']}/i", $curl_result)){
$this->error = "TEXT ERROR : Pattern '{$this->server['pattern']}' " .
($this->server['pattern_online'] == 'yes' ? 'not' : 'was') .
' found.';
$result = false;
}
}

View File

@ -65,6 +65,15 @@
<input type="text" id="pattern" name="pattern" value="{{ edit_value_pattern }}" maxlength="255" data-toggle="tooltip" title="{{ label_pattern_description }}" />
</div>
</div>
<div class="control-group types typeWebsite">
<label class="control-label" for="pattern_online">{{ label_pattern_online }}</label>
<div class="controls">
<select class="input-mini" id="pattern_online" name="pattern_online" data-toggle="tooltip" title="{{ label_pattern_online_description}}">
<option value="yes" {{ edit_pattern_selected_yes|raw }}>{{ label_online }}</option>
<option value="no" {{ edit_pattern_selected_no|raw }}>{{ label_offline }}</option>
</select>
</div>
</div>
<div class="control-group types typeWebsite">
<label class="control-label" for="header_name">{{ label_header }}</label>
<div class="controls">