| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | #==================================================================== | ||
| 4 | # Script designed for Cacti http://www.raxnet.net/products/cacti/ | ||
| 5 | # Returns informations from the server status page of Apache | ||
| 6 | # | ||
| 7 | # You need to configure Apache to enable server-status : | ||
| 8 | # | ||
| 9 | # ExtendedStatus On | ||
| 10 | # <Location /server-status> | ||
| 11 | # SetHandler server-status | ||
| 12 | # Order deny,allow | ||
| 13 | # Deny from all | ||
| 14 | # Allow from IPCACTI/255.255.255.255 | ||
| 15 | # </Location> | ||
| 16 | # | ||
| 17 | # 2005 (c) Clement OUDOT - LINAGORA | ||
| 18 | # | ||
| 19 | # GPL license | ||
| 20 | #==================================================================== | ||
| 21 | |||
| 22 | #==================================================================== | ||
| 23 | # Packages | ||
| 24 | #==================================================================== | ||
| 25 | use strict; | ||
| 26 | use LWP; | ||
| 27 | use Getopt::Std; | ||
| 28 | |||
| 29 | #==================================================================== | ||
| 30 | # Global parameters | ||
| 31 | #==================================================================== | ||
| 32 | my ( $host, $port, $path, $timeout ) = &options ; | ||
| 33 | |||
| 34 | #==================================================================== | ||
| 35 | # Create an User Agent | ||
| 36 | #==================================================================== | ||
| 37 | my $ua = LWP::UserAgent->new; | ||
| 38 | $ua->agent("CactiScript/1.0"); | ||
| 39 | $ua->timeout($timeout); | ||
| 40 | |||
| 41 | #==================================================================== | ||
| 42 | # HTTP Request | ||
| 43 | #==================================================================== | ||
| 44 | my $request = HTTP::Request->new(GET => "http://${host}:${port}${path}"); | ||
| 45 | |||
| 46 | #==================================================================== | ||
| 47 | # HTTP Response | ||
| 48 | #==================================================================== | ||
| 49 | |||
| 50 | my $response = $ua->request($request); | ||
| 51 | |||
| 52 | if ( $response->is_error ) { | ||
| 53 | print "Unable to get $path\n"; | ||
| 54 | my $m = $response->message ; | ||
| 55 | print "pb = $m \n"; | ||
| 56 | exit 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $content = $response->content ; | ||
| 60 | #print $content ; | ||
| 61 | |||
| 62 | #==================================================================== | ||
| 63 | # Parse response | ||
| 64 | #==================================================================== | ||
| 65 | my ($total_accesses) = ( $content =~ /Total accesses: (\d*)/i ) ; | ||
| 66 | my ($total_traffic) = ( $content =~ /Total traffic: (\d*\.?\d*)/i ) ; | ||
| 67 | # 3/01/2006 : modif pour pouvoir parser cluster-status => 1 ou 2 espaces avant "requests currently.. | ||
| 68 | my ($current_requests) = ( $content =~ /(\d*) {1,2}requests currently being processed/i ) ; | ||
| 69 | my ($idle_servers) = ( $content =~ /(\d*) idle servers/i ) ; | ||
| 70 | |||
| 71 | #==================================================================== | ||
| 72 | # Print results for Cacti | ||
| 73 | #==================================================================== | ||
| 74 | print "total_accesses:$total_accesses total_traffic:$total_traffic current_requests:$current_requests idle_servers:$idle_servers"; | ||
| 75 | |||
| 76 | exit 0 ; | ||
| 77 | |||
| 78 | #==================================================================== | ||
| 79 | # Local functions | ||
| 80 | #==================================================================== | ||
| 81 | sub options { | ||
| 82 | # Get and check args | ||
| 83 | my %opts ; | ||
| 84 | getopt('hpPt',\%opts) ; | ||
| 85 | &usage unless (exists($opts{"h"})) ; | ||
| 86 | $opts{"p"} = 80 unless (exists($opts{"p"})) ; | ||
| 87 | $opts{"P"} = "/server-status/" unless (exists($opts{"P"})) ; | ||
| 88 | $opts{"t"} = 25 unless (exists($opts{"t"})) ; # defaut = 5 (augmenté a 25 le 16/1/06) | ||
| 89 | return ( $opts{"h"}, $opts{"p"}, $opts{"P"}, $opts{"t"} ) ; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub usage { | ||
| 93 | # Print Help/Error message | ||
| 94 | print STDERR "Usage: $0 -h hostname (-p port -P path -t timeout)\n" ; | ||
| 95 | exit 1 ; | ||
| 96 | } |