Archive for the 'JSP' Category

Web site builder - 590 Part IV: Using PHP for Sysadmin Tasks

Friday, March 28th, 2008

590 Part IV: Using PHP for Sysadmin Tasks Listing 16-11 (Continued) { array_push($headers, From: $match[1] rn ); } 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] rn ); } else if (preg_match( /Bcc:s*(.+)/i , $str, $match)) { array_push($headers, Bcc: $match[1] rn ); } else if (preg_match( /Content-Type:s*(.+)/i , $str, $match)) { if (preg_match( /html/ , $match[1])) { array_push($headers, Content-Type: text/htmlrn ); } else { array_push($headers, Content-Type: text/plainn ); } $contentTypeSet = TRUE; } else if (preg_match( /MIME-Version:s*(.+)/i , $str, $match)) { array_push($headers, MIME-Version: $match[1] rn ); } else { array_push($message, $str); } } if (! $contentTypeSet)

Free web space - Chapter 16: Command-Line PHP Utilities 589 $diskInfo =

Thursday, March 27th, 2008

Chapter 16: Command-Line PHP Utilities 589 $diskInfo = getDiskInfo($system->filesystems()); $alert = 0; foreach ($diskInfo as $mount => $currentPercent) { if (!empty($MAXSIZE[$mount]) && $MAXSIZE[$mount] <= $currentPercent ) { $alert++; $alertInfo[ /<%DISK_STATUS%>/ ] .= Filesystem: $mount exceeds limit. . Currently used: $currentPercent%n ; if (DEBUG) echo Filesystem: $mount exceeds limits.n ; } } if ($alert) sendAlert($alertInfo); exit; function sendAlert($info = null) { $lines = file($GLOBALS[ MAIL_TEMPLATE ]); $contentTypeSet = FALSE; $message = array(); $headers = array(); foreach ($lines as $str) { $index++; if (preg_match( /To:s*(.+)/i , $str, $match)) { $to = $match[1]; } else if (preg_match( /From:s*(.+)/i , $str, $match)) Continued

Web site optimization - 588 Part IV: Using PHP for Sysadmin Tasks

Wednesday, March 26th, 2008

588 Part IV: Using PHP for Sysadmin Tasks Listing 16-10 shows an example configuration file for this script. Listing 16-10: hdmonitor.conf Defined in the $MAXSIZE array are three mount points (partitions), which can be also written as follows: $MAXSIZE = array ( / => 30, /usr => 50, /mnt/win => 90 ); These three mount points will be monitored by the script when it is run daily via cron. Whenever any of these mount points exceed the usage percentage stated here, the $MAIL_TEMPLATE file is used to send mail to the e-mail addresses listed in this mail template. Listing 16-11 shows the monitoring script called hdmonitor.php. Listing 16-11: hdmonitor.php #!/usr/bin/php -q / ] = $system->chostname(); $alertInfo[ /<%IP_ADDR%>/ ] = $system->ip_addr(); $alertInfo[ /<%KERNEL%>/ ] = $system->kernel(); $alertInfo[ /<%TODAY%>/ ] = date( M-d-Y h:i:s A );

Msn web hosting - Chapter 16: Command-Line PHP Utilities 587 You can

Wednesday, March 26th, 2008

Chapter 16: Command-Line PHP Utilities 587 You can see www.csus.com (California State University, Sacramento) appears to be located in Sacramento, CA, U.S., which makes sense. This script should be used from the command line as needed. However, if you wish to make it available to everyone, you can install it in /usr/bin, which is typically in any user s path. Here is how: 1. Make a directory called /usr/local/src/php/gelocator and copy the netgeo.php class into that directory. Make sure the directory is r+x by all users. In addition, make sure netgeo.php is readable by all users, but neither should be writable. 2. Copy geolocator.php into /usr/bin as geolocator. We remove the php extension because executable scripts typically do not need extensions. 3. Modify /usr/bin/geolocator such that require_once( netgeo.php ) now is require_once( /usr/local/src/php/geolocator/netgeo.php ). This will ensure that when users run geolocator from the command line, the /usr/bin/geolocator script will find the netgeo.php class. Now you and your users can run geolocator from anywhere. Note that the geolocator script is fairly accurate, but its output should not be used as a final (and perhaps critical) determination of a particular host s location. Building a Hard Disk Usage Monitoring Utility Now we will develop a hard disk usage monitoring tool that uses Linux proc file system information to determine hard disk usage, and if usage for a given mounted file system exceeds a specified percentage, the utility sends an e-mail message to the administrator. The script we will develop here requires the classLinux.inc.php and common_functions.php classes from the phpSysInfo project, which is located at http://phpsysinfo.sourceforge.net/project. Download the phpSysInfo project and you will find the common_ functions.php in the main distribution directory; the class.Linux. inc.phpcan be found in the os subdirectory within the includes directory.

586 Part IV: Using PHP for Sysadmin Tasks (Web hosting support)

Wednesday, March 26th, 2008

586 Part IV: Using PHP for Sysadmin Tasks This script requires the netgeo.php class. It works as follows: . It gets a list of IP addresses or host names from the command line using the getHostList() function. . For each given IP or host name, it performs netgeo lookup using findLocation(), which prints the geographic data available for the given IP or host name. Note that not all IP addresses or host names are in the netgeo database, so a result might not always be available. Here are some example runs of this script: $ ./geolocator.php www.yahoo.com www.yahoo.com: Approximate location: City : SUNNYVALE State :CALIFORNIA Country :US Longitude:122degree West Latitude:37.4degree North You can see that www.yahoo.com appears to be located in Sunnyvale, CA, U.S. Following is another example: $./geolocator.php www.amazon.com www.amazon.com:Approximate location: City : SEATTLE State :WASHINGTON Country :US Longitude:122.31degree West Latitude:47.55degree North In the preceding example, you can see that www.amazon.com appears to be located in Seattle, Washington, U.S. One last example: $./geolocator.php www.csus.edu www.csus.edu: Approximate location: City : SACRAMENTO State :CALIFORNIA Country :US Longitude:121.44degree West Latitude:38.57degree North

Chapter 16: Command-Line PHP Utilities 585 // Show (Make my own web site)

Tuesday, March 25th, 2008

Chapter 16: Command-Line PHP Utilities 585 // Show output echo $host : Approximate location:n ; if(IsSet($location[ CITY ]) || IsSet($location[ STATE ]) || IsSet($location[ COUNTRY ])) { if(IsSet($location[ CITY ])) echo City : . $location[ CITY ] . n ; if(IsSet($location[ STATE ])) echo State : . $location[ STATE ] . n ; if(IsSet($location[ COUNTRY ])) echo Country : . $location[ COUNTRY ] . n ; } echo Longitude: . ($longitude>=0.0 ? $longitude . degree East : (-$longitude). degree West ). n ; echo Latitude: . ($latitude>=0.0 ? $latitude . degree North : (-$latitude). degree South ). n ; } else { echo Cannot find location.n ; echo Error: .$netgeo->error. n ; } } function getHostList() { $arr = array(); // Except for the first argument in the command // line, insert all in a list as host/ip // Note: first argument is the name of the script. foreach($GLOBALS[ argv ] as $key => $value) { if ($key) array_push($arr, $value); } return $arr; } ?>

Web site developers - 584 Part IV: Using PHP for Sysadmin Tasks

Tuesday, March 25th, 2008

584 Part IV: Using PHP for Sysadmin Tasks This class uses The Internet Geographic Database, which maps IP addresses to physical world locations. To learn more about this, visit http://www.caida.org/ tools/utilities/netgeo. Listing 16-9 shows the geolocator.php script. Listing 16-9: geolocator.php #!/usr/bin/php -q GetAddressLocation($hostname,$location)) { // Set longitude and latitude from retrieved data $longitude=doubleval($location[ LONG ]); $latitude=doubleval($location[ LAT ]);

Chapter 16: Command-Line PHP Utilities 583 (Photography web hosting) For example,

Monday, March 24th, 2008

Chapter 16: Command-Line PHP Utilities 583 For example, say you kept reminder.php and reminder.conf in /usr/ local/src/reminder directory, you can run the following commands as root to create the link: cd /etc/cron.daily ln -s /usr/local/src/reminder/reminder.php 2. Once the symlink is created, run: /etc/cron.daily/reminder.php as a test. If you get an error message about reminder.conf not being found, you need to edit the reminder.php to change require_once ( reminder.conf ) to require_once( /path/to/reminder/ reminder.conf ). For our example case, this would be require_once( /usr/local/src/reminder/reminder.conf ). 3. Make sure reminder.php is executable. You can run chown root:root reminder.* chmod 700 reminder.php from the directory of the script to allow root to own and be able to execute the reminder 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. Now you can set up reminders in one or more user reminder directories (~username/remidners/reminders.txt) and create necessary mail files in the reminders directory. 5. Let cron run the job at the regularly scheduled time and you should receive reminders if you have set any for yourself. If you do not receive a reminder you expect to receive, check the ~usernmame/reminders/ username.log file. Also check /var/log/cron for possible file execute permission issues Building a Geo Location Finder Tool for IP Ever find an IP address in a log file that looked suspicious or interesting and you wanted to know from which part of the world that IP came? A trace route might give you clues but it is too much work to find geographic locations of an IP address. In this section, we will develop a simple script called geolocator.php using the netgeo.php class, which you can download from http://www.phpclasses.org/ netgeoclass.

582 Part IV: Using PHP for Sysadmin Tasks (Florida web design)

Monday, March 24th, 2008

582 Part IV: Using PHP for Sysadmin Tasks The default content type is set to text/plain in this function. Then the $body string is created by concatenating the lines in the $messages array using the implode() function. Both the $body and the $subject line are parsed for the <%TODAY%> tag, which is replaced with the current date and time. The $subject line is stored outside the $headers array even though it is a header too. This is done because the PHP mail() function requires the subject as a separate argument from the other headers. The same is true for the To: header, which is also stored outside $headers in $to . Finally, the $headers are imploded into $headerString, and the mail() function is called with all the necessary arguments. The mail is sent out. If the actual mail() function encounters any errors a log entry is added to that effect. getRemindersForToday () This function receives a list of lines (the contents of the reminders.txt file), and parses through each line to determine whether the line is a reminder configuration or should be ignored (blank or a comment line starting with the # character). Each reminder configuration line is compared against the current date (MM, DD, and weekday) values to determine whether any of the lines match a reminder for today. If a match is found, the reminder mail filename is added into the $reminders array, which is returned by the function. writeLog () This function writes a log file as ~username/reminders/username.log. The log entries are generated by other functions in the script. getUsers () This function loads the user list file (/etc/passwd) in an array, loops through each record, and finds the username (field position 0) and home directory (field position 5) from each line separated by colons. It stores each username and home directory in an associative array called $user and returns it. Installing the reminder tool as a cron job To set up reminder.php as a cron job under Linux, do the following: 1. As root, create a symbolic link in /etc/cron.daily as follows: ln -s /path/to/reminder.php

Chapter 16: Command-Line PHP Utilities 581 doRemind() This (Make my own web site)

Sunday, March 23rd, 2008

Chapter 16: Command-Line PHP Utilities 581 doRemind() This function obtains four things: the user s reminder directory ($userDir, which is USER_REMINDER_DIR set in reminder.conf), the user reminder file name ($userFile, which is USER_REMINDER_FILE set in reminder.conf), $username, and the user s home directory ($homeDir). It first determines whether the user has a reminder file in the reminder directory inside the user s home directory. If there is no reminder configuration (reminders.txt) file, the function returns. That ends the user s reminder processing. However, because the user does have a reminders.txt file in the reminders directory, the getRemindersForToday() function is called see if any of the reminders are meant for the current day. The getRemindersForToday() method parses the reminders.txt file and if any reminder matches, it returns the associated reminder mail file. For example, suppose a user has the following configuration in reminders.txt: weekly:mon:my_monday_tasklist.txt weekly:tue:my_tuesday_tasklist.txt weekly:wed:my_wednesday_tasklist.txt weekly:thu:my_thursday_tasklist.txt weekly:fri:my_friday_tasklist.txt weekly:sat:my_saturday_plans.txt weekly:sun:my_sunday_plans.txt Whenever the reminder is run, one of the weekly reminders will match, as the user has a weekly reminder for each day. The matching reminder mail file will be returned in an array called $mailings by the getRemindersForToday() function. For each of the entries in $mailings, the doMail() method is called only if the mail file exists. In other words, if it is Monday, the preceding configuration will return $mailings = array( monday_tasklist.txt ). If ~username/reminders/ monday_tasklist.txt exists, then doMail() will send out the mail. In the case of a missing file, log entries will be created. The log is later written to the ~username/reminders/username.log file so that the user can review it and fix the configuration file or create missing mail files. doMail() This function sends the mail out. It receives a reminder mail filename, which exists in the ~username/reminders directory. The function loads the file into an array called $lines using the file() function. Each line is parsed for mail headers, such as To:, From:, Cc:, Bcc:, Subject:, and Content-Type:. These headers are stored in appropriate format in the $headers array. The other lines are considered part of the body of the message, and are stored in the $messages array.