参照跟老男孩学linux运维搭建nagios实验小结

 
 
nagios效果示例
http://192.168.0.236/nagios       用户名:hong     密码:123
 
一. 服务端安装准备
 
1. 更新源
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
 
2. 解决perl软件编译问题
echo 'export LC_ALL=C' >>/etc/profile
source /etc/profile
 
3. 关闭防火墙和selinux
/etc/init.d/iptables stop
chkconfig iptables off
 
sed -i 's/SELINUX=enforcing/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
 
4. 解决系统时间同步问题
crontab -e中添加
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1
 
5. 安装服务器所需软件包
yum install -y gcc glibc glibc-common gd gd-devel mysql-server httpd php php-gd -y
 
6. 创建nagios服务器所需用户及组
useradd nagios
useradd apache -M -s /sbin/nologin
groupadd nagcmd
usermod -aG nagcmd nagios
usermod -aG nagcmd apache
 
7. 启动HTTP服务
/etc/init.d/httpd start
检查 lsof -i :80
 
 
二. 安装配置 nagios服务端
 
1. 下载nagios并编译
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.5.0.tar.gz(需翻墙)
tar xf nagios-3.5.0.tar.gz
cd nagios
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-commandmode
make install-config
 
2. 安装nagios web配置文件及创建登陆用户
make install-webconf
htpasswd -bc /usr/local/nagios/etc/htpasswd.users hong 123    #设置nagios页面用户名和密码
/etc/init.d/httpd reload
 
3. 添加监控报警信息接收的email地址
vim /usr/local/nagios/etc/objects/contacts.cfg +35
修改原email地址为接收报警的邮箱地址
 
4. 安装第三方邮件服务
yum install mailx
修改/etc/mail.rc,添加如下配置
set smtp=smtp.163.com
set smtp-auth-user=pshyms
set smtp-auth-password=xxxx  #这里要写SMTP授权码,需要到邮箱设置中开通
set smtp-auth=login
 
使用mail命令发送邮件:
echo "邮件内容"|mail -s 标题 xxx@163.com
mail -s 标题 xxx@163.com < file
 
5. 配置启动Apache服务
/etc/init.d/httpd start
chkconfig httpd on
此时浏览器访问http://192.168.0.236/nagios(ip 为服务端地址)会出现用户名和密码提示窗口
 
6. 安装nagios插件软件包
yum install perl-devel openssl-devel -y
tar xf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios
--with-nagios-group=nagios
--enable-perl-modules
--with-mysql
make && make install
 
7. 安装nrpe软件
tar zxvf nrpe-2.14.tar.gz
cd nrpe-2.14
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
 
8. 配置并启动nagios服务
chkconfig nagios on
或者 echo "/etc/init.d/nagios start >> /etc/rc.local
验证nagios配置文件(检查语法) /etc/init.d/nagios checkconfig
使用命令检查报错 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
启动nagios服务 /etc/init.d/nagios start
 
 
 
三. 客户端安装
 
1. 安装初始化软件
yum install gcc glibc glibc-common mysql-server perl-devel perl-CPAN openssl-devel -y
 
2. 添加nagios用户
useradd nagios -M -s /sbin/nologin
 
3. 安装nagios-plugins插件
tar xf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios
--with-nagios-group=nagios
--enable-perl-modules
--with-mysql
make && make install
 
4. 安装nrpe软件
tar zxvf nrpe-2.14.tar.gz
cd nrpe-2.14
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
 
5. 配置nagios客户端nrpe服务
cd /usr/local/nagios/etc/
nrpe.cfg的79行添加监控端IP,例如allowed_hosts=127.0.0.1,192.168.0.236
在199-203行为监控内容,需要根据需求进行调整修改
例如command[check_disk]=/usr/local/nagios/libexec/check_disk -w 15% -c 7% -p /
 
启动nrpe守护进程
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrep.cfg -d
需要注意的是每次修改nrpe.cfg文件都需要重启守护进程,可以pkill nrpe,再执行上面命令
 
可将上面命令加入/etc/rc.local中进行nrpe开机自启动
 
 
 
四.  nagios服务端监控
 
所有客户端本地服务的监控都是通过执行/usr/local/nagios/libexec目录下的插件来实现的
 
1. nagios服务端核心配置文件
主配置文件为/usr/local/nagios/etc/目录下的nagios.cfg文件,
这个目录下有个objects目录,里面放的是主配置文件nagios.cfg包含的其他nagios配置文件
 
2. 配置nagios.cfg
在文件中找到cfg_file部分,添加如下设置
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg  #存放具体被监控的主机相关配置内容
cfg_file=/usr/local/nagios/etc/objects/services.cfg #存放具体被监控的服务相关配置内容
注释掉localhost.cfg这一行,它表示监控nagios服务端本地服务的配置文件
 
3. 生成一个新的hosts.cfg主机文件,以及services.cfg服务文件
cd /usr/local/nagios/etc/objects
head -51 localhost.cfg > hosts.cfg
chown nagios:nagios /usr/local/nagios/etc/objects/hosts.cfg
touch services.cfg
chown nagios:nagios services.cfg
 
 
文件结构如下
 
|-- cgi.cfg
|-- htpasswd.users
|-- nagios.cfg
|-- nrpe.cfg
|-- objects
|   |-- commands.cfg
|   |-- contacts.cfg
|   |-- hosts.cfg
|   |-- hosts.cfg1
|   |-- localhost.cfg
|   |-- printer.cfg
|   |-- services.cfg
|   |-- switch.cfg
|   |-- templates.cfg
|   |-- timeperiods.cfg
|   `-- windows.cfg
`-- resource.cfg
 
 
4. 配置hosts.cfg
 
define host{        use                     linux-server            
        host_name               web01
        alias                   web01
        address                 192.168.0.239  #第一个被监控主机,可定义多个,都按这个格式写
        }
      
define host{        use                     linux-server            
        host_name               web02  #不需要和真实主机名一样
        alias                   web02
        address                 192.168.0.240  #第二个被监控主机
        }
 
#下面的可选
define hostgroup{
        hostgroup_name  linux-servers
        alias           Linux Servers
        members         web01,web02  #把2个主机当作一组进行监控,可选
        }
 
 
5. 配置service .cfg文件
 
这里只简单写个3个监控,ping,负载值,硬盘空间。他们会显示在监控网页上,可任意加入监控项
 
define service {
use     generic-service
host_name       web01
service_description     Disk Partition
check_command   check_nrpe!check_disk
}
 
define service {
use     generic-service
host_name       web01
service_description     current load
check_command   check_nrpe!check_load
}
 
define service {
use     generic-service
host_name       web01
service_description     ping
check_command   check_ping!100.0,20%!500.0,60%
}
 
 
6. 定义check_nrpe插件
/usr/local/nagios/etc/objects/目录下的commands.cfg文件中加入如下内容
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
 
 
7. 调试host.cfg和service.cfg
检查命令为:/etc/init.d/nagios checkconfig
 
 
8. 增加nagios用户权限
cd /usr/local/nagios/etc/
修改cgi.cfg文件,把注册用户加入 sed -i 's#nagiosadmin#hong#g' cgi.cfg
 
 
9. 启动nagios服务
/etc/init.d/nagios/start 或者/etc/init.d/nagios reload
 
10. 服务端调试命令例子
检查硬盘监控
/usr/local/nagios/libexec/check_nrpe -H 192.168.0.239 -c check_disk
 
 
 
 
几个注意:
 
一. CENTOS为服务器,做nagios的问题
参照书中的实验,发现网页中监控内存和硬盘iostat的脚本有问题,网上找了2个shell写的脚本,如下
 
1)nagios参考文档
 
2)内存监控脚本,即书中的check_memory.pl
 
3)硬盘iostat监控脚本,即书中的check_iostat
 
第3个链接提及了在commands.cfg中添加check_iostat,我加了这个配置,测试成功。下次试试不加行不行
在监控机/usr/local/nagios/etc/objects/commands.cfg里面添加
define command{
        command_name    check_iostat
        command_line    $USER1$/check_iostat -d $ARG1$ -w $ARG2$ -c $ARG3$
        }
 
 
 
二. Ubuntu为服务端,上面做nagios的问题
 
ubuntu上编译安装nagios-plugins插件make失败的问题
./stdio.h:456:1: error: 'gets' undeclared here (not in a function)
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
 
在ubuntu上配置nagios客户端nrpe服务和nagios-plugins插件
 
 
 
 
 
原文地址:https://www.cnblogs.com/regit/p/8426944.html