-не так Часто Задаваемые Вопросы- обновлено 2010/12/26
|
|
Make a graphReal-time counting with CGIThe previous method counts an access_log file at a fixed time, and real-time counting is not possible. Here we add some lines to the webplot.pl program in order to generate an HTML file. Again we define the place of files firstly. However we have to distinguish the absolute PATH inside the web server, and that in the area where httpd can access (so call-ed 'htdocs' directory). $abspath='/absolute/path/to/parent_of_image/'; $webpath='/relative/path/to/parent_of_image/'; $imgfile="access.png"; $logfile='/home/www/httpd/logs/access_log'; $gnuplot='/path/to/gnuplot'; The $abspath variable is the directory which is specified by an absolute PATH on the server. While the $webpath is a relative PATH from the httpd document root directory, and URL includes this. This CGI generates an image file in this directory, so that httpd is allowed to write files there. The structure of this script is the same as the previous one. open(LOG,$logfile); while(<LOG>){ if(/\.html/){ split; $day = substr($_[3],1,2); $mon = substr($_[3],4,3); $year= substr($_[3],8,4); $count[$day]++; } } close(LOG); $day=$#count; $count[$day+1]=$count[$day]; & make_gnuplot; open(GNUPLOT,"| ".$gnuplot); foreach $i (0..$#plot){ print GNUPLOT $plot[$i]; } for($i=1;$i<=$#count;$i++){ printf(GNUPLOT "%10d%10d\n",$i,$count[$i]); } print GNUPLOT "end\n"; close(GNUPLOT); & generate_html; exit 0; sub make_gnuplot{ $i=0; $plot[$i++]=sprintf("set term png color\n"); $plot[$i++]=sprintf("set output '%s'\n",$abspath.$imgfile); $plot[$i++]=sprintf("set size 0.7,0.7\n"); $plot[$i++]=sprintf("set xrange [0:32]\n"); $plot[$i++]=sprintf("set yrange [0:*]\n"); $plot[$i++]=sprintf("set xtics 1,7,31\n"); $plot[$i++]=sprintf("set mxtics 7\n"); $plot[$i++]=sprintf("set nokey\n"); $plot[$i++]=sprintf("set grid\n"); $plot[$i++]=sprintf("set title '%s %s'\n",$mon,$year); $plot[$i++]=sprintf("plot '-' with step\n"); } Newly added lines are the next subroutine which generates HTML data and tells a client browser the place of generated image file. sub generate_html{ print << "EOF"; Content-Type: text/html <html> <head><title> access_log stat </title></head> <body bgcolor="white"> <center><img src=\"$webpath$imgfile\"></center> </body> </html> EOF This perl script is copied into cgi-bin, and you make a following link in an HTML file. < a href="/cgi-bin/webplot_cgi.pl/"> Web Plot </a>
Since browsers cache image files generally, and if the file name is the same (like above), browser shows you an old image even it is renewed. An easy way to avoid this is to make a file with the different names. For example, perl holds PID in the variable $$, and when the file name is something like "access$$.png", each time you will see the different images. However this method makes many garbages in the directory and you have to clean-up them sometimes. |