Skip to content

Instantly share code, notes, and snippets.

@mattrude
Last active July 23, 2020 18:35
Show Gist options
  • Save mattrude/4362399 to your computer and use it in GitHub Desktop.
Save mattrude/4362399 to your computer and use it in GitHub Desktop.
A Nginx rrdtools graphing solution using perl. This script will only update the rrd databases and create graphs, not the actually html site.
#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;
# define location of rrdtool databases
my $rrd = '/var/www/status.example.com/rrd';
# define location of images
my $img = '/var/www/status.example.com/images';
# define your nginx stats URL
my $URL = "http://127.0.0.1/nginx_status";
my $HOST = "milly";
my $ua = LWP::UserAgent->new(timeout => 30);
my $response = $ua->request(HTTP::Request->new('GET', $URL));
my $requests = 0;
my $total = 0;
my $reading = 0;
my $writing = 0;
my $waiting = 0;
foreach (split(/\n/, $response->content)) {
$total = $1 if (/^Active connections:\s+(\d+)/);
if (/^Reading:\s+(\d+).*Writing:\s+(\d+).*Waiting:\s+(\d+)/) {
$reading = $1;
$writing = $2;
$waiting = $3;
}
$requests = $3 if (/^\s+(\d+)\s+(\d+)\s+(\d+)/);
}
# if rrdtool database doesn't exist, create it
if (! -e "$rrd/nginx-$HOST.rrd") {
RRDs::create "$rrd/nginx-$HOST.rrd",
"-s 60",
"DS:requests:COUNTER:90:0:60000",
"DS:total:ABSOLUTE:90:0:10000",
"DS:reading:ABSOLUTE:90:0:10000",
"DS:writing:ABSOLUTE:90:0:10000",
"DS:waiting:ABSOLUTE:90:0:10000",
"RRA:AVERAGE:0.5:1:1440",
"RRA:AVERAGE:0.5:5:288",
"RRA:AVERAGE:0.5:30:672",
"RRA:AVERAGE:0.5:120:732",
"RRA:AVERAGE:0.5:720:1460";
}
# insert values into rrd database
RRDs::update "$rrd/nginx-$HOST.rrd",
"-t", "requests:total:reading:writing:waiting",
"N:$requests:$total:$reading:$writing:$waiting";
# Generate graphs
CreateGraphs("hour");
CreateGraphs("day");
CreateGraphs("week");
CreateGraphs("month");
CreateGraphs("year");
#------------------------------------------------------------------------------
sub CreateGraphs($){
my $period = shift;
RRDs::graph "$img/requests-$HOST-$period.png",
"-s -1$period",
"-t HTTP requests on nginx server $HOST.mattrude.com for the last $period",
"--lazy",
"-h", "150", "-w", "700",
"-l 0",
"-a", "PNG",
"-v requests/minute",
"DEF:requests=$rrd/nginx-$HOST.rrd:requests:AVERAGE",
"CDEF:request=requests,60,*",
"LINE2:request#336600:Requests",
"GPRINT:request:LAST: Current\\: %5.1lf %s",
"GPRINT:request:MIN: Min\\: %5.1lf %s",
"GPRINT:request:AVERAGE: Avg\\: %5.1lf %s",
"GPRINT:request:MAX: Max\\: %5.1lf %s\\n",
"HRULE:0#000000";
if ($ERROR = RRDs::error) {
print "$0: unable to generate $period graph: $ERROR\n";
}
RRDs::graph "$img/connections-$HOST-$period.png",
"-s -1$period",
"-t HTTP requests on nginx server $HOST.mattrude.com for the last $period",
"--lazy",
"-h", "150", "-w", "700",
"-l 0",
"-a", "PNG",
"-v requests/minute",
"DEF:total=$rrd/nginx-$HOST.rrd:total:AVERAGE",
"DEF:reading=$rrd/nginx-$HOST.rrd:reading:AVERAGE",
"DEF:writing=$rrd/nginx-$HOST.rrd:writing:AVERAGE",
"DEF:waiting=$rrd/nginx-$HOST.rrd:waiting:AVERAGE",
"CDEF:totals=total,60,*",
"CDEF:readings=reading,60,*",
"CDEF:writings=writing,60,*",
"CDEF:waitings=waiting,60,*",
"LINE2:totals#336600:Total",
"GPRINT:totals:LAST: Current\\: %5.1lf %S",
"GPRINT:totals:MIN: Min\\: %5.1lf %S",
"GPRINT:totals:AVERAGE: Avg\\: %5.1lf %S",
"GPRINT:totals:MAX: Max\\: %5.1lf %S\\n",
"LINE2:readings#0022FF:Reading",
"GPRINT:readings:LAST: Current\\: %5.1lf %S",
"GPRINT:readings:MIN: Min\\: %5.1lf %S",
"GPRINT:readings:AVERAGE: Avg\\: %5.1lf %S",
"GPRINT:readings:MAX: Max\\: %5.1lf %S\\n",
"LINE2:writings#FF0000:Writing",
"GPRINT:writings:LAST: Current\\: %5.1lf %S",
"GPRINT:writings:MIN: Min\\: %5.1lf %S",
"GPRINT:writings:AVERAGE: Avg\\: %5.1lf %S",
"GPRINT:writings:MAX: Max\\: %5.1lf %S\\n",
"LINE2:waitings#00AAAA:Waiting",
"GPRINT:waitings:LAST: Current\\: %5.1lf %S",
"GPRINT:waitings:MIN: Min\\: %5.1lf %S",
"GPRINT:waitings:AVERAGE: Avg\\: %5.1lf %S",
"GPRINT:waitings:MAX: Max\\: %5.1lf %S\\n",
"HRULE:0#000000";
if ($ERROR = RRDs::error) {
print "$0: unable to generate $period graph: $ERROR\n";
}
}
@matroskinkot
Copy link

В 2006 году написан был скрипт не вами. Вот автор https://kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/

@TheBarret
Copy link

В 2006 году написан был скрипт не вами. Вот автор https://kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/

So where did he say or write it was made by him?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment