nagios介绍及Server安装(四)
批量添加主机和服务
centreon的模板功能是做的非常强大的,而且优化过的nagios配置十分简单,加host的时候只需要输入了hostname,alias和ip地址就可以加一台host上去,service配在hostgroup上,这样只要把host添加到hostgroup上就可以了只添加host,service和hostgroup自己配。运行脚本之前,要先准备好几件事情:
1、要有一个host的模板,将所有的属性基本上定义完整,使用脚本添加的host会和模板一模一样,只有ip地址和hostname有差别 (推荐自定义一个host模版)
2、要确认了host要添加到哪台nagios上,在centreon里叫poller
3、要有一个hosts文件,里面内容为要批量添加的hostname和ip地址,类似/etc/hosts的格式,第一栏ip,第二栏hostname
下面开始演示添加主机:添加前:
引用代码:
脚本用perl写的,最前面db的部分需要修改,代码如下:#!/usr/bin/perl###====================================================##Filename:insert_host.pl##Use:inserthostintocentreondatabase###====================================================usestrict;usewarnings;useDBI;useDBD::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($argeq'-f'){$file_path=shift;}#==nameoftemplate==elsif($argeq'-t'){$tpl_name=shift;}#==nameofnagiosname==elsif($argeq'-n'){$nagios_name=shift;}else{&print_help();exit1;}}#-----------------------------------------------------open(HOST,"$file_path")||die"Cannotopen$file_pathforread$!";my$sql;my$sth;my$line;my($host,$ipaddr);my($host_id,$tpl_id,$nagios_id)=(0,0,0);while(defined($line=<HOST>)){#==skipblanklines=================nextif($line=~/^\s*$/);#==skipif#========================nextif($line=~/^\s*#/);#==gethostandipaddress===========($ipaddr,$host)=split(/\s+/,$line);nextif($ipaddreq''||$hosteq'');#==insertthehosttotablehost====$sql="inserthostsethost_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);#==gethost_id======================$sql="selecthost_idfromhostwherehost_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";}nextif($host_id==0);#==insertextended_host_information==$sql="insertextended_host_informationsethost_host_id='$host_id'";$sth=$dbh->do($sql);#==inserthost_template_relation=====$sql="selecthost_idfromhostwherehost_name='$tpl_name'";$sth=$dbh->prepare($sql);$sth->execute();while(my$ref=$sth->fetchrow_hashref()){$tpl_id=$ref->{'host_id'};print"templateidis$tpl_id\n";}nextif($tpl_id==0);$sql="inserthost_template_relationsethost_host_id='$host_id',host_tpl_id='$tpl_id',`order`='1'";$sth=$dbh->prepare($sql);$sth->execute();#==insertns_host_relation===========$sql="selectidfromnagios_serverwherename='$nagios_name'";$sth=$dbh->prepare($sql);$sth->execute();while(my$ref=$sth->fetchrow_hashref()){$nagios_id=$ref->{'id'};print"nagiosidis$nagios_id\n";}nextif($nagios_id==0);$sql="insertns_host_relationsethost_host_id='$host_id',nagios_server_id='$nagios_id'";$sth=$dbh->prepare($sql);$sth->execute();#==insertcomplete==print"insert$hosttocentreoncomplete\n";}close(HOST);$dbh->disconnect();exit0;#--------------------------------------------------------------------------------subprint_help{print"Usage./insert_host.pl[-fpathofhostfile][-nnagiosname][-ttemplatename]\n";print"\n";}
添加后:刷新WEB页面:多了三台;
批量生成和主机相关联的服务
上面的脚本能够批量添加主机,但是不能自动生成和主机相关联的服务
使用CentreonCLAPI可以解决这个问题,CentreonCLAPI是centreon命令行接口,可以替代在网页上的许多工作,这里我们只介绍下怎么解决我们的问题。了解更多请看网址:
http://forge.centreon.com/projects/centreon-clapi/wiki
进行安装
http://download.centreon.com/Modules/CLAPI/centreon-clapi-1.1.tar.gz
tar zxvfcentreon-clapi-1.1.tar.gz
cd centreon-clapi-1.1
提示输入instCentWeb.conf配置文件的路径:/usr/local/centreon/etc/
安装完成后:
cd /usr/local/centreon/www/modules/centreon-clapi/core/
vimcentreon +64
require_once "$centreon_etc/centreon.conf.php";
改为:
require_once "/usr/local/centreon/etc/centreon.conf.php";
cd /usr/local/centreon/www/modules/centreon-clapi/core/
vim centreon +64
require_once"$centreon_etc/centreon.conf.php";
改为:
require_once"/usr/local/centreon/etc/centreon.conf.php";
对client主机应用所关联的模板服务:
关联前:
进行关联:
[root@mastercore]# ./centreon -uadmin-padmin123 -o HOST -a applytpl -v"cc.cc.cc.cc"
查看页面:
通过以上命令可以关联模板的服务,如果需要批量添加,只需写个简单的脚本就能实现,见下图,执行前可删除刚才手动执行的命令添加的client服务:
刷新页面查看:
批量添加完主机和服务要需要重新生成nagios配置后生效。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。