Fork me on GitHub
Translations of this page:

NDO to PNP

Contributed by Yannig Perré

Benutzung dieses Scripts

Das Ziel dieses Scripts ist es, Ihre NDO-Daten direkt in PNP4Nagios zu importieren. Um dies zu tun, benutzen wir einen Exporter (ndo2pnp.pl), der sich mit der MySQL-Datenbank verbindet und den Inhalt im gleichen Format exportiert wie die Performance-Daten von Nagios. Dann werden diese Daten mit Hilfe der Bulk-Option in PNP4Nagios importiert.

  $ ./ndo2pnp.pl --help
  Usage :
    -h  --help            Display this message.
        --version         Display version then exit.
    -v  --verbose         Verbose run.
    -u  --user <NDOUSER>  Log on to database with <NDOUSER> (default root).
    -p  --pass <PASSWD>   Use <PASSWD> to logon (default gbu2kfe).
    -t  --type <DBTYPE>   Change database type (default mysql).
        --host <DBHOST>   Use <DBHOST> (default localhost).
        --dbname <DB>     Use <DB> for ndo database name (default ndoutils).
        --list-machine    Display machine definition in ndo database.
        --list-service    Show services defined.
        --export-as-pnp   Export ndo content as a bulk file used by process_perfdata.pl.

Importieren der NDO-Daten in PNP4Nagios

So geht es:

  • prüfen Sie zuerst die Datenbankverbindung durch Auflistung Ihrer Services :
  $ ./ndo2pnp.pl --user <MYSQL_USER> -p <MYSQL_PASS> --list-service
  Hostname                       | Service
  -------------------------------+-------------------
  gateway                        | PING
  localhost                      | Current Load
  localhost                      | Current Users
  localhost                      | Disk Space
  localhost                      | HTTP
  localhost                      | SSH
  localhost                      | Total Processes
  • dann exportieren Sie den Datenbankinhalt in eine perfdata-Datei:
  $ ./ndo2pnp.pl --user <MYSQL_USER> -p <MYSQL_PASS> --export-as-pnp > /tmp/perfdata.bulk

Kinder aufgepasst! Dieser Schritt kann ziemlich lange dauern!

Hier ein kleiner Ausschnitt dieses Exports:

  $ more /tmp/perfdata.bulk 
  DATATYPE::SERVICEPERFDATA       TIMET::1277405698[...]SERVICESTATE::WARNING   SERVICESTATETYPE::HARD
  • Jetzt benutzen Sie process_perfdata.pl (als Nagios-Benutzer), um die exportierten Daten in PNP4Nagios zu importieren:
  nagios@nagioshost:~$ /usr/share/pnp4nagios/libexec/process_perfdata.pl -b /tmp/perfdata.bulk --timeout 0

Anmerkungen

 Vergessen Sie nicht die Option **--timeout 0**. Wenn Sie diese Option vergessen, dann werden Sie einen Timeout erhalten, während Sie eine große Menge von Services importieren.
 Benutzen Sie die Option LOG_LEVEL in der Konfigurationsdatei ''process_perfdata.cfg'', um eine Rückmeldung in der Datei ''perfdata.log'' zu erhalten.

ndo2pnp.pl

ndo2pnp.pl
#!/usr/bin/perl
 
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
 
my $version       = 0.1;
my $verbose       = 0;
my $show_help     = 0;
my $show_machine  = 0;
my $show_service  = 0;
my $type          = "mysql";
my $db_name       = "ndoutils";
my $user          = "root";
my $password      = "root";
my $host          = "localhost";
my $export_as_pnp = 0;
 
my $service_table = "nagios_servicechecks";
my $object_table  = "nagios_objects";
my $start_time = 0;
my $end_time = 0;
my $filter_machine = 0;
my $filter_service = 0;
 
sub usage {
  print "Usage :
  -h  --help            Display this message.
      --version         Display version then exit.
  -v  --verbose         Verbose run.
  -u  --user <NDOUSER>  Log on to database with <NDOUSER> (default $user).
  -p  --pass <PASSWD>   Use <PASSWD> to logon (default $password).
  -t  --type <DBTYPE>   Change database type (default $type).
      --host <DBHOST>   Use <DBHOST> (default $host).
      --dbname <DB>     Use <DB> for ndo database name (default $db_name).
      --list-machine    Display machine definition in ndo database.
      --list-service    Show services defined.
      --export-as-pnp   Export ndo content as a bulk file used by process_perfdata.pl.
 
";
}
 
GetOptions(
    "version"       => \$version,
    "v"             => \$verbose,
    "verbose"       => \$verbose,
    "h"             => \$show_help,
    "help"          => \$show_help,
    "u=s"           => \$user,
    "user=s"        => \$user,
    "username=s"    => \$user,
    "p=s"           => \$password,
    "pass=s"        => \$password,
    "password=s"    => \$password,
    "type=s"        => \$type,
    "dbname=s"      => \$db_name,
    "host=s"        => \$host,
    "list-machine"  => \$show_machine,
    "list-service"  => \$show_service,
    "export-as-pnp" => \$export_as_pnp,
    "machines=s"    => \$filter_machine,
    "services=s"    => \$filter_service,
);
 
if($show_help) {
  usage ; exit();
}
 
use DBI;
my $dbh = 0;
 
sub connect_db {
  $dbh = DBI->connect("DBI:$type:$db_name;host=$host", $user, $password) || die "Could not connect to database: $DBI::errstr";
}
 
my $request = 0;
 
sub show_machine {
  $request = "SELECT name1 FROM $object_table WHERE objecttype_id = 1";
  my $sth = $dbh->prepare($request);
  $sth->execute();
  while(my @result = $sth->fetchrow_array()) {
    print $result[0]."\n";
  }
}
 
sub show_service {
  $request = "SELECT name1, name2 FROM $object_table WHERE objecttype_id = 2";
  print STDERR "Hostname                       | Service\n";
  print STDERR "-------------------------------+-------------------\n";
  my $sth = $dbh->prepare($request);
  $sth->execute();
  while(my @result = $sth->fetchrow_array()) {
    printf("%-30s | %s\n", $result[0], $result[1]);
  }
}
 
sub export_as_pnp {
  my %status = (0, "OK", "1", "WARNING", 2, "CRITICAL", 3, "UNKNOWN");
  my %state_type = (0, "SOFT", 1 , "HARD");
  my $request_filter = "1";
  if($start_time) {
    $request_filter .= " AND $service_table.start_time > '$start_time' ";
  }
  if($end_time) {
    $request_filter .= " AND $service_table.start_time <= '$end_time' ";
  }
  if($filter_machine) {
    my @machines = split(/\s*,\s*/, $filter_machine);
    $request_filter .= " AND $object_table.name1 IN ('".join("','", @machines)."') ";
  }
  if($filter_service) {
    my @services = split(/\s*,\s*/, $filter_service);
    $request_filter .= " AND $object_table.name2 IN ('".join("','", @services)."') ";
  }
  $request = "SELECT
  UNIX_TIMESTAMP(start_time), name1, name2, perfdata, command_line, command_args, output, state, state_type
FROM
  $service_table, $object_table
WHERE
  $service_table.service_object_id = $object_table.object_id AND length(perfdata) > 0 AND $request_filter
ORDER BY
  $service_table.start_time
";
  print "$request\n" if($verbose);
  my $sth = $dbh->prepare($request);
  $sth->execute();
  while(my @r = $sth->fetchrow_array()) {
    basename($r[4]) =~ /^(\w+).*/;
    my $cmd = "$1";
    $cmd .= ($r[5] ? "!".$r[5] : "");
    print "DATATYPE::SERVICEPERFDATA\tTIMET::".$r[0]."\tHOSTNAME::".$r[1]."\tSERVICEDESC::".$r[2]."\tSERVICEPERFDATA::".$r[3].
          "\tSERVICECHECKCOMMAND::$cmd\tHOSTSTATE::OK\tHOSTSTATETYPE::HARD\tSERVICESTATE::".$status{$r[7]}."\tSERVICESTATETYPE::".$state_type{$r[8]}."\n";
  }
}
 
my $format = "%s";
if($show_machine) {
  connect_db();
  show_machine();
} elsif($show_service) {
  connect_db();
  show_service();
} elsif($export_as_pnp) {
  connect_db();
  export_as_pnp();
} else {
  print "Nothing to do\n";
  exit;
}
de/pnp-0.6/ndo2pnp.txt · Last modified: 2012/01/17 11:30 by Wolfgang
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0