nagios介紹及Server安裝(四)

批量添加主機(jī)和服務(wù)

創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括治多網(wǎng)站建設(shè)、治多網(wǎng)站制作、治多網(wǎng)頁制作以及治多網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,治多網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到治多省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

nagios介紹及Server安裝(四)centreon的模板功能是做的非常強(qiáng)大的,而且優(yōu)化過的nagios配置十分簡單,加host的時(shí)候只需要輸入了hostname,alias和ip 地址就可以加一臺host上去,service配在hostgroup上,這樣只要把host添加到hostgroup上就可以了nagios介紹及Server安裝(四) 只添加host,service和hostgroup自己配。nagios介紹及Server安裝(四)運(yùn)行腳本之前,要先準(zhǔn)備好幾件事情:
   1、要有一個(gè)host的模板,將所有的屬性基本上定義完整,使用腳本添加的host會和模板一模一樣,只有ip地址和hostname有差別 (推薦自定義一個(gè)host模版)
   2、要確認(rèn)了host要添加到哪臺nagios上,在centreon里叫poller
   3、要有一個(gè)hosts文件,里面內(nèi)容為要批量添加的hostname和ip地址,類似/etc/hosts的格式,第一欄ip,第二欄hostname

下面開始演示添加主機(jī):nagios介紹及Server安裝(四)添加前:

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 = <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 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";
    }

nagios介紹及Server安裝(四)

添加后:刷新WEB頁面:多了三臺;

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)批量生成和主機(jī)相關(guān)聯(lián)的服務(wù)nagios介紹及Server安裝(四)

上面的腳本能夠批量添加主機(jī),但是不能自動生成和主機(jī)相關(guān)聯(lián)的服務(wù)

nagios介紹及Server安裝(四)使用 CentreonCLAPI  可以解決這個(gè)問題,CentreonCLAPI 是centreon 命令行接口,可以替代在網(wǎng)頁上的許多工作,這里我們只介紹下怎么解決我們的問題。了解更多請看網(wǎng)址:

nagios介紹及Server安裝(四)http://forge.centreon.com/projects/centreon-clapi/wiki
進(jìn)行安裝

nagios介紹及Server安裝(四)http://download.centreon.com/Modules/CLAPI/centreon-clapi-1.1.tar.gznagios介紹及Server安裝(四)

tar zxvfcentreon-clapi-1.1.tar.gz nagios介紹及Server安裝(四)

cd centreon-clapi-1.1

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)

提示輸入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";

nagios介紹及Server安裝(四)cd /usr/local/centreon/www/modules/centreon-clapi/core/nagios介紹及Server安裝(四)

 vim  centreon +64

nagios介紹及Server安裝(四)require_once"$centreon_etc/centreon.conf.php";nagios介紹及Server安裝(四)

改為:

nagios介紹及Server安裝(四)require_once"/usr/local/centreon/etc/centreon.conf.php";

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)對client主機(jī)應(yīng)用所關(guān)聯(lián)的模板服務(wù):nagios介紹及Server安裝(四)

關(guān)聯(lián)前:

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)進(jìn)行關(guān)聯(lián):

nagios介紹及Server安裝(四)[root@mastercore]# ./centreon -uadmin-padmin123 -o HOST -a applytpl -v"cc.cc.cc.cc"

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)查看頁面:

nagios介紹及Server安裝(四)

通過以上命令可以關(guān)聯(lián)模板的服務(wù),如果需要批量添加,只需寫個(gè)簡單的腳本就能實(shí)現(xiàn),見下圖,執(zhí)行前可刪除剛才手動執(zhí)行的命令添加的client服務(wù):

nagios介紹及Server安裝(四)

nagios介紹及Server安裝(四)刷新頁面查看:

nagios介紹及Server安裝(四)

批量添加完主機(jī)和服務(wù)要需要重新生成nagios配置后生效。

網(wǎng)頁標(biāo)題:nagios介紹及Server安裝(四)
標(biāo)題來源:http://www.muchs.cn/article30/gjggpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版微信小程序、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作服務(wù)器托管、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司