Exemple #1
0
 /**
  * Upgrade for v3.0.0 release
  */
 protected function upgrade300()
 {
     $queries = array();
     // language is now stored as language code (ISO 639-1) + country code (ISO 3166-1)
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='bg_BG' WHERE `key`='language' AND `value`='bg';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='de_DE' WHERE `key`='language' AND `value`='de';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='en_US' WHERE `key`='language' AND `value`='en';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='fr_FR' WHERE `key`='language' AND `value`='fr';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='ko_KR' WHERE `key`='language' AND `value`='kr';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='nl_NL' WHERE `key`='language' AND `value`='nl';";
     $queries[] = "UPDATE `" . PSM_DB_PREFIX . "config` SET `value`='pt_BR' WHERE `key`='language' AND `value`='br';";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('version_update_check', '" . PSM_VERSION . "');";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('email_smtp', '');";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('email_smtp_host', '');";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('email_smtp_port', '');";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('email_smtp_username', '');";
     $queries[] = "INSERT INTO `" . PSM_DB_PREFIX . "config` (`key`, `value`) VALUES ('email_smtp_password', '');";
     $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "log` CHANGE `log_id` `log_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT;";
     $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "log` CHANGE `server_id` `server_id` INT( 11 ) UNSIGNED NOT NULL;";
     $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` CHANGE `server_id` `server_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT;";
     $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `warning_threshold` MEDIUMINT( 1 ) UNSIGNED NOT NULL DEFAULT '1';";
     $queries[] = "ALTER TABLE `" . PSM_DB_PREFIX . "servers` ADD `warning_threshold_counter` MEDIUMINT( 1 ) UNSIGNED NOT NULL DEFAULT '0';";
     $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`\n\t\t\tADD `user_name` varchar(64) COLLATE utf8_general_ci NOT NULL COMMENT 'user\\'s name, unique' AFTER `user_id`,\n\t\t\tADD `password` varchar(255) COLLATE utf8_general_ci NOT NULL COMMENT 'user\\'s password in salted and hashed format' AFTER `user_name`,\n\t\t\tADD `password_reset_hash` char(40) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\\'s password reset code' AFTER `password`,\n\t\t\tADD `password_reset_timestamp` bigint(20) DEFAULT NULL COMMENT 'timestamp of the password reset request' AFTER `password_reset_hash`,\n\t\t\tADD `rememberme_token` varchar(64) COLLATE utf8_general_ci DEFAULT NULL COMMENT 'user\\'s remember-me cookie token' AFTER `password_reset_timestamp`,\n\t\t\tADD `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` (\n\t\t\t\t\t\t`servers_uptime_id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t`server_id` int(11) unsigned NOT NULL,\n\t\t\t\t\t\t`date` datetime NOT NULL,\n\t\t\t\t\t\t`status` tinyint(1) unsigned NOT NULL,\n\t\t\t\t\t\t`latency` float(9,7) DEFAULT NULL,\n\t\t\t\t\t\tPRIMARY KEY (`servers_uptime_id`),\n\t\t\t\t\t\tKEY `server_id` (`server_id`)\n\t\t\t\t\t  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
     $queries[] = "CREATE TABLE IF NOT EXISTS `" . PSM_DB_PREFIX . "servers_history` (\n\t\t\t\t\t\t  `servers_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t  `server_id` int(11) unsigned NOT NULL,\n\t\t\t\t\t\t  `date` date NOT NULL,\n\t\t\t\t\t\t  `latency_min` float(9,7) NOT NULL,\n\t\t\t\t\t\t  `latency_avg` float(9,7) NOT NULL,\n\t\t\t\t\t\t  `latency_max` float(9,7) NOT NULL,\n\t\t\t\t\t\t  `checks_total` int(11) unsigned NOT NULL,\n\t\t\t\t\t\t  `checks_failed` int(11) unsigned NOT NULL,\n\t\t\t\t\t\t  PRIMARY KEY (`servers_history_id`),\n\t\t\t\t\t\t  UNIQUE KEY `server_id_date` (`server_id`,`date`)\n\t\t\t\t\t\t) ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
     $queries[] = "CREATE TABLE `" . PSM_DB_PREFIX . "users_servers` (\n\t\t\t\t\t\t`user_id` INT( 11 ) UNSIGNED NOT NULL ,\n\t\t\t\t\t\t`server_id` INT( 11 ) UNSIGNED NOT NULL ,\n\t\t\t\t\t\tPRIMARY KEY ( `user_id` , `server_id` )\n\t\t\t\t\t\t) ENGINE = MYISAM ;";
     $this->execSQL($queries);
     // from 3.0 all user-server relations are in a separate table
     $users = $this->db->select(PSM_DB_PREFIX . 'users', null, array('user_id', 'server_id'));
     foreach ($users as $user) {
         $idc = array();
         if ($user['server_id'] == '') {
             continue;
         }
         if (strpos($user['server_id'], ',') === false) {
             $idc[] = $user['server_id'];
         } else {
             $idc = explode(',', $user['server_id']);
         }
         foreach ($idc as $id) {
             $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`;");
 }
 /**
  * The function its all about. This one checks whether the given ip and port are up and running!
  * If the server check fails it will try one more time, depending on the $max_runs.
  *
  * Please note: if the server is down but has not met the warning threshold, this will return true
  * to avoid any "we are down" events.
  * @param int $server_id
  * @param int $max_runs how many times should the script recheck the server if unavailable. default is 2
  * @return boolean TRUE if server is up, FALSE otherwise
  */
 public function update($server_id, $max_runs = 2)
 {
     $this->server_id = $server_id;
     $this->error = '';
     $this->rtime = '';
     // 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', 'active', 'warning_threshold', 'warning_threshold_counter', 'timeout', 'website_username', 'website_password'));
     if (empty($this->server)) {
         return false;
     }
     switch ($this->server['type']) {
         case 'service':
             $this->status_new = $this->updateService($max_runs);
             break;
         case 'website':
             $this->status_new = $this->updateWebsite($max_runs);
             break;
     }
     // update server status
     $save = array('last_check' => date('Y-m-d H:i:s'), 'error' => $this->error, 'rtime' => $this->rtime);
     // log the uptime before checking the warning threshold,
     // so that the warnings can still be reviewed in the server history.
     psm_log_uptime($this->server_id, (int) $this->status_new, $this->rtime);
     if ($this->status_new == true) {
         // if the server is on, add the last_online value and reset the error threshold counter
         $save['status'] = 'on';
         $save['last_online'] = date('Y-m-d H:i:s');
         $save['warning_threshold_counter'] = 0;
     } else {
         // server is offline, increase the error counter
         $save['warning_threshold_counter'] = $this->server['warning_threshold_counter'] + 1;
         if ($save['warning_threshold_counter'] < $this->server['warning_threshold']) {
             // the server is offline but the error threshold has not been met yet.
             // so we are going to leave the status "on" for now while we are in a sort of warning state..
             $save['status'] = 'on';
             $this->status_new = true;
         } else {
             $save['status'] = 'off';
         }
     }
     $this->db->save(PSM_DB_PREFIX . 'servers', $save, array('server_id' => $this->server_id));
     return $this->status_new;
 }