Apache Security - Ivan Ristic [122]
. ":" . $results{"IdleWorkers"}
. ":" . $results{"s_ _"}
. ":" . $results{"s_s"}
. ":" . $results{"s_r"}
. ":" . $results{"s_w"}
. ":" . $results{"s_k"}
. ":" . $results{"s_d"}
. ":" . $results{"s_c"}
. ":" . $results{"s_l"}
. ":" . $results{"s_g"}
. ":" . $results{"s_i"}
);
Graphing
Creating graphs from the information stored in the RRD file is the really fun part of the operation. Everyone loves the RRDtool because no skills are required to produce fabulous graphs. For example, the Perl code below creates a graph of the number of active and idle servers throughout a designated time period, such as the third graph shown in Figure 8-3. The graph is stored in a file specified by $pic_name.
RRDs::graph($pic_name,
"-v Servers",
"-s $start_time",
"-e $end_time",
# extracts the busyWorkers field from the RRD file
"DEF:busy=$rrd_name:busyWorkers:AVERAGE",
# extracts the idleWorkers field from the RRD file
"DEF:idle=$rrd_name:idleWorkers:AVERAGE",
# draws a filled area in blue
"AREA:busy#0000ff:Busy servers",
# draws a line in green
"LINE2:idle#00ff00:Idle servers"
);
Figure 8-3. Graphs representing web server activity
I decided to create four graphs out of the available data:
Hits per second
Bytes transferred per second
Active and idle servers (workers in Apache 2 terminology)
Process activity (scoreboard)
The graphs are shown in Figure 8-3. You may want to create other graphs, such as ones showing the uptime and the CPU load. Note: The live view of the web server statistics for http://apache.org are available at http://www.apachesecurity.net/stats/, where they will remain for as long as the Apache Foundation keeps their mod_status output public.
Using the scripts
Two scripts, parts of which were shown above, are used to record the statistics and create graphs. Both are available from the web site for this book. One script, apache-monitor, fetches statistics from a server and stores them. It expects two parameters. The first specifies the (RRD) file in which the results should be stored, and the second specifies the web page from which server statistics are obtained. Here is a sample invocation:
$ apache-monitor /var/www/stats/apache.org http://www.apache.org/server-status/
For a web page that requires a username and password, you can embed these directly in the URL (e.g., http://username:password@www.example.com/server-status/). The script is smart enough to create a new RRD file if one does not exist. To get detailed statistics of the web server activity, configure cron to execute this script once a minute.
The second script, apache-monitor-graph, draws graphs for a given RRD file. It needs to know the path to the RRD file (given as the first parameter), the output folder (the second parameter), and the duration in seconds for the time period the graphs need to cover (the third parameter). The script calculates the starting time by deducting the given duration from the present time. The following invocation will create graphs for the last six hours:
$ apache-monitor-graph /var/www/stats/apache.org /var/www/stats/ 21600
Four files will be created and stored in the output folder, each showing a single graph:
$ cd /var/www/stats
$ ls
apache.org_servers-21600.gif
apache.org_hits-21600.gif
apache.org_transfer-21600.gif
apache.org_scoreboard-21600.gif
You will probably want to create several graphs to monitor the activity over different time periods. Use the values in seconds from Table 8-9.
Table 8-9. Duration of frequently used time periods
Period
Value in seconds
Hour
3600
Six hours
21600
Day
86400
Week
604800
Month
2592000
Year
31536000
Calling the graphing script every five minutes is sufficient. Having created the graphs, you only need to create some HTML code to glue them together if you want to show multiple graphs on a single page (see Figure 8-3).
* * *
Warning
The mod_status output is useful, but the figures it offers can be unreliable under some circumstances, making this approach