[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.monitor.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.monitor.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  require_once("DB.php");
  32  require_once (APP_INC_PATH . "class.misc.php");
  33  
  34  class Monitor
  35  {
  36      /**
  37       * Checks the mail queue logs for any email that wasn't delivered.
  38       *
  39       * @access  public
  40       */
  41      function checkMailQueue()
  42      {
  43          // try to connect to the database
  44          $dsn = array(
  45              'phptype'  => APP_SQL_DBTYPE,
  46              'hostspec' => APP_SQL_DBHOST,
  47              'database' => APP_SQL_DBNAME,
  48              'username' => APP_SQL_DBUSER,
  49              'password' => APP_SQL_DBPASS
  50          );
  51          $dbh = DB::connect($dsn);
  52  
  53          $limit = 10;
  54          $stmt = "SELECT
  55                      maq_id,
  56                      COUNT(mql_id) total_tries
  57                   FROM
  58                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "mail_queue,
  59                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "mail_queue_log
  60                   WHERE
  61                      maq_status='error' AND
  62                      maq_id=mql_maq_id
  63                   GROUP BY
  64                      maq_id";
  65          $queue_ids = $dbh->getCol($stmt);
  66          if (count($queue_ids) > 0) {
  67              echo "ERROR: There is a total of " . count($queue_ids) . " queued emails that were not sent yet.\n";
  68          }
  69      }
  70  
  71  
  72      /**
  73       * Checks the free disk space status on the server.
  74       *
  75       * @access  public
  76       */
  77      function checkDiskspace($partition)
  78      {
  79          $low_limit = 5;
  80          $high_limit = 15;
  81          $total_space = disk_total_space($partition);
  82          $free_space = disk_free_space($partition);
  83          $free_percentage = ($free_space * 100) / $total_space;
  84          if ($free_percentage < $low_limit) {
  85              echo ev_gettext("ERROR: Almost no free disk space left (percentage left") . ": $free_percentage)\n";
  86              exit;
  87          }
  88          if ($free_percentage < $high_limit) {
  89              echo ev_gettext("ERROR: Free disk space left is getting very low (percentage left") . ": $free_percentage)\n";
  90          }
  91      }
  92  
  93  
  94      /**
  95       * Checks on the status of the required configuration and auxiliary files
  96       * and directories.
  97       *
  98       * @access  public
  99       * @param   array $required_files An array of files that should be checked on.
 100       */
 101      function checkConfiguration($required_files)
 102      {
 103          foreach ($required_files as $file_path => $options) {
 104              // check if file exists
 105              if (!file_exists($file_path)) {
 106                  echo ev_gettext("ERROR: File could not be found (path") . ": $file_path)\n";
 107                  continue;
 108              }
 109              // check the owner and group for these files
 110              list($owner, $group) = Monitor::_getOwnerAndGroup($file_path);
 111              if ((@$options['check_owner']) && ($options['owner'] != $owner)) {
 112                  echo ev_gettext('ERROR: File owner mismatch (path: %1$s; current owner: %2$s; correct owner: %3$s)', $file_path, $owner, $options['owner']) . "\n";
 113              }
 114              if ((@$options['check_group']) && ($options['group'] != $group)) {
 115                  echo ev_gettext('ERROR: File group mismatch (path: %1$s; current group: %2$s; correct group: %3$s)', $file_path, $group, $options['group']) . "\n";
 116              }
 117              // check permission bits
 118              $perm = Monitor::_getOctalPerms($file_path);
 119              if ((@$options['check_permission']) && ($options['permission'] != $perm)) {
 120                  echo ev_gettext('ERROR: File permission mismatch (path: %1$s; current perm: %2$s; correct perm: %3$s)', $file_path, $perm, $options['permission']) . "\n";
 121              }
 122              // check filesize
 123              if ((@$options['check_filesize']) && (filesize($file_path) < $options['filesize'])) {
 124                  echo ev_gettext('ERROR: File size mismatch (path: %1$s; current filesize: %2$s)', $file_path, filesize($file_path)) . ")\n";
 125              }
 126          }
 127          $required_directories = array(
 128              APP_PATH . 'misc/routed_emails' => array(
 129                  'check_permission' => true,
 130                  'permission'       => 770,
 131              ),
 132              APP_PATH . 'misc/routed_notes' => array(
 133                  'check_permission' => true,
 134                  'permission'       => 770,
 135              ),
 136              APP_PATH . 'setup' => array(
 137                  'check_permission' => true,
 138                  'permission'       => 100,
 139              ),
 140          );
 141          foreach ($required_directories as $dir_path => $options) {
 142              // check if directory exists
 143              if (!file_exists($dir_path)) {
 144                  echo ev_gettext('ERROR: Directory could not be found (path: %1$s)', $dir_path) . "\n";
 145                  continue;
 146              }
 147              // check permission bits
 148              $perm = Monitor::_getOctalPerms($dir_path);
 149              if ((@$options['check_permission']) && ($options['permission'] != $perm)) {
 150                  echo ev_gettext('ERROR: Directory permission mismatch (path: %1$s; current perm: %2$s; correct perm: %3$s)', $dir_path, $perm, $options['permission']) . "\n";
 151              }
 152          }
 153      }
 154  
 155  
 156      /**
 157       * Checks on the status of the MySQL database.
 158       *
 159       * @access  public
 160       */
 161      function checkDatabase()
 162      {
 163          // try to connect to the database
 164          $dsn = array(
 165              'phptype'  => APP_SQL_DBTYPE,
 166              'hostspec' => APP_SQL_DBHOST,
 167              'database' => APP_SQL_DBNAME,
 168              'username' => APP_SQL_DBUSER,
 169              'password' => APP_SQL_DBPASS
 170          );
 171          $dbh = DB::connect($dsn);
 172          if (PEAR::isError($dbh)) {
 173              echo ev_gettext("ERROR: Could not connect to the mysql database. Detailed error message:") . "\n\n";
 174              echo $dbh->getMessage() . '/' .  $dbh->getDebugInfo();
 175          } else {
 176              $required_tables = array(
 177                  "custom_field",
 178                  "custom_field_option",
 179                  "custom_filter",
 180                  "customer_account_manager",
 181                  "customer_note",
 182                  "email_account",
 183                  "email_draft",
 184                  "email_draft_recipient",
 185                  "email_response",
 186                  "faq",
 187                  "faq_support_level",
 188                  "group",
 189                  "history_type",
 190                  "irc_notice",
 191                  "issue",
 192                  "issue_association",
 193                  "issue_attachment",
 194                  "issue_attachment_file",
 195                  "issue_checkin",
 196                  "issue_custom_field",
 197                  "issue_history",
 198                  "issue_quarantine",
 199                  "issue_requirement",
 200                  "issue_user",
 201                  "issue_user_replier",
 202                  "mail_queue",
 203                  "mail_queue_log",
 204                  "news",
 205                  "note",
 206                  "phone_support",
 207                  "project",
 208                  "project_category",
 209                  "project_custom_field",
 210                  "project_email_response",
 211                  "project_group",
 212                  "project_news",
 213                  "project_phone_category",
 214                  "project_priority",
 215                  "project_release",
 216                  "project_round_robin",
 217                  "project_status",
 218                  "project_status_date",
 219                  "project_user",
 220                  "reminder_action",
 221                  "reminder_action_list",
 222                  "reminder_action_type",
 223                  "reminder_field",
 224                  "reminder_history",
 225                  "reminder_level",
 226                  "reminder_level_condition",
 227                  "reminder_operator",
 228                  "reminder_priority",
 229                  "reminder_requirement",
 230                  "reminder_triggered_action",
 231                  "resolution",
 232                  "round_robin_user",
 233                  "search_profile",
 234                  "status",
 235                  "subscription",
 236                  "subscription_type",
 237                  "support_email",
 238                  "support_email_body",
 239                  "time_tracking",
 240                  "time_tracking_category",
 241                  "user"
 242              );
 243              // add the table prefix to all of the required tables
 244              $required_tables = Misc::array_map_deep($required_tables, array('Monitor', '_add_table_prefix'));
 245              // check if all of the required tables are really there
 246              $stmt = "SHOW TABLES";
 247              $table_list = $dbh->getCol($stmt);
 248              for ($i = 0; $i < count($required_tables); $i++) {
 249                  if (!in_array($required_tables[$i], $table_list)) {
 250                      echo "ERROR: Could not find required table '" . $required_tables[$i] . "'\n";
 251                  }
 252              }
 253          }
 254      }
 255  
 256  
 257      /**
 258       * Checks on the status of the IRC bot.
 259       *
 260       * @access  public
 261       */
 262      function checkIRCBot()
 263      {
 264          // check if any bot.php process is still running (lame, but oh well)
 265          ob_start();
 266          passthru("ps -ef | grep '[-]q bot.php'");
 267          $contents = ob_get_contents();
 268          ob_end_clean();
 269          $lines = explode("\n", $contents);
 270          if (count($lines) <= 1) {
 271              echo ev_gettext("ERROR: Could not find IRC bot pid from process list.") . "\n";
 272          }
 273      }
 274  
 275  
 276      /**
 277       * Method used by the code that checks if the required tables
 278       * do exist in the appropriate database. It returns the given
 279       * table name prepended with the appropriate table prefix.
 280       *
 281       * @access  private
 282       * @param   string $table_name The table name
 283       * @return  string The table name with the prefix added to it
 284       */
 285      function _add_table_prefix($table_name)
 286      {
 287          return APP_TABLE_PREFIX . $table_name;
 288      }
 289  
 290  
 291      /**
 292       * Returns the owner and group name for the given file.
 293       *
 294       * @access  private
 295       * @param   string $file The full path to the file
 296       * @return  array The owner and group name associated with that file
 297       */
 298      function _getOwnerAndGroup($file)
 299      {
 300          $owner_info = posix_getpwuid(fileowner($file));
 301          $group_info = posix_getgrgid(filegroup($file));
 302          return array(
 303              $owner_info['name'],
 304              $group_info['name']
 305          );
 306      }
 307  
 308  
 309      /**
 310       * Returns the octal permission string for a given file.
 311       *
 312       * @access  private
 313       * @param   string $file The full path to the file
 314       * @return  string The octal permission string
 315       */
 316      function _getOctalPerms($file)
 317      {
 318          return substr(sprintf("%o", fileperms($file)), -3);
 319      }
 320  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7