Project

General

Profile

« Previous | Next » 

Revision 04a6764e

Added by Hamish Coleman almost 16 years ago

  • ID 04a6764e32e837bfb2265e0bdfa1b9ff02fa8abd

output indexmrtg to json, start renaming fields

View differences:

indexmrtg.cgi
use FileHandle;
use JSON;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
......
$db->{mrtg}{default}{post}{ysize} = 135;
sub config_save_val($$$$) {
my ($filename,$target,$var,$val) = @_;
my ($filename,$name,$var,$val) = @_;
my $this_ysize;
my $this_xsize;
......
}
# Save any default initializers
if ($target eq '_') {
if ($name eq '_') {
$db->{mrtg}{default}{post}{$var} = $val;
return;
}
if ($target eq '^') {
if ($name eq '^') {
$db->{mrtg}{default}{pre}{$var} = $val;
return;
}
# Check for target name collision, and uniqueify
my $unique_target = $target;
# Check for name name collision, and uniqueify
my $unique_name = $name;
my $unique_id = 1;
while (defined $db->{target}->{$unique_target}
&& $db->{target}->{$unique_target}->{_cfgfile} ne $filename) {
$unique_target = $target . '_' . $unique_id;
while (defined $db->{graph}->{$unique_name}
&& $db->{graph}->{$unique_name}->{srcfile} ne $filename) {
$unique_name = $name . '_' . $unique_id;
$unique_id++;
}
if (!defined $db->{target}->{$unique_target}) {
# initialize a new target
if (!defined $db->{graph}->{$unique_name}) {
# initialize a new name
#TODO - instantiate new tests from all of the "_"/"^" defaults
push @{$db->{sequence}}, $unique_target;
push @{$db->{sequence}}, $unique_name;
$db->{target}->{$unique_target}->{_target} = $target;
$db->{target}->{$unique_target}->{_cfgfile} = $filename;
$db->{target}->{$unique_target}->{_sequence} = @{$db->{sequence}};
$db->{graph}->{$unique_name}->{name} = $name;
$db->{graph}->{$unique_name}->{srcfile} = $filename;
$db->{graph}->{$unique_name}->{sequence} = @{$db->{sequence}};
$db->{target}{$unique_target}{xsize} = $db->{mrtg}{default}{post}{xsize};
$db->{target}{$unique_target}{ysize} = $db->{mrtg}{default}{post}{ysize};
$db->{graph}{$unique_name}{img}{xsize} = $db->{mrtg}{default}{post}{xsize};
$db->{graph}{$unique_name}{img}{ysize} = $db->{mrtg}{default}{post}{ysize};
}
$db->{target}{$unique_target}{mrtg}{$var} = $val;
$db->{graph}{$unique_name}{mrtg}{$var} = $val;
}
# Load one or more files
......
# Is this a standard MRTG setting?
if ( $_ =~ /^([^#[][^[]+)\[(.*)\]:\s*(.+)$/ ) {
my $var=lc $1;
my $target=lc $2;
my $name=lc $2;
my $val=$3;
config_save_val($filename,$target,$var,$val);
config_save_val($filename,$name,$var,$val);
}
# TODO - Handle extra statements:
......
}
}
# Itterate through the loaded targets and read their mtime and
# Itterate through the loaded name and read their mtime and
# any other filesystem details for them.
#
sub config_read_filesystem() {
# check and update details for all known tests;
for my $i (keys %{$db->{target}}) {
my $target = $db->{target}->{$i};
my $testname = $target->{_target};
for my $i (keys %{$db->{graph}}) {
my $name = $db->{graph}->{$i};
my $testname = $name->{name};
# TODO - check workdir and validate it here
# Default is GIF, unless a PNG file exists
$target->{_imagetype} = 'gif';
$name->{mrtg}{_imagetype} = 'gif';
if ( -e "$testname-day.png" ) {
$target->{_imagetype} = "png";
$name->{mrtg}{_imagetype} = "png";
}
# TODO - change the filename based on the displayed period
my $filename = $testname . "-day." . $target->{_imagetype};
my $filename = $testname . "-day." . $name->{mrtg}{_imagetype};
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime,$mtime,$ctime,$blksize,$blocks)
......
next;
}
$target->{_mtime} = $mtime;
$name->{_mtime} = $mtime;
# FIXME - global
# find the newest file
......
return ($group,$host,$test);
}
# Read through the list of targets and find common hostname prefixes,
# Read through the list of name and find common hostname prefixes,
# so that we can group these hosts together
# (a hosttype entry)
#
sub maketree_find_hosttypes() {
for my $name (keys %{$db->{target}}) {
for my $name (keys %{$db->{graph}}) {
my ($group,$host,$test) = extract_grouphosttest($name);
if (!defined($group)) {
next;
......
next;
}
$db->{target}->{$name}->{_hosttype} = $hosttype;
$db->{graph}->{$name}->{_hosttype} = $hosttype;
$db->{hosttype}->{$hosttype}->{$host}++;
}
......
}
# a valid hosttype both:
# defined($db->{target}->{$name}->{hosttype}) &&
# defined($db->{graph}->{$name}->{hosttype}) &&
# defined($db->{hosttype}->{$hosttype})
my $hosttype = $db->{target}->{$name}->{_hosttype};
my $hosttype = $db->{graph}->{$name}->{_hosttype};
if (defined($hosttype) && !defined($db->{hosttype}->{$hosttype})) {
$hosttype=undef;
}
......
}
}
sub maketree_cfgfile($$) {
sub maketree_srcfile($$) {
my ($name,$sequence) = @_;
# config files
my $cfgfile = $db->{target}->{$name}->{_cfgfile};
my $srcfile = $db->{graph}->{$name}->{srcfile};
my @cfgpath = split /\//,$cfgfile;
my @srcpath = split /\//,$srcfile;
shift @cfgpath; # remove initial slash
shift @srcpath; # remove initial slash
unshift @cfgpath,"config"; # ensure that the config node exists
unshift @srcpath,"config"; # ensure that the config node exists
my $node = $db->{tree};
while (@cfgpath ) {
if (!defined $node->{$cfgpath[0]}) {
$node->{$cfgpath[0]} = {};
while (@srcpath ) {
if (!defined $node->{$srcpath[0]}) {
$node->{$srcpath[0]} = {};
}
$node = $node->{$cfgpath[0]};
shift @cfgpath;
$node = $node->{$srcpath[0]};
shift @srcpath;
}
# assign a target name and sequence to the final node
# assign a name name and sequence to the final node
$node->{$name} = $sequence;
}
# Look through the target database and create tree entries for each cfgfile
# Look through the name database and create tree entries for each cfgfile
#
sub maketree() {
maketree_find_hosttypes();
for my $name (keys %{$db->{target}}) {
my $sequence = $db->{target}->{$name}->{_sequence};
maketree_cfgfile($name,$sequence);
for my $name (keys %{$db->{graph}}) {
my $sequence = $db->{graph}->{$name}->{sequence};
maketree_srcfile($name,$sequence);
maketree_grouphosttest($name,$sequence);
# TODO - parse the Target setting and create trees
# TODO - parse magic comments and create trees here
......
print "\n",Dumper(\$db),"\n";
}
print "\n",JSON->new->utf8(1)->pretty(1)->encode($db),"\n";

Also available in: Unified diff