#!/usr/bin/perl ##################just for test##################### use strict; use warnings; use DBI; use DBD::mysql; #################################################### my $DB_HOST = "127.0.0.1"; 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";#为ip地址,主机名列表,一行一个主机,中间空格隔开。 my $tpl_name = "Servers-Linux";#一定要与实际模板一致 my $nagios_name = "Central";#一定要与实际节点名一致 foreach my $arg (@ARGV) { #file of hostname and ipaddress if ($arg eq '-f') { $file_path = shift; } #name of template elsif ($arg eq '-t') { $tpl_name = shift; } #name of nagios name elsif ($arg eq '-n') { $nagios_name = shift; &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 = <HOST>)) { #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 $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_id is $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 "nagios id 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"; } $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 "nagios id 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 nagios name] [-t template name]\n"; print "\n"; } ~
运行脚本之前,要先准备好几件事情:
1、要有一个host的模板,将所有的属性基本上定义完整,使用脚本添加的host会和模板一模一样,只有ip地址和 hostname有差别
2、要确认了host要添加到哪台nagios上,在centreon里叫poller
3、要有一个hosts文件,里面内容为要批量添加的hostname和ip地址,类似/etc/hosts的格式,第一栏ip,第二栏hostname