首页 > 代码库 > Linux下nagios的搭建及相关配置
Linux下nagios的搭建及相关配置
一、LAMP环境部署
1、安装php
1.安装yum源
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
2.执行yum安装
yum --enablerepo=remi,remi-php55 install php php-devel php-common php-fpm php-opcache \
php-cli php-pear php-pdo php-mysqlnd php-sqlite php-pecl-memcached php-gd \
php-mbstring php-mcrypt php-xml php-openssl php-intl php-zmq -y
3.查看版本
php -v
2、安装mysql(安装之前先卸载掉系统自带的版本)
1.安装oracle官方yum源
rpm -ivh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
2.yum安装
yum -y install mysql-server mysql
3.查看版本
mysql -V
3、Apache默认系统已经安装,开启支持php页面解析即可
1.修改配置文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.html index.php index.html.var
/etc/init.d/httpd restart
2.编辑测试页进行测试
cat index.php
<?php
phpinfo();
?>
二、nagios环境部署
1、下载源码包
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.3.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz#_ga=1.84131165.51197378.1492996402
2、安装依赖环境
yum install gcc gd gd-devel libpng libpng-devel libjpeg libjpeg-devel zlib zlib-devel openssl-devel -y
3、创建用户
/usr/sbin/useradd -m -s /sbin/nologin nagios
4、编辑安装主程序包
1.安装
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
make all
make install //安装nagios的主程序,CGI和HTML文件
make install-init //生成/etc/rc.d/init.d/nagios启动脚本
make install-config //安装示例配置文件,路径/usr/local/nagios/etc
make install-commandmode //设定相应nagios工作目录的权限
make install-webconf //安装Nagios的WEB配置文件到Apache的conf.d目录下
2.查看安装是否成功
ls /usr/local/nagios/
bin etc libexec sbin share var
5、安装插件包
1.安装
tar zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1/
./configure
make && make install
2.检查安装是否成功(如果下面有很多东西,说明成功)
ls /usr/local/nagios/libexec/
6、创建nagios用户,进行web登录(用户必须是nagiosadmin,不然web页面有的功能不能用)
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
7、web登录测试
http://ip/nagios/
8、安装NRPE(监控别的主机需要的插件)
1.安装
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12/
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
2.启动nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
3.检查是否成功(查看端口并连接自己测试,如果返回版本号说明正常)
netstat -antp |grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 43336/nrpe
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.12
4.修改配置文件,让监控主机可以连接(修改客户机,服务端连接测试)
vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,122.200.94.29
5.重启nrpe
killall nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
6.在监控端进行测试,是否能成功连接被监控端的nrpe
/usr/local/nagios/libexec/check_nrpe -H 122.200.94.43
NRPE v2.12
7.如果返回
Connection refused by host
8.解决:编辑客户端的ssh文件,允许root可以直接登录(一般ubuntu系统会出现这个问题,centos默认root可以登录)
vim /etc/ssh/sshd_config
PermitRootLogin yes
/etc/init.d/ssh restart
注意:
在ubuntu下安装nrpe之前需要安装ssl(监控端和被监控端版本需要保持一致)
apt-get install libssl-dev libssl0.9.8
三:添加监控
NRPE实际上是使用check_nrpe来远程调用NRPE,然后再由NRPE调用本地的插件,所以,一部分是在监控机配置,用于加载定义NRPE要监测的服务,另一部分是在被监控机配置,主要定义监控的命令,具体操作如下
1.定义NRPE插件需要使用的命令(文件末尾添加如下内容)
vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
2.一般监控都定义在servers下面,所以在配置文件中开启一项参数,将servers下的文件包含进来
vim /usr/local/nagios/etc/nagios.cfg
cfg_dir=/usr/local/nagios/etc/servers
3.添加监控主机及监控的服务(servers默认没有,手动创建)
cd servers
cat 200.cfg
#监控的主机
define host{
use linux-server //调用的/usr/local/nagios/etc/objects/templates.cfg中已经定义好的模块(可以根据需求自己定义)
host_name 122.200.94.200
alias 122.200.94.200
address 122.200.94.200
}
#监控ping
define service{
use local-service
host_name 122.200.94.200
service_description Ping //web页面展示的名称
check_command check_nrpe!check_ping//使用check_nrpe模块远程调用被监控主机中定义好的命令,然后再调用本地的check_ping插件进行监控
}
#负载情况
define service{
use local-service
host_name 122.200.94.200
service_description Load
check_command check_nrpe!check_load
}
#所有进程
define service{
use local-service
host_name 122.200.94.200
service_description Processes
check_command check_nrpe!check_total_procs
}
#当前登录的用户
define service{
use local-service
host_name 122.200.94.200
service_description Users
check_command check_nrpe!check_users
}
#僵死的进程
define service{
use local-service
host_name 122.200.94.200
service_description Zombie procs
check_command check_nrpe!check_zombie_procs
}
#swap使用
define service{
use local-service
host_name 122.200.94.200
service_description Swap
check_command check_nrpe!check_swap
}
#Apache服务
define service{
use local-service
host_name 122.200.94.200
service_description HTTP
check_command check_nrpe!check_http
}
#mysql服务
define service{
use local-service
host_name 122.200.94.200
service_description Mysql
check_command check_mysql!122.200.94.200!3306!root!passw0rd //check_mysql模块,IP,端口,被监控端授权用户,授权密码
}
#用以上方法监控mysql,需要更改另外一个文件,定义mysql监控命令,客户端需要授权用户
vim /usr/local/nagios/etc/objects/commands.cfg(文件末尾添加一下内容)
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
}
4.客户端修改(被监控端,监控端监控的各项服务,都需要再此文件中指定具体命令,否则无法执行)
vim /usr/local/nagios/etc/nrpe.cfg
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
command[check_ping]=/usr/local/nagios/libexec/check_ping -H 127.0.0.1 -w 3000.0,80% -c 5000.0,100%、
#服务端监控客户端的这些服务,都需要在客户端的nrpe.cfg文件中添加这些命令,daemon只运行nrpe.cfg中所定义的命令
四、邮件报警
因为系统自带的sendmail发送邮件延迟很大,所以使用sendEmail进行发送邮件
1.下载
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar zxvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56
cp sendEmail /usr/bin/
chmod +x /usr/bin/sendEmail
2.在本地进行发送测试
sendEmail -f hulin_yu@sina.com -t hulin_yu@126.com -s smtp.sina.com -u ‘This is title‘ -m ‘This is body‘ -xu ‘hulin_yu@sina.com‘ -xp ‘123456‘
Apr 27 16:02:17 nagios sendEmail[2413]: Email was sent successfully!
3.修改配置文件
vim /usr/local/nagios/etc/objects/commands.cfg
#将发送邮件部分更改为如下(这样修改用Foxmail接受邮件不会出现乱码问题,否则会有乱码)
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "----- 监控通知 -----<br><br>告警类型: $NOTIFICATIONTYPE$<br>主机状态: $HOSTSTATE$<br>告警地址: $HOSTADDRESS$<br>告警时间: `date +%F_%H:%M:%S`<br>详细信息: <br>$HOSTOUTPUT$" | /usr/bin/sendEmail -f hulin_yu@sina.com -t $CONTACTEMAIL$ -s smtp.sina.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu hulin_yu@sina.com -xp yhl940519! -o message-content-type=html -o message-charset=utf8
}
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "----- 监控通知 -----<br><br>告警类型: $NOTIFICATIONTYPE$<br>告警服务: $SERVICEDESC$<br>告警地址: $HOSTADDRESS$<br>服务状态: $SERVICESTATE$<br>告警时间: `date +%F_%H:%M:%S`<br>详情信息:<br>$SERVICEOUTPUT$<br>" | /usr/bin/sendEmail -f hulin_yu@sina.com -t $CONTACTEMAIL$ -u "Nagios Warning" -s smtp.sina.com -xu hulin_yu@sina.com -xp yhl940519! -o message-content-type=html -o message-charset=utf8
}
此时,基本的配置都已完成。
本文出自 “sunshineboy” 博客,请务必保留此出处http://sunshineboy.blog.51cto.com/10310940/1921680
Linux下nagios的搭建及相关配置