The current load $loadInfo[0], the 5-minute-old load average $loadInfo[5], and the 15-minute-old load average $loadInfo[15]are stored separately in $load0, $load5, and $load15, respectiv
Trang 1if (preg_match(‘/To:\s*(.+)/i’,
$str,
$match)) {
$to = $match[1];
} else if (preg_match(‘/From:\s*(.+)/i’,
$str,
$match)) {
array_push($headers, “From: $match[1] \r\n”);
} else if (preg_match(‘/Subject:\s*(.+)/i’,
$str,
$match)) {
$subject = $match[1];
} else if (preg_match(‘/^CC:\s*(.+)/i’,
$str,
$match)) {
array_push($headers, “Cc: $match[1] \r\n”);
} else if (preg_match(‘/Bcc:\s*(.+)/i’,
$str,
$match)) {
array_push($headers, “Bcc: $match[1] \r\n”);
} else if (preg_match(‘/Content-Type:\s*(.+)/i’,
$str,
$match)) {
if (preg_match(‘/html/’, $match[1])) {
array_push($headers,
“Content-Type: text/html\r\n”);
} else { array_push($headers,
“Content-Type: text/plain\n”);
}
Continued
Trang 2Listing 16-14(Continued)
$contentTypeSet = TRUE;
} else if (preg_match(‘/MIME-Version:\s*(.+)/i’,
$str,
$match)) {
array_push($headers,
“MIME-Version: $match[1] \r\n”);
} else { array_push($message, $str);
} }
if (! $contentTypeSet) array_push($headers,
“Content-Type: text/plain\r\n”);
$body = implode(‘’, $message);
$search = array_keys($info);
$replace = array_values($info);
$body = preg_replace($search, $replace, $body);
$subject = preg_replace($search,
$replace,
$subject);
$headerStr = implode(‘’, $headers);
if (DEBUG) echo “Sending mail to: $to “.
“(subject: $subject)\n”;
mail($to, $subject, $body, $headerStr);
}
?>
The loadmonitor script works as follows:
1 It creates a sysinfoobject called $system
2 The $systemobject is used to retrieve host name, IP address, and kernel information into an associative array called $alertInfo The $alertInfo
Trang 3stores the information using custom tags as keys For example, <%HOST%>
stores the host name returned by the $system->chostname()method
3 The load average information is retrieved by calling the loadavg() method of the $systemobject The returned load average array is stored
in the $loadInfoarray The current load $loadInfo[0], the 5-minute-old load average $loadInfo[5], and the 15-minute-old load average
$loadInfo[15]are stored separately in $load0, $load5, and $load15, respectively
4 For each known alert condition, the script compares the current,
5-minute-old, and 15-minute-old loads against the alert condition’s maximum load average, stored in $alertRange If the alert condition’s maximum load is less than or equal to the current, last 5-minute, and last 15-minute load average, the system can be said to be in that alert status for the last 15 minutes The three points in time serve as three samples of time in that 15-minute interval, which is sufficient for our purposes to make the assumption that the system has entered an alert condition
5 However, to find the highest alert condition met by the system, the system
keeps track of matched alert conditions using the $highestAlertand
$highestAlertRangevariables These variables are set only if the current alert condition is higher than the last one
6 Finally, if there is an alert condition set in $highestAlert, the
$alertInfo[‘/<%ALERT%>/’]entry is set to $highestAlert;and the PS_BIN program is run with PS_OPT options using the execute_pro-gram() method found in the common_functions.php script
7 The output of the PS_BIN(i.e., the top command) is stored in
$alertInfo[‘/<%PSAUX%>/’]
8 Finally, the isOKtoSendMail ()function is called to determine whether the current alert condition has been already reported within the alert mail control interval If it has not been reported yet or was reported before the interval expired, the sendAlert()method is called to send the e-mail alert
The loadmonitor tool has the following methods
isOKtoSendMail ()
This function determines if an alert mail message should be sent in the event of an alert condition This check is performed to ensure that the administrator is not sent repetitive alert messages too frequently if the loadmonitor is run frequently via cron or other means
This function is passed the mail control file name and the alert mail frequency, both of which are configured in loadmonitor.conf The isOKtoSendMail()function
Trang 4loads the previous mailing’s timestamp stored in the mail control file (if any) and compares it against the current time
If the difference between the current time and the last alert message timestamp
is greater than or equal to the allowed interval, it returns TRUEand updates the mail control file with the current time Otherwise, it returns FALSE
writeControlFile ()
This function writes the mail control file The mail control file stores the last time
an alert mail was sent This timestamp is used to ensure that alert messages do not bombard the administrator’s mailbox if the loadmonitor is run frequently
sendAlert()
See the sendAlert() function details in the hdmonitor section above
Listing 16-15 shows the default e-mail template:
Listing 16-15: loadmonitor_mail.txt
To: kabir@example.com From: THE LOAD WATCHER <admin@example.com>
Subject: [ALERT-<%ALERT%>] load on <%HOST%> need your attention Content-Type: text/html
<html>
<body>
The <%HOST%> is experiencing <%ALERT%> level load.
<br> <br>
Current load averages are <%LOAD%>
<br> <br>
Here is the output of the top command:
<br>
<pre>
<font color=”<%ALERT%>”>
<%PSAUX%>
</font>
</pre>
<br>
Thanks,
<br>
The Wheel Group
<br>
</body>
</html>
Trang 5Installing the loadmonitor tool as a cron job
To set up loadmonitor.phpas a cron job under Linux, do the following:
1 As root, create a symbolic link in /etc/cron.dailyas follows:
ln -s /path/to/loadmonitor.php
For example, if you kept loadmonitor.phpand loadmonitor.confin the /usr/local/src/loadmonitordirectory, you could run the following commands as root to create the link:
cd /etc/cron.daily
ln -s /usr/local/src/loadmonitor/loadmonitor.php
2 Once the symlink is created, run /etc/cron.daily/loadmonitor.
phpas a test If you get an error message that loadmonitor.conf
was not found or that any of the classes (class.Linux.inc.php
or common_functions.php) were not found, you need to edit loadmonitor.phpto change require_once(‘loadmonitor.conf’), require_once(‘class.Linux.inc.php’), and require_once (‘common_functions.php’); to require_once(‘/path/to/
loadmonitor/loadmonitor.conf’), require_once(‘/path/to/
loadmonitor/class.Linux.inc.php), require_once(‘/path/
to/loadmonitor/common_functions.php’), respectively For our example case, this would be require_once(‘/usr/local/src/
loadmonitor/loadmonitor.conf’), require_once(‘/usr/local/
src/loadmonitor/class.Linux.inc.php’), and require_once (‘/usr/local/src/loadmonitor/common_functions.php ‘).
3 Make sure loadmonitor.phpis executable You can run:
chown root:root loadmonitor.* \
class.Linux.inc.php \ common_functions.php chmod 700 loadmonitor.php
from the directory of the script to allow root own and be able to execute the loadmonitor scripts If your cron daemon does not run as root, make sure you replace root:root with the appropriate user and group names that enable cron to execute the script
4 Configure the loadmonitor.conf file to set the load averages that you want
to consider as RED, YELLOW, and BLUE Set the mail frequency you want
to use
5 Let cron run the job at the regularly scheduled time; you should receive
excessive load alert messages when the load average exceeds the specified alert condition level