Ejemplo n.º 1
0
 public function __construct(\psm\Service\Database $db)
 {
     $this->db = $db;
     $this->setRetentionPeriod(psm_get_conf('log_retention_period', 365));
     $this->archivers[] = new Archiver\UptimeArchiver($db);
     $this->archivers[] = new Archiver\LogsArchiver($db);
 }
Ejemplo n.º 2
0
 /**
  * Check if an upgrade is required for the current version.
  * @return boolean
  * @see upgrade()
  */
 public function isUpgradeRequired()
 {
     $version_db = psm_get_conf('version');
     if (version_compare(PSM_VERSION, $version_db, '==')) {
         // version is up to date
         return false;
     }
     // different DB version, check if the version requires any changes
     // @todo this is currently a manual check for each version, similar to upgrade().. not a clean way
     if (version_compare($version_db, '3.1.0', '<')) {
         return true;
     } else {
         // change database version to current version so this check won't be required next time
         psm_update_conf('version', PSM_VERSION);
     }
     return false;
 }
Ejemplo n.º 3
0
 /**
  * Prepare the template to show a list of all servers
  * @todo move the background colurs to the config
  */
 protected function executeIndex()
 {
     // set background color to black
     $this->black_background = true;
     $this->twig->addGlobal('subtitle', psm_get_lang('menu', 'server_status'));
     // add header accessories
     $layout = $this->getUser()->getUserPref('status_layout', 0);
     $layout_data = array('label_last_check' => psm_get_lang('servers', 'last_check'), 'label_last_online' => psm_get_lang('servers', 'last_online'), 'label_rtime' => psm_get_lang('servers', 'latency'), 'block_layout_active' => $layout == 0 ? 'active' : '', 'list_layout_active' => $layout != 0 ? 'active' : '');
     $this->setHeaderAccessories($this->twig->render('module/server/status/header.tpl.html', $layout_data));
     $this->addFooter(false);
     // get the active servers from database
     $servers = $this->getServers();
     $layout_data['servers_offline'] = array();
     $layout_data['servers_online'] = array();
     foreach ($servers as $server) {
         if ($server['active'] == 'no') {
             continue;
         }
         $server['last_checked_nice'] = psm_timespan($server['last_check']);
         $server['last_online_nice'] = psm_timespan($server['last_online']);
         $server['url_view'] = psm_build_url(array('mod' => 'server', 'action' => 'view', 'id' => $server['server_id'], 'back_to' => 'server_status'));
         if ($server['status'] == "off") {
             $layout_data['servers_offline'][] = $server;
         } elseif ($server['warning_threshold_counter'] > 0) {
             $server['class_warning'] = 'warning';
             $layout_data['servers_offline'][] = $server;
         } else {
             $layout_data['servers_online'][] = $server;
         }
     }
     $auto_refresh_seconds = psm_get_conf('auto_refresh_servers');
     if (intval($auto_refresh_seconds) > 0) {
         $this->twig->addGlobal('auto_refresh', true);
         $this->twig->addGlobal('auto_refresh_seconds', $auto_refresh_seconds);
     }
     return $this->twig->render('module/server/status/index.tpl.html', $layout_data);
 }
Ejemplo n.º 4
0
 /**
  * Execute pushover test
  *
  * @todo move test to separate class
  */
 protected function testPushover()
 {
     $pushover = psm_build_pushover();
     $pushover->setDebug(true);
     $user = $this->getUser()->getUser();
     $api_token = psm_get_conf('pushover_api_token');
     if (empty($api_token)) {
         $this->addMessage(psm_get_lang('config', 'pushover_error_noapp'), 'error');
     } elseif (empty($user->pushover_key)) {
         $this->addMessage(psm_get_lang('config', 'pushover_error_nokey'), 'error');
     } else {
         $pushover->setPriority(0);
         $pushover->setTitle(psm_get_lang('config', 'test_subject'));
         $pushover->setMessage(psm_get_lang('config', 'test_message'));
         $pushover->setUser($user->pushover_key);
         if ($user->pushover_device != '') {
             $pushover->setDevice($user->pushover_device);
         }
         $result = $pushover->send();
         if (isset($result['output']->status) && $result['output']->status == 1) {
             $this->addMessage(psm_get_lang('config', 'pushover_sent'), 'success');
         } else {
             if (isset($result['output']->errors->error)) {
                 $error = $result['output']->errors->error;
             } else {
                 $error = 'Unknown';
             }
             $this->addMessage(sprintf(psm_get_lang('config', 'pushover_error'), $error), 'error');
         }
     }
 }
 /**
  * Prepare the template to show the update screen for a single server
  */
 protected function executeEdit()
 {
     $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : '';
     $tpl_data = $this->getLabels();
     $tpl_data['edit_server_id'] = $this->server_id;
     $tpl_data['url_save'] = psm_build_url(array('mod' => 'server', 'action' => 'save', 'id' => $this->server_id, 'back_to' => $back_to));
     // depending on where the user came from, add the go back url:
     if ($back_to == 'view' && $this->server_id > 0) {
         $tpl_data['url_go_back'] = psm_build_url(array('mod' => 'server', 'action' => 'view', 'id' => $this->server_id));
     } else {
         $tpl_data['url_go_back'] = psm_build_url(array('mod' => 'server'));
     }
     $tpl_data['users'] = $this->db->select(PSM_DB_PREFIX . 'users', null, array('user_id', 'name'), '', 'name');
     switch ($this->server_id) {
         case 0:
             // insert mode
             $tpl_data['titlemode'] = psm_get_lang('system', 'insert');
             $tpl_data['edit_value_warning_threshold'] = '1';
             $edit_server = $_POST;
             break;
         default:
             // edit mode
             // get server entry
             $edit_server = $this->getServers($this->server_id);
             if (empty($edit_server)) {
                 $this->addMessage(psm_get_lang('servers', 'error_server_no_match'), 'error');
                 return $this->initializeAction('index');
             }
             $tpl_data['titlemode'] = psm_get_lang('system', 'edit') . ' ' . $edit_server['label'];
             $user_idc_selected = $this->getServerUsers($this->server_id);
             foreach ($tpl_data['users'] as &$user) {
                 if (in_array($user['user_id'], $user_idc_selected)) {
                     $user['edit_selected'] = 'selected="selected"';
                 }
             }
             break;
     }
     if (!empty($edit_server)) {
         // attempt to prefill previously posted fields
         foreach ($edit_server as $key => $value) {
             $edit_server[$key] = psm_POST($key, $value);
         }
         $tpl_data = array_merge($tpl_data, array('edit_value_label' => $edit_server['label'], 'edit_value_ip' => $edit_server['ip'], 'edit_value_port' => $edit_server['port'], 'edit_value_timeout' => $edit_server['timeout'], 'default_value_timeout' => PSM_CURL_TIMEOUT, 'edit_value_pattern' => $edit_server['pattern'], 'edit_value_warning_threshold' => $edit_server['warning_threshold'], 'edit_type_selected_' . $edit_server['type'] => 'selected="selected"', 'edit_active_selected_' . $edit_server['active'] => 'selected="selected"', 'edit_email_selected_' . $edit_server['email'] => 'selected="selected"', 'edit_sms_selected_' . $edit_server['sms'] => 'selected="selected"', 'edit_pushover_selected_' . $edit_server['pushover'] => 'selected="selected"'));
     }
     $notifications = array('email', 'sms', 'pushover');
     foreach ($notifications as $notification) {
         if (psm_get_conf($notification . '_status') == 0) {
             $tpl_data['warning_' . $notification] = true;
             $tpl_data['control_class_' . $notification] = 'warning';
             $tpl_data['label_warning_' . $notification] = psm_get_lang('servers', 'warning_notifications_disabled_' . $notification);
         } else {
             $tpl_data['warning_' . $notification] = false;
         }
     }
     return $this->twig->render('module/server/server/update.tpl.html', $tpl_data);
 }
Ejemplo n.º 6
0
/**
 * Prepare a new SMS util.
 *
 * @return \psm\Txtmsg\TxtmsgInterface
 */
function psm_build_sms()
{
    $sms = null;
    // open the right class
    // not making this any more dynamic, because perhaps some gateways need custom settings (like Mollie)
    switch (strtolower(psm_get_conf('sms_gateway'))) {
        case 'mosms':
            $sms = new \psm\Txtmsg\Mosms();
            break;
        case 'smsit':
            $sms = new \psm\Txtmsg\Smsit();
            break;
        case 'inetworx':
            $sms = new \psm\Txtmsg\Inetworx();
            break;
        case 'mollie':
            $sms = new \psm\Txtmsg\Mollie();
            $sms->setGateway(1);
            break;
        case 'spryng':
            $sms = new \psm\Txtmsg\Spryng();
            break;
        case 'clickatell':
            $sms = new \psm\Txtmsg\Clickatell();
            break;
        case 'textmarketer':
            $sms = new \psm\Txtmsg\Textmarketer();
            break;
        case 'smsglobal':
            $sms = new \psm\Txtmsg\Smsglobal();
            break;
    }
    // copy login information from the config file
    if ($sms) {
        $sms->setLogin(psm_get_conf('sms_gateway_username'), psm_get_conf('sms_gateway_password'));
        $sms->setOriginator(psm_get_conf('sms_from'));
    }
    return $sms;
}
Ejemplo n.º 7
0
 /**
  * This functions performs the text message notifications
  *
  * @param array $users
  * @return boolean
  */
 protected function notifyByTxtMsg($users)
 {
     $sms = psm_build_sms();
     if (!$sms) {
         return false;
     }
     // we have to build an userlist for the log table..
     $userlist = array();
     // add all users to the recipients list
     foreach ($users as $user) {
         $userlist[] = $user['user_id'];
         $sms->addRecipients($user['mobile']);
     }
     $message = psm_parse_msg($this->status_new, 'sms', $this->server);
     // Send sms
     $result = $sms->sendSMS($message);
     if (psm_get_conf('log_sms')) {
         // save to log
         psm_add_log($this->server_id, 'sms', $message, implode(',', $userlist));
     }
     return $result;
 }
Ejemplo n.º 8
0
 /**
  * Executes the saving of one of the servers
  */
 protected function executeSave()
 {
     if (empty($_POST)) {
         // dont process anything if no data has been posted
         return $this->executeIndex();
     }
     $encrypted_password = '';
     if (!empty($_POST['website_password'])) {
         $new_password = psm_POST('website_password');
         if ($this->server_id > 0) {
             $edit_server = $this->getServers($this->server_id);
             $hash = sha1($edit_server['website_password']);
             if ($new_password == $hash) {
                 $encrypted_password = $edit_server['website_password'];
             } else {
                 $encrypted_password = psm_password_encrypt($this->server_id . psm_get_conf('password_encrypt_key'), $new_password);
             }
         } else {
             // We need the server id to encrypt the password. Encryption will be done after the server is added
             $encrypted_password = '';
         }
     }
     $clean = array('label' => trim(strip_tags(psm_POST('label', ''))), 'ip' => trim(strip_tags(psm_POST('ip', ''))), 'timeout' => isset($_POST['timeout']) && intval($_POST['timeout']) > 0 ? intval($_POST['timeout']) : null, 'website_username' => psm_POST('website_username', null), 'website_password' => $encrypted_password, 'port' => intval(psm_POST('port', 0)), 'type' => psm_POST('type', ''), 'pattern' => psm_POST('pattern', ''), 'warning_threshold' => intval(psm_POST('warning_threshold', 0)), 'active' => in_array($_POST['active'], array('yes', 'no')) ? $_POST['active'] : 'no', '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');
     // make sure websites start with http://
     if ($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
         $clean['ip'] = 'http://' . $clean['ip'];
     }
     // validate the lot
     $server_validator = new \psm\Util\Server\ServerValidator($this->db);
     try {
         if ($this->server_id > 0) {
             $server_validator->serverId($this->server_id);
         }
         $server_validator->label($clean['label']);
         $server_validator->type($clean['type']);
         $server_validator->ip($clean['ip'], $clean['type']);
         $server_validator->warningThreshold($clean['warning_threshold']);
     } catch (\InvalidArgumentException $ex) {
         $this->addMessage(psm_get_lang('servers', 'error_' . $ex->getMessage()), 'error');
         return $this->executeEdit();
     }
     // check for edit or add
     if ($this->server_id > 0) {
         // edit
         $this->db->save(PSM_DB_PREFIX . 'servers', $clean, array('server_id' => $this->server_id));
         $this->addMessage(psm_get_lang('servers', 'updated'), 'success');
     } else {
         // add
         $clean['status'] = 'on';
         $this->server_id = $this->db->save(PSM_DB_PREFIX . 'servers', $clean);
         // server has been added, re-encrypt
         if (!empty($_POST['website_password'])) {
             $cleanWebsitePassword = array('website_password' => psm_password_encrypt($this->server_id . psm_get_conf('password_encrypt_key'), psm_POST('website_password')));
             $this->db->save(PSM_DB_PREFIX . 'servers', $cleanWebsitePassword, array('server_id' => $this->server_id));
         }
         $this->addMessage(psm_get_lang('servers', 'inserted'), 'success');
     }
     // update users
     $user_idc = psm_POST('user_id', array());
     $user_idc_save = array();
     foreach ($user_idc as $user_id) {
         $user_idc_save[] = array('user_id' => intval($user_id), 'server_id' => intval($this->server_id));
     }
     $this->db->delete(PSM_DB_PREFIX . 'users_servers', array('server_id' => $this->server_id));
     if (!empty($user_idc_save)) {
         // add all new users
         $this->db->insertMultiple(PSM_DB_PREFIX . 'users_servers', $user_idc_save);
     }
     $back_to = isset($_GET['back_to']) ? $_GET['back_to'] : 'index';
     if ($back_to == 'view') {
         return $this->runAction('view');
     } else {
         return $this->runAction('index');
     }
 }
Ejemplo n.º 9
0
$includes = glob(PSM_PATH_INC . '*.inc.php');
foreach ($includes as $file) {
    include_once $file;
}
// init db connection
$db = new psm\Service\Database();
// sanity check!
if (!defined('PSM_INSTALL') || !PSM_INSTALL) {
    if ($db->getDbHost() === null) {
        // no config file has been loaded, redirect the user to the install
        header('Location: install.php');
        die;
    }
    // config file has been loaded, check if we have a connection
    if (!$db->status()) {
        die('Unable to establish database connection...');
    }
    // attempt to load configuration from database
    if (!psm_load_conf()) {
        // unable to load from config table
        header('Location: install.php');
        die;
    }
    // config load OK, make sure database version is up to date
    $installer = new \psm\Util\Install\Installer($db);
    if ($installer->isUpgradeRequired()) {
        die('Your database is for an older version and requires an upgrade, <a href="install.php">please click here</a> to update your database to the latest version.');
    }
}
$lang = psm_get_conf('language', 'en_US');
psm_load_lang($lang);
Ejemplo n.º 10
0
 /**
  * Create the HTML code for the module.
  *
  * If XHR is on, no main template will be added.
  *
  * @param string $html HTML code to add to the main body
  * @return \Symfony\Component\HttpFoundation\Response
  */
 protected function createHTML($html = null)
 {
     if (!$this->xhr) {
         // in XHR mode, we will not add the main template
         $tpl_data = array('title' => strtoupper(psm_get_lang('system', 'title')), 'label_back_to_top' => psm_get_lang('system', 'back_to_top'), 'add_footer' => $this->add_footer, 'version' => 'v' . PSM_VERSION, 'messages' => $this->getMessages(), 'html_content' => $html);
         // add menu to page?
         if ($this->add_menu) {
             $tpl_data['html_menu'] = $this->createHTMLMenu();
         }
         // add header accessories to page ?
         if ($this->header_accessories) {
             $tpl_data['header_accessories'] = $this->header_accessories;
         }
         // add modal dialog to page ?
         if (sizeof($this->modal)) {
             $html_modal = '';
             foreach ($this->modal as $modal) {
                 $html_modal .= $modal->createHTML();
             }
             $tpl_data['html_modal'] = $html_modal;
         }
         // add sidebar to page?
         if ($this->sidebar !== null) {
             $tpl_data['html_sidebar'] = $this->sidebar->createHTML();
         }
         if (psm_update_available()) {
             $tpl_data['update_available'] = str_replace('{version}', 'v' . psm_get_conf('version_update_check'), psm_get_lang('system', 'update_available'));
         }
         if ($this->black_background) {
             $tpl_data['body_class'] = 'black_background';
         }
         $html = $this->twig->render('main/body.tpl.html', $tpl_data);
     }
     $response = new Response($html);
     return $response;
 }
Ejemplo n.º 11
0
if (!empty($_SERVER['argv'])) {
    foreach ($_SERVER['argv'] as $argv) {
        $argi = explode('=', ltrim($argv, '--'));
        if (count($argi) !== 2) {
            continue;
        }
        switch ($argi[0]) {
            case 'uri':
                define('PSM_BASE_URL', $argi[1]);
                break;
            case 'timeout':
                $cron_timeout = intval($argi[1]);
                break;
        }
    }
}
// prevent cron from running twice at the same time
// however if the cron has been running for X mins, we'll assume it died and run anyway
// if you want to change PSM_CRON_TIMEOUT, have a look in src/includes/psmconfig.inc.php.
// or you can provide the --timeout=x argument
$time = time();
if (psm_get_conf('cron_running') == 1 && $cron_timeout > 0 && $time - psm_get_conf('cron_running_time') < $cron_timeout) {
    die('Cron is already running. Exiting.');
}
if (!defined('PSM_DEBUG') || !PSM_DEBUG) {
    psm_update_conf('cron_running', 1);
}
psm_update_conf('cron_running_time', $time);
$autorun = $router->getService('util.server.updatemanager');
$autorun->run(true);
psm_update_conf('cron_running', 0);
Ejemplo n.º 12
0
 /**
  * Check the current server as a website
  * @param int $max_runs
  * @param int $run
  * @return boolean
  */
 protected function updateWebsite($max_runs, $run = 1)
 {
     $starttime = microtime(true);
     // We're only interested in the header, because that should tell us plenty!
     // unless we have a pattern to search for!
     $curl_result = psm_curl_get($this->server['ip'], true, $this->server['pattern'] == '' ? false : true, $this->server['timeout'], true, $this->server['website_username'], psm_password_decrypt($this->server['server_id'] . psm_get_conf('password_encrypt_key'), $this->server['website_password']));
     $this->rtime = microtime(true) - $starttime;
     // the first line would be the status code..
     $status_code = strtok($curl_result, "\r\n");
     // keep it general
     // $code[1][0] = status code
     // $code[2][0] = name of status code
     $code_matches = array();
     preg_match_all("/[A-Z]{2,5}\\/\\d\\.\\d\\s(\\d{3})\\s(.*)/", $status_code, $code_matches);
     if (empty($code_matches[0])) {
         // somehow we dont have a proper response.
         $this->error = 'TIMEOUT ERROR: no response from server';
         $result = false;
     } else {
         $code = $code_matches[1][0];
         $msg = $code_matches[2][0];
         // All status codes starting with a 4 or higher mean trouble!
         if (substr($code, 0, 1) >= '4') {
             $this->error = "HTTP STATUS ERROR: " . $code . ' ' . $msg;
             $result = false;
         } else {
             $result = true;
             //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.';
                     $result = false;
                 }
             }
         }
     }
     // check if server is available and rerun if asked.
     if (!$result && $run < $max_runs) {
         return $this->updateWebsite($max_runs, $run + 1);
     }
     return $result;
 }
Ejemplo n.º 13
0
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP Server Monitor.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     phpservermon
 * @author      Pepijn Over <*****@*****.**>
 * @copyright   Copyright (c) 2008-2014 Pepijn Over <*****@*****.**>
 * @license     http://www.gnu.org/licenses/gpl.txt GNU GPL v3
 * @version     Release: @package_version@
 * @link        http://www.phpservermonitor.org/
 **/
// include main configuration and functionality
require_once dirname(__FILE__) . '/../src/bootstrap.php';
if (!psm_is_cli()) {
    die('This script can only be run from the command line.');
}
// prevent cron from running twice at the same time
// however if the cron has been running for X mins, we'll assume it died and run anyway
// if you want to change PSM_CRON_TIMEOUT, have a look in src/includes/psmconfig.inc.php.
$time = time();
if (psm_get_conf('cron_running') == 1 && $time - psm_get_conf('cron_running_time') < PSM_CRON_TIMEOUT) {
    die('Cron is already running. Exiting.');
}
if (!defined('PSM_DEBUG') || !PSM_DEBUG) {
    psm_update_conf('cron_running', 1);
}
psm_update_conf('cron_running_time', $time);
$autorun = new \psm\Util\Server\UpdateManager($db);
$autorun->run();
psm_update_conf('cron_running', 0);