Changeset 1353

Show
Ignore:
Timestamp:
10/24/07 21:54:17 (4 years ago)
Author:
jo
Message:

Some cleanups after a lot of coding.

Files:

Legend:

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

    r1344 r1353  
    3030use Getopt::Long; 
    3131use Time::HiRes; 
    32 use Data::Dumper qw(Dumper); 
    3332 
    3433my $graph_time= Time::HiRes::time; 
     
    917916    print STATS "GS|$service_time\n" unless $skip_stats; 
    918917 
    919     print "Final look of service subtree: ", Dumper (munin_get_separated_node ($service)), "\n\n"; 
    920918    foreach (@added) { 
    921919        delete $service->{$_} if exists $service->{$_}; 
  • people/jo/multilevel-groups-2/server/munin-html.in

    r1352 r1353  
    2828use Time::HiRes; 
    2929use Munin; 
     30use POSIX qw(strftime); 
    3031use Data::Dumper; 
    31 use POSIX qw(strftime); 
    3232 
    3333my @times = ( "day", "week", "month", "year" ); 
     
    166166} 
    167167 
    168 # Make array of what services 
    169 my $work_array = []; 
    170 if (@limit_hosts) { # Limit what to update if needed 
    171         foreach my $nodename (@limit_hosts) { 
    172                     push @$work_array, map { @{munin_find_field ($_->{$nodename}, "graph_title")} } @{munin_find_field($config, $nodename)}; 
    173                         } 
    174 } else { # ...else just search for all adresses to update 
    175         push @$work_array, @{munin_find_field($config, "graph_title")}; 
    176 } 
    177  
    178168# For timestamping graphs 
    179169my $timestamp = strftime("%Y-%m-%d T %T", localtime); 
    180  
    181 # Draw service pages 
    182 for my $service (@$work_array) { 
    183  
    184     next unless munin_get_bool ($service, "graph", 1); 
    185  
    186     my %srv; 
    187     my $fieldnum = 0; 
    188     my @graph_info; 
    189     my @field_info; 
    190     my @loc       = munin_get_node_loc ($service); 
    191     my $pathnodes = get_path_nodes ($service); 
    192     my $peers     = get_peer_nodes ($service, lc munin_get ($service, "graph_category", "other")); 
    193     (my $csspath  = $pathnodes->[0]->{'link'}) =~ s/index.html$/style.css/; 
    194  
    195     $srv{'node'}     = munin_get_node_name ($service); 
    196     logger("processing service: $srv{node}"); 
    197     $srv{'service'}  = $service; 
    198     $srv{'label'}    = munin_get ($service, "graph_title"); 
    199     $srv{'category'} = lc( munin_get ($service, "graph_category", "other") ); 
    200  
    201     my $method = munin_get ($service, "graph_strategy", "cron"); 
    202  
    203     $srv{'url'}      = "$srv{node}.html"; 
    204  
    205     my $path = join ('/', @loc); 
    206  
    207     if ($method eq "cgi") { 
    208         $srv{'imgday'}  =$config->{'cgiurl_graph'}."/$path-day.png"; 
    209         $srv{'imgweek'} =$config->{'cgiurl_graph'}."/$path-week.png"; 
    210         $srv{'imgmonth'}=$config->{'cgiurl_graph'}."/$path-month.png"; 
    211         $srv{'imgyear'} =$config->{'cgiurl_graph'}."/$path-year.png"; 
    212  
    213         if (munin_get_bool ($service, "graph_sums", 0)) { 
    214             $srv{'imgweeksum'} = $config->{'cgiurl_graph'}."/$path-week-sum.png"; 
    215             $srv{'imgyearsum'} = $config->{'cgiurl_graph'}."/$path-year-sum.png"; 
    216         } 
    217     } else { 
    218         # graph strategy cron 
    219           
    220         $srv{'imgday'}  ="$srv{node}-day.png"; 
    221         $srv{'imgweek'} ="$srv{node}-week.png"; 
    222         $srv{'imgmonth'}="$srv{node}-month.png"; 
    223         $srv{'imgyear'} ="$srv{node}-year.png"; 
    224  
    225         for my $scale (@times) { 
    226             if (my ($w, $h) = get_png_size(munin_get_picture_filename($service, $scale))) { 
    227                 $srv{"img".$scale."width"} = $w; 
    228                 $srv{"img".$scale."height"} = $h; 
    229             } 
    230         } 
    231  
    232         if (munin_get_bool ($service, "graph_sums", 0)) { 
    233             $srv{imgweeksum} = "$srv{node}-week-sum.png"; 
    234             $srv{imgyearsum} = "$srv{node}-year-sum.png"; 
    235             for my $scale (["week", "year"]) { 
    236                 if (my ($w, $h) = get_png_size (munin_get_picture_filename($service, $scale, 1))) { 
    237                     $srv{"img".$scale."sumwidth"} = $w; 
    238                     $srv{"img".$scale."sumheight"} = $h; 
    239                 } 
    240             } 
    241         } 
    242     } 
    243  
    244     # Do "help" section 
    245     if (my $info = munin_get ($service, "graph_info")) { 
    246         my %graph_info; 
    247         $graph_info{info} = $info; 
    248         push @{$srv{graphinfo}}, \%graph_info; 
    249     } 
    250  
    251     $srv{fieldlist} .= "<tr><th align='left' valign='top'>Field</th><th align='left' valign='top'>Type</th><th align='left' valign='top'>Warn</th><th align='left' valign='top'>Crit</th><th></tr>"; 
    252     foreach my $f (@{munin_get_field_order ($service)}) { 
    253         $f =~ s/=(.*)$//; 
    254         my $path = $1; 
    255         next if (!defined $service->{$f}); 
    256         my $fieldobj = $service->{$f}; 
    257         next if (ref ($fieldobj) != "HASH" or !defined $fieldobj->{'label'}); 
    258         next if (!munin_draw_field ($fieldobj)); 
    259  
    260         logger ("DEBUG: single_value: Checking field \"$f\" ($path).") 
    261             if $DEBUG; 
    262  
    263         if (defined $path) { 
    264             # This call is to make sure field settings are copied 
    265             # for aliases, .stack, et al. Todo: put that part of 
    266             # munin_get_rrd_filename into its own functino. 
    267             munin_get_rrd_filename ($f, $path); 
    268         } 
    269  
    270         my %field_info; 
    271         $fieldnum++; 
    272  
    273         $field_info{'hr'}    = 1 unless ($fieldnum % 3); 
    274         $field_info{'field'} = $f; 
    275         $field_info{'label'} = munin_get ($fieldobj, "label", $f); 
    276         $field_info{'type'}  = lc( munin_get ($fieldobj, "type", "GAUGE") ); 
    277         $field_info{'warn'}  = munin_get ($fieldobj, "warning"); 
    278         $field_info{'crit'}  = munin_get ($fieldobj, "critical"); 
    279         $field_info{'info'}  = munin_get ($fieldobj, "info"); 
    280  
    281         my $state = munin_field_status ($config, $limits, 1); 
    282  
    283         if (defined $state) { 
    284             $field_info{'state_warning'}  = 1 if $state eq "warning"; 
    285             $field_info{'state_critical'} = 1 if $state eq "critical"; 
    286             $field_info{'state_unknown'}  = 1 if $state eq "unknown"; 
    287         } 
    288         push @{$srv{'fieldinfo'}}, \%field_info; 
    289     } 
    290  
    291     my $state = munin_service_status ($service, $limits, 1); 
    292     if (defined $state) { 
    293         $srv{'state_warning'}  = 1 if $state eq "warning"; 
    294         $srv{'state_critical'} = 1 if $state eq "critical"; 
    295         $srv{'state_unknown'}  = 1 if $state eq "unknown"; 
    296     } 
    297  
    298     $servicetemplate->param(SERVICES  => [\%srv], 
    299                             PATH      => $pathnodes, 
    300                             PEERS     => $peers, 
    301                             CSS       => $csspath, 
    302                             CATEGORY  => ucfirst $srv{'category'}, 
    303                             TIMESTAMP => $timestamp); 
    304     my $filename = munin_get_html_filename ($service); 
    305     open (FILE, ">$filename") or 
    306         die "Cannot open $filename for writing: $!"; 
    307     print FILE $servicetemplate->output; 
    308     close FILE; 
    309 } 
    310170 
    311171# Preparing the group tree... 
     
    325185 
    326186generate_group_templates ($groups); 
    327  
    328 exit 0; 
    329 #make domain list 
    330 my @domainlist = map { { DOMAIN => $_ } } @domainorder; 
    331 my $timestamp = strftime("%Y-%m-%d T %T", localtime); 
    332 for my $domain (@domainorder) { 
    333   logger("processing domain: $domain"); 
    334   my %domain; 
    335   $domain{domain}=$domain; 
    336   my @nodes; 
    337   my %comparisons; 
    338   my @nodeorder = (); 
    339   if ($config->{domain}->{$domain}->{node_order}) { 
    340     @nodeorder = split /\s+/, $config->{domain}->{$domain}->{node_order}; 
    341   } 
    342   foreach my $n (sort (keys %{$config->{domain}->{$domain}->{node}})) { 
    343     unless (grep (/^$n$/, @nodeorder)) { 
    344       push @nodeorder, $n; 
    345     } 
    346   } 
    347   for my $node (@nodeorder) { 
    348     logger("processing node: $node"); 
    349     my %node; 
    350     $node{node}=$node; 
    351     $node{url}="$domain/$node.html"; 
    352     my @services; 
    353     my @categories; 
    354     my %categories; 
    355     my %tmp_cats; 
    356     my @serviceorder; 
    357     if ($config->{domain}->{$domain}->{node}->{$node}->{service_order}) { 
    358       @serviceorder = split /\s+/, 
    359         $config->{domain}->{$domain}->{node}->{$node}->{service_order}; 
    360     } else { 
    361       @serviceorder = sort 
    362         keys %{$config->{domain}->{$domain}->{node}->{$node}->{client}}; 
    363     } 
    364  
    365     foreach my $key (keys %tmp_cats) { 
    366       next if $key eq "other"; 
    367       my %tmp; 
    368       my $state = &munin_category_status ($config, $limits, $domain, 
    369                                           $node, $key, 1); 
    370       print "DEBUG: Pushing category \"$node\" -> \"$key\"...\n" if $DEBUG; 
    371       if (defined $state) { 
    372         $tmp{'state_warning'}  = 1 if $state eq "warning"; 
    373         $tmp{'state_critical'} = 1 if $state eq "critical"; 
    374         $tmp{'state_unknown'}  = 1 if $state eq "unknown"; 
    375       } 
    376       $tmp{name} = ucfirst $key; 
    377       $tmp{services} = \@{$tmp_cats{$key}}; 
    378       $tmp{node} = $node; 
    379       $tmp{domain} = $domain; 
    380       $categories{ucfirst $key} = \%tmp; 
    381       $comparisons{$key}{$node} = \%tmp; 
    382     } 
    383     if (defined $tmp_cats{'other'}) { 
    384       my $key = 'other'; 
    385       my %tmp; 
    386       my $state = &munin_category_status ($config, $limits, $domain, 
    387                                           $node, $key, 1); 
    388       print "DEBUG: Pushing category \"$node\" -> \"$key\"...\n" if $DEBUG; 
    389       if (defined $state) { 
    390         $tmp{'state_warning'}  = 1 if $state eq "warning"; 
    391         $tmp{'state_critical'} = 1 if $state eq "critical"; 
    392         $tmp{'state_unknown'}  = 1 if $state eq "unknown"; 
    393       } 
    394       $tmp{name} = ucfirst $key; 
    395       $tmp{services} = \@{$tmp_cats{$key}}; 
    396       $tmp{node} = $node; 
    397       $tmp{domain} = $domain; 
    398       $categories{ucfirst $key} = \%tmp; 
    399       $comparisons{'other'}{$node} = \%tmp; 
    400     } 
    401     # Handle category_order 
    402     @categories = (); 
    403     if ($config->{domain}->{$domain}->{node}->{$node}->{category_order}) { 
    404       foreach my $cat (split /\s+/, 
    405                        $config->{domain}->{$domain}-> 
    406                        {node}->{$node}->{category_order}) { 
    407         push @categories, $categories{ucfirst $cat}; 
    408       } 
    409       foreach my $cat (sort keys %categories) { 
    410         push @categories, $categories{$cat} 
    411           unless (grep { $_->{name} eq $cat } @categories); 
    412       } 
    413     } else { 
    414       @categories = map { $categories{$_} } sort keys %categories; 
    415     } 
    416     $nodetemplate->param(SERVICES => \@services, 
    417                          NODE => $node, 
    418                          DOMAIN => $domain, 
    419                          DOMAINS => \@domainlist, 
    420                          TIMESTAMP => $timestamp, 
    421                          CATEGORIES => \@categories); 
    422     open (FILE, ">$config->{htmldir}/$domain/$node.html") or 
    423       die "Cannot open $config->{htmldir}/$domain/$node.html: $!"; 
    424     print FILE $nodetemplate->output; 
    425     close FILE; 
    426     $node{services} = \@services; 
    427     $node{categories} = \@categories; 
    428     $node{domain} = $domain; 
    429     push @nodes,\%node; 
    430  
    431   } 
    432   $domaintemplate->param(NODES => \@nodes, 
    433                          DOMAIN => $domain,  
    434                          DOMAINS => \@domainlist,  
    435                          COMPARE => (&munin_get_bool ($config, "compare", 1, 
    436                                                       $domain) 
    437                                      and @nodeorder > 1), 
    438                          TIMESTAMP => $timestamp); 
    439   open (FILE, ">$config->{htmldir}/$domain/index.html") or 
    440     die "Cannot open $config->{htmldir}/$domain/index.html: $!"; 
    441   print FILE $domaintemplate->output; 
    442   close FILE; 
    443  
    444   $domain{nodes} = \@nodes; 
    445   $domain{domain} = $domain; 
    446   $domain{compare} = (&munin_get_bool ($config, "compare", 1, $domain)  
    447                       and @nodeorder > 1); 
    448  
    449   my @cats = (); 
    450   foreach my $key (sort keys %comparisons) { 
    451     my %cat; 
    452     my %servlist; 
    453     my $nodewidth = 0; 
    454  
    455     foreach my $node (sort keys %{$comparisons{$key}}) { 
    456       foreach my $serv (@{$comparisons{$key}{$node}->{services}}) { 
    457         $servlist{$serv->{service}}{$node} = $serv; 
    458         $nodewidth = $serv->{imgdaywidth} 
    459           if (defined $serv->{imgdaywidth} 
    460               and $serv->{imgdaywidth} > $nodewidth); 
    461       } 
    462     } 
    463     foreach my $sname (sort keys %servlist) { 
    464       my %s; 
    465       foreach my $node (@nodeorder) { 
    466         if (defined $servlist{$sname}{$node}) { 
    467           $servlist{$sname}{$node}->{width} = $nodewidth; 
    468           push (@{$s{nodes}}, $servlist{$sname}{$node}); 
    469         } else { 
    470           my %ts; 
    471           $ts{label} = "Not present"; 
    472           $ts{service} = "$sname"; 
    473           $ts{title} = "Not present"; 
    474           $ts{node} = $node; 
    475           $ts{width} = $nodewidth; 
    476           push (@{$s{nodes}}, \%ts); 
    477         } 
    478       } 
    479       push @{$cat{services}}, \%s; 
    480     } 
    481     $cat{name} = ucfirst $key; 
    482     $cat{numnodes} = @nodeorder; 
    483     $cat{numnodes} = @nodeorder; 
    484     push @cats, \%cat; 
    485   } 
    486  
    487   if (&munin_get_bool ($config, "compare", 1, $domain) and @nodeorder > 1) { 
    488     foreach my $t (@times) { 
    489       $comparisontemplates{$t}->param(DOMAIN => $domain, 
    490                                       DOMAINS => \@domainlist, 
    491                                       TIMESTAMP => $timestamp, 
    492                                       CATEGORIES => \@cats); 
    493       open (FILE, ">$config->{htmldir}/$domain/comparison-$t.html") or 
    494         die "Cannot open $config->{htmldir}/$domain/comparison-$t.html: $!"; 
    495       print FILE $comparisontemplates{$t}->output; 
    496       close FILE; 
    497     } 
    498   } 
    499   push @domains,\%domain; 
    500 } 
    501  
    502 $template->param(DOMAINS => \@domains,  
    503                  TIMESTAMP => $timestamp); 
    504 open (FILE, ">$config->{htmldir}/index.html") or die "Cannot open $config->{htmldir}/index.html: $!"; 
    505 print FILE $template->output; 
    506 close FILE; 
    507187 
    508188sub logger_open { 
     
    659339 
    660340    foreach my $child (@{munin_get_children ($hash)}) { 
     341        next unless defined $child and ref ($child) eq "HASH" and $child; 
    661342        if (defined $child->{"graph_title"}) { 
    662343            my $childname = munin_get_node_name ($child); 
     
    682363        $obj->{'state_'.lc munin_category_status ($hash, $limits, $cat, 1)} = 1; 
    683364        for (my $shrinkpath = $obj->{'url'}, my $counter = 0; $shrinkpath =~ /\//; $shrinkpath =~ s/^[^\/]+\/*//, $counter++) { 
    684             print "DEBUG: Left is $shrinkpath\n"; 
    685365            $obj->{'url'.$counter} = $shrinkpath; 
    686366        } 
     
    707387        "peers"       => get_peer_nodes ($hash), 
    708388    }; 
    709     print "DEBUG: Url for ", $ret->{'name'}, "is ", $ret->{'url'}, "\n"; 
    710389    if ($ret->{'url'} ne "/index.html") { 
    711390        for (my $shrinkpath = $ret->{'url'}, my $counter = 0; $shrinkpath =~ /\//; $shrinkpath =~ s/^[^\/]+\/*//, $counter++) { 
     
    721400    return undef unless ref ($arr) eq "ARRAY"; 
    722401 
    723     print Dumper ($arr); 
    724      
    725402    foreach my $key (@$arr) { 
    726403        if (defined $key and ref ($key) eq "HASH") { 
    727404            if (defined $key->{'ngroups'} and $key->{'ngroups'}) { 
    728                 print "DEBUG: *** ".$key->{'name'}."\n"; 
    729405                generate_group_templates ($key->{'groups'}); 
    730406 
  • people/jo/multilevel-groups-2/server/munin-update.in

    r1344 r1353  
    3333use POSIX ":sys_wait_h"; 
    3434use Storable qw(fd_retrieve nstore_fd); 
    35 use Data::Dumper qw(Dumper); 
    3635 
    3736my $TIMEOUT = 240;