系统: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。

安装前准备:[root@localhost ~]# yum install -y gcc gcc-c++ gcc-devel cmake

安装apr

[root@localhost soft]# tar xvf apr-1.5.0.tar.gz

[root@localhost soft]# cd apr-1.5.0

[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr

[root@localhost apr-1.5.0]# make &&make install

安装apr-util

[root@localhost soft]# tar xvf apr-util-1.5.3.tar.gz

[root@localhost soft]# cd apr-util-1.5.3

[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@localhost apr-util-1.5.3]# make &&make install

安装pcre

[root@localhost soft]# tar xvf pcre-8.33.tar.gz

[root@localhost soft]# cd pcre-8.33

[root@localhost pcre-8.33]# ./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

[root@localhost pcre-8.33]# make && make install

安装apache

[root@localhost soft]# tar xvf httpd-2.4.7.tar.gz

[root@localhost 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

[root@localhost httpd-2.4.7]# make && make install

启动apache

[root@localhost bin]# cd /usr/local/httpd/bin/

[root@localhost bin]# ./apachectl start

查看端口监听

[root@localhost bin]# netstat -antl|grep 80

2.安装mysql

解压,编译安装mysql

[root@localhost soft]# tar xf mysql-5.6.10.tar.gz

[root@localhost soft]# cd mysql-5.6.10

[root@localhost 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:

 

[root@localhost mysql-5.6.10]# yum  install -y  ncurses-devel

[root@localhost mysql-5.6.10]# rm CMakeCache.txt

然后重新编译

[root@localhost mysql-5.6.10]# make && make install

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

[root@localhost mysql-5.6.10]# mkdir /database

[root@localhost mysql-5.6.10]# mkdir /usr/local/mysql/var/log -p

[root@localhost mysql-5.6.10]# mkdir /usr/local/mysql/var/run

[root@localhost mysql-5.6.10]# mkdir /usr/local/mysql/etc

[root@localhost mysql-5.6.10]# mkdir /usr/local/mysql/binlog

[root@localhost mysql-5.6.10]# useradd mysql

[root@localhost mysql-5.6.10]# chown -R mysql:mysql /database /usr/local/mysql/

修改配置文件

[root@localhost mysql-5.6.10]# cp support-files/my-default.cnf /usr/local/mysql/etc/my.cnf

[root@localhost 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数据库

[root@localhost mysql-5.6.10]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/database --user=mysql

添加mysql自启动

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

[root@localhost mysql-5.6.10]# chkconfig --add mysql5.6d

[root@localhost mysql-5.6.10]# chkconfig mysql5.6d on

[root@localhost mysql-5.6.10]# service mysql5.6d start

Starting MySQL...                                          [  OK  ]

[root@localhost mysql-5.6

修改root密码:

[root@localhost soft]# /usr/local/mysql/bin/mysql –uroot

mysql> use mysql

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

[root@localhost soft]# /usr/local/mysql/bin/mysql -uroot -p123654

3.安装php

安装mhash

[root@localhost soft]# tar jxvf mhash-0.9.9.9.tar.bz2

[root@localhost soft]# cd mhash-0.9.9.9

[root@localhost mhash-0.9.9.9]# ./configure

[root@localhost mhash-0.9.9.9]# make &&make install

安装libmcrypt

[root@localhost soft]# tar xf libmcrypt-2.5.6.tar.gz

[root@localhost soft]# cd libmcrypt-2.5.6

# ./configure

#make

#make install

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

[root@localhost soft]# tar xf zlib-1.2.5.1.tar.gz

[root@localhost soft]# cd zlib-1.2.5.1

[root@localhost zlib-1.2.5.1]# CFLAGS="-O3 -fPIC"./configure

[root@localhost zlib-1.2.5.1]# make && make install

[root@localhost zlib-1.2.5.1]#CFLAGS="-O3 -fPIC" ./configure --prefix=/usr/local/zlib

[root@localhost zlib-1.2.5.1]# make && make install

 

安装libpng

[root@localhost soft]# tar xf libpng-1.2.47.tar.xz

[root@localhost soft]# cd libpng-1.2.47

[root@localhost libpng-1.2.47]# CFLAGS="-O3 -fPIC"./configure --prefix=/usr/local/libpng

安装jpeg

[root@localhost soft]# tar xf jpeg-6b.tar.gz

[root@localhost soft]# cd jpeg/src/

[root@localhost src]# cp /usr/share/libtool/config/config.guess config.guess

cp: overwrite `config.guess'? y

[root@localhost src]# cp /usr/share/libtool/config/config.sub config.sub

cp: overwrite `config.sub'? y

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

[root@localhost src]# make && make install

错误2:

 

[root@localhost src]# mkdir -p /usr/local/jpeg/bin

[root@localhost src]# mkdir -p /usr/local/jpeg/lib

[root@localhost src]# mkdir -p /usr/local/jpeg/include

[root@localhost src]# mkdir -p /usr/local/jpeg/man

[root@localhost src]# mkdir -p /usr/local/jpeg/man/man1

安装freetype

[root@localhost soft]# tar jxvf freetype-2.3.11.tar.bz2

[root@localhost soft]# cd freetype-2.3.11

[root@localhost freetype-2.3.11]# ./configure --prefix=/usr/local/freetype --enable-shared

[root@localhost freetype-2.3.11]# make && make install

安装gd

[root@localhost soft]# tar xf gd-2.0.35.tar.gz

[root@localhost soft]# cd gd/2.0.35/    

[root@localhost 2.0.35]# ./configure --prefix=/usr/local/gd  --with-jpeg=/usrl/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype

[root@localhost 2.0.35]# make && make install

出现错误3:

 

[root@localhost 2.0.35]# sed -i 's#png.h#/usr/local/libpng/include/png.h#' gd_png.c

安装php

[root@localhost ~]# yum install libxml2* -y

[root@localhost soft]# tar xf php-5.5.13.tar.gz

[root@localhost soft]# cd php-5.5.13

[root@localhost 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

[root@localhost 2.0.35]#make clean

[root@localhost 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

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

[root@localhost libgd-2.1.0]# make && make install

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

[root@localhost php-5.5.13]# make

错误5:

 

[root@localhost php-5.5.13]# yum install libXpm-devel –y

[root@localhost php-5.5.13]# make

[root@localhost php-5.5.13]# make test

[root@localhost php-5.5.13]# make install

安装pdo_mysql模块

[root@localhost php-5.5.13]# cd ext/pdo_mysql/

[root@localhost pdo_mysql]# /usr/local/php/bin/phpize

[root@localhost pdo_mysql]# mkdir ext/pdo/tests -p

[root@localhost pdo_mysql]# ./configure --with-php-config=/usr/local/php/bin/php-config  --with-pdo-mysql=/usr/local/mysql

[root@localhost pdo_mysql]# make

[root@localhost pdo_mysql]# make test

[root@localhost pdo_mysql]# make install

[root@localhost php-5.5.13]# cp php.ini-development /usr/local/php/etc/php.ini

[root@localhost pdo_mysql]# echo 'extension_dir=”/usr/local/php/lib/php/extensions/no-debug-zts-20121212/”' >>/usr/local/php/etc/php.ini

[root@localhost pdo_mysql]# echo 'extension=”pdo_mysql.so”' >>/usr/local/php/etc/php.ini

查看模块是否被加载

[root@localhost php-5.5.13]# /usr/local/php/bin/php  -m |grep pdo_mysql

pdo_mysql

让apache支持php

[root@localhost 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

[root@localhost bin]# vim ../htdocs/test.php

<?php

Phpinfo();

?>

测试php能否连接数据库

[root@localhost 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用户

[root@localhost ~]# useradd -s /sbin/nologin nagios

[root@localhost ~]# mkdir -p /usr/local/nagios

[root@localhost ~]# chown -R nagios:nagios /usr/local/nagios

解压,编译安装

[root@localhost soft]# tar xvf nagios-3.4.3.tar.gz

[root@localhost soft]# cd nagios

[root@localhost nagios]# ./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/local/gd/lib  --with-gd-inc=/usr/local/gd/include

[root@localhost nagios]# make all

[root@localhost nagios]# make install

[root@localhost nagios]#make install-init

[root@localhost nagios]#make install-commandmode

[root@localhost nagios]#make install-config

编译安装nagios插件

[root@localhost soft]# tar xvf nagios-plugins-1.4.16.gz

[root@localhost soft]# cd nagios-plugins-1.4.16

[root@localhost nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios/

[root@localhost nagios-plugins-1.4.16]# make && make install

编译安装nrpe

[root@localhost soft]# tar xf nrpe-2.14.tar.gz

[root@localhost soft]# cd nrpe-2.14

[root@localhost nrpe-2.14]# ./configure

[root@localhost nrpe-2.14]# make all

[root@localhost nrpe-2.14]# make install-plugin

配置apache和nagios

[root@localhost 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

[root@localhost 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用户权限

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

[root@localhost 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

[root@localhost nrpe-2.14]# cd /usr/local/httpd/bin/

[root@localhost bin]# ./apachectl restart

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

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

[root@localhost 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插件

[root@localhost nagiossoft]# useradd -s /sbin/nologin nagios

[root@localhost nagiossoft]# mkdir /usr/local/nagios

[root@localhost nagiossoft]# tar xf nagios-plugins-1.4.16.gz

[root@localhost nagiossoft]# cd nagios-plugins-1.4.16

[root@localhost nagios-plugins-1.4.16]# ./configure  --prefix=/usr/local/nagios/

[root@localhost nagios-plugins-1.4.16]#make && make install

安装nrpe

[root@localhost nrpe-2.14]# ./configure

[root@localhost nrpe-2.14]# make all

[root@localhost nrpe-2.14]# make instlal-plugin

[root@localhost nrpe-2.14]# make install-daemon

[root@localhost nrpe-2.14]#  make install-daemon-config

[root@localhost nrpe-2.14]# chown  -R  nagios.nagios  /usr/local/nagios

[root@localhost nrpe-2.14]# vim /usr/local/nagios/etc/nrpe.cfg

找到这句加上监控机的ip

allowed_hosts=127.0.0.1,192.168.1.124

启动nrpe

[root@localhost nrpe-2.14]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

查看监听端口:

[root@localhost nrpe-2.14]# netstat -antl|grep 5666

tcp        0      0 0.0.0.0:5666                0.0.0.0:*

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

检测自己的nrpe是否成功

 [root@localhost nrpe-2.14]# /usr/local/nagios/libexec/check_nrpe -H localhost

NRPE v2.15

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

[root@localhost ~]# /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服务

[root@localhost nrpe-2.14]# service snmpd restart

四、配置nagios

先看看etc下面有哪些文件

 

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

 

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

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

 

在监控主机定义监控

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

[root@localhost 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

[root@localhost 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

[root@localhost 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主配置文件

[root@localhost 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

先检测配置文件有无错误

[root@localhost objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

重启

[root@localhost 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掉,然后再启动。

[root@localhost ~]# 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

[root@localhost ~]# kill -9 19527

[root@localhost ~]# /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去定义这个命令去检查了。

[root@localhost 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的主配置文件

[root@localhost 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

[root@localhost soft]# tar xf sendEmail-v1.56.tar.gz

[root@localhost soft]# cd sendEmail-v1.56

[root@localhost sendEmail-v1.56]# ls

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

[root@localhost 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 邮件内容

首先在命令行测试一下:

[root@localhost sendEmail-v1.56]# /usr/local/bin/sendEmail -f 13873356730@163.com  -t 603461655@qq.com  13873356730@163.com -s smtp.163.com -u "hello" -xu 13873356730@163.com  -xp password -m test

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

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

①    对nagios添加邮件报警了。

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

[root@localhost 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                           603461655@qq.com        ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

        }

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

[root@localhost 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 13873356730@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu 13873356730@163.com -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 13873356730@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu 13873356730@163.com -xp password

        }

七、安装cacti

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

[root@localhost soft]# yum install rrdtool* -y

[root@localhost 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

[root@localhost soft]# tar xf cacti-0.8.8b.tar.gz

[root@localhost soft]# cd cacti-0.8.8b

[root@localhost soft]# mv cacti-0.8.8b /usr/local/httpd/htdocs/cacti

[root@localhost soft]# cd /usr/local/httpd/htdocs/cacti/

[root@localhost cacti]# mysql -ucactier  -p123654 cacti <cacti.sql

[root@localhost cacti]# useradd cactier

[root@localhost cacti]# chown -R cactier  rra log

修改config.php

[root@localhost 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;

写计划任务进行轮询

[root@localhost 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

[root@localhost soft]# tar xf cacti-spine-0.8.8b.tar.gz

[root@localhost soft]# cd cacti-spine-0.8.8b

[root@localhost cacti-spine-0.8.8b]# ./configure --prefix=/usr/local/spine --with-mysql=/usr/local/mysql/

[root@localhost cacti-spine-0.8.8b]# make && make install

[root@localhost cacti-spine-0.8.8b]# mv /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf

[root@localhost 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

[root@localhost soft]# tar xf ntop-4.0.1.tar.gz

[root@localhost soft]# cd ntop-4.0.1

[root@localhost ntop-4.0.1]# ./autogen.sh

错误七:

 

[root@localhost ntop-4.0.1]# yum install libpcap libpcap-devel –y

[root@localhost ntop-4.0.1]# ./autogen.sh

[root@localhost ntop-4.0.1]# ./configure --prefix=/usr/local/ntop

 

[root@localhost soft]# tar xf GeoIP.tar.gz

[root@localhost soft]# cd GeoIP-1.4.8/

[root@localhost GeoIP-1.4.8]# ./configure

[root@localhost GeoIP-1.4.8]# make && make install

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

[root@localhost ntop-4.0.1]# make&&make install

给ntop设置密码:

[root@localhost ntop-4.0.1]# /usr/local/ntop/bin/ntop –A

[root@localhost ntop-4.0.1]# useradd ntop

[root@localhost ntop-4.0.1]# chown -R ntop:ntop /usr/local/ntop/share/ntop/

[root@localhost ntop-4.0.1]# chown -R ntop:ntop /usr/local/ntop/var/ntop/

[root@localhost ntop-4.0.1]# /usr/local/ntop/bin/ntop -u ntop -c –d –》启动ntop

 

写入开机自启动

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

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

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

[root@localhost 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等插件

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

[root@localhost soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "settings";

在cacti网站上激活settings

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

 

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

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

[root@localhost soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "monitor";

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

[root@localhost soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "thold";

八、Cacti和nagios整合

①安装ndoutils

[root@localhost soft]# tar zxvf ndoutils-1.5.2.tar.gz

[root@localhost soft]# cd ndoutils-1.5.2

[root@localhost 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/

[root@localhost ndoutils-1.5.2]# make && make install

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

[root@localhost ndoutils-1.5.2]# cd db/

[root@localhost db]# mkdir /var/lib/mysql

[root@localhost db]# ln -s /usr/local/mysql/var/run/mysql.sock  /var/lib/mysql/mysql.sock

[root@localhost db]# ./installdb -ucactier -p123654 -hlocalhost –dcacti

[root@localhost db]# cd ..

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

[root@localhost ndoutils-1.5.2]# mv /usr/local/nagios/etc/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg

[root@localhost ndoutils-1.5.2]# chown -R nagios.nagios /usr/local/nagios/etc/ndo*

[root@localhost ndoutils-1.5.2]# chown -R nagios.nagios /usr/local/nagios/bin/*

[root@localhost 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

[root@localhost 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

[root@localhost ndoutils-1.5.2]# vim /usr/local/nagios/etc/ndomod.cfg

output_type=tcpsocket 

output=127.0.0.1

[root@localhost ndoutils-1.5.2]# cp daemon-init /etc/init.d/ndo2db

[root@localhost ndoutils-1.5.2]# vim /etc/init.d/ndo2db

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

[root@localhost ndoutils-1.5.2]# chmod +x /etc/init.d/ndo2db

[root@localhost ndoutils-1.5.2]# service ndo2db start

安装npc插件

[root@localhost soft]# tar xf npc-2.0.4.tar.gz -C /usr/local/httpd/htdocs/cacti/plugins

[root@localhost soft]# vim /usr/local/httpd/htdocs/cacti/include/config.php

$plugins[] = "npc";

安装npc支持json

[root@localhost soft]# cd php-5.5.3/ext/json

 [root@localhost json]# /usr/local/php/bin/phpize

[root@localhost json]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@localhost json]# make

[root@localhost json]# make test

[root@localhost json]# make install

[root@localhost json]# vim /usr/local/php/etc/php.ini添加

extension="json.so"

[root@localhost json]# /usr/local/php/bin/php –m查看json是否加载

修改ndo2db.cfg

[root@localhost json]# vim /usr/local/nagios/etc/ndo2db.cfg

db_prefix=npc_

 

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

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

 

 

修改数据库表结构:

[root@localhost 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