4.使用NDOUtils将Nagios监控信息存入数据库

  • A+
所属分类:Nagios

使用NDOUtils将Nagios监控信息存入数据库

1、NDOUtils安装需求:
Nagios正确安装运行,版本>=2.0安装目录:/usr/local/nagios
MySQL正确安装并运行,我用的是二进制安装包,安装目录:/usr/local/mysql-5.1.36

mysql
图4-1

2、安装
下载地址:http://www.nagios.org/download/
进入addons查找DBI和DBD下载地址
如果嫌麻烦可直接进入DBI官网http://dbi.perl.org/下载
确认DBD-mysql包安装,如没有安装,请先安装。
2.1、安装DBD-mysql前需要先安装DBI包
DBI最新下载地址:http://search.cpan.org/~timb/DBI/
[codesyntax lang="text"]
cd /usr/local/src/tarbag/
wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.618.tar.gz
tar zxvf DBI-1.618.tar.gz -C ../software/
cd ../software/DBI-1.618/
perl Makefile.PL
make
make install
[/codesyntax]

2.2、安装DBD-mysql包
DBD最新下载地址:http://search.cpan.org/dist/DBD-mysql/
[codesyntax lang="text"]
cd /usr/local/src/tarbag/
wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.020.tar.gz
tar zxvf DBD-mysql-4.020.tar.gz -C ../software/
cd ../software/DBD-mysql-4.020/
unset LANG
perl Makefile.PL --libs="-L/usr/local/mysql-5.1.36/lib/mysql -lmysqlclient -L/usr/lib -lz " --cflags="-I/usr/local/mysql-5.1.36/include/mysql" --testhost=127.0.0.1 --mysql_config=/usr/local/mysql-5.1.36/bin/mysql_config --testuser=root --testpassword=123456
make
make install
[/codesyntax]
排错
安装DBD-mysql的时候可能会出现无法make的错误,并提示make: *** [dbdimp.o] Error 1
解决方法:一般碰到这种情况都是编译的时候链接库问题导致的,所以需要将以上几个mysql的路径确切的对应上,象我出现如上问题就是由于--cflags参数未指定到真正包含链接库文件的目录导致无法正常make
原参数:--cflags="-I/usr/local/mysql-5.1.36/include/"
现参数:--cflags="-I/usr/local/mysql-5.1.36/include/mysql"
只要对原理清楚了,这些问题都是细心的问题,本人的一些小经验,希望能帮助到大家。

做好MySQL库文件lib和include文件的软链接,对接下来的ndo安装很有用。
[codesyntax lang="text"]
ln -s /usr/local/mysql-5.1.36/include/mysql/* /usr/include/
mkdir /usr/include/mysql
ln -s /usr/local/mysql-5.1.36/include/mysql/* /usr/include/mysql/
ln -s /usr/local/mysql-5.1.36/lib/* /usr/lib/
mkdir /usr/lib/mysql
ln -s /usr/local/mysql-5.1.36/lib/* /usr/lib/mysql
[/codesyntax]

2.3、安装ndoutils
ndoutils下载链接:http://exchange.nagios.org/directory/Addons/Database-Backends/NDOUtils/details
[codesyntax lang="text"]
cd /usr/local/src/tarbag/
wget http://downloads.sourceforge.net/project/nagios/ndoutils-1.x/ndoutils-1.5/ndoutils-1-5.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fnagios%2Ffiles%2Fndoutils-1.x%2Fndoutils-1.5%2F&ts=1331697712&use_mirror=nchc
tar zxvf ndoutils-1-5.tar.gz -C ../software/
cd ../software/ndoutils-1-5/
./configure --enable-mysql
# 执行完成后,注意向上看看打印出的信息,看看是否找到了MySQL的库文件和include文件。
make
make install
[/codesyntax]
# 我的Nagios是Nagios Core 3.3.1,安装完提示如下,所以如果是Nagios 2.x,执行make install-2x即可
[codesyntax lang="text"]
Hint: NDOUtils Installation against Nagios v3.x
completed.

If you want to install NDOUtils for Nagios v2.x
please type 'make install-2x

Next step should be the database initialization/upgrade
cd into the db/ directory and either:
./installdb (for a new installation) or:
./upgradedb (for an existing one)

make[1]: Leaving directory `/usr/local/src/software/ndoutils-1-5/src'

Main NDOUtils components installed
[/codesyntax]

拷贝相关类库到/usr/local/nagios/bin下
[codesyntax lang="text"]
cd src/
cp ndomod-3x.o ndo2db-3x /usr/local/nagios/bin/
[/codesyntax]
[codesyntax lang="text"]
cd ../db
# 创建数据库,使用root用户,密码是123456
mysqladmin -uroot -p123456 create nagios
chmod +x installdb
./installdb -uroot -p123456 -d nagios
[/codesyntax]
排错
在初始化数据库的时候会出现如下报错
[codesyntax lang="text"]
install_driver(mysql) failed: Can't load '/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.so' for module DBD::mysql: libmysqlclient.so.16: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.
at (eval 5) line 3
Compilation failed in require at (eval 5) line 3.
Perhaps a required shared library or dll isn't installed where expected
at ./installdb line 41
[/codesyntax]
根据错误提示,发现可能是动态库libmysqlclient.so.16的问题,google了一下,在斩月朋友的文章中找到了解决方法http://hi.baidu.com/naruto6006/blog/item/e5ad30254bec1938c8955912.html
[codesyntax lang="text"]
ln -s /usr/local/mysql-5.1.36/lib/mysql/libmysqlclient.so.16 /usr/lib
ln -s /usr/local/mysql-5.1.36/lib/mysql/libmysqlclient.so.16 /usr/lib64
[/codesyntax]
出现如下提示
[codesyntax lang="text"]
DBD::mysql::db do failed: Table 'nagios.nagios_dbversion' doesn't exist at ./installdb line 51.
** Creating tables for version 1.4b7
Using mysql.sql for installation...
** Updating table nagios_dbversion
Done!

DBD::mysql::db do failed: Table 'nagios.nagios_dbversion' doesn't exist at ./installdb line 51(这个错误可以忽略…)
[/codesyntax]

2.4、相关配置
# 拷贝配置文件
[codesyntax lang="text"]
cd ../config
cp ndo* /usr/local/nagios/etc/
cd /usr/local/nagios/etc/
cp ndo2db.cfg-sample ndo2db.cfg
cp ndomod.cfg-sample ndomod.cfg
[/codesyntax]
# 修改/usr/local/nagios/etc/ndo2db.cfg文件的数据库信息。
[codesyntax lang="text"]
cd /usr/local/nagios/etc/
vi /usr/local/nagios/etc/ndo2db.cfg
# 主要改以下四条
db_host=localhost
db_name=nagios
db_user=nagios
db_pass=nagios
[/codesyntax]
排错
如果启动时碰到如下报错一般是由于数据库权限问题导致的
[codesyntax lang="text"]
[1331707820] ndomod: Still unable to reconnect to data sink. 0 items lost, 1367 queued items to flush.
[/codesyntax]
[codesyntax lang="text"]
vi nagios.cfg
# 复制下面内容粘贴到/usr/local/nagios/etc/nagios.cfg配置文件的#broker_module=...下面。
# Uncomment the line below if you're running Nagios 3.x
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
# 修改/usr/local/nagios/etc/nagios.cfg配置文件下面参数的值为-1(一般默认如此)。
event_broker_options=-1
[/codesyntax]
[codesyntax lang="text"]
rm -rf ndo2db.cfg-sample* ndomod.cfg-sample*
chown -R nagios\: /usr/local/nagios
[/codesyntax]

# 启动ndo2db
[codesyntax lang="text"]
/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
[/codesyntax]
将启动命令加入到/etc/rc.local中去
# 查看系统日志是否有错误信息:
[codesyntax lang="text"]
tail -30 /var/log/messages
Nov 27 14:09:26 tech nagios: ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad (nagios@nagios.org)
Nov 27 14:09:26 tech nagios: ndomod: Successfully connected to data sink. 0 queued items to flush.
Nov 27 14:09:26 tech nagios: Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
Nov 27 14:09:26 tech nagios: Finished daemonizing... (New PID=18848)
[/codesyntax]
# 重启Nagios
[codesyntax lang="text"]
service nagios restart
[/codesyntax]
# 查看Nagios日志,看是否正常启动。
[codesyntax lang="text"]
tail -20 /usr/local/nagios/var/nagios.log
[1227766166] ndomod: NDOMOD 1.4b7 (10-31-2007) Copyright (c) 2005-2007 Ethan Galstad (nagios@nagios.org)
[1227766166] ndomod: Successfully connected to data sink. 0 queued items to flush.
[1227766166] Event broker module '/usr/local/nagios/bin/ndomod-3x.o' initialized successfully.
[1227766166] Finished daemonizing... (New PID=18848)
[/codesyntax]

3、如何用NDOUtils将多个Nagios实例的性能数据写入一个数据库?
mysql
图2
配置方法类似于上面的单实例单数据库模式,只是在装第二个实例时省去安装DB一步,并需要修改/usr/local/nagios/etc/ndomod.cfg中的实例名。
实例名默认为instance_name=default,如果有多个实例必需修改成不同的实例名。
如:instance_name=nagios1
注意修改/usr/local/nagios/etc/ndo2db.cfg中数据库连接的相关配置。

weinxin
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: