Changeset 1496

Show
Ignore:
Timestamp:
02/28/08 22:00:17 (4 years ago)
Author:
jo
Message:

munin-limits should work with multilevel-groups now.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • people/jo/multilevel-groups-2/server/Munin.pm.in

    r1495 r1496  
    581581    my $loc  = shift; 
    582582 
    583     while (my $tmpvar = shift @$loc) { 
     583    foreach my $tmpvar (@$loc) { 
    584584        if ($tmpvar !~ /\S/) { 
    585585            ::logger ("Error: munin_get_node: Cannot work on hash node \"$tmpvar\""); 
     
    587587        } 
    588588        return undef if !exists $hash->{$tmpvar}; 
    589         return $hash->{$tmpvar} if @$loc <= 0; 
    590589        $hash = $hash->{$tmpvar}; 
    591590    } 
     591    return $hash; 
    592592} 
    593593 
     
    716716    } elsif ($var =~ /^\s*([^;:\.]+)\s*$/) { 
    717717        $result = munin_set_var_loc ($hash, [$1], $val); 
     718    } elsif ($var =~ /^\s*([^:;]+)$/) { 
     719        my @leftarr = split (/\./, $1); 
     720        $result = munin_set_var_loc ($hash, [@leftarr], $val); 
    718721    } elsif ($var =~ /^\s*(.+)\.([^\.:;]+)$/) { 
    719722        my ($leftstring, $rightstring) = ($1, $2); 
  • people/jo/multilevel-groups-2/server/munin-limits.in

    r1495 r1496  
    2424use Time::HiRes; 
    2525use Text::Balanced qw (extract_multiple extract_delimited extract_quotelike extract_bracketed); 
     26use Data::Dumper; 
    2627 
    2728my $DEBUG=0; 
     
    9697 
    9798if (!defined $config->{'contact'}->{'nagios'}->{'command'} and 
    98         defined $config->{'nsca'}) 
    99 
     99        defined $config->{'nsca'}) { 
    100100    $config->{'contact'}->{'old-nagios'}->{'command'} = "$config->{nsca} $config->{nsca_server} -c $config->{nsca_config} -to 60"; 
    101101    $config->{'contact'}->{'old-nagios'}->{'always_send'} = "critical warning"; 
    102102} 
    103 if (!defined $config->{'contact'}->{'nagios'}->{'always_send'}) 
    104 
     103if (!defined $config->{'contact'}->{'nagios'}->{'always_send'}) { 
    105104    $config->{'contact'}->{'nagios'}->{'always_send'} = "critical warning"; 
    106105} 
     106 
     107my $defaultcontacts = munin_get ($config, "contacts", ""); 
     108if (!length $defaultcontacts) { 
     109    my @tmpcontacts = (); 
     110    foreach my $cont (@{munin_get_children ($config->{"contact"})}) { 
     111        if (munin_get ($cont, "command")) { 
     112            push @tmpcontacts, munin_get_node_name ($cont); 
     113        } 
     114    } 
     115    $defaultcontacts = join (' ', @tmpcontacts); 
     116} 
     117munin_set_var_loc ($config, ["contacts"], $defaultcontacts); 
     118logger ("Debug: Set default \"contacts\" to \"$defaultcontacts\"") if $DEBUG; 
    107119 
    108120# Make array of what needs to be checked 
     
    128140logger("munin-limits finished ($update_time sec)"); 
    129141 
    130 print Data::Dumper ($work_array); 
     142#logger ("Dumper: " . Dumper (%notes)); 
    131143 
    132144exit 0; 
     
    155167        my $warn    = munin_get ($field, "warning", undef); 
    156168        my $crit    = munin_get ($field, "critical", undef); 
    157         my $path    = munin_get_node_loc ($field); 
    158         my $onfield = munin_get_node ($oldnotes, $path); 
     169        my $fpath   = munin_get_node_loc ($field); 
     170        my $onfield = munin_get_node ($oldnotes, $fpath); 
    159171 
    160172        # Skip fields without warning/critical definitions 
    161173        next if (!defined $warn and !defined $crit); 
    162174 
    163         logger ("processing field: $fname") if $DEBUG; 
     175        logger ("processing field: ".join ('::', @$fpath)) if $DEBUG; 
    164176        next if (@limit_services and !grep (/^$service$/, @limit_services)); 
    165177 
     
    180192        $field->{'wrange'} = (defined $warn->[0]?$warn->[0]:"").":".(defined $warn->[1]?$warn->[1]:""); 
    181193 
    182         logger ("value: ". join ('::', @{munin_get_node_loc ($hash)})) if $DEBUG; 
     194        logger ("value: ". join ('::', @{munin_get_node_loc ($hash)}) .": $value (crit: $crit->[0]:$crit->[1]) (warn: $warn->[0]:$warn->[1])") if $DEBUG; 
    183195        if ($value eq "unknown") { 
    184196            $crit->[0] ||= ""; 
     
    186198            $hash->{'worst'} = "UNKNOWN" if $hash->{"worst"} eq "OK"; 
    187199            $hash->{'worstid'} = 3 if $hash->{"worstid"} == 0; 
    188             munin_set_var_loc (\%notes, [$path, "state"], "unknown"); 
    189             munin_set_var_loc (\%notes, [$path, "unknown"], (defined $field->{"extinfo"} ? "unknown: " . $field->{"extinfo"} : "Value is unknown.")); 
     200            munin_set_var_loc (\%notes, [@$fpath, "state"], "unknown"); 
     201            munin_set_var_loc (\%notes, [@$fpath, "unknown"], (defined $field->{"extinfo"} ? "unknown: " . $field->{"extinfo"} : "Value is unknown.")); 
    190202 
    191203            if (!defined $onfield or !defined $onfield->{"state"} or $onfield->{"state"} ne "unknown") { 
    192                 $field->{'state_changed'} = 1; 
     204                $hash->{'state_changed'} = 1; 
    193205            } 
    194206        } elsif ((defined ($crit->[0]) and $value < $crit->[0]) or 
     
    198210            $hash->{'worst'} = "CRITICAL"; 
    199211            $hash->{'worstid'} = 2; 
    200             munin_set_var_loc (\%notes, [$path, "state"], "critical"); 
    201             munin_set_var_loc (\%notes, [$path, "critical"],  
     212            munin_set_var_loc (\%notes, [@$fpath, "state"], "critical"); 
     213            munin_set_var_loc (\%notes, [@$fpath, "critical"],  
    202214                (defined $field->{"extinfo"}?  
    203215                "$value (not in $crit->[0]:$crit->[1]): ". 
     
    206218 
    207219            if (!defined $onfield or !defined $onfield->{"state"} or $onfield->{"state"} ne "critical") { 
    208                 $field->{'state_changed'} = 1; 
     220                $hash->{'state_changed'} = 1; 
    209221            } 
    210222        } elsif ((defined ($warn->[0]) and $value < $warn->[0]) or  
    211                 (defined ($warn->[1]) and $value > $warn->[1]))  
    212         { 
     223                (defined ($warn->[1]) and $value > $warn->[1])) { 
    213224            $warn->[0] ||= ""; 
    214225            $warn->[1] ||= ""; 
    215226            $hash->{'worst'} = "WARNING" if $hash->{"worst"} ne "CRITICAL"; 
    216227            $hash->{'worstid'} = 1 if $hash->{"worstid"} != 2; 
    217             munin_set_var_loc (\%notes, [$path, "state"], "warning"); 
    218             munin_set_var_loc (\%notes, [$path, "warning"],  
     228            munin_set_var_loc (\%notes, [@$fpath, "state"], "warning"); 
     229            munin_set_var_loc (\%notes, [@$fpath, "warning"],  
    219230                (defined $field->{"extinfo"}? 
    220231                "$value (not in $warn->[0]:$warn->[1]): ". 
     
    226237            } 
    227238        } elsif (defined $onfield and defined $onfield->{"state"} or $force) { 
    228             munin_set_var_loc (\%notes, [$path, "state"], "ok"); 
    229             munin_set_var_loc (\%notes, [$path, "warning"], "OK"); 
     239            munin_set_var_loc (\%notes, [@$fpath, "state"], "ok"); 
     240            munin_set_var_loc (\%notes, [@$fpath, "ok"], "OK"); 
    230241            $hash->{'state_changed'} = 1; 
    231242        } 
     
    273284    my $worst      = $hash->{"worst"}; 
    274285    my %stats      = ('critical' => [], 'warning' => [], 'unknown' => [], 'foks' => [], 'ok' => []); 
    275     my $contacts   = munin_get_children (munin_get_node ($config, ["contacts"])); 
     286    my $contacts   = munin_get_children (munin_get_node ($config, ["contact"])); 
    276287 
    277288    logger ("generating service message: ". join ('::', @{munin_get_node_loc ($hash)})) if $DEBUG; 
     
    295306    $hash->{'numofields'}  = scalar @{$stats{'ok'}}; 
    296307 
    297     my $defaultcontacts = join (' ', map { munin_get_node_name ($_) } @$contacts); 
    298     foreach my $c (split (/\s+/, munin_get ($hash, "contacts", $defaultcontacts))) { 
     308    my $contactlist =  munin_get ($hash, "contacts", ""); 
     309    logger ("Contact list for ". join ('::', @{munin_get_node_loc ($hash)}) . ": $contactlist"); 
     310    foreach my $c (split (/\s+/, $contactlist)) { 
    299311        next if $c eq "none"; 
    300312        my $contactobj = munin_get_node ($config, ["contact", $c]); 
     
    309321            $obsess = grep {scalar(@{$stats{$_}})} (split (/\s+/, lc $cas)); 
    310322        } 
     323        logger ("checking if state has changed $c"); 
    311324        if (!$hash->{'state_changed'} and !$obsess) { 
    312325            next; # No need to send notification 
    313326        } 
     327        logger ("state has changed, notifying $c"); 
    314328        my $precmd = munin_get ($contactobj, "command"); 
    315329        my $pretxt = munin_get ($contactobj, "text", munin_get (munin_get_node ($config, ["contact", "default"]), "text", $default_text{$c} || $default_text{"default"})); 
    316         my $txt = message_expand ($pretxt, $hash, ""); 
    317         my $cmd = message_expand ($precmd, $hash, ""); 
     330        my $txt = message_expand ($hash, $pretxt, ""); 
     331        my $cmd = message_expand ($hash, $precmd, ""); 
    318332        $txt =~ s/\\n/\n/g; 
    319333        $txt =~ s/\\t/\t/g; 
     
    327341            close ($pipe); 
    328342            $pipe = undef; 
    329             munin_set_var ($contactobj, "pipe", undef); 
     343            munin_set_var_loc ($contactobj, ["pipe"], undef); 
     344            logger ("Debug: Closing \"$c\" -> command (max number of messages reached).") if $DEBUG; 
    330345        } elsif ($curcmd and $curcmd ne $cmd) { 
    331346            close ($pipe); 
    332347            $pipe = undef; 
    333             munin_set_var ($contactobj, "pipe", undef); 
     348            munin_set_var_loc ($contactobj, ["pipe"], undef); 
     349            logger ("Debug: Closing \"$c\" -> command (command has changed).") if $DEBUG; 
    334350        } 
    335351     
    336352        if (!defined $pipe) { 
    337353            my @cmd = extract_multiple ( 
    338                     message_expand ($cmd), 
     354                    message_expand ($hash, $cmd), 
    339355                    [ sub { extract_delimited ($_[0], q{"'})}, 
    340356                      qr/\S+/ 
     
    342358                    undef, 1); 
    343359            @cmd = map { s/['"]$//; s/^['"]//; $_ } @cmd; 
    344             munin_set_var ($contactobj, "num_messages", 0)
     360            $contactobj->{"num_messages"} = 0
    345361            if ($cmd[0] eq "|") { 
    346362                $cmd[0] = "|-"; 
     
    348364                unshift (@cmd, "|-"); 
    349365            } 
    350             logger ("Debug: opening for writing: \"" . join('" "',@cmd) . "\".") if $DEBUG; 
     366            logger ("Debug: opening \"$c\" for writing: \"" . join('" "',@cmd) . "\".") if $DEBUG; 
    351367            if ($cmd[0] eq ">") { 
    352368                if (! open ($pipe, join (' ', @cmd))) { 
     
    396412                } 
    397413            } 
    398             munin_set_var ($contactobj, "pipe_command", $cmd); 
    399             munin_set_var ($contactobj, "pipe", $pipe); 
     414            logger ("baz?"); 
     415            munin_set_var_loc ($contactobj, ["pipe_command"], $cmd); 
     416            munin_set_var_loc ($contactobj, ["pipe"], $pipe); 
    400417        }  
    401418        logger ("sending message: \"$txt\"") if ($DEBUG); 
     419        logger ("bar?"); 
    402420        print $pipe $txt, "\n" if (defined $pipe); 
    403         munin_set_var ($contactobj, "num_messages", 1 + munin_get ($contactobj, "num_messages", 0)); # $num_messages++ 
     421        $contactobj->{"num_messages"} = 1 + munin_get ($contactobj, "num_messages", 0); # $num_messages++ 
    404422    } 
    405423}