Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / dnsresponse_ @ 7da1b039

Historique | Voir | Annoter | Télécharger (3,81 ko)

1 27594890 Sotiris Tsimbonis
#!/usr/bin/perl -w
2
3
=head1 NAME
4
5
dnsresponse - Plugin to monitor DNS resolution times.
6
"Poor man's smokeping" :)
7
8
=head1 APPLICABLE SYSTEMS
9
10
Any unix system.
11
12
=head1 CONFIGURATION
13
14
The following shows the default configuration.
15
16
  [dnsresponse_*]
17
    env.site www.google.com
18
    env.times 20
19
20
=head1 INTERPRETATION
21
22
The plugin shows the average and median times taken to resolve a site.
23
24
=head1 MAGIC MARKERS
25
26
  #%# family=auto
27
  #%# capabilities=autoconf,suggest
28
29
=head1 BUGS
30
31
None known.
32
33
=head1 VERSION
34
35
$Id: dnsresponse_ 61 2009-04-14 09:11:00Z stsimb $
36
37
=head1 AUTHOR
38
39
Copyright (c) 2009 by Sotiris Tsimbonis.
40
41
=head1 LICENSE
42
43
GPLv2
44
45
=cut
46
47
#%# family=auto
48
#%# capabilities=autoconf suggest
49
50
use strict;
51
use warnings;
52
53
my $DEBUG=0;
54
my $site = exists $ENV{'site'} ? $ENV{'site'} : "www.google.com";
55
my $times = exists $ENV{'times'} ? $ENV{'times'} : "20";
56
my $resconf="/etc/resolv.conf";
57
58
use File::Basename;
59
my $basename=basename($0);
60
my $scriptname;
61
my $dnsip;
62
($scriptname, $dnsip) = split("_", $basename);
63
print "DBG: target dns ip $dnsip\n" if ($DEBUG>0);
64
65
if ( defined $ARGV[0] and $ARGV[0] eq "config" ) {
66
	print "graph_title $dnsip DNS response time\n";
67
	print "graph_vlabel milliseconds\n";
68
	print "graph_scale no\n";
69
	print "graph_category Other\n";
70
	print "graph_info Time taken by $dnsip to resolve $site $times times.\n";
71
	#my @val = ("min", "avg", "median", "max");
72
	my @val = ("avg", "median", "stddev");
73
	my $value;
74
	foreach $value ( @val ) {
75
		if ($value eq "stddev") {
76
			print "$value.info Standard deviation (variance).\n";
77
		} else {
78
			print "$value.info $value time taken by $dnsip to resolve $site $times times.\n";
79
		}
80
		print "$value.label $value\n";
81
#		print "$value.type DERIVE\n";
82
#		print "$value.min 0\n";
83
#		print "$value.warning 100\n";
84
#		print "$value.critical 600\n";
85
	}
86
	exit 0;
87
}
88
89
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
90
	my $ret;
91
	if (! eval "require Net::DNS;") { $ret .= "Net::DNS not found. "; }
92
	if (! eval "require Time::HiRes;") { $ret .= "Time::HiRes not found. "; }
93
	if (! -s $resconf) { $ret .= "$resconf not found. "; }
94
	if ($ret) {
95
		print "no ($ret)\n";
96
		exit 1;
97
	} else {
98
		print "yes\n";
99
		exit 0;
100
	}
101
}
102
103
if ( defined $ARGV[0] and $ARGV[0] eq "suggest" ) {
104
	if (-s $resconf) {
105
		open (FILE, "< $resconf") || die "Could not open $resconf: $!\n";
106
		my $line;
107
		while ($line = <FILE>) {
108
			if ($line =~ /^nameserver/) {
109
				my $ns; my $ip;
110
				($ns, $ip) = split(" ", $line);
111
				print "$ip\n";
112
			}
113
		}
114
		exit 0;
115
	} else {
116
		print "ERROR reading $resconf\n";
117
		exit 1;
118
	}
119
}
120
121
use Time::HiRes qw ( gettimeofday tv_interval );
122
use Net::DNS;
123
124
my $res = Net::DNS::Resolver->new(
125
	nameservers => [$dnsip],
126
	recurse     => 1,
127
	debug       => $DEBUG,
128
);
129
130
my $i;
131
my @restimes;
132
for ($i=1; $i<=$times; $i++) {
133
	my $t0 = [gettimeofday];
134
	my $answer = $res->send($site);
135
	my $elapsed = tv_interval ($t0);
136
	push(@restimes, $elapsed);
137
	print "DBG: count $i elapsed $elapsed\n" if ($DEBUG>0);
138
}
139
140
141
@restimes=sort(@restimes);
142
143
#my $min=$restimes[0]*1000;
144
my $average=mean(@restimes)*1000;
145
my $median=median(@restimes)*1000;
146
my $stddev=std_dev_ref_sum(@restimes)*1000;
147
#my $max=$restimes[$times-1]*1000;
148
149
#print "min.value $min\n";
150
print "avg.value $average\n";
151
print "median.value $median\n";
152
print "stddev.value $stddev\n";
153
#print "max.value $max\n";
154
155
sub mean {
156
	my $result;
157
	foreach (@_) { $result += $_ }
158
	return $result / @_;
159
}
160
161
sub median {
162
	my @ar = @_;
163
	my $elements = scalar(@ar);
164
	return $ar[(int($elements-1)/2)];
165
#	if ($elements % 2) {
166
#		return $ar[($elements-1)/2];
167
#	} else {
168
#	return ($ar[($elements-1)/2-0.5]+$ar[($elements-1)/2+0.5])/2;
169
#	}
170
}
171
172
# Standard Deviance function from http://www.linuxjournal.com/article/6540
173
sub std_dev_ref_sum {
174
	my @ar = @_;
175
	my $elements = scalar @ar;
176
	my $sum = 0;
177
	my $sumsq = 0;
178
179
	foreach (@ar) {
180
		$sum += $_;
181
		$sumsq += ($_ **2);
182
	}
183
184
	return sqrt( $sumsq/$elements - (($sum/$elements) ** 2));
185
}