From: <buc...@us...> - 2010-11-30 12:04:52
|
Revision: 186 http://devmon.svn.sourceforge.net/devmon/?rev=186&view=rev Author: buchanmilne Date: 2010-11-30 11:22:54 +0000 (Tue, 30 Nov 2010) Log Message: ----------- Add support for DBTABLE tag in messages Modified Paths: -------------- branches/dbtable/modules/dm_tests.pm Modified: branches/dbtable/modules/dm_tests.pm =================================================================== --- branches/dbtable/modules/dm_tests.pm 2010-11-30 11:05:27 UTC (rev 185) +++ branches/dbtable/modules/dm_tests.pm 2010-11-30 11:22:54 UTC (rev 186) @@ -1703,6 +1703,9 @@ my $errors = ''; my $worst_color = 'green'; my $table = undef; + my $dbtable = undef; + my @dbkeys; + my @dbvals; my $extrastatus = ''; my (%t_opts,%rrd); @@ -1776,6 +1779,13 @@ next; } + # Check if we have a database table coming + if($line =~ /^DBTABLE:/o) { + do_log("DBTABLE requires MULTINODE=yes") if ($g{'multinode'} ne 'yes'); + $dbtable = '1'; + next; + } + # If we have seen a TABLE: placeholder, do table logic if(defined $table) { # First check and see if this is our table header @@ -1811,6 +1821,7 @@ do_log("No primary OID found for $test test for $device", 0); $msg .= "&yellow No primary OID found.\n"; $worst_color = 'yellow'; + $table = undef; next; } @@ -1822,6 +1833,7 @@ do_log("Missing repeater data for $pri for $test msg", 0); $msg .= "&clear Missing repeater data for primary OID $pri\n"; $worst_color = 'clear'; + $table = undef; next; } @@ -1830,6 +1842,7 @@ do_log("Primary OID $pri in $test table is a non-repeater", 0); $msg .= "&yellow primary OID $pri in table is a non-repeater\n"; $worst_color = 'yellow'; + $table = undef; next; } @@ -2058,6 +2071,204 @@ } # End table data processing + elsif (defined $dbtable) { + #Look for database table key definitions + if($line !~ /\{(.+?)}/) { + do_log("DBTABLE: parsing attribute definitions: $line"); + @dbkeys = split /\|/, $line or do_log("Could not parse database keys"); + do_log("DBTABLE: parsed attributes into @dbkeys"); + next; + } + + my $set_data = ''; + my $temp_data = ''; + #my $dir = $rrd{$name}{'dir'}; + #my $pri = ${dbkeys[0]}; + #my $do_max = $rrd{$name}{'do_max'}; + my $time; + + my $pri = $1 if $line =~ /\{(.+?)}/; + if(!defined $pri) { + do_log("No primary OID found for $test test for $device", 0); + $msg .= "&yellow No primary OID found.\n"; + $worst_color = 'yellow'; + $dbtable = undef; + next; + } + + # Remove any flags the primary oid might have on it... + $pri =~ s/\..*//; + + + # Make sure we have leaf data for our primary oid + if(!defined $oids->{$pri}{'val'}) { + do_log("Missing repeater data for $pri for $test msg", 0); + $dbtable = undef; + next; + } + + # Make sure our primary OID is a repeater + if(!$oids->{$pri}{'repeat'}) { + do_log("Primary OID $pri in $test table is a non-repeater", 0); + $dbtable = undef; + next; + } + + # If the primary oids leaves are non-numeric, then we cant sort it + # numerically, we'll have to resort to a cmp + my @table_leaves = (); + + if($oids->{$pri}{'repeat'} == 2) { + my @unsorted = keys %{$oids->{$pri}{'val'}}; + @table_leaves = leaf_sort(\@unsorted); + } + # Otherwise sort them numerically ascending + else { + @table_leaves = sort {$a <=> $b} keys %{$oids->{$pri}{'val'}}; + } + + # Now go through all oid vals, using the primary's leaves + T_LEAF: for my $leaf (@table_leaves) { + + my $row_data = $line; + + # Now go through all the oids in our table row and replace them + for my $root ($row_data =~ /\{(.+?)}/g) { + + # Chop off any flags and store them for later + my $oid = $root; + my $flag = $1 if $oid =~ s/\.(.+)$//; + my $oid_h = \%{$oids->{$oid}}; + + # Get our oid vars + my $val = $oid_h->{'repeat'} ? $oid_h->{'val'}{$leaf} : $oid_h->{'val'}; + my $color = $oid_h->{'repeat'} ? $oid_h->{'color'}{$leaf} : $oid_h->{'color'}; + $time = $oid_h->{'repeat'} ? $oid_h->{'time'}{$leaf} : $oid_h->{'time'}; + if (!defined $val) { + do_log("Undefined value for $oid in test $test on $device, ignoring row for $pri_val") if $g{'debug'}; + do_log("Dumped value ^^"); + next T_LEAF; + } + + # Check the exception types, if it is an 'ignore' + # dont include this leaf row if the data for this + # oid matches, if it is an 'only' type, ONLY include + # this leaf row if the data matches + my $ignore = $dev->{'except'}{$test}{$oid}{'ignore'} || + $dev->{'except'}{'all'}{$oid}{'ignore'} || + $tmpl->{'oids'}{$oid}{'except'}{'ignore'}; + my $only = $dev->{'except'}{$test}{$oid}{'only'} || + $dev->{'except'}{'all'}{$oid}{'only'} || + $tmpl->{'oids'}{$oid}{'except'}{'only'}; + next T_LEAF if defined $ignore and $val =~ /^($ignore)$/; + next T_LEAF if defined $only and $val !~ /^($only)$/; + + + # If we arent alarming on a value, its green by default + #$color = 'green' if !$alarm; + + # Keep track of our primary value + if($oid eq $pri) { + + # Add our primary key to our rrd set, if needed + #for my $name (keys %rrd) { + # $rrd{$name}{'pri'} = $oid if $rrd{$name}{'pri'} eq 'pri'; + + # if ($rrd{$name}{'all'} or $alarm) { + # # add to list, but check we're not pushing multiple times + # push @{$rrd{$name}{'leaves'}}, $leaf unless grep {$_ eq $leaf} @{$rrd{$name}{'leaves'}}; + # } + # } + + # If this is our primary oid, and we are have an alarm + # variable defined, add this to our alarm header if we are + # alarming on it; Wrap our header at 60 chars + # if(!defined $t_opts{'noalarmsmsg'} and $alarm) { + # $alarm_ints =~ s/(.{60,}),$/$1)\nAlarming on (/; + # $alarm_ints .= "$val,"; + # } + } + + # See if we have a valid flag, if so, replace the + # place holder with flag data, if not, just replace + # it with the oid value. Also modify the global color + # Display a bb/hobbit color string (i.e. "&red ") + if(defined $flag) { + if($flag eq 'color') { + + # Honor the 'alarm' exceptions + $row_data =~ s/\{$root}/&$color /; + + # If this test has a worse color, use it for the global color + $worst_color = $color if !defined $worst_color or + $colors{$worst_color} < $colors{$color}; + } + + # Display threshold messages if we get the msg flag + elsif ($flag eq 'msg') { + my $substr = $oid_h->{'msg'}{$leaf}; + $substr = 'Undefined' if !defined $substr; + $row_data =~ s/\{$root}/$substr/; + } + + # This flag only causes errors (with the color) to be displayed + # Will also modify global color type + elsif ($flag eq 'errors') { + $row_data =~ s/\{$root}//; + + next if $color eq 'green'; + + # Get oid msg and replace any inline oid dependencies + my $oid_msg = $oid_h->{'msg'}{$leaf}; + $oid_msg = 'Undefined' if !defined $oid_msg; + $oid_msg = parse_deps($oids, $oid_msg, $leaf); + + # If this test has a worse color, use it for the global color + $worst_color = $color if !defined $worst_color or + $colors{$worst_color} < $colors{$color}; + + # Now add it to our msg + #$errors .= "&$color $oid_msg\n"; + } + + # Display color threshold value + elsif ($flag =~ /^thresh\:(\w+)$/i) { + my $th_col = lc $1; + my $thresh = (defined $dev->{'thresh'}{$test}{$oid}{$th_col}) ? + $dev->{'thresh'}{$test}{$oid}{$th_col} : + $oids->{$oid}{'thresh'}{$th_col}{'val'}; + + $thresh = 'Undefined' if !defined $thresh; + $row_data =~ s/\{$root}/$thresh/; + } + + # Uknown flag + else { + do_log("Unknown flag ($flag) for $oid on $device\n"); + } + } + + # Otherwise just display the oid val + else { + my $substr = $oids->{$root}{'repeat'} ? $oids->{$root}{'val'}{$leaf} : $oids->{$root}{'val'}; + $substr = 'Undefined' if !defined $substr; + $row_data =~ s/\{$root}/$substr/; + } + + } + + @dbvals = split /\|/, $row_data; + for (my $i=1;$i<=$#dbkeys;$i++) { + do_log("DBTABLE: preparing row for $dbkeys[$i]"); + my $query = "insert into test_data (host,test,instance,attr,val,time) values ('$device','$test','$dbvals[0]','$dbkeys[$i]','$dbvals[$i]',$time) on duplicate key update val=values(val),time=values(time)"; + do_log("DBTABLE: Running query $query"); + db_do($query); + } + do_log("DBTABLE: Finished with line $line"); + } + do_log("DBTABLE: Finished with test $test"); + $dbtable = undef; + } # Not table data, so it should be a non-repeater type variable else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |