forked from HariSekhon/DevOps-Perl-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_nginx_stats.pl
executable file
·167 lines (153 loc) · 5.39 KB
/
watch_nginx_stats.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl -T
#
# Author: Hari Sekhon
# Date: 2011-05-24 10:38:54 +0100 (Tue, 24 May 2011)
#
# https://github.com/harisekhon/tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help improve or steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
$DESCRIPTION = "Watch Nginx stats. Nginx will need to be configured to support this, see documentation at http://wiki.nginx.org/HttpStubStatusModule";
$VERSION = "0.2.2";
# TODO: split off and unify this with my check_nginx_stats.pl Nagios plugin
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use LWP::UserAgent;
use POSIX;
use Time::HiRes qw/sleep time/;
my $accepted;
my $active;
my $conns_sec;
my $content;
my $count = 0;
my $end_time;
my $handled;
my $last_accepted = 0;
my $last_requests = 0;
my $last_start_time;
my $reading;
my $requests;
my $requests_sec;
my $res;
my $interval = 1;
my $sleep_time;
my $start_time;
my $status;
my $status_line;
my $time;
my $time_diff;
my $time_taken;
my $url;
my $waiting;
my $writing;
$usage_line = "usage: $progname --url http://host/nginx_status --interval=1 --count=0 (unlimited)\n";
%options = (
"u|url=s" => [ \$url, "URL to the Nginx status/stats page from which to collect nginx stats. Will use first arg as the URL if omitting this switch" ],
"c|count=i" => [ \$count, "Number of times to collect stats. Default: 0 (unlimited)" ],
"i|interval=f" => [ \$interval, "Interval in secs between stats requests. Default: 1" ],
);
@usage_order = qw/url count interval/;
delete $HariSekhonUtils::default_options{"t|timeout=i"};
get_options();
$url = $ARGV[0] if not defined($url) and defined($ARGV[0]);
#$url =~ /^(http:\/\/\w[\w\.-]+\w\/[\w\.\;\=\&\%\/-]+)$/ or die "Invalid URL given\n";
$url = validate_url($url, "Nginx Stats");
isInt($count) or usage "Invalid number of attempts given";
isFloat($interval) or usage "Invalid sleep interval given, must be a float";
$interval > 0 or usage "Interval must be greater than zero";
vlog_option "Count", $count ? $count : "$count (unlimited)";
vlog_option "Sleep interval", $interval;
my $ua = LWP::UserAgent->new;
$ua->agent("Hari Sekhon Watch Nginx Stats version $main::VERSION");
my $req = HTTP::Request->new(GET => $url);
print "="x111 . " Totals " . "="x17 . "\n";
print "Time\t\t\tCount\t\tActive\tReading\tWriting\tWaiting\tConn/s\tRequests/s | Accepted\tHandled\t\tRequests\n";
print "="x136 . "\n";
for(my $i=1;$i<=$count or $count eq 0;$i++){
vlog3 "sending request";
$time = strftime("%F %T", localtime);
$start_time = time;
$res = $ua->request($req);
$end_time = time;
$time_taken = $end_time - $start_time;
vlog3 "got response";
$status = $status_line = $res->status_line;
$status =~ s/\s.*$//;
if(! isInt($status)){
print "$time\tCODE ERROR: status code '$status' is not a number (status line was: '$status_line')\n";
next;
}
vlog3 "status line: $status_line";
unless($status eq 200){
warn "$time\tWARNING: '$status_line'\n";
sleep $interval;
next;
}
$content = $res->content;
if($content =~ /^\s*$/){
warn "$time\tWARNING: no content received from '$url'\n";
sleep $interval;
next;
}
unless($content =~ /Active connections:\s+(\d+)/){
warn "$time\tWARNING: Cannot find Active connections in output (not an Nginx server or wrong /stats_stub url?)\n";
warn "content: '$content'\n" if $verbose >= 3;
sleep $interval;
next;
}
$active = $1;
unless($content =~ /server accepts handled requests\n\s+(\d+)\s+(\d+)\s+(\d+)/){
warn "$time\tWARNING: Cannot find 'server accepts handled requests' in output\n";
warn "content: '$content'\n" if $verbose >= 3;
sleep $interval;
next;
}
$accepted = $1;
$handled = $2;
$requests = $3;
#if($requests > $handled or $handled > $accepted){
# warn "WARNING: requests > handled or handled > accepted, something is WRONG!\n";
if($handled > $accepted){
warn "WARNING: handled > accepted, something is WRONG!\n";
#sleep $interval;
#next;
}
unless($content =~ /Reading:\s+(\d+)\s+Writing:\s+(\d+)\s+Waiting:\s+(\d+)/){
warn "\tWARNING: Cannot find 'Reading/Writing/Waiting' in output\n";
warn "content: '$content'\n";
sleep $interval;
next;
}
$reading = $1;
$writing = $2;
$waiting = $3;
if($i eq 1){
$conns_sec = $requests_sec = "N/A";
} else {
$time_diff = $start_time - $last_start_time;
#if($time_diff < 1){
#$conns_sec = $requests_sec = "N/A";
#} else {
$conns_sec = sprintf("%.0f", ($accepted - $last_accepted) / $time_diff);
$requests_sec = sprintf("%.0f", ($requests - $last_requests) / $time_diff);
#}
}
$last_start_time = $start_time;
$last_accepted = $accepted;
$last_requests = $requests;
print "$time\t$i\t\t$active\t$reading\t$writing\t$waiting\t$conns_sec\t$requests_sec\t\t$accepted\t\t$handled\t\t$requests\n";
$sleep_time = ($interval - $time_taken) < 0 ? 0 : ($interval - $time_taken);
vlog2 "* sleeping for $sleep_time seconds\n";
sleep $sleep_time;
}
exit 0;