[mrtg] Re: script w/ values always reads zero on graph
Takumi Yamane
yamtak at b-session.com
Thu Sep 22 00:00:26 MEST 2005
On Mon, 19 Sep 2005 14:55:40 -0800, "Jim W. Weller" wrote
in <6015C4C8638F0F4A9405A44EC4502DB5063B0E19 at uaaexch01.uaa.alaska.edu>:
>
> I have a mail log that I parse with an external script to do counts
> of sent and recipients (which are bcc's).
...
> Why are my graphs for this (http://bb.uaa.alaska.edu/mrtg2/emails.
> html) always zero?
> Even when I send a bunch of mails to make sure the values
> for last and this are different and verify the numeric difference.
...
> I think I'm clear on the difference between "normal", gauge, and
> absolute. This one is normal because the counter continues to grow
> and I want mrtg to calc the diff between this and last.
My suggestion:
1) Read MRTG manual
http://people.ee.ethz.ch/~oetiker/webtools/mrtg/mrtg-reference.html
2) repeat step 1 several times.
3) Use 'gauge' option.
I wrote template script so that you can use 'gauge' option:
--- >8 ------ >8 ------ >8 ------ >8 ------ >8 ------ >8 ------ >8 ---
#!/usr/bin/perl -w
use strict;
use constant BASE_DIR => '.';
use constant LASTSEEN_LOG => BASE_DIR . '/app.lastseen.log';
use constant APP_LOG => BASE_DIR . '/app.log';
sub load_lastseen()
{
my $lastseen;
if (open(LASTSEEN, '<', LASTSEEN_LOG)) {
$lastseen = <LASTSEEN>;
close(LASTSEEN);
chomp($lastseen);
}
else {
$lastseen = '00000000000000';
}
return $lastseen;
}
sub save_lastseen($)
{
my ($lastseen) = @_;
open(LASTSEEN, '>', LASTSEEN_LOG)
or die "open failed: " . LASTSEEN_LOG . ": $!";
print LASTSEEN $lastseen, "\n"; # add NL for human readability
close(LASTSEEN);
}
sub process_applog($)
{
my ($lastseen) = @_;
my ($in_val, $out_val) = (0, 0);
my ($timestamp);
my ($year, $mon_str, $day, $hour, $min, $sec);
my %str2mon = (
'Jan' => 1,
'Feb' => 2,
'Mar' => 3,
'Apr' => 4,
'May' => 5,
'Jun' => 6,
'Jul' => 7,
'Aug' => 8,
'Sep' => 9,
'Oct' => 10,
'Nov' => 11,
'Dec' => 12,
);
$year = (localtime(time))[5] + 1900;
open(APPLOG, '<', APP_LOG)
or die "open failed: " . APP_LOG . ": $!";
while (<APPLOG>) {
# expect syslogd style log format
($mon_str, $day, $hour, $min, $sec)
= (m/^(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+/o);
$timestamp = sprintf("%04d%02d%02d%02d%02d%02d",
$year, $str2mon{$mon_str}, $day, $hour, $min, $sec);
next if ($timestamp le $$lastseen);
$$lastseen = $timestamp;
# your matching logic goes here
if (m/something in/o) {
$in_val++;
}
elsif (m/anything out/o) {
$out_val++;
}
}
close(APPLOG);
return ($in_val, $out_val);
}
sub show_result($$)
{
my ($in_val, $out_val) = @_;
print $in_val, "\n";
print $out_val, "\n";
print "\n"; # uptime in human readable format
print "app name\n"; # target name
}
sub main()
{
my ($lastseen, $in_val, $out_val);
$lastseen = load_lastseen();
($in_val, $out_val) = process_applog(\$lastseen);
save_lastseen($lastseen);
show_result($in_val, $out_val);
}
main();
# EOF
--- >8 ------ >8 ------ >8 ------ >8 ------ >8 ------ >8 ------ >8 ---
Hope this helps,
Takumi Yamane <yamtak at b-session.com>
--
Unsubscribe mailto:mrtg-request at list.ee.ethz.ch?subject=unsubscribe
Archive http://lists.ee.ethz.ch/mrtg
FAQ http://faq.mrtg.org Homepage http://www.mrtg.org
WebAdmin http://lists.ee.ethz.ch/lsg2.cgi
More information about the mrtg
mailing list