centreon+nagios安裝-批量添加主機和服務

原文鏈接:

 

 

本文引用了一位同事的文檔及網絡文章,在此感謝~ 實驗的部分自己完成

 

一、centreon批量添加主機

本部分參考了同事的文檔和鏈接:http://blog.chinaunix.net/uid-17196076-id-2817703.html

 

centreon的模板功能是做的非常強大的,而且優化過的nagios配置十分簡單,加host的時候只需要輸入了hostnamealiasip 地址就可以加一臺host上去,service配在hostgroup上,這樣只要把host添加到hostgroup上就可以了。

  
 如果你要加一兩臺機器,那是很方便的,但是如果上百臺呢,上千臺呢?那手豈不是要點的抽筋了?

  
 這點來看,還是用腳本批量添加來的方便,呵呵,共享一個自己寫的批量添加host的腳本,只添加hostservicehostgroup自己配。
  
  
 運行腳本之前,要先準備好幾件事情:
   1
、要有一個host的模板,將所有的屬性基本上定義完整,使用腳本添加的host會和模板一模一樣,只有ip地址和hostname有差別
   2
、要確認了host要添加到哪臺nagios上,在centreon裏叫poller
   3
、要有一個hosts文件,裏面內容爲要批量添加的hostnameip地址,類似/etc/hosts的格式,第一欄ip,第二欄hostname

  
 腳本用perl寫的,最前面db的部分需要修改,代碼如下:
#!/usr/bin/perl
## ====================================================
#
# File name: insert_host.pl
# Use: insert host into centreon database
# Creater: lianming
# Date: 2009-04-24
# Last_change: 2009-04-24
#
## ====================================================
use strict;
use warnings;
use DBI;
use DBD::mysql;

# ----------------------------------------------------
my $DB_HOST = "db_ipaddress";            #
修改爲127.0.0.1
my $DB_USER = "db_user";             web安裝時設置的數據庫訪問用戶,修改爲centreon
my $DB_PASSWD = "db_password";     # web安裝時設置的數據庫密碼,修改爲centreon
my $DB_NAME = "centreon";                   # web安裝時設置的數據庫名,默認也是centreon
my $dbh = DBI->connect("DBI:mysql:database=$DB_NAME;host=$DB_HOST",
                       "$DB_USER", "$DB_PASSWD", { RaiseError => 1 });

# ----------------------------------------------------
my $file_path = "./hosts";       #hosts
文件,自己創建
my $tpl_name = "generic-host";     #主機模板,填寫你需要繼承的模板;
my $nagios_name = "nagios_name";     #poller,修改爲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;
        }

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


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

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

 

演示:

上面的腳本名我換成了add_host.pl,並創建hosts文件:

centreon+nagios安裝測試(四)-批量添加主機和服務

添加主機前:

centreon+nagios安裝測試(四)-批量添加主機和服務

 

執行腳本:

centreon+nagios安裝測試(四)-批量添加主機和服務

 

刷新WEB頁面:

centreon+nagios安裝測試(四)-批量添加主機和服務

二、批量生成和主機相關聯的服務

注:此部分引用了同事的文檔和腳本~

 

上面的腳本能夠批量添加主機,但是不能自動生成和主機相關聯的服務,如果對每一臺主機增加一個服務,要一個一個在頁面點擊,非常麻煩。

 

使用 Centreon CLAPI  可以解決這個問題,Centreon CLAPI 是centreon 命令行接口,可以替代在網頁上的許多工作,這裏我們只介紹下怎麼解決我們的問題。瞭解更多請看網址:

http://forge.centreon.com/projects/centreon-clapi/wiki

 

安裝clapi

[root@centreon ~]# cd /usr/local/src/

[root@centreon src]# tar zxf centreon-clapi-1.1.tar.gz

[root@centreon src]# cd centreon-clapi-1.1

[root@centreon centreon-clapi-1.1]# ./install.sh -i

centreon+nagios安裝測試(四)-批量添加主機和服務

提示輸入instCentWeb.conf配置文件的路徑:/usr/local/centreon/etc/

 

[root@centreon centreon-clapi-1.1]# cd /usr/local/centreon/www/modules/centreon-clapi/core/

[root@centreon core]# vi +64 centreon

require_once "$centreon_etc/centreon.conf.php";

改爲:

require_once "/usr/local/centreon/etc/centreon.conf.php";

 

查看所有主機:

[root@centreon core]# ./centreon -uadmin -p111111 -o HOST -a show

查看主機名包含client-1的主機,如果是輸入client,則會顯示client,client-1,client-2

[root@centreon core]# ./centreon -uadmin -p111111 -o HOST -a show -v "client-1"

client主機應用所關聯的模板服務:

[root@centreon core]# ./centreon -uadmin -p111111 -o HOST -a applytpl -v "client-1"

centreon+nagios安裝測試(四)-批量添加主機和服務

執行命令前的服務:

centreon+nagios安裝測試(四)-批量添加主機和服務

 

執行後client-1服務都已添加:

centreon+nagios安裝測試(四)-批量添加主機和服務

通過以上命令可以關聯模板的服務,如果需要批量添加,只需寫個簡單的腳本就能實現,見下圖,執行前可刪除剛纔手動執行的命令添加的client服務:

centreon+nagios安裝測試(四)-批量添加主機和服務

 

WEB刷新服務:

centreon+nagios安裝測試(四)-批量添加主機和服務
 

批量添加完主機和服務要需要重新生成nagios配置後生效。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章