nagios+cacti整合

系统:CentOS -6.5-x86_64

软件环境:apr-1.5.0.tar.gz apr-util-1.5.3.tar.gz httpd-2.4.7.tar.gz pcre-8.33.tar.gz mysql-5.6.10.tar.gz mhash-0.9.9.9.tar.gz libmcrypt-2.5.6.tar.gz zlib-1.2.5.1.tar.gz libpng-1.2.47.tar.xz jpeg-6b.tar.gz freetype-2.3.11.tar.bz2 gd-2.1.0.tar.gz php-5.5.13.tar.gz nagios-3.4.3.tar.gz  nagios-plugins-1.4.16.gz  nrpe-2.14.tar.gz sendEmail-v1.56.tar.gz rrdtool* net-snmp* cacti-0.8.8b.tar.gz cacti-spine-0.8.8b.tar.gz settings-v0.7-1.tgz monitor-v1.3-1.tgz thold-v0.5.0.tgz ntop-4.0.1.tar.gz ntop-v0.2-1.tgz npc-2.0.4.tar.gz json-1.2.1.tgz ndoutils-1.5.2.tar.gz GeoIP.tar.gz

Yum安装的软件

perl-GD perl-DBI perl-Digest-SHA1 perl-Digest-HMAC net-snmp-utils  perl-Socket6 perl-IO-Socket-INET6 net-snmp-devel dmidecode net-snmp-perl  perl-Crypt-DES fping perl-Config-IniFiles graphviz

监控主机ip:192.168.1.124

被监控机ip:192.168.1.43 192.168.1.212

一、搭建lamp环境

1.安装apache。

安装前准备:[[email protected] ~]# yum install -y gcc gcc-c++ gcc-devel cmake

安装apr

[[email protected] soft]# tar xvf apr-1.5.0.tar.gz

[[email protected] soft]# cd apr-1.5.0

[[email protected] apr-1.5.0]# ./configure --prefix=/usr/local/apr

[[email protected] apr-1.5.0]# make &&make install

安装apr-util

[[email protected] soft]# tar xvf apr-util-1.5.3.tar.gz

[[email protected] soft]# cd apr-util-1.5.3

[[email protected] apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[[email protected] apr-util-1.5.3]# make &&make install

安装pcre

[[email protected] soft]# tar xvf pcre-8.33.tar.gz

[[email protected] soft]# cd pcre-8.33

[[email protected] pcre-8.33]# ./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

[[email protected] pcre-8.33]# make && make install

安装apache

[[email protected] soft]# tar xvf httpd-2.4.7.tar.gz

[[email protected] httpd-2.4.7]# ./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-so --enable-rewrite

[[email protected] httpd-2.4.7]# make && make install

启动apache

[[email protected] bin]# cd /usr/local/httpd/bin/

[[email protected] bin]# ./apachectl start

查看端口监听

[[email protected] bin]# netstat -antl|grep 80

2.安装mysql

解压,编译安装mysql

[[email protected] soft]# tar xf mysql-5.6.10.tar.gz

[[email protected] soft]# cd mysql-5.6.10

[[email protected] mysql-5.6.10]# cmake  -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql  -DMYSQL_DATADIR:PATH=/database -DWITH_DEBUG:BOOL=on -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_INNOBASE_STORAGE_ENGINE:BOOL=on -DWITH_PARTITION_STORAGE_ENGINE:BOOL=on

出错1:

[[email protected] mysql-5.6.10]# yum  install -y  ncurses-devel

[[email protected] mysql-5.6.10]# rm CMakeCache.txt

然后重新编译

[[email protected] mysql-5.6.10]# make && make install

新建mysql用户及相关目录,并更改属组和属主创建

[[email protected] mysql-5.6.10]# mkdir /database

[[email protected] mysql-5.6.10]# mkdir /usr/local/mysql/var/log -p

[[email protected] mysql-5.6.10]# mkdir /usr/local/mysql/var/run

[[email protected] mysql-5.6.10]# mkdir /usr/local/mysql/etc

[[email protected] mysql-5.6.10]# mkdir /usr/local/mysql/binlog

[[email protected] mysql-5.6.10]# useradd mysql

[[email protected] mysql-5.6.10]# chown -R mysql:mysql /database /usr/local/mysql/

修改配置文件

[[email protected] mysql-5.6.10]# cp support-files/my-default.cnf /usr/local/mysql/etc/my.cnf

[[email protected] mysql-5.6.10]# vim /usr/local/mysql/etc/my.cnf

添加如下内容:

[mysqld]

basedir=/usr/local/mysql

datadir=/database

socket=/usr/local/mysql/var/run/mysql.sock

pid-file=/usr/local/mysql/var/run/mysql.pid

log-bin=/usr/local/mysql/binlog/mysqlbin-log

port=3306

user=mysql

[mysqld_safe]

log-error=/usr/local/mysql/var/log/mysql.log

[mysql]

socket=/usr/local/mysql/var/run/mysql.sock

初始化mysql数据库

[[email protected] mysql-5.6.10]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/database --user=mysql

添加mysql自启动

[[email protected] mysql-5.6.10]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql5.6d

[[email protected] mysql-5.6.10]# chkconfig --add mysql5.6d

[[email protected] mysql-5.6.10]# chkconfig mysql5.6d on

[[email protected] mysql-5.6.10]# service mysql5.6d start

Starting MySQL...                                          [  OK  ]

[[email protected] mysql-5.6

修改root密码:

[[email protected] soft]# /usr/local/mysql/bin/mysql –uroot

mysql> use mysql

mysql> update user set password=password(‘123654‘) where user=‘root‘;

[[email protected] soft]# /usr/local/mysql/bin/mysql -uroot -p123654

3.安装php

安装mhash

[[email protected] soft]# tar jxvf mhash-0.9.9.9.tar.bz2

[[email protected] soft]# cd mhash-0.9.9.9

[[email protected] mhash-0.9.9.9]# ./configure

[[email protected] mhash-0.9.9.9]# make &&make install

安装libmcrypt

[[email protected] soft]# tar xf libmcrypt-2.5.6.tar.gz

[[email protected] soft]# cd libmcrypt-2.5.6

# ./configure

#make

#make install

安装zlib,装两次,一次指定路径,一次不指定,64位系统安装库编译的时候要在前面加上CFLAGS="-O3 -fPIC"

[[email protected] soft]# tar xf zlib-1.2.5.1.tar.gz

[[email protected] soft]# cd zlib-1.2.5.1

[[email protected] zlib-1.2.5.1]# CFLAGS="-O3 -fPIC"./configure

[[email protected] zlib-1.2.5.1]# make && make install

[[email protected] zlib-1.2.5.1]#CFLAGS="-O3 -fPIC" ./configure --prefix=/usr/local/zlib

[[email protected] zlib-1.2.5.1]# make && make install

安装libpng

[[email protected] soft]# tar xf libpng-1.2.47.tar.xz

[[email protected] soft]# cd libpng-1.2.47

[[email protected] libpng-1.2.47]# CFLAGS="-O3 -fPIC"./configure --prefix=/usr/local/libpng

安装jpeg

[[email protected] soft]# tar xf jpeg-6b.tar.gz

[[email protected] soft]# cd jpeg/src/

[[email protected] src]# cp /usr/share/libtool/config/config.guess config.guess

cp: overwrite `config.guess‘? y

[[email protected] src]# cp /usr/share/libtool/config/config.sub config.sub

cp: overwrite `config.sub‘? y

[[email protected] src]# CFLAGS="-O3 -fPIC" ./configure --prefix=/usr/local/jpeg --enable-shared --enable-static

[[email protected] src]# make && make install

错误2:

[[email protected] src]# mkdir -p /usr/local/jpeg/bin

[[email protected] src]# mkdir -p /usr/local/jpeg/lib

[[email protected] src]# mkdir -p /usr/local/jpeg/include

[[email protected] src]# mkdir -p /usr/local/jpeg/man

[[email protected] src]# mkdir -p /usr/local/jpeg/man/man1

安装freetype

[[email protected] soft]# tar jxvf freetype-2.3.11.tar.bz2

[[email protected] soft]# cd freetype-2.3.11

[[email protected] freetype-2.3.11]# ./configure --prefix=/usr/local/freetype --enable-shared

[[email protected] freetype-2.3.11]# make && make install

安装gd

[[email protected] soft]# tar xf gd-2.0.35.tar.gz

[[email protected] soft]# cd gd/2.0.35/

[[email protected] 2.0.35]# ./configure --prefix=/usr/local/gd  --with-jpeg=/usrl/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype

[[email protected] 2.0.35]# make && make install

出现错误3:

[[email protected] 2.0.35]# sed -i ‘s#png.h#/usr/local/libpng/include/png.h#‘ gd_png.c

安装php

[[email protected] ~]# yum install libxml2* -y

[[email protected] soft]# tar xf php-5.5.13.tar.gz

[[email protected]localhost soft]# cd php-5.5.13

[[email protected] php-5.5.13]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd --with-apxs2=/usr/local/httpd/bin/apxs --with-libxml-dir=/usr/include/libxml2 --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-gd=/usr/local/gd --with-zlib-dir=/usr/local/zlib --with-mysqli=mysqlnd --with-mcrypt=/usr/local/lib/libmcrypt  --enable-soap --enable-mbstring=all --enable-sockets --enable-bcmath

错误4:

版本太低,换了最高版本的libgd-2.1.0.tar.gz

#cd /soft/gd/2.0.35

[[email protected] 2.0.35]#make clean

[[email protected] 2.0.35]#rm –rf /usr/local/gd

下载libgd-2.1.0.tar.gz

#tar xf libgd-2.1.0.tar.gz

# cd libgd-2.1.0

[[email protected] libgd-2.1.0]# ./configure --prefix=/usr/local/gd  --with-jpeg=/usrl/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype

[[email protected] libgd-2.1.0]# make && make install

再重新编译php,已经没错误了

[[email protected] php-5.5.13]# make

错误5:

[[email protected] php-5.5.13]# yum install libXpm-devel –y

[[email protected] php-5.5.13]# make

[[email protected] php-5.5.13]# make test

[[email protected] php-5.5.13]# make install

安装pdo_mysql模块

[[email protected] php-5.5.13]# cd ext/pdo_mysql/

[[email protected] pdo_mysql]# /usr/local/php/bin/phpize

[[email protected] pdo_mysql]# mkdir ext/pdo/tests -p

[[email protected] pdo_mysql]# ./configure --with-php-config=/usr/local/php/bin/php-config  --with-pdo-mysql=/usr/local/mysql

[[email protected] pdo_mysql]# make

[[email protected] pdo_mysql]# make test

[[email protected] pdo_mysql]# make install

[[email protected] php-5.5.13]# cp php.ini-development /usr/local/php/etc/php.ini

[[email protected] pdo_mysql]# echo ‘extension_dir=”/usr/local/php/lib/php/extensions/no-debug-zts-20121212/”‘ >>/usr/local/php/etc/php.ini

[[email protected] pdo_mysql]# echo ‘extension=”pdo_mysql.so”‘ >>/usr/local/php/etc/php.ini

查看模块是否被加载

[[email protected] php-5.5.13]# /usr/local/php/bin/php  -m |grep pdo_mysql

pdo_mysql

让apache支持php

[[email protected] php-5.5.13]# vim /usr/local/httpd/conf/httpd.conf

增加

AddType application/x-httpd-php .php

找到这一段,加入index.php

<IfModule dir_module>

DirectoryIndex index.html index.php

</IfModule>

重启apache

在htdocs下面写一个test.php做测试看是否支持php

[[email protected] bin]# vim ../htdocs/test.php

<?php

Phpinfo();

?>

测试php能否连接数据库

[[email protected] bin]# vim ../htdocs/con.php

<?php

$link = mysqli_connect(‘127.0.0.1‘,‘root‘,‘123654‘);

if(!$link) echo "FAILD!";

else echo "OK!";

?>

二、监控机安装nagios和nagios-plugins以及nrpe

#yum install perl-GD perl-DBI perl-Digest-SHA1 perl-Digest-HMAC net-snmp-utils  perl-Socket6 perl-IO-Socket-INET6 net-snmp-devel dmidecode net-snmp-perl  perl-Crypt-DES fping perl-Config-IniFiles graphviz –y

Nagios监控原理:

Nagios 通过NRPE 来远端管理服务

1. Nagios 执行安装在它里面的check_nrpe 插件,并告诉check_nrpe 去检测哪些服务。

2. 通过SSL,check_nrpe 连接远端机子上的NRPE daemon

3. NRPE 运行本地的各种插件去检测本地的服务和状态(check_disk,..etc)

4. 最后,NRPE 把检测的结果传给主机端的check_nrpe,check_nrpe 再把结果送到Nagios状态队列中。

5. Nagios 依次读取队列中的信息,再把结果显示出来。

参考:http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html

创建nagios用户

[[email protected] ~]# useradd -s /sbin/nologin nagios

[[email protected] ~]# mkdir -p /usr/local/nagios

[[email protected] ~]# chown -R nagios:nagios /usr/local/nagios

解压,编译安装

[[email protected] soft]# tar xvf nagios-3.4.3.tar.gz

[[email protected] soft]# cd nagios

[[email protected] nagios]# ./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/gd/lib  --with-gd-inc=/usr/local/gd/include

[[email protected] nagios]# make all

[[email protected] nagios]# make install

[[email protected] nagios]#make install-init

[[email protected] nagios]#make install-commandmode

[[email protected] nagios]#make install-config

编译安装nagios插件

[[email protected] soft]# tar xvf nagios-plugins-1.4.16.gz

[[email protected] soft]# cd nagios-plugins-1.4.16

[[email protected] nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios/

[[email protected] nagios-plugins-1.4.16]# make && make install

编译安装nrpe

[[email protected] soft]# tar xf nrpe-2.14.tar.gz

[[email protected] soft]# cd nrpe-2.14

[[email protected] nrpe-2.14]# ./configure

[[email protected] nrpe-2.14]# make all

[[email protected] nrpe-2.14]# make install-plugin

配置apache和nagios

[[email protected] nrpe-2.14]# vim /usr/local/httpd/conf/httpd.conf添加

#setting for nagios
 ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
 <Directory "/usr/local/nagios/sbin">
 AuthType Basic
 Options ExecCGI
 AllowOverride None
 Order allow,deny
 Allow from all
 AuthName "Nagios Access"
 AuthUserFile /usr/local/nagios/etc/htpasswd
 Require valid-user
 </Directory>

Alias /nagios /usr/local/nagios/share
 <Directory "/usr/local/nagios/share">
 AuthType Basic
 Options None
 AllowOverride None
 Order allow,deny
 Allow from all
 AuthName "nagios Access"
 AuthUserFile /usr/local/nagios/etc/htpasswd
 Require valid-user
 </Directory>

保存,创建admin用户,用于登陆nagios,此文档中设置的密码是123654

[[email protected] nrpe-2.14]# /usr/local/httpd/bin/htpasswd -c /usr/local/nagios/etc/htpasswd admin

New password:

Re-type new password:

Adding password for user admin

修改cgi配置文件

修改nagios的cgi配置文件,给admin用户权限

[[email protected] nrpe-2.14]# sed -i ‘s/nagiosadmin/admin/g‘ /usr/local/nagios/etc/cgi.cfg  ---》这是我直接改用户的方法。也可以使用下面这种方法:

[[email protected] nrpe-2.14]# vim /usr/local/nagios/etc/cgi.cfg

authorized_for_system_information=admin
 authorized_for_configuration_information=admin
 authorized_for_system_commands=admin
 authorized_for_all_services=admin
 authorized_for_all_hosts=admin
 authorized_for_all_service_commands=admin
 authorized_for_all_host_commands=admin

确保以上参数值为:admin

重启apache

[[email protected] nrpe-2.14]# cd /usr/local/httpd/bin/

[[email protected] bin]# ./apachectl restart

访问nagios:http://192.168.1.124/nagios,输入刚才设置的用户名:admin密码:123654,出现nagios的主页面。

到这里会出现在ie浏览器点击service或者hosts会出现乱码,原因是Apache没开启cgi模块。修改apache配置文件

[[email protected] bin]# vim ../conf/httpd.conf  将以下两句注释去掉,重启apache

LoadModule cgid_module modules/mod_cgid.so

LoadModule actions_module modules/mod_actions.so

三、linux被监控机安装nagios插件、nrpe

被监控主机需要安装nagios插件,以及nrpe

装nagios插件

[[email protected] nagiossoft]# useradd -s /sbin/nologin nagios

[[email protected] nagiossoft]# mkdir /usr/local/nagios

[[email protected] nagiossoft]# tar xf nagios-plugins-1.4.16.gz

[[email protected] nagiossoft]# cd nagios-plugins-1.4.16

[[email protected] nagios-plugins-1.4.16]# ./configure  --prefix=/usr/local/nagios/

[[email protected] nagios-plugins-1.4.16]#make && make install

安装nrpe

[[email protected] nrpe-2.14]# ./configure

[[email protected] nrpe-2.14]# make all

[[email protected] nrpe-2.14]# make instlal-plugin

[[email protected] nrpe-2.14]# make install-daemon

[[email protected] nrpe-2.14]#  make install-daemon-config

[[email protected] nrpe-2.14]# chown  -R  nagios.nagios  /usr/local/nagios

[[email protected] nrpe-2.14]# vim /usr/local/nagios/etc/nrpe.cfg

找到这句加上监控机的ip

allowed_hosts=127.0.0.1,192.168.1.124

启动nrpe

[[email protected] nrpe-2.14]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

查看监听端口:

[[email protected] nrpe-2.14]# netstat -antl|grep 5666

tcp        0      0 0.0.0.0:5666                0.0.0.0:*

确保防火墙跟selinux都是关闭的

检测自己的nrpe是否成功

[[email protected] nrpe-2.14]# /usr/local/nagios/libexec/check_nrpe -H localhost

NRPE v2.15

到监控机去检测npre能否正常通信:

[[email protected] ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.43

NRPE v2.14

③配置snmp

com2sec notConfigUser  192.168.1.124(监控主机的ip)       public

view all    included  .1                               80

access  notConfigGroup ""      any       noauth    exact  all none none

重启snmp服务

[[email protected] nrpe-2.14]# service snmpd restart

四、配置nagios

先看看etc下面有哪些文件

其中nagios.cfg是主配置文件,这下面有个objects的目录,这个目录下面的配置文件是定义监控的

其中templates.cfg里面有很多模板,我们在自定义监控的时候可以引用此文件中的模板。现在我们来定义监控。

在objects的目录下我们可以看到localhost.cfg这个配置文件,打开可以看到里面已经定义了对本机的监控,所以不用对本机做定义,打开nagios网页http://192.168.1.124/nagios然后点击services可以看到如下图的监控信息。

在监控主机定义监控

[[email protected] objects]# cd /usr/local/nagios/etc/objects/,创建hosts.cfg自定义监控配置文件此文件用来定义被监控主机的名字和ip

[[email protected] objects]# vim hosts.cfg

define host{

use linux-server

host_name  linux-43

alias      test

address  192.168.1.43

}

解释这段的意思

use linux-server   这句是引用template里面的linux-server,指被监控的是linux主机

host_name linux-43   这句是给被监控主机的机器名,随便取

alias  test                  被监控主机别名,也随便取

address  192.168.1.43 被监控主机的ip

定义了主机之后,现在来定义主机组,在当前路径下又新建hostgroups.cfg

[[email protected] objects]# vim hostgroups.cfg

define hostgroup{

hostgroup_name          zj-servers

alias                   zj-servers

members                 linux-43

}

解释这段的意思

hostgooup_name zj-server  意思是主机组的名字

alias                     zj-server      意思是主机组的别名

members              linux-43      意思是这组的成员,多个成员用逗号隔开,这些成员必须是在hosts.cfg定义的。

接下来定义服务,在当前路径新建services.cfg

[[email protected] objects]# vim services.cfg

define service{

use                     local-service

host_name               linux-43

service_description     check-host-alive

check_command           check-host-alive

}

解释这段的意思:

use          local-service    这句也是引用template.cfg中的local-service意思是监控的服务

host_name           linux-43       这句的意思是监控的是linux-43这个主机的服务

service_description                  check-host-alive  这句的意思服务的描述,即检查主机的存活

check_command              check-host-alive  这句的意思是使用什么命令去检查,这里是使用check-host-alive,这个命令必须在commands.cfg有定义,打开comands.cfg可以看到定义此命令的这一段。

修改nagios主配置文件

[[email protected] objects]# vim ../nagios.cfg

增加三行,刚才自定义的文件,都要在主配置文件指定他们的路径

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg

cfg_file=/usr/local/nagios/etc/objects/services.cfg

重启nagios,到网页上去看http://192.168.1.124/nagios

先检测配置文件有无错误

[[email protected] objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

重启

[[email protected] objects]# service nagios restart

打开网站,点击hosts可以看到linux-43,刚才我们自定义的主机已经监控起来了

我们可以对同一个主机监控多个服务,下面来利用nrpe插件对远程主机监控

check_nrpe的用法:

check_nrpe –H 被监控的主机 -c 要执行的监控命令

注意:-c 后面接的监控命令必须是nrpe.cfg 文件中定义的。也就是NRPE daemon只运行nrpe.cfg中所定义的命令。而且在nrpe.cfg定义的命令在这个路径(/usr/local/nagios/libexec/)下面必须有这个命令才能定义。这个nrpe.cfg是在被监控机定义,从nagios的监控原理可以知道,是在被监控端执行检测命令,得到结果再发送给监控主机的nrpe插件。

举例,现在我们要监控远程主机的负载,是用check_load这个命令去检测的。所以首先查看被监控机(192.168.1.43)的nrpe.cfg文件是否有定义这个命令,有则不定义。

查看修改nrpe.cfg

从上图中,可看到check_load已经被定义了,再看看在监控主机这边是怎么远程使用这个命令的。

修改dont_blame_nrpe=0为dont_blame_nrpe=1,意思是允许带参数,然后重启nrpe。

先找出nrpe进程,然后kill掉,然后再启动。

[[email protected] ~]# ps -ef |grep nrpe

nagios   19527     1  0 13:26 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

[[email protected] ~]# kill -9 19527

[[email protected] ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg  -d

Check_nrpe的解释

在使用check_nrpe之前首先要检查commands.cfg文件有没有定义这个命令,没有则需要自定义。经查找是没有定义check_nrpe的,所以在此我们要先定义check_nrpe,如下图定义。

-c 后面带的$ARG1$ 参数是传给nrpe daemon 执行的检测命令,之前说过了它必须是nrpe.cfg 中所定义的那些命令中的其中一条。在services.cfg 中使用check_nrpe 的时候要用 “!” 带上这个参数。

现在我们可以在services.cfg去定义这个命令去检查了。

[[email protected] objects]# vim services.cfg

增加这一段

define service{

use                     local-service

host_name               linux-43

service_description     current load

check_command           check_nrpe!check_load

}

最后一行check_nrpe就是commands.cfg里定义的命令,!check_load是用!把check_load这个参数带到被监控机上去执行。

修改了配置文件之后,检查配置文件,并重启,从网页上看是否检查到此信息。

已经添加了,但是还未开始检测状态,要等一会。

看到ok说明监控好了。

五、添加windows监控

在Nagios的libexec下有check_nt这个插件,它就是用来检查windows机器的服务的。其功能类似于check_nrpe。不过还需要搭配另外一个软件NSClient++,它则类似于NRPE。

NSClient与nrpe最大的区别:

·         被监控机上安装有nrpe,并且还有插件,最终的监控是由这些插件来进行的。当监控主机将监控请求发给nrpe后,nrpe调用插件来完成监控。

·         NSClient++则不同,被监控机上只安装NSClient,没有任何的插件。当监控主机将监控请求发给NSClient++后,NSClient直接完成监控,所有的监控是由NSClient完成的。

下载NSClient++-0.2.7.zip

下载地址:http://www.nsclient.org/nscp/downloads

解压,打开cmd,进入解压后的目录

安装nsclient并安装系统托盘

打开服务

找到nsclientpp,双击,然后弹出的窗口,点击登录,大上允许服务与桌面的勾,然后点确定。

修改NSClient++-0.2.7下的NSC.ini文件。

将 [modules]部分的所有模块前面的注释都去掉,除了CheckWMI.dll 和 RemoteConfiguration.dll 这两个。

在[Settings]部分设置‘password‘选项来设置密码,作用是在nagios连接过来时要求提供密码。这一步是可选的,我这里设置为‘123654‘。

将[Settings]部分‘allowed_hosts‘选项的注释去掉,并且加上运行nagios的监控主机的IP。各IP之间以逗号相隔。这个地方是支持子网的,如果写成192.168.1.0/24则表示该子网内的所有机器都可以访问。如果这个地方是空白则表示所有的主机都可以连接上来。

将[NSClient]的‘port‘选项的注释去掉,这是nsclient的默认端口

启动nsclient服务,并查看监听端口

监控主机开启对windows的监控

修改nagios的主配置文件

[[email protected] objects]# vim ../nagios.cfg

#cfg_file=/usr/local/nagios/etc/objects/windows.cfg找到这行,去掉注释。

修改commands.cfg,找到check_nt那一段,nagios使用check_nt这个去检查windows服务的。将check_nt这一段修改

define command{

command_name    check_nt

command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123654 -v $ARG1$ $ARG2$

}

修改widows.cfg,找到host这段,将ip修改为要监控的windows主机的ip

define host{

use             windows-server  ; Inherit default values from a template

host_name       winserver       ; The name we‘re giving to this host

alias           My Windows Server       ; A longer name associated with the host

address         192.168.1.212   ; IP address of the host

}

重启nagios,去网页上查看windows有没有被监控。

错误六:

出现这个错误是由于操作系统语言的问题

修改counters.defs文件

Description = "chinese"—》这句是修改为chinese

在最后面增加下面一段

[0x0804]

Description = "Chinese"

NT4_SystemTotalProcessorTime = "\System\% Total Processor Time"

NT4_SystemSystemUpTime = "\System\System Up Time"

NT4_MemoryCommitLimit = "\Memory\Commit Limit"

NT4_MemoryCommitByte = "\Memory\Committed Bytes"

W2K_SystemTotalProcessorTime = "\Processor(_total)\% Processor Time"

W2K_SystemSystemUpTime = "\System\System Up Time"

W2K_MemoryCommitLimit = "\Memory\Commit Limit"

W2K_MemoryCommitByte = "\Memory\Committed Bytes"

然后重启nsclient

六、nagios添加邮件报警

安装sendEmail

wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz  下载sendEmail

[[email protected] soft]# tar xf sendEmail-v1.56.tar.gz

[[email protected] soft]# cd sendEmail-v1.56

[[email protected] sendEmail-v1.56]# ls

CHANGELOG  README  README-BR.txt  sendEmail  sendEmail.pl  TODO

[[email protected] sendEmail-v1.56]# cp sendEmail /usr/local/bin/

此命令的解释:

-f 表示发送者的邮箱
-t 表示接收者的邮箱
(接收邮件和发送邮件,尽量不要用同一个邮箱帐号,容易出错)
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容

用法:
/usr/local/bin/sendEmail -f  发件人邮件  -t  收件人邮件  -s smtp.163.com -u "邮件主题" -xu 用户名 -xp 密码 -m 邮件内容

首先在命令行测试一下:

[[email protected] sendEmail-v1.56]# /usr/local/bin/sendEmail -f [email protected].com  -t [email protected]  [email protected] -s smtp.163.com -u "hello" -xu [email protected]  -xp password -m test

看到下面这行信息,说明邮件发送成功。

Aug 06 16:28:05 localhost sendEmail[24227]: Email was sent successfully!

①    对nagios添加邮件报警了。

修改配置文件contacts.cfg,修改邮箱,此处为接受报警邮件的邮箱

[[email protected] objects]# vim contacts.cfg

define contact{

contact_name                    nagiosadmin             ; Short name of user

use                             generic-contact         ; Inherit default values from generic-contact template (defined above)

alias                           Nagios Admin            ; Full name of user

email                           [email protected]        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

}

修改commands.cfg 定义发送邮件命令

[[email protected] objects]# vim commands.cfg

define command{

command_name    notify-host-by-email        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail  -f [email protected] -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu [email protected] -xp password

}

define command{

command_name    notify-service-by-email

command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" |  /usr/local/bin/sendEmail  -f [email protected] -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu [email protected] -xp password

}

七、安装cacti

安装依赖软件及创建数据库

[[email protected] soft]# yum install rrdtool* -y

[[email protected] soft]# mysql -uroot -p123654

mysql> create database cacti;

mysql> grant all privileges on cacti.* to "cactier"@‘localhost‘ identified by ‘123654‘;

mysql> flush privileges;

修改snmp配置文件

#       sec.name  source          community

com2sec notConfigUser  default       public

#       name           incl/excl     subtree         mask(optional)

view    systemview    included   .1.3.6.1.2.1.1

view    systemview    included   .1.3.6.1.2.1.25.1.1

#       group          context sec.model sec.level prefix read   write  notif

access  notConfigGroup ""      any       noauth    exact  all  none  none

##           incl/excl subtree                          mask

view all    included  .1                               80

开始安装cacti

[[email protected] soft]# tar xf cacti-0.8.8b.tar.gz

[[email protected] soft]# cd cacti-0.8.8b

[[email protected] soft]# mv cacti-0.8.8b /usr/local/httpd/htdocs/cacti

[[email protected] soft]# cd /usr/local/httpd/htdocs/cacti/

[[email protected] cacti]# mysql -ucactier  -p123654 cacti <cacti.sql

[[email protected] cacti]# useradd cactier

[[email protected] cacti]# chown -R cactier  rra log

修改config.php

[[email protected] cacti]# vim include/config.php

$database_type = "mysql";

$database_default = "cacti";

$database_hostname = "localhost";

$database_username = "cactier";

$database_password = "123654";

$database_port = "3306";

$database_ssl = false;

写计划任务进行轮询

[[email protected] cacti]# crontab –e

*/5  * * * *  /usr/local/php/bin/php  /usr/local/httpd/htdocs/cacti/poller.php  &>/dev/null

在网页上访问cacti:http://192.168.1.124/cacti

出现这个错误是默认用了unix-socket去连接。我们把config.php中的host改为127.0.0.1

之后下一步,就到修改密码了

设置好密码123654然后就能进入cacti了

安装spine

[[email protected] soft]# tar xf cacti-spine-0.8.8b.tar.gz

[[email protected] soft]# cd cacti-spine-0.8.8b

[[email protected] cacti-spine-0.8.8b]# ./configure --prefix=/usr/local/spine --with-mysql=/usr/local/mysql/

[[email protected] cacti-spine-0.8.8b]# make && make install

[[email protected] cacti-spine-0.8.8b]# mv /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf

[[email protected] cacti-spine-0.8.8b]# vim /usr/local/spine/etc/spine.conf

DB_Host         localhost

DB_Database     cacti

DB_User         cactier

DB_Pass         123654

DB_Port         3306

DB_PreG         0

更改轮询器为spine

Setting->poller->poller type 选择 spine ,记得save哦

安装ntop

[[email protected] soft]# tar xf ntop-4.0.1.tar.gz

[[email protected] soft]# cd ntop-4.0.1

[[email protected] ntop-4.0.1]# ./autogen.sh

错误七:

[[email protected] ntop-4.0.1]# yum install libpcap libpcap-devel –y

[[email protected] ntop-4.0.1]# ./autogen.sh

[[email protected] ntop-4.0.1]# ./configure --prefix=/usr/local/ntop

[[email protected] soft]# tar xf GeoIP.tar.gz

[[email protected] soft]# cd GeoIP-1.4.8/

[[email protected] GeoIP-1.4.8]# ./configure

[[email protected] GeoIP-1.4.8]# make && make install

进入到ntop的解压路径,重新编译再安装

[[email protected] ntop-4.0.1]# make&&make install

给ntop设置密码:

[[email protected] ntop-4.0.1]# /usr/local/ntop/bin/ntop –A

[[email protected] ntop-4.0.1]# useradd ntop

[[email protected] ntop-4.0.1]# chown -R ntop:ntop /usr/local/ntop/share/ntop/

[[email protected] ntop-4.0.1]# chown -R ntop:ntop /usr/local/ntop/var/ntop/

[[email protected] ntop-4.0.1]# /usr/local/ntop/bin/ntop -u ntop -c –d –》启动ntop

写入开机自启动

[[email protected] ntop-4.0.1]# echo "/usr/local/ntop/bin/top  -u ntop  -c -d" >>/etc/rc.local

安装ntop插件ntop-v0.2-1.tgz

[[email protected] soft]# tar zxvf ntop-v0.2-1.tgz -C /usr/local/httpd/htdocs/cacti/plugins

[[email protected] soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "ntop";

然后登陆cacti网站:console –》plugin managemaent 点击蓝色的箭头,激活ntop

然后点击User management—》admin—》把view ntop勾上,save,然后点击ntop可以看到如下图

安装setting、monitor,thold等插件

[[email protected] soft]# tar xf settings-v0.7-1.tgz  -C /usr/local/httpd/htdocs/cacti/plugins

[[email protected] soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "settings";

在cacti网站上激活settings

Console---》plugin managementà点点点就激活了,激活后如下图

其他插件如settings一样的方法。

[[email protected] soft]# tar xf monitor-v1.3-1.tgz -C /usr/local/httpd/htdocs/cacti/plugins

[[email protected] soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "monitor";

[[email protected] soft]# tar xf thold-v0.5.0.tgz -C /usr/local/httpd/htdocs/cacti/plugins

[[email protected] soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "thold";

八、Cacti和nagios整合

①安装ndoutils

[[email protected] soft]# tar zxvf ndoutils-1.5.2.tar.gz

[[email protected] soft]# cd ndoutils-1.5.2

[[email protected] ndoutils-1.5.2]# ./configure --prefix=/usr/local/nagios/ --enable-mysql --disable-pgsql --with-mysql-inc=/usr/local/mysql/include/ --with-mysql-lib=/usr/local/mysql/lib/

[[email protected] ndoutils-1.5.2]# make && make install

[[email protected] ndoutils-1.5.2]# cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /usr/local/nagios/bin

[[email protected] ndoutils-1.5.2]# cd db/

[[email protected] db]# mkdir /var/lib/mysql

[[email protected] db]# ln -s /usr/local/mysql/var/run/mysql.sock  /var/lib/mysql/mysql.sock

[[email protected] db]# ./installdb -ucactier -p123654 -hlocalhost –dcacti

[[email protected] db]# cd ..

[[email protected] ndoutils-1.5.2]# cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /usr/local/nagios/etc/ [[email protected] ndoutils-1.5.2]# mv /usr/local/nagios/etc/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg

[[email protected] ndoutils-1.5.2]# mv /usr/local/nagios/etc/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg

[[email protected] ndoutils-1.5.2]# chown -R nagios.nagios /usr/local/nagios/etc/ndo*

[[email protected] ndoutils-1.5.2]# chown -R nagios.nagios /usr/local/nagios/bin/*

[[email protected] ndoutils-1.5.2]# vim /usr/local/nagios/etc/nagios.cfg

添加

broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg –》这两行必须是放在同一行,中间空格隔开,否则报错

然后确保event_broker_options=-1的参数值是-1,这个值默认就是-1。

安装ndo2db前要先安装mysql-devel

Yum install –y mysql-devel

[[email protected] ndoutils-1.5.2]# vim /usr/local/nagios/etc/ndo2db.cfg

socket_type=unix---》修改为socket_type=tcp

db_servertype=mysql

db_host=localhost

db_port=3306

db_name=cacti

db_prefix=nagios_

db_user=cactier

db_pass=123654

[[email protected] ndoutils-1.5.2]# vim /usr/local/nagios/etc/ndomod.cfg

output_type=tcpsocket

output=127.0.0.1

[[email protected] ndoutils-1.5.2]# cp daemon-init /etc/init.d/ndo2db

[[email protected] ndoutils-1.5.2]# vim /etc/init.d/ndo2db

Ndo2dbBin=/usr/local/nagios/bin/ndo2db-3x

[[email protected] ndoutils-1.5.2]# chmod +x /etc/init.d/ndo2db

[[email protected] ndoutils-1.5.2]# service ndo2db start

安装npc插件

[[email protected] soft]# tar xf npc-2.0.4.tar.gz -C /usr/local/httpd/htdocs/cacti/plugins

[[email protected] soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "npc";

安装npc支持json

[[email protected] soft]# cd php-5.5.3/ext/json

[[email protected] json]# /usr/local/php/bin/phpize

[[email protected] json]# ./configure --with-php-config=/usr/local/php/bin/php-config

[[email protected] json]# make

[[email protected] json]# make test

[[email protected] json]# make install

[[email protected] json]# vim /usr/local/php/etc/php.ini添加

extension="json.so"

[[email protected] json]# /usr/local/php/bin/php –m查看json是否加载

修改ndo2db.cfg

[[email protected] json]# vim /usr/local/nagios/etc/ndo2db.cfg

db_prefix=npc_

先激活npc插件,再修改cacti npc插件

Console-->settings-->npc  修改如下图

修改数据库表结构:

[[email protected] ndoutils-1.5.2]# mysql -ucactier -p123654

Msyql>use  cacti

ALTER TABLE `npc_hostchecks` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_hoststatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_servicestatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_statehistory` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_eventhandlers` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_systemcommands` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

ALTER TABLE `npc_notifications` ADD COLUMN `long_output` varchar(8192) NOT NULL default ‘‘ AFTER `output`;

Mysql>exit

重启ndo2db和nagios

#service  ndo2db  restart

#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

nagios+cacti整合

时间: 2024-11-07 13:39:38

nagios+cacti整合的相关文章

开源监控解决方案Nagios+Cacti+PNP4Nagios+NConf+NDOUtils+Nagvis(七)nagios+cacti整合

nagios和cacti的安装请参考之前的文章安装,这里介绍二者如何整合在一起. ndoutils将nagios的监控数据写入数据库 cacti则将ndoutil插件写入数据库的数据展示在页面上,因为数据写入数据库会因为各种因素延时,所以有时候nagios自身的页面有的监控项已经告警,但是cacti页面可能还是正常状态. 所以这套方案的核心还是nagios,所有监控和故障通知都由nagios完成,cacti只是担任web管理页面. 1.修改数据库结构 mysql> use nagios; mys

Cacti+Nagios监控系统(四):Cacti整合Nagios

一.原理 整合cacti和nagios是利用了cacti的一个插件nagios for cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来. 二.cacti扩展模块 cacti扩展模块需要下载安装cacti-plugin,cacti-0.8.8a及以后版本已集成此扩展模块不需要再单独安装,如果使用的是老版本cacti,扩展模块安装如下: cd /root/download wget htt

Cacti+Nagios监控系统(六):Cacti整合Ntop

Ntop是一种监控网络流量工具 一.安装环境 cd /root/download yum install libtool libpcap libpcap-devel gdbm gdbm-devel zlib zlib-devel wget http://mirror01.idc.hinet.net/EPEL/5Server/x86_64/GeoIP-1.4.8-1.el5.x86_64.rpm wget http://mirror01.idc.hinet.net/EPEL/5Server/x86

合nagios+cacti+微信、飞信实现网络监控报警

系统环境:rhel6.3         selinux disabled  和 iptables     整合cacti和nagios是利用了cacti的一个插件nagiosfor cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来. 一.nagios监控本地主机 注释掉localhost.cfg,新增加hosts.cfg,services.cfg [[email protected]

开源监控解决方案Nagios+Cacti+PNP4Nagios+NConf+NDOUtils+Nagvis(一)介绍

以nagios为核心.伴以多款开源软件来进行的整合部署,从而实现监控信息图形化.web页面定义监控配置.监控信息存储.图形化监控主机和服务等一系列功能的解决方案. pnp4nagios是基于RRD轮循(环状)数据库中所提供的综合信息,以可视化图形的方式呈现给用户的一款nagios插件:nconf是基于PHP使用户通过web页面实现对nagios各项属性进行配置,诸如模板定义.添加修改监控的主机/服务等操作:ndoutils可以导出nagios通过SNMP捕获到的当前和历史数据并存储到MySQL数

linux 架设nagios+cacti+nconf完整配置,生产环境中的配置。

以下是傻瓜配置,复制粘贴可以完成.有空我会写个script来帮助大家进一步解决傻瓜的复制粘贴步骤. 但还是希望大家了解nagios cacti nconf是怎样工作的,框架是怎样的. 下面是配置全过程 1.先安装各个nagios+cacti的依赖包 更新源rpm -ivh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm yum update yum -y install httpd p

nagios+cacti部署

nagios+cacti部署安装 一.说明 闲暇之余,安装nagios+cacti,想看下最终实现效果:本文档规范网络监控系统安装流程及安装内容,供运维人员在部署新的运营环境时参考使用. 二.Nagios的安装 1.下载 #wget http://jaist.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.5.0/nagios-3.5.0.tar.gz  (nagios主程序) #wgethttp://jaist.dl.sourcefo

nagios+cacti部署安装

一.说明 闲暇之余,安装nagios+cacti,想看下最终实现效果:本文档规范网络监控系统安装流程及安装内容,供运维人员在部署新的运营环境时参考使用. 二.Nagios的安装 1.下载 #wget http://jaist.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.5.0/nagios-3.5.0.tar.gz  (nagios主程序) #wgethttp://jaist.dl.sourceforge.net/project/n

开源监控解决方案Nagios+Cacti+PNP4Nagios+NConf+NDOUtils+Nagvis(五)cacti安装

1.安装LAMP环境这里就不做介绍,网上一堆的相关文档,还有很多牛人写的自动化安装脚本 2.安装rrdtoolyum install cairo-devel libxml2-devel pango pango-devel rrdtool –y 3.安装SNMP操作系统一般自带SNMP,如果没有即安装 yum -y install net-snmp net-snmp-libs net-snmp-utils 4.安装Cacti (1)下载Cacti#wget http://www.cacti.net