PHP rrdtool - PERL rrdtool monitorizacion


rrdtool es un modulo de control de estados, estadisticas , etc del servidor web. Analiza todos los paquetes ,etc , etc.. esto es una opcion avanzada no se va a explicar nada mas..

Esta las versiones de rrtool para windows y tambien para linux ,etc.. si usted no sabe nad de esto puede buscar informacion sobre cacti.

PHP rrdtool



___________________________________________________________________________________

PERL rrdtool
1. setup
http {
    ...
    server {
        listen SOME.IP.ADD.RESS;
        ...
        location /nginx_statu {
            stub_status on;
            access_log   off;
            allow SOME.IP.ADD.RESS;
            deny all;
        }
        ...
    }
    ...
}
2. test config
# /your/installaion/sbin/nginx -t
  the configuration file /opt/nginx/conf/nginx.conf syntax is ok
  the configuration file /opt/nginx/conf/nginx.conf was tested successfully
#

3. es obligatorio tener habilitado el modulo  ----with-http_stub_status_module
entras a localhost/nginx_status y veras algo asi
james:~# GET http://localhost/nginx_status
Active connections: 83835
server accepts handled requests
  78585 54656 2342344
Reading: 3 Writing: 465 Waiting: 453
croesus:~#


SCRIPT PARA EL PASO 4
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;

# define location of rrdtool databases
my $rrd = '/opt/rrd';
# define location of images
my $img = '/opt/rrd';
# define your nginx stats URL
my $URL = "http://moviewer.com/nginx_status";

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+)/);
}

#print "RQ:$requests; TT:$total; RD:$reading; WR:$writing; WA:$waiting\n";

# if rrdtool database doesn't exist, create it
if (! -e "$rrd/nginx.rrd") {
  RRDs::create "$rrd/nginx.rrd",
        "-s 60",
 "DS:requests:COUNTER:120:0:100000000",
 "DS:total:ABSOLUTE:120:0:60000",
 "DS:reading:ABSOLUTE:120:0:60000",
 "DS:writing:ABSOLUTE:120:0:60000",
 "DS:waiting:ABSOLUTE:120:0:60000",
 "RRA:AVERAGE:0.5:1:2880",
 "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.rrd",
  "-t", "requests:total:reading:writing:waiting",
  "N:$requests:$total:$reading:$writing:$waiting";

# Generate graphs
CreateGraphs("day");
CreateGraphs("week");
CreateGraphs("month");
CreateGraphs("year");

#------------------------------------------------------------------------------
sub CreateGraphs($){
  my $period = shift;
  
  RRDs::graph "$img/requests-$period.png",
  "-s -1$period",
  "-t Requests on nginx",
  "--lazy",
  "-h", "150", "-w", "700",
  "-l 0",
  "-a", "PNG",
  "-v requests/sec",
  "DEF:requests=$rrd/nginx.rrd:requests:AVERAGE",
  "LINE2:requests#336600:Requests",
  "GPRINT:requests:MAX:  Max\\: %5.1lf %S",
  "GPRINT:requests:AVERAGE: Avg\\: %5.1lf %S",
  "GPRINT:requests:LAST: Current\\: %5.1lf %Sreq/sec",
  "HRULE:0#000000";
  if ($ERROR = RRDs::error) { 
    print "$0: unable to generate $period graph: $ERROR\n"; 
  }

  RRDs::graph "$img/connections-$period.png",
  "-s -1$period",
  "-t Requests on nginx",
  "--lazy",
  "-h", "150", "-w", "700",
  "-l 0",
  "-a", "PNG",
  "-v requests/sec",
  "DEF:total=$rrd/nginx.rrd:total:AVERAGE",
  "DEF:reading=$rrd/nginx.rrd:reading:AVERAGE",
  "DEF:writing=$rrd/nginx.rrd:writing:AVERAGE",
  "DEF:waiting=$rrd/nginx.rrd:waiting:AVERAGE",

  "LINE2:total#22FF22:Total",
  "GPRINT:total:LAST:   Current\\: %5.1lf %S",
  "GPRINT:total:MIN:  Min\\: %5.1lf %S",
  "GPRINT:total:AVERAGE: Avg\\: %5.1lf %S",
  "GPRINT:total:MAX:  Max\\: %5.1lf %S\\n",
  
  "LINE2:reading#0022FF:Reading",
  "GPRINT:reading:LAST: Current\\: %5.1lf %S",
  "GPRINT:reading:MIN:  Min\\: %5.1lf %S",
  "GPRINT:reading:AVERAGE: Avg\\: %5.1lf %S",
  "GPRINT:reading:MAX:  Max\\: %5.1lf %S\\n",
  
  "LINE2:writing#FF0000:Writing",
  "GPRINT:writing:LAST: Current\\: %5.1lf %S",
  "GPRINT:writing:MIN:  Min\\: %5.1lf %S",
  "GPRINT:writing:AVERAGE: Avg\\: %5.1lf %S",
  "GPRINT:writing:MAX:  Max\\: %5.1lf %S\\n",
  
  "LINE2:waiting#00AAAA:Waiting",
  "GPRINT:waiting:LAST: Current\\: %5.1lf %S",
  "GPRINT:waiting:MIN:  Min\\: %5.1lf %S",
  "GPRINT:waiting:AVERAGE: Avg\\: %5.1lf %S",
  "GPRINT:waiting:MAX:  Max\\: %5.1lf %S\\n",

  "HRULE:0#000000";
  if ($ERROR = RRDs::error) { 
    print "$0: unable to generate $period graph: $ERROR\n"; 
  }
} 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
4. configuras el script perl
#!/usr/bin/perl
use RRDs;
use LWP::UserAgent;
# define location of rrdtool databases
my $rrd = '/op/rrd';
# define location of images
my $img = '/op/rrd/html';
# define your nginx stats URL
my $URL = "http://localhost/nginx_status";
...
5.listo ahora veras los estados de tu servidor apache asi:

 

0 pensamientos:

Post a Comment

feedback!