-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener-count.pl
executable file
·129 lines (96 loc) · 3.52 KB
/
listener-count.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/perl
use 5.014; # so keys/values/each work on scalars
no warnings qw(experimental::autoderef); # quiet some warnings
use strict;
use Data::Dumper;
use Getopt::Long;
Getopt::Long::Configure ( "bundling" );
use Pod::Usage;
use LWP;
use constant URL => 'http://YOUR.HOSTNAME.HERE:YOUR-PORT-NUMBER-HERE/listener-count.xsl';
sub main ();
sub max ( $$$ );
main;
1;
sub main () {
my ( $result, $verbose, $debug, $help, $man ) = ( 0, 0, 0, 0, 0 );
my $output = $ENV{'LISTENER_COUNT'} || '/var/tmp/listener-count';
my $ua = LWP::UserAgent->new();
my $request = undef;
my $response = undef;
$result = GetOptions (
"output|o=s" => \$output,
"verbose|v" => \$verbose,
"debug|d" => \$debug,
"help|h" => \$help,
"man|m" => \$man,
);
die "GetOptions returned " . $result . ". Stopped" if ( ! $result );
pod2usage ( 1 ) if ( defined ( $help ) && $help );
pod2usage ( -verbose => 2 ) if ( defined ( $man ) && $man );
# see LWP
$ua->agent( 'DataRetrieval/0.1 ' );
$request = HTTP::Request->new( GET => URL );
$response = $ua->request( $request );
if ( $response->is_success()) {
my $currentTime = time();
my $content = $response->content();
my @mounts = ();
my $current = undef;
$content =~ s{^/}{};
@mounts = split( /\//, $content );
chomp( @mounts );
# this makes a hash of {mountpoint}->{count}, {mountpoint}->{time} for each mount point
my $mountcount = map {
my( $mountpoint, $count, $junk ) = split( /:/, $_ );
$current->{$mountpoint}->{count} = $count;
$current->{$mountpoint}->{time} = $currentTime;
} @mounts;
print STDERR "Mounts: ", $mountcount, "\n", Data::Dumper->Dump( [$current], [qw(current)] ), "\n" if $verbose;
open( OUT, ">$output" ) || die "Cannot open $output for writing ($!). Stopped";
foreach my $mount ( keys( $current )) {
print OUT $mount, ":", $current->{$mount}->{count}, ":", $current->{$mount}->{time}, "\n";
}
close( OUT );
max( $current, $verbose, $debug );
} else {
die "HTTP request failed: " . $response->status_line . ". Stopped";
}
}
# rewrite the max file if necessary
# return true or false depending on whether we rewrote it
sub max ( $$$ ) {
my $current = shift;
my $verbose = shift;
my $debug = shift;
my $maximum = undef;
my $touched = 0;
my $maxfile = $ENV{'LISTENER_COUNT_MAX'} || '/var/tmp/listener-count-max';
open( MAX, $maxfile ) || die "Cannot open $maxfile for reading ($!). Stopped";
my @maximums = <MAX>;
close( MAX );
chomp( @maximums );
foreach my $max ( @maximums ) {
my ( $mountpoint, $count, $time ) = split( /:/, $max );
# use the new values and set a flag if the new count for this
# mount point is greater than the previous count
if ( $current->{$mountpoint}->{count} > $count ) {
$maximum->{$mountpoint}->{count} = $current->{$mountpoint}->{count};
$maximum->{$mountpoint}->{time} = $current->{$mountpoint}->{time};
$touched = 1;
print STDERR "New maximum value for ", $mountpoint, ": ", $maximum->{$mountpoint}->{count}, "\n";
} else {
$maximum->{$mountpoint}->{count} = $count;
$maximum->{$mountpoint}->{time} = $time;
}
}
print STDERR Data::Dumper->Dump( [$maximum], [qw(maximum)] ), "\n" if $debug;
if ( $touched ) {
open( MAX, ">$maxfile" ) || die "Cannot open $maxfile for writing ($!). Stopped";
foreach my $mountpoint ( keys( $maximum )) {
print MAX $mountpoint, ":", $maximum->{$mountpoint}->{count}, ":", $maximum->{$mountpoint}->{time}, "\n";
}
close( MAX );
}
$touched;
}