Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141053
  • 博文数量: 19
  • 博客积分: 1416
  • 博客等级: 上尉
  • 技术积分: 273
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-22 00:49
文章分类

全部博文(19)

文章存档

2010年(4)

2009年(15)

我的朋友

分类:

2009-08-06 14:15:41

   centreon的模板功能是做的非常强大的,而且优化过的nagios配置十分简单,加host的时候只需要输入了hostname,alias和ip 地址就可以加一台host上去,service配在hostgroup上,这样只要把host添加到hostgroup上就可以了。

   如果你要加一两台机器,那是很方便的,但是如果上百台呢,上千台呢?那手岂不是要点的抽筋了?

   这点来看,还是用脚本批量添加来的方便,呵呵,共享一个自己写的批量添加host的脚本,只添加host,service和hostgroup自己配。
  
   运行脚本之前,要先准备好几件事情:
   1、要有一个host的模板,将所有的属性基本上定义完整,使用脚本添加的host会和模板一模一样,只有ip地址和hostname有差别
   2、要确认了host要添加到哪台nagios上,在centreon里叫poller
   3、要有一个hosts文件,里面内容为要批量添加的hostname和ip地址,类似/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";
my $DB_USER = "db_user";
my $DB_PASSWD = "db_password";
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 = "nagios_name";

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 = )) {

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

阅读(4980) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

uselsee2012-11-21 12:28:56

楼主   主机模版可以继承多个   你这个脚本能不能改造成继承多个主机模版的呢
这样就不用再给新的主机模版添加服务模版了啊

chinaunix网友2009-09-09 13:31:24

当nagios收到这些文件,并且reload后,会通过ndo2db把新的配置信息传递到nagios的ndo数据库中,可能是ndo可能是nagios。 然后你在centreon的monitor项目下就可以看见你的配置了。 选择configuration->host->add配置添加主机 选择configuration->service->add配置添加服务 选择configuration->nagios->export导出配置到nagios服务器

chinaunix网友2009-09-09 13:10:21

请教一下,是不是在centreon中加入了用户,在监控选项之类都能见那个新的主机,在nagios中也能,是吗?我安装后,centreon中加入了用户,除了configure中的hosts见得到别的都见不到,不知为什么?