Ignore:
Timestamp:
Jun 25, 2018, 10:30:10 AM (6 years ago)
Author:
g7moreau
Message:
  • First try to make remote command more generic...
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/nagios-velvice/velvice.cgi

    r244 r245  
    1010# Possible command http://old.nagios.org/developerinfo/externalcommands/commandlist.php
    1111#
    12 # apt-get install libnagios-object-perl libhtml-parser-perl perl-modules liburi-encode-perl libcolor-calc-perl libyaml-syck-perl libhash-merge-perl
     12# apt-get install libnagios-object-perl libhtml-parser-perl perl-modules liburi-encode-perl libcolor-calc-perl libyaml-syck-perl
    1313
    1414use strict;
     
    2222use Color::Calc ();
    2323use YAML::Syck;
    24 #use Hash::Merge;
    2524
    2625my $config = {};
     
    3534$config->{'downtime'}{'max'}     ||= 50;
    3635$config->{'downtime'}{'factor'}  ||= 0.7;
     36$config->{'service'}             ||= {};
    3737
    3838my $query = CGI->new();
     
    157157   my %sshdown       = ();
    158158   my @aptuptodate   = ();
     159   my %cmdafter      = ();
     160   my $after;
     161
    159162   my $current_host  = '';
    160163   $htmlpage .= "<table border=\"1\">\n";
     
    162165      my $hostname = $srv->host_name;
    163166      my $service  = $srv->service_description;
    164       my $level    = $srv->status;
     167      my $status   = $srv->status;
    165168      my $downtime = downtime($srv->last_state_change);
    166169      my $output   = HTML::Entities::encode($srv->plugin_output);
    167170      $output =~ s/^[A-Z_\s]+?[:-]//;
    168171
    169       my $color = $level eq 'CRITICAL' ? '#F88888' : '#FFFF00';
     172      my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00';
    170173      $color = alertcolor($color, $downtime);
    171174      $htmlpage .= " <tr style='background:$color;'>\n";
     
    174177         $htmlpage .= "  <td rowspan='$hostcount{$hostname}' style='vertical-align:middle;'><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n";
    175178         }
    176       if ($service eq 'APT UPTODATE') {
    177          $htmlpage .= "  <td><b>$service</b></td>\n";
    178          }
    179       else {
    180          $htmlpage .= "  <td>$service</td>\n";
    181          }
    182       $htmlpage .= "  <td>$level</td>\n";
     179
     180      my $bold;
     181      for my $srv_name (keys %{$config->{'service'}}) {
     182         my $srv_regex = $config->{'service'}{$srv_name}{'regex'};
     183         $bold++ if $service =~ m/$srv_regex/ and $config->{'service'}{$srv_name}{'style'} eq 'bold';
     184         }
     185      $htmlpage .= "  <td>";
     186      $htmlpage .= "<b>" if $bold;
     187      $htmlpage .= "$service";
     188      $htmlpage .= "</b>" if $bold;
     189      $htmlpage .= "</td>\n";
     190
     191      $htmlpage .= "  <td>$status</td>\n";
    183192      $htmlpage .= "  <td style='max-width:60%;'><small>$output";
    184193
    185194      $sshdown{$hostname}++ if $service eq 'SSH';
    186195
    187       #      or ($check =~ m/apt/i      and $service eq 'APT UPTODATE')
    188196      if (($check =~ m/all/i)
    189197            or ($check =~ m/^$service$/i)
    190             or ($check =~ m/critical/i and $level  eq 'CRITICAL')
    191             or ($check =~ m/warning/i  and $level  eq 'WARNING')
    192             or ($check =~ m/pending/i  and $level  eq 'PENDING')
     198            or ($check =~ m/critical/i and $status eq 'CRITICAL')
     199            or ($check =~ m/warning/i  and $status eq 'WARNING')
     200            or ($check =~ m/pending/i  and $status eq 'PENDING')
    193201            ) {
    194202         $now++;
     
    205213
    206214      push @aptuptodate, $hostname if $service eq 'APT UPTODATE';
    207       push @oomkiller,   $hostname if $service eq 'OOM Killer' and $level ne 'PENDING';
     215      push @oomkiller,   $hostname if $service eq 'OOM Killer' and $status ne 'PENDING';
     216      for my $srv_name (keys %{$config->{'service'}}) {
     217         my $srv_regex  = $config->{'service'}{$srv_name}{'regex'};
     218         my $srv_status = $config->{'service'}{$srv_name}{'status'};
     219         if ($service =~ m/$srv_regex/ and ($srv_status eq 'ALL' or $status =~ m/$srv_status/)) {
     220            $cmdafter{$srv_name} ||= [];
     221            push @{$cmdafter{$srv_name}}, $hostname;
     222            $after++;
     223            }
     224         }
    208225
    209226      $htmlpage .= "</small></td>\n";
     
    236253      }
    237254
    238    if (@oomkiller or @aptuptodate) {
     255   if (@oomkiller or @aptuptodate or $after) {
    239256      require Nagios::Object::Config;
    240257      my $parser = Nagios::Object::Config->new();
    241258      $parser->parse("/var/cache/nagios3/objects.cache");
     259
     260      for my $srv_name (keys %cmdafter) {
     261         my @action = grep !exists $sshdown{$_}, @{$cmdafter{$srv_name}};
     262         if (@action) {
     263            $htmlpage .= "<h2>Action: $srv_name</h2>\n";
     264            $htmlpage .= "<pre>\n";
     265            my $remote_action = $config->{'service'}{$srv_name}{'command'};
     266            $remote_action = $config->{'service'}{$srv_name}{'command-one'} if @action == 1;
     267            my @hosts;
     268            for my $host (@action) {
     269               my $object = $parser->find_object("$host", "Nagios::Host");
     270               push @hosts, hostmapping($object->address =~ s/\..*$//r);
     271               }
     272            my $hosts_list = join ' ', @hosts;
     273            $htmlpage .= $remote_action =~ s{\%m}{$hosts_list}r;
     274            $htmlpage .= "</pre>\n";
     275            }
     276         }
    242277
    243278      @oomkiller = grep !exists $sshdown{$_}, @oomkiller;
Note: See TracChangeset for help on using the changeset viewer.