/[perl]/rrd/log2rrd.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /rrd/log2rrd.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Aug 1 15:24:42 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/plain
script which can turn your old access.log from apache into rrdtool database.
It will NOT update database in real time -- it depends on dates in log

1 #!/usr/local/bin/perl -w
2
3 # Convert http access log to rrdtool database
4 # 2002-08-01 Dobrica Pavlinisic, <dpavlin@rot13.org>
5 #
6 # based on work of Jean-Edouard BABIN (listes@jeb.com.fr)
7 # http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&selm=aap7n0%241slf%241%40FreeBSD.csie.NCTU.edu.tw
8 #
9 # Usage: log2rrd.pl < access.log [2> errors]
10 #
11 # there is one counter called tmp which can be used to graph some
12 # perticual URL hit (see below in code how to edit it).
13
14 use strict;
15 use RRDs;
16 use Date::Parse;
17
18 # feed data how often to rrdtool (seconds)
19 my $step = 300;
20 # name of rrdfile to create
21 my $rrd_file = "access";
22
23 my $counter = 0;
24 my ($dv, $sv, $ov, $tmp) = (0, 0, 0);
25
26 my $last_utime = 0;
27 my $utime = 0;
28
29 print " all\t dyn\t sta\t oth tmp\n";
30
31 while (<STDIN>) {
32 if ($utime - $last_utime > $step) {
33 printf "%7d %7d %7d %7d %7d\n", $counter, $dv, $sv, $ov, $tmp;
34 &create_rrd("$rrd_file.rrd") unless -f "$rrd_file.rrd";
35 RRDs::update("$rrd_file.rrd", "--template",
36 "dynamic_views:static_views:other_views:tmp",
37 "$utime:$dv:$sv:$ov:$tmp");
38 my $ERR=RRDs::error;
39 die "ERROR while updating $rrd_file.rrd: $ERR\n" if $ERR;
40 ($dv, $sv, $ov, $tmp) = (0, 0, 0, 0);
41 $last_utime = $utime;
42 } else {
43 my $request = $_;
44 if ($request =~ m/\[([^\]]+)\]/) {
45 $utime = str2time($1);
46 } else {
47 print STDERR "can't parse date in line '$request'\n";
48 next;
49 }
50 my $url;
51 if ($request =~ m,\"((GET)|(POST))\s+(\S+)\s+HTTP,) {
52 $url = $4;
53 } else {
54 print STDERR "can't parse request in line '$request'\n";
55 next;
56 }
57
58 my ($page, undef) = split(m/\?/, $url);
59 if ($page =~ m/(cgi-bin\/|\.php$|\.pl$|\.sh$)/ ) {
60 $dv++; # dynamic content
61 } elsif ($page =~ m/\.html?$/) {
62 $sv++; # static content
63 } else {
64 $ov++; # image/other file types
65 }
66
67 # tmp counter specification
68 if ($page =~ m,/cgi-bin/search/search.pl,) {
69 $tmp++;
70 }
71
72 $counter++;
73 }
74 }
75
76 sub create_rrd {
77 my($file) = @_;
78
79 print "creating rrd database $file (step: $step utime: $utime)\n";
80
81 RRDs::create($file,
82 "--step", $step, # we're feeding data every $step seconds
83 "--start", $utime - $step, # start at current utime
84
85 "DS:dynamic_views:ABSOLUTE:600:0:U",
86 "DS:static_views:ABSOLUTE:600:0:U",
87 "DS:other_views:ABSOLUTE:600:0:U",
88 "DS:tmp:ABSOLUTE:600:0:U",
89
90 "RRA:AVERAGE:0.5:1:600",
91
92 "RRA:AVERAGE:0.5:6:600",
93 "RRA:AVERAGE:0.5:24:600",
94 "RRA:AVERAGE:0.5:288:600",
95
96 "RRA:MAX:0.5:6:600",
97 "RRA:MAX:0.5:24:600",
98 "RRA:MAX:0.5:288:600",
99
100 "RRA:LAST:0.5:1:1"); # keep latest data point
101
102 my $ERR=RRDs::error;
103 die "ERROR while updating $rrd_file.rrd: $ERR\n" if $ERR;
104 }

  ViewVC Help
Powered by ViewVC 1.1.26