nagios介绍及Server安装(四)
脚本用perl写的,最前面db的部分需要修改,代码如下:#!/usr/bin/perl
### ====================================================
## File name: insert_host.pl
## Use: insert host into centreondatabase
### ====================================================
use strict;
use warnings;
use DBI;
use DBD::mysql;
# ----------------------------------------------------
my $DB_HOST = "10.199.95.165";
my $DB_USER = "centreon";
my $DB_PASSWD = "centreon";
my $DB_NAME = "centreon";
my $dbh= DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST","$DB_USER", "$DB_PASSWD", { RaiseError=> 1 });
# ----------------------------------------------------
my $file_path = "hosts";
my $tpl_name= "generic-host";
my $nagios_name= "Central";
while (defined(my $arg= shift)) {
if ($arg eq '-f') {
$file_path= shift;
}
# == name of template ==
elsif($arg eq '-t') {
$tpl_name= shift;
}
# == name of nagiosname ==
elsif($arg eq '-n') {
$nagios_name= shift;
}
else {
&print_help();
exit 1;
}
}
# -----------------------------------------------------
open (HOST, "$file_path")|| die "Cannot open $file_path for read $!";
my $sql;
my $sth;
my $line;
my ($host, $ipaddr);
my ($host_id,$tpl_id,$nagios_id)= (0, 0, 0);
while (defined($line = )) {
# == skip blank lines =================
next if ($line =~ /^\s*$/);
# == skip if # ========================
next if ($line =~ /^\s*#/);
# == get host and ipaddress===========
($ipaddr,$host) = split(/\s+/, $line);
next if ($ipaddr eq '' || $host eq '');
# == insert the host to table host ====
$sql="insert host set host_template_model_htm_id='2',host_name='$host',host_alias='$host',host_address='$ipaddr',host_active_checks_enabled='2',host_passive_checks_enabled='2',host_checks_enabled='2',host_event_handler_enabled='2',host_flap_detection_enabled='2',host_process_perf_data='2',host_retain_status_information='2',host_retain_nonstatus_information='2',host_notifications_enabled='2',host_register='1',host_activate='1',host_obsess_over_host='2',host_check_freshness='2'";
$sth= $dbh->do($sql);
sleep(1);
# == get host_id======================
$sql= "select host_id from host where host_name='$host'";
$sth= $dbh->prepare($sql);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()){
$host_id= $ref->{'host_id'};
print "host_idis $host_id\n";
}
next if ($host_id== 0);
# == insert extended_host_information==
$sql= "insert extended_host_information set host_host_id='$host_id'";
$sth= $dbh->do($sql);
# == insert host_template_relation=====
$sql= "select host_id from host where host_name='$tpl_name'";
$sth= $dbh->prepare($sql);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()){
$tpl_id= $ref->{'host_id'};
print"template id is $tpl_id\n";
}
next if ($tpl_id== 0);
$sql= "insert host_template_relation set host_host_id='$host_id',host_tpl_id='$tpl_id',`order`='1'";
$sth= $dbh->prepare($sql);
$sth->execute();
# == insert ns_host_relation===========
$sql= "select id from nagios_server where name='$nagios_name'";
$sth = $dbh->prepare($sql);
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()){
$nagios_id=$ref->{'id'};
print "nagiosid is $nagios_id\n";
}
next if ($nagios_id== 0);
$sql = "insert ns_host_relation set host_host_id='$host_id',nagios_server_id='$nagios_id'";
$sth = $dbh->prepare($sql);
$sth->execute();
# == insert complete ==
print "insert $host to centreon complete\n";
}
close(HOST);
$dbh->disconnect();
exit 0;
#--------------------------------------------------------------------------------
sub print_help{
print "Usage ./insert_host.pl [-f path of host file][-n nagiosname] [-t template name]\n";
print "\n";
}
页:
[1]