实验环境
Red Hat Enterprise Linux Server release 6.5 (Santiago)
一、appache安装
tar -zxvf httpd-2.2.15.tar.gz
cd httpd-2.2.15
./configure –prefix=/usr/local/apache2 –enable-so –enable-mods-shared=all –enable-cgi –enable-rewrite –enable-deflate –with-mpm=worker
make
make install
安装完后增加nagios用户
groupadd nagios
useradd -g nagios -d /home/nagios -m nagios
passwd nagios
找到apache 的配置文件/usr/local/apache2/conf/httpd.conf
修改httpd.conf中的user group为下面的参数
找到
User daemon
Group daemon
修改为
User nagios
Group nagios
修改ServerName=ip(服务器的ip地址):80
mkdir /usr/local/nagios
chown nagios:nagios /usr/local/nagios
二、安装php+mysql(mysql可以不用安装,为了以后使用最好先安装) 但是环境为red hat 6.5 所以不装了
安装mysql
rpm -ivh MySQL-client-community-5.1.50-1.rhel5.i386.rpm
rpm -ivh MySQL-devel-community-5.1.50-1.rhel5.i386.rpm
rpm -ivh MySQL-client-community-5.1.50-1.rhel5.i386.rpm
安装php前先安装以下软件:
1.安装zlib (安装libpng和gd前需要先安装zlib),
tar -zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure –prefix=/usr/local/zlib
make
make install
2.安装libpng
tar -zxvf libpng-1.2.18.tar.gz
cd libpng-1.2.18
./configure –prefix=/usr/local/libpng
make
make install
3.安装freetype
tar -zxvf freetype-2.3.12.tar.gz
cd freetype-2.3.12
./configure –prefix=/usr/local/freetype
make
make install
4. 安装jpeg
tar -zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
mkdir /usr/local/jpeg
mkdir /usr/local/jpeg/bin
mkdir /usr/local/jpeg/lib
mkdir /usr/local/jpeg/include
mkdir /usr/local/jpeg/man
mkdir /usr/local/jpeg/man/man1
./configure –prefix=/usr/local/jpeg –enable-shared –enable-static
make
make install
以上如果make报错:
./libtool –mode=compile gcc -O2 -I. -c ./jcapimin.c
make: ./libtool:命令未找到
make: *** [jcapimin.lo] 错误 127
首先看有没有安装libtool及libtool-ltdl-devel
rpm -qa|grep libtool
然后进入jpes-6b的源码目录,人后执行以下步骤,切记Copy到当前目录后面的点(.)
cp /usr/share/libtool/config/config.sub .
cp /usr/share/libtool/config/config.guess .
//****************************************//
首先看有没有安装libtool 及 libtool-ltdl-devel
1.rpm -qa | grep libtool #wget:http://ftp.gnu.org/gnu/libtool/libtool-2.2.6a.tar.gz
#./configure
#make
#make install
然后进入jpeg-6b的源码目录,然后执行以下步骤,切记!COPY到当前目录注意后面的点(.)
网上好多都把config.sub和config.guess的路径弄错了,应该是在/usr/share/libtool/config/下,而不是在
/usr/share/libtool/下
#cd jpeg
#cp /usr/share/libtool/config/config.sub .
#cp /usr/share/libtool/config/config.guess .
也就是把 libtool里面的两个配置文件拿来覆盖掉jpeg-6b目录下的对应文件
make clean 再重新configure(切记必须重新configure,否则仍提示这个错误)
没有权限的时候先建立对应的文件夹,再次make install就行了
//****************************************//
5. 安装gd
tar -zxvf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure –prefix=/usr/local/gd –with-jpeg=/usr/local/jpeg –with-freetype=/usr/local/freetype –with-png=/usr/local/libpng/ –with-zlib
//编译过程中会看到如下信息
** Configuration summary for gd 2.0.35:
Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: no
Support for Xpm library: no
Support for pthreads: yes
//可以看到png 、 jpeg 、 freetype都已经安装上了
make
make install
若出现错误
png.h:402:21: error: pngconf.h(或png.h): No such file or directory
In file included from gd_png.c:16:
png.h:513: error: expected specifier-qualifier-list before \’png_byte\’
将/usr/local/libpng/include/pngconf.h png.h拷贝到gd-2.0.35安装目录下。
上面软件都安装好后,现在就可以安装php了
tar -zxvf php-5.3.2.tar.gz
cd php-5.3.2
./configure –prefix=/usr/local/php5 –with-apxs2=/usr/local/apache2/bin/apxs –with-gd=/usr/local/gd –with-zlib –with-libpng=/usr/local/libpng –with-jpeg=/usr/local/jpeg –with-freetype=/usr/local/freetype –enable-sockets –with-iconv –enable-mbstring –enable-track-vars –enable-force-cgi-redirect –with-config-file-path=/usr/local/php5/etc
./configure –prefix=/usr/local/php5 –with-apxs2=/usr/local/apache2/bin/apxs –with-zlib –enable-sockets –with-iconv –enable-mbstring –enable-track-vars –enable-force-cgi-redirect –with-config-file-path=/usr/local/php5/etc
拷贝一些所需内容
make
make install
安装完后执行
cp php.ini-development /usr/local/php5/etc/php.ini
最后修改httpd.conf,使apache能使用php,增加如下参数
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
然后找到
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
修改为
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
三、安装nagios软件
下载nagios-3.2.1.tar.gz(安装4.0)
tar -zxvf nagios-3.2.1.tar.gz
cd nagios-3.2.1
./configure –prefix=/usr/local/nagios –with-gd-lib=/usr/local/lib –with-gd-inc=/usr/local/include
make all //编译nagios
make install //安装主要的程序,CGI及HTML文件
make install-init //在/etc/rc.d/init.d安装启动脚本
make install-commandmode //给外部命令访问nagios配置文件的权限
make install-config //将配置文件的例子复制到nagios的安装目录
验证程序是否被正确安装 切换到/usr/local/nagios
看是否存在etc,bin,sbin,share,var五个目录
bin Nagios执行程序所在目录,nagios文件即为主程序
etc Nagios配置文件位置
sbin Nagios cgi文件所在目录,执行外部命令所需文件所在的目录
share Nagios网页文件所在的目录
var Nagios日志文件,spid等文件所在的目录
libexec Nagios 外部插件所在目录
四、安装nagios插件
下载nagios-plugins-1.4.14.tar.gz
tar -zxvf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure –prefix=/usr/local/nagios-plugins
./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios
make all
make install
安装完成以后在/usr/local/nagios-plugins会产生一个libexec的目录,将该目录全部移动到/usr/local/nagios目录下
cp -r /usr/local/nagios-plugins/libexec /usr/local/nagios/
chown -R nagios:nagios /usr/local/nagios/
五、安装imagepak-base
下载imagepak-base.tar.gz
tar -zxvf imagepak-base.tar.gz
解压以后是一个base目录,将该目录拷贝到 /usr/local/nagios/share/images/logos目录下
cp -r base /usr/local/nagios/share/images/logos
chown -R nagios:nagios /usr/local/nagios/share/images/logos
六、配置httpd.conf使用nagios
在httpd.conf后面加入如下内容
vi /usr/local/apache2/conf/httpd.conf
#Setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory \”/usr/local/nagios/sbin\”>
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName \”Nagios Access\”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory \”/usr/local/nagios/share\”>
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName \”Nagios Access\”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
对增加后的参数进行验证
/usr/local/apache2/bin/apachectl -t //检查配置文件是否正确
生成apache访问的认证文件并启动apache进行验证
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios
cat /usr/local/nagios/etc/htpasswd //查看认证文件内容
/usr/local/apache2/bin/apachectl start //启动apache
到目前为止,监控主机上的nagios的所有安装已经全部完成,接下来就是要配置具体的监控项目了
七、配置nagios(主要是定义监控的对象所存放的是哪些文件)
预备知识 :
在Nagios里面定义了一些基本的对象,一般用到的有:
监控时间段 timeperiod 7X24小时不间断还是周一至周五,或是自定义的其他时间段
联系人 contact 出了问题向谁报告?一般当然是系统管理员了
被监控主机 Host 所需要监控的服务器,当然可以是监控机自己
监控命令 command nagios发出的哪个指令来执行某个监控,这也是自己定义的
被监控的服务 Service 例如主机是否存活,80端口是否开,磁盘使用情况或者自定义的服务等
注意:多个被监控主机可以定义为一个主机组,多个联系人可以被定义为一个联系人组
修改Nagios的配置文件:
————————————————————————
vi /usr/local/nagios/etc/nagios.cfg //修改nagios的主配置文件
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg //注释此行
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //联系人配置文件路径
cfg_file=/usr/local/nagios/etc/objects/commands.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 //服务配置文件路径
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg //联系组配置文件路径
//**********************//
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/dba.cfg
cfg_file=/usr/local/nagios/etc/objects/service.cfg
cfg_file=/usr/local/nagios/etc/objects/servicegroup.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg
//**********************//
check_external_commands=0 //将 0 改成 1,允许在web界面下执行重启Nagios
command_check_interval=60s //改成 60s, 命令检查时间间隔
check_external_commands=0 //将0改为1,允许在web界面执行external_commands
然后检查配置文件是否出错
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果正常就显示如下信息
Total Warnings: 0
Total Errors: 0
因为默认的nagios配置文件没有 hosts.cfg、hostgroups.cfg等文件,因此在检查的时候会报错,这时需要手工的去创建这些文件:
cd /usr/local/nagios/etc/objects
touch hosts.cfg
touch hostgroups.cfg
……….
建完后再检查是否报错:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
八、配置相关cfg文件
contacts.cfg timeperiods.cfg printer.cfg switch.cfg windows.cfg 这几个自带先不管
vi cgi.cfg //修改cgi脚本控制文件
use_authentication=1 //确保值为 1 —20161226 改为0
default_user_name=nagios //修改为认证用户
//后面修改内容如下:
authorized_for_system_information=nagiosadmin,nagios
authorized_for_configuration_information=nagiosadmin,nagios
authorized_for_system_commands=nagiosadmin,nagios
authorized_for_all_services=nagiosadmin,nagios
authorized_for_all_hosts=nagiosadmin,nagios
authorized_for_all_service_commands=nagiosadmin,nagios
authorized_for_all_host_commands=nagiosadmin,nagios
vi hosts.cfg
define host{
use linux-server ; Name of host template to use
host_name wbsc_app_192.168.22.16
alias wbsc_ap_192.168.22.16
address 192.168.22.16
}
define host{
use linux-server ; Name of host template to use
host_name wbsc_app_192.168.22.17
alias wbsc_ap_192.168.22.17
address 192.168.22.17
}
——————————-
vi hostgroups.cfg
define hostgroup{
hostgroup_name WSBS_SC_APP
alias APP_SC_GROUP
members wbsc_app_192.168.22.16,wbsc_app_192.168.22.17
}
——————————–
vi templates.cfg
# Local service definition template – This is NOT a real service, just a template!
define service{
name local-service ; The name of this service template
use generic-service ; Inherit default values from the generic-service definition
max_check_attempts 4 ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until a hard state can be determined
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
define service{
name wls-service ; The name of this service template
use generic-service ; Inherit default values from the generic-service definition
max_check_attempts 1 ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 2 ; Check the service every 5 minutes under normal conditions
retry_check_interval 1 ; Re-check the service every minute until a hard state can be determined
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
——————————————————–
vi commands.cfg
define command {
command_name host-notify-by-sms
command_line /usr/local/bin/sms_send \”Host $HOSTSTATE$ alert for $HOSTNAME$! on \’$DATETIME$\’ \” $CONTACTPAGER$
}
#service notify by sms //发送短信报警
define command {
command_name service-notify-by-sms
command_line /usr/local/bin/sms_send \”\’$HOSTADDRESS$\’ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\” $CONTACTPAGER$
}
vi hostgroups.cfg //将多个主机定义一个主机组
define hostgroup{
hostgroup_name sa-servers //主机组名称
alias sa Servers //别名
members nagios-server
//组的成员主机,多个主机以逗号相隔,必须是上面hosts.cfg中定义的
}
vi service.cfg
define service{
use local-service #引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。
host_name Nagios-Linux #指定要监控哪个主机上的服务,“Nagios-Server”在hosts.cfg文件中进行了定义。
service_description check-host-alive #对监控服务内容的描述,以供维护人员参考。
check_command check-host-alive #指定检查的命令。
}
vi servicegroup.cfg
//书写时要注意的是,check_tcp与要监控的服务端口之间要用”!”做分隔符。如果服务太多,以考虑用脚本来生成。
—————————————————————————–
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //检查所有配置文件的正确性
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg //作为守护进程后台启动Nagios
echo \”/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg\” >> /etc/rc.local //开机自动运行
—————————————————————————–
注意事项:
监控中间件:
1,安装weblogic软件,并把modules文件夹拷贝到wlst目录下,并增加执行权限到modules文件夹内。
2. 在main.sh中设置weblogic软件W具体位置,并把main.py main.sh增加执行权限
3.相关的check_wls*插件拷贝到nagios的libexec目录下
——————————————————————————————————–
使用命令和插件监控更多信息
cd /usr/local/nagios/libexec //插件默认的安装路径
./check_disk -w 10% -c 5% /
//检查根分区的使用情况,若剩余10%以下,为警告状态(warning)
//若剩余 5%以下,为严重状态(critical)
设置并熟悉以上的配置后,下面就需要进行具体的监控工作了
被监控主机上的安装部署配置
九、Nagios的启动与停止
启动Nagios
a. 通过初始化脚本启动nagios
# /etc/init.d/nagios start
or
# service nagios start
b. 手工方式启动nagios
通过nagios命令的“-d”参数来启动nagios守护进程:
# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
重启Nagios
当修改了配置文件让其生效时,需要重启/重载Nagios服务。
a. 通过初始化脚本来重启nagios
# /etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart
b. 通过web监控页重启nagios
可以通过web监控页的 \”Process Info\” -> \”Restart the Nagios process\”来重启nagios
十、安装nrpe
—1.在被监控机(Nagios-Linux)上
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure //NRPE port: 5666
make all
make install-plugin
make install-daemon //安装daemon
make install-daemon-config //安装配置文件
ls /usr/local/nagios/
bin/ etc/ libexec/ share/ //现在nagios目录会有4个目录了
# chown nagios.nagios /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios/libexec
——————————-
将NRPE daemon作为xinetd下的一个服务运行
yum -y install xinetd
service xinetd start
chkconfig –level 3 xinetd on
make install-xinetd //安装xinetd脚本
vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 192.168.4.226 //在后面增加监控主机的地址,以空格间隔
vi /etc/services
nrpe 5666/tcp # nrpe //增加这一行
service xinetd restart
netstat -at | grep nrpe //查看NRPE是否已经启动
netstat -an | grep 5666 //查看5666端口是否被监听
. 测试NRPE是否则正常工作
使用上面在被监控机上安装的check_nrpe 这个插件测试NRPE 是否工作正常。
# /usr/local/nagios/libexec/check_nrpe -H localhost
# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
注:为了后面工作的顺利进行,注意本地防火墙要打开5666能让外部的监控机访问
vi /etc/sysconfig/iptables //增加一条5666的端口
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 5666 -j ACCEPT
查看check_nrpe 命令用法
# /usr/local/nagios/libexec/check_nrpe –h
check_nrpe –H 被监控的主机 -c 要执行的监控命令
注意:-c 后面接的监控命令必须是nrpe.cfg 文件中定义的。也就是NRPE daemon只运行nrpe.cfg中所定义的命令。
————————————–
查看NRPE的监控命令
cd /usr/local/nagios/etc
cat nrpe.cfg |grep -v \”^#\”|grep -v \”^$\” 或者 vi nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_http]=/usr/local/nagios/libexec/check_http -I 127.0.0.1
HTTP CRITICAL – Unable to open TCP socket http 会报错
[***]中是命令名,也就是check_nrpe的-c参数可以接的内容,=后面是实际执行的插件程序
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users //检测登陆用户数
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load //CPU负载
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_zombie_procs //僵尸进程
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_total_procs //总进程数
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk //磁盘使用量
其中:
//check_load -w 15,10,5 -c 30,25,20
//在unix里面负载的均值通常表示是1分钟,5分钟,15分钟内平均有多少进程处于等待状态
//当1分钟多于15个进程等待,5分钟多于10个,15分钟多于5个则为warning状态
//当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态
记住监测磁盘时,先用
fdisk -l //查看磁盘的分区,如果sda, 则应该改成下面的形式,把默认的hda1改成sda1
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 268 2048287+ 82 Linux swap / Solaris
/dev/sda3 269 19457 154135642+ 83 Linux
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
command[check_sda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda1
DISK OK – free space: /boot 77 MB (82% inode=99%);| /boot=16MB;78;88;0;98
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda3
DISK OK – free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809
例如查看根分区的使用情况,执行
/usr/local/nagios/libexec/check_disk -w 10% -c 5% /
命令的含义是检查分区/的使用情况,若剩余10%以下,为警告状态(warning),5%以下为严重状态(critical),
执行后我们会看到下面这条信息
DISK WARNING – free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809
说明当前是warning的状态,空闲空间还有97%.
—-2.在监控主机(Nagios-Server)上
在运行Nagios的监控主机上
因为之前已经将Nagios运行起来了,现在要做的是:
安装check_nrpe插件
在commands.cfg中创建check_nrpe中的命令,只有在commands.cfg中定义过的命令才能在services.cfg中使用
创建对被监控主机的监控项目
tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install-plugin //只运行这一步即可,只需要check_nrpe插件
/usr/local/nagios/libexec/check_nrpe -H 192.168.20.53
NRPE v2.8.1
在commands.cfg中增加对check_nrpe的定义
vi /usr/local/nagios/etc/commands.cfg
#################################################################
# 2008.12.4 by ritto
#################################################################
# \’check_nrpe\’ command definition
define command{
command_name check_nrpe # 定义命令名称为check_nrpe,在services.cfg中要使用这个名称.
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ #这是定义实际运行的插件程序.
}
-c 后面带的$ARG1$ 参数是传给nrpe daemon 执行的检测命令,之前说过了它必须是nrpe.cfg 中所定义的那5条命令中的其中一条。在services.cfg 中使用check_nrpe 的时候要用 “!” 带上这个参数。
/usr/local/nagios/libexec/check_ora ora_jobs -s
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29615265/viewspace-2139615/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29615265/viewspace-2139615/
- 点赞
- 收藏
- 分享
- 文章举报
couding3688发布了0 篇原创文章 · 获赞 0 · 访问量 50私信关注