设为首页 收藏本站
查看: 1807|回复: 0

nagios介绍及Server安装(四)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-6-16 13:24:25 | 显示全部楼层 |阅读模式
批量添加主机和服务
spacer.jpg centreon的模板功能是做的非常强大的,而且优化过的nagios配置十分简单,加host的时候只需要输入了hostname,alias和ip 地址就可以加一台host上去,service配在hostgroup上,这样只要把host添加到hostgroup上就可以了 spacer.jpg 只添加host,service和hostgroup自己配。 spacer.jpg 运行脚本之前,要先准备好几件事情:
   1、要有一个host的模板,将所有的属性基本上定义完整,使用脚本添加的host会和模板一模一样,只有ip地址和hostname有差别 (推荐自定义一个host模版)
   2、要确认了host要添加到哪台nagios上,在centreon里叫poller
   3、要有一个hosts文件,里面内容为要批量添加的hostname和ip地址,类似/etc/hosts的格式,第一栏ip,第二栏hostname

下面开始演示添加主机: spacer.jpg 添加前:
wKiom1Odl6SzP1qYAAFvzJbAQ-I497.jpg

引用代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
脚本用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 = "127.0.0.1";                                                    #修改为127.0.0.1
my $DB_USER = "centreon";                                                           # web安装时设置的数据库访问用户,修改为centreon
my $DB_PASSWD = "centreon";                                                    # 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= "Central";             #poller,修改为Central

while (defined(my $arg= shift)) {
if ($argeq'-f') {
       $file_path= shift;
        }

# == name of template ==

elsif($argeq'-t') {
        $tpl_name= shift;
        }

# == name of nagiosname ==

  elsif($argeq'-n') {
        $nagios_name= shift;
        }

    else {
        &print_help();
        exit1;
        }
    }

# -----------------------------------------------------

open (HOST, "$file_path")|| die "Cannot open $file_pathfor 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 ($ipaddreq'' || $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_idfrom 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_informationset host_host_id='$host_id'";
$sth= $dbh->do($sql);

# == insert host_template_relation=====

$sql= "select host_idfrom 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_relationset 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_serverwhere 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_relationset host_host_id='$host_id',nagios_server_id='$nagios_id'";
        $sth = $dbh->prepare($sql);
        $sth->execute();

# == insert complete ==

  print "insert $host to centreoncomplete\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";
    }



wKiom1Odl_OwmHtLAAFFE1d-roY382.jpg
添加后:刷新WEB页面:多了三台;
wKiom1OdmAaTlGp6AAJcg7OI4X4313.jpg

spacer.jpg 批量生成和主机相关联的服务 spacer.jpg

上面的脚本能够批量添加主机,但是不能自动生成和主机相关联的服务
spacer.jpg 使用 CentreonCLAPI  可以解决这个问题,CentreonCLAPI 是centreon 命令行接口,可以替代在网页上的许多工作,这里我们只介绍下怎么解决我们的问题。了解更多请看网址:
spacer.jpg http://forge.centreon.com/projects/centreon-clapi/wiki进行安装
spacer.jpg http://download.centreon.com/Modules/CLAPI/centreon-clapi-1.1.tar.gz spacer.jpg

tar zxvfcentreon-clapi-1.1.tar.gz spacer.jpg
cd centreon-clapi-1.1
wKiom1OdmH3znJOzAAFxnL5AVMY114.jpg

wKioL1OdmIGA1L8aAAH8WvV0ays344.jpg


提示输入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";
spacer.jpg cd /usr/local/centreon/www/modules/centreon-clapi/core/ spacer.jpg
vim  centreon +64
spacer.jpg require_once"$centreon_etc/centreon.conf.php"; spacer.jpg
改为:
spacer.jpg require_once"/usr/local/centreon/etc/centreon.conf.php";
wKiom1OdmVTDXJe5AADx--GchMQ817.jpg

spacer.jpg 对client主机应用所关联的模板服务: spacer.jpg

关联前:
wKiom1OdmZyz5PrhAAO827cvXHQ173.jpg

spacer.jpg 进行关联:
spacer.jpg [iyunv@mastercore]# ./centreon -uadmin-padmin123 -o HOST -a applytpl -v"cc.cc.cc.cc"
wKioL1OdmaKg4-GvAABdhFU-u6o560.jpg


spacer.jpg 查看页面:
wKiom1Odmg-TRKd4AASenBe09pY767.jpg


通过以上命令可以关联模板的服务,如果需要批量添加,只需写个简单的脚本就能实现,见下图,执行前可删除刚才手动执行的命令添加的client服务:

wKioL1OdmfaCf1j4AAG-zIBl1e4589.jpg

spacer.jpg 刷新页面查看:
wKiom1Odmj-SmwGCAAVMmgkbVDc049.jpg

批量添加完主机和服务要需要重新生成nagios配置后生效。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-20572-1-1.html 上篇帖子: nagios介绍及Server安装(三) 下篇帖子: nagios介绍及Server安装(五)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表