1#!/usr/local/bin/perl 2use strict; 3 4# HTTP response times for either entire page views 5# or just the index, depending on configuration. 6# Full or index resource pulling is determined by the suffix of the 7# script name. ie. http_response_full or http_response_index 8# 9 10# Parameters: 11# 12# config 13# 14# Configuration variables: 15# 16# domain - domain of the server to measure (default takelessons.com) 17# url[1..n] - url to measure against 18 19my $domain = $ENV{'domain'} || 'http://takelessons.com'; 20 21# determine the intended "action" of the script 22# this is determined by the end of the script name 23# http_response_full or http_response_index 24my ($action) = ($0 =~ m/http_response_(\S+)$/); 25$ENV{'action'} = $action; 26if (! $ENV{'action'} =~ m/(full|index)/) { 27 die "invalid action (determined by script suffix)"; 28} 29 30my @urls; 31 32#debug mode 33#build urls manually 34if ($ARGV[0] eq "debug") { 35 push(@urls, ( "/", 36 "/san-diego-ca-92106/piano-lessons", 37 "/category/browse", 38 "/cities-music-lessons", 39 "/new-york-music-lessons", 40 "/category/singing-lessons")); 41} 42 43#get the urls - build from configuration variables 44my $i = 1; 45while (defined ($ENV{"url$i"})) { 46 push(@urls, $ENV{"url$i"}); 47 $i++; 48} 49if (! @urls) { 50 die "No urls defined\n"; 51} 52 53#output configuration and exit - required by Munin 54if (exists $ARGV[0] and $ARGV[0] eq "config") { 55 print "graph_title $domain Site Response Times - $ENV{'action'}\n"; 56 print "graph_category webserver\n"; 57 print "graph_vlabel request time (seconds)\n"; 58 print "graph_info Response times for public areas of $domain.\n"; 59 60 foreach my $url (@urls) { 61 my $label = $url; 62 #fix up our label name to be a valid variable name 63 $label =~ s@[^A-Za-z0-9_]@_@g; 64 print "$label.label $url\n"; 65 print "$label.type GAUGE\n"; 66 } 67 68 exit 0; 69 70} 71 72 73 74#function to make the request and get the response time 75sub req_time { 76 my $url = shift; 77 my $time; 78 my $outdir = '/tmp/munin/' . int(rand(32000)); 79 system("mkdir -p $outdir"); 80 if ($ENV{'action'} eq "full") { 81 $time = `/usr/bin/time -f "%e" wget -pq -P $outdir --header "Accept-Encoding: gzip,deflate" "$url" 2>&1`; 82 } elsif ($ENV{'action'} eq "index") { 83 $time = `/usr/bin/time -f "%e" wget -q -P $outdir --header "Accept-Encoding: gzip,deflate" "$url" 2>&1`; 84 } 85 system("rm -rf $outdir"); 86 return $time; 87} 88 89#loop over every url and output the responses 90foreach my $url (@urls) { 91 my $label = $url; 92 #fix up our label name to be a valid name 93 $label =~ s@[^A-Za-z0-9_]@_@g; 94 print "$label.value " . req_time($domain.$url); 95} 96 97exit 0; 98 99 100