-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnginx_request_time
executable file
·125 lines (100 loc) · 2.88 KB
/
nginx_request_time
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
#!/usr/bin/perl -w
=head1 NAME
nginx_request_time - Munin plugin to show statistics of nginx's request_time.
=head1 APPLICABLE SYSTEMS
nginx request_time statistics
=head1 CONFIGURATION
File::ReadBackwards, Statistics::Lite, and Statistics::Descriptive are used and must be installed.
You can override the log file location.
[nginx_request_time]
env.logfile /var/log/nginx/access.log
env.loglimit 1000
Nginx must be configured to get $request_time.
nginx_request_time expects TSV log format.
Example format:
log_format request_time_stats '$time_local\t'
'$status\t'
'$request_time\t'
'$remote_addr\t'
'$upstream_addr\t'
'$upstream_response_time\t'
'$request';
By default the last 1000 log lines are read, you may want to change this.
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 LICENSE
MIT
=cut
use File::ReadBackwards;
use Statistics::Lite qw(max min mean median);
use Statistics::Descriptive;
my $line_counter = exists $ENV{'loglimit'} ? $ENV{'loglimit'} : 1000;
my $LOG = exists $ENV{'logfile'} ? $ENV{'logfile'}
: "/var/log/nginx/access.log";
if (exists $ARGV[0] and $ARGV[0] eq "autoconf") {
unless (-r $LOG) {
print "$LOG not readable\n";
} else {
print "yes\n";
}
exit 0;
}
my $mode = $0;
$mode =~ s/.*\/nginx_request_time//;
if (exists $ARGV[0] and $ARGV[0] eq "config") {
print <<'EOS';
graph_title NGINX response time
graph_args -l 0 --base 1000
graph_category nginx
graph_vlabel millseconds
EOS
if ($mode eq "_max") {
print <<'EOS';
max.label max
max.draw LINE2
max.min 0
EOS
} else {
print <<'EOS';
min.label min
min.draw LINE1
min.min 0
median.label median
median.draw LINE2
median.min 0
percentile90.label 90th percentile
percentile90.draw LINE2
percentile90.min 0
percentile95.label 95th percentile
percentile95.draw LINE2
percentile95.min 0
EOS
}
exit 0;
}
my @request_time_array = ();
my $log_handle = File::ReadBackwards->new($LOG);
for (my $i = 0; $i < $line_counter; $i++) {
my $log_line = $log_handle->readline;
if (defined($log_line)) {
my $time_s = (split "\t", $log_line)[2];
if (!defined($time_s)) { next; }
push @request_time_array, $time_s * 1000;
} else {
last;
}
}
$log_handle->close;
$stat = Statistics::Descriptive::Full->new();
$stat->add_data(@request_time_array);
$percentile90 = $stat->percentile(90);
$percentile95 = $stat->percentile(95);
my $min = min @request_time_array;
my $max = max @request_time_array;
my $median = median @request_time_array;
print "min.value $min\n";
print "max.value $max\n";
print "median.value $median\n";
print "percentile90.value $percentile90\n";
print "percentile95.value $percentile95\n";