1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P126 pdf

5 149 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 84,46 KB

Nội dung

Listing 16-13 shows a sample configuration file for the load monitoring tool. Listing 16-13: loadmonitor.conf <?php define(DEBUG, TRUE); $ALERT_CONDITIONS = array(‘RED’, ‘YELLOW’, ‘BLUE’); // When system load has been in any of the following // alert condition for at least last 15 minutes // system is said to be at that alert level // Note: the order does not matter as loadmonitor // automatically chooses the highest alert level $ALERT[‘RED’] = 25; $ALERT[‘YELLOW’] = 15; $ALERT[‘BLUE’] = 5; $MAIL_TEMPLATE = ‘loadmonitor_mail.txt’; // Mail every n seconds when alert condition exists // Mail is sent only if the loadmonitor is run // every n-1 seconds. Recommend running loadmonitor // every 5 min and mail frequency 30 min (30 * 60 sec) // 0 = always send mail $MAIL_FREQUENCY = 30 * 60; // Store when was the last time mail was sent // in a file so that we don’t mail bomb the // admin $MAIL_CONTROL_FILE = ‘/tmp/loadmonitor.out’; // Include this program output in mail $PS_BIN = ‘top’; $PS_OPT = ‘ -b -n 1’; ?> Here we have defined $ALERT_CONDITIONS as an array of alerts. Currently, there are three alert conditions: RED, YELLOW, BLUE. These names are arbitrary and you can choose any name you like. An associative array called $ALERT defines the load average associated with each alert level. For example, alert condition RED is set to load average 25. This means that a RED alert is sent out when the system is experiencing a load average of 25 for at least the last 15 minutes until the current time. 596 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 596 Similarly, a load average of 15 triggers an alert condition YELLOW, and a load average of 5 triggers a BLUE alert. Whenever an alert condition is reached, the $MAIL_TEMPLATE file is used to send out the alert message. However, we do not want to bombard an administrator about an alert condition that exists among multiple runs of the loadmonitor script. For example, if loadmonitor were run every minute, it could note an alert condition and message the administrator every minute. This would be bad, so we can use $MAIL_FREQUENCY to control how frequently an administrator is notified when an alert condition is identified. The sample setting is 1800, which is 30 minutes. Therefore, we only resend notification about an alert condition if the last message was sent at least 30 minutes ago. The $MAIL_CONTROL_FILE keeps the timestamp information for each notification sent. This information is used to determine the last delivery time of the alert mail. The $PS_BIN and $PS_OPT are set to the top command and its argument -b and -n 1. The output of the top command is appended in the mail message to enable an administrator get a glimpse of the trou- bled system just from the e-mail. This is very useful if the main administrator receives the alert from a remote location; he or she can guide on-site junior admin- istrators to appropriately deal with the situation based on the information provided by the top command. Listing 16-14 shows the loadmonitor script. Listing 16-14: loadmonitor.php #!/usr/bin/php -q <?php require_once(‘loadmonitor.conf’); require_once(‘class.Linux.inc.php’); require_once(‘common_functions.php’); $alertInfo = array(); $system = new sysinfo; $alertInfo[‘/<%HOST%>/’] = $system->chostname(); $alertInfo[‘/<%IP_ADDR%>/’] = $system->ip_addr(); $alertInfo[‘/<%KERNEL%>/’] = $system->kernel(); $alertInfo[‘/<%TODAY%>/’] = date(‘M-d-Y h:i:s A’); $loadInfo = $system->loadavg(); $load0 = $loadInfo[0]; $load5 = $loadInfo[1]; $load15 = $loadInfo[2]; $alertInfo[‘/<%LOAD%>/’] = “Now: $load0 “. “Last 5 Min: $load5”. “Last 15 Min: $load15”; Continued Chapter 16: Command-Line PHP Utilities 597 21 549669 ch16.qxd 4/4/03 9:27 AM Page 597 Listing 16-14 (Continued) $highestAlertRange = 0; $highestAlert = null; foreach ($ALERT_CONDITIONS as $alertType) { $alertRange = $ALERT[$alertType]; if (DEBUG) echo “Alert: $alertRange => “. “Current $load0 $load5 $load15\n”; if ( ($alertRange <= $load0) && ($alertRange <= $load5) && ($alertRange <= $load15) ) { if (DEBUG) echo “Alert: $alertType ($alertRange)”. “ as load for last 15 min till “. “ now is $load15 $load0 \n”; if ($alertRange > $highestAlertRange) { $highestAlertRange = $alertRange; $highestAlert = $alertType; } } } if ($highestAlert != null) { if (DEBUG) echo “Highest alert $highestAlert \n”; $alertInfo[‘/<%ALERT%>/’] = $highestAlert; $ps =execute_program($PS_BIN, $PS_OPT); $alertInfo[‘/<%PSAUX%>/’] = $ps; // Find out if last mail sent was // within mail frequency range // or not, if not send mail if (isOKtoSendMail($MAIL_CONTROL_FILE, $MAIL_FREQUENCY)) { 598 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 598 sendAlert($alertInfo); } } exit; function isOKtoSendMail($ctrlFile = null, $interval = null) { $now = time(); if (DEBUG) echo “Now: “ . date(‘M-d-Y h:i:s A’, $now) . “\n”; if (file_exists($ctrlFile)) { // Read file $lastTime = file($ctrlFile); if (DEBUG) echo “Last time mail sent: “ . date(‘M-d-Y h:i:s A’, $lastTime[0]) . “\n”; } else { // If control file does not exist // Create one and yes we can send mail if (DEBUG) echo “Create new control file.\n”; writeControlFile($ctrlFile); return TRUE; } // If current time - last time is greater than // or equal to the mail interval, we can send mail if ($now - $lastTime[0] >= $interval) { // Update file if (DEBUG) echo “$now - $lastTime[0] => $interval\n”; writeControlFile($ctrlFile); return TRUE; } Continued Chapter 16: Command-Line PHP Utilities 599 21 549669 ch16.qxd 4/4/03 9:27 AM Page 599 Listing 16-14 (Continued) // No cannot send mail as we already // did not too long ago return FALSE; } function writeControlFile($file = null) { $now = time(); $fp = fopen($file, ‘w’); if ($fp) { if (DEBUG) echo “Writing control $file: $now\n”; fputs($fp, $now); fclose($fp); return TRUE; } else { echo “Error: could not create “. “control file $file \n”; } return FALSE; } function sendAlert($info = null) { $lines = file($GLOBALS[‘MAIL_TEMPLATE’]); $contentTypeSet = FALSE; $message = array(); $headers = array(); foreach ($lines as $str) { $index++; 600 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 600 . script. Listing 16-14: loadmonitor .php #!/usr/bin /php -q < ?php require_once(‘loadmonitor.conf’); require_once(‘class.Linux.inc .php ); require_once(‘common_functions .php ); $alertInfo = array(); $system. load average of 25 for at least the last 15 minutes until the current time. 596 Part IV: Using PHP for Sysadmin Tasks 21 549669 ch16.qxd 4/4/03 9:27 AM Page 596 Similarly, a load average of 15. a sample configuration file for the load monitoring tool. Listing 16-13: loadmonitor.conf < ?php define(DEBUG, TRUE); $ALERT_CONDITIONS = array(‘RED’, ‘YELLOW’, ‘BLUE’); // When system load

Ngày đăng: 07/07/2014, 07:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN