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'";