设为首页 收藏本站
查看: 2780|回复: 0

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

[复制链接]

尚未签到

发表于 2015-12-24 12:02:56 | 显示全部楼层 |阅读模式
注:192.168.0.21 服务端
192.168.0.22 客户端
环境:两台centos6.0 64位系统,都已经搭建好了源码的lnmp平台
结尾附上所需的软件包


1.nagios安装(中文版)



1
2
3
4
5
6
7
8
9
10
11
12


tarxvf nagios-cn-3.2.3.tar.bz2
cdnagios-cn-3.2.3
useradd-m -s /bin/bashnagios
usermod-a -G nagcmd nagios
./configure--prefix=/usr/local/nagios--with-command-group=nagcmd
make
makeall
makeinstall
makeinstall-init     # 生成init启动脚本
makeinstall-config     # 安装示例配置文件
makeinstall-commandmode     # 设置相应的目录权限
chmodo+rwx /usr/local/nagios/var/rw
2.nagios-plugins安装



1
2
3
4
5
6
7
8


wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins
tarzxvf nagios-plugins-1.4.16.tar.gz
cdnagios-plugins-1.4.16
yum installmakeapr* autoconf automake curl curl-devel gcc gcc-c++ zlib-devel \
openssl openssl-devel pcre-devel gd gd-devel kernel keyutils patch perl perl-devel \
kernel keyutils kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl \
cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel \
krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch -y



1
2
3


./configure--prefix=/usr/local/nagios--with-mysql=/home/mysql/
make
makeinstall


3.nrpe安装



1
2
3
4
5
6
7
8
9
10
11
12


tarxzvf nrpe-2.12.tar.gz
cdnrpe-2.12
./configure
make
./configure
makeall
makeinstall-plugin
makeinstall-daemon
makeinstall-daemon-config
\cpsrc/check_nrpe/usr/local/nagios/libexec/
/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
echo'/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d'>> /etc/rc.local



1
2
3
4
5


要重启nrpe进行就先杀掉进行,然后重启
kill`psaux |grepnrpe |grep-vgrep|awk'{print $2}'`
/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
本机测试下:
/usr/local/nagios/libexec/check_nrpe-H localhost -c check_users


加入系统服务



1
2
3
4
5
6


加入系统服务并设为开机自动
chkconfig --add nagios
chkconfig nagios on
chownnagios.nagios /usr/local/nagios/var/rw
# 测试配置文件是否正确
/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg


添加别名命令,方便测试配置文件



1
2
3
4
5


vi~/.bashrc
在里面用alias来自定义一个命令来代替,这里我用check
aliascheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'
source~/.bashrc
此时可以用check命令来检测配置文件了


修改联系人邮箱,修改为用于报警接收的邮件地址



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


vi/usr/local/nagios/etc/objects/contacts.cfg
###############################################################################
# CONTACTS.CFG - SAMPLE CONTACT/CONTACTGROUP DEFINITIONS
#
# Last Modified: 05-31-2007
#
# NOTES: This config file provides you with some example contact and contact
#        group definitions that you can reference in host and service
#        definitions.
#
#        You don't need to keep these definitions in a separate file from your
#        other object definitions.  This has been done just to make things
#        easier to understand.
#
###############################################################################
###############################################################################
###############################################################################
#
# CONTACTS
#
###############################################################################
###############################################################################
# Just one contact defined by default - the Nagios admin (that's you)
# This contact definition inherits a lot of default values from the 'generic-contact'
# template which is defined elsewhere.
define contact{
contact_name                    nagiosadmin             ; Short name of user
use                             generic-contact         ; Inherit default values from generic-contact template (defined above)
aliasNagios Admin            ; Full name of user
email                           nagios@localhost        ; /dev/null
echo"stop perl-fcgi done"
}
start ()
{
rm$dir/now_start_perl_fcgi.sh 2>/dev/null
chownnobody.root $dir/logs
echo"$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock">>$dir/now_start_perl_fcgi.sh
chownnobody.nobody $dir/now_start_perl_fcgi.sh
chmodu+x $dir/now_start_perl_fcgi.sh
sudo-u nobody $dir/now_start_perl_fcgi.sh
echo"start perl-fcgi done"
}
case$1 in
stop)
stop
;;
start)
start
;;
restart)
stop
start
;;
esac


start_perl_cgi.sh文件中的nobody全部用nagios替换,nginx 目录上的用户







1
2
3


sed-i 's@nobody@nagios@g'/usr/local/nginx/start_perl_cgi.sh
chmod755 /usr/local/nginx/start_perl_cgi.sh
/usr/local/nginx/start_perl_cgi.sh start
DSC0000.jpg



1
2
3
4
5


# 取消用户认证(方便调试)
vi/usr/local/nagios/etc/cgi.cfg
找到use_authentication=1并把值改为0
修改联系人邮箱,修改为用于报警接收的邮件地址
vi/usr/local/nagios/etc/objects/contacts.cfg
DSC0001.jpg


DSC0002.jpg
到这一步就是正常的
下面nginx 配置
我把监听改成80的了
DSC0003.jpg
然后开启服务
就可以访问了,然后继续安装客户端,最后给大家截图看效果
service nagios start




nagios被控端安装




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


yum installopenssl-devel -y
1. nagios-plugins安装
groupadd nagios
useraddnagios -M -s /sbin/nologin-g nagios
tarxvf nagios-plugins-1.4.16.tar.gz
cdnagios-plugins-1.4.16
./configure--prefix=/usr/local/nagios--with-nagios-user=nagios --with-nagios-gourp=nagios --with-mysql=/usr/local/mysql&& make&& makeinstall
cd
2. nrpe安装
tarzxvf nrpe-2.13.tar.gz
cdnrpe-2.13
./configure
makeall
makeinstall-plugin
makeinstall-daemon
makeinstall-daemon-config




1
2
3


启动nrpe
/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
echo'/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d'>> /etc/rc.local


监控服务端本机:自己监控自己不需要配置nrpe,服务端的nrpe只用于获取客户端的nrpe传送过来的数据,在这里因为中文版的nagios已经默认有些配置,等会儿修改下直接用了
监控客户端:监控的服务有:mysqlnginxmemoryip连接数、僵死的进程、磁盘空间、磁盘IO、登录用户数、进程总数、cpu负载、PINGSSH




1
2
3


unzip libexec.zip
\cplibexec/* /usr/local/nagios/libexec
chmod-R +x /usr/local/nagios/libexec
装插件




1
2
3
4
5


创建一个空的数据库nagios,授权nagios这个用户从任何地方访问nagios这个数据库,刷新授权设置,查询下nagios这个用户是否创建成功
create database nagios;
grant selecton nagios.* to nagios @'%'identified by '123456';
flush privileges;
selectUser,Password,Host from mysql.user;

DSC0004.jpg



1
2
3
4
5
6
7
8


添加mysql库到系统搜索库
vim /etc/ld.so.conf
/usr/local/mysql/lib
ldconfig
要监控磁盘io,还得安装sysstat这个工具包
yum installsysstat -y
配置客户端上面的nrpe
vim /usr/local/nagios/etc/nrpe.cfg
DSC0005.jpg


DSC0006.jpg
DSC0007.jpg
DSC0008.jpg



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184


配置客户端上面的nrpe
vim /usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users-w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_cpu.sh -w 80% -c 90%
command[check_sda1]=/usr/local/nagios/libexec/check_disk-w 20% -c 10% -p /dev/sda1
command[check_sda2]=/usr/local/nagios/libexec/check_disk-w 20% -c 10% -p /dev/sda2
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_iostat]=/usr/local/nagios/libexec/check_iostat.sh -d sda -w 6 -c 10
command[check_mysql]=/usr/local/nagios/libexec/check_mysql-H 192.168.0.22 -u nagios -p 123456 -d nagios
command[check_nginx]=/usr/local/nagios/libexec/check_nginx.sh -u 192.168.0.22 -p /status-w 4000 -c 5000
command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -f -w 20 -c 10
command[check_ip_conn]=/usr/local/nagios/libexec/ip_conn.sh 200 250
command[check_ssh]=/usr/local/nagios/libexec/check_tcp-p 22 -w 1.0 -c 10.0
配置完成后,重启nrpe
kill`psaux |grepnrpe |grep-vgrep|awk'{print $2}'`
/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d
服务端配置:
监控服务端本机的配置:
vim /usr/local/nagios/etc/objects/localhost.cfg
修改里面的配置,最后修改完成的配置如下
define host{
use                     linux-server
host_name               localhost
aliaslocalhost
address                 127.0.0.1
icon_image              server.gif
statusmap_image         server.gd2
2d_coords       500,200
3d_coords       500,200,100
}
define hostgroup{
hostgroup_name  linux-servers ; The name of the hostgroup
aliasLinux Servers ; Long name of the group
members         *     ; Comma separated list of hosts that belong to this group
}
define servicegroup{
servicegroup_name 全部联通性检查
alias联通性检查
members localhost,PING,nagios-client,PING
}
define service{
use                             local-service         ; Name of service template to use
host_name                       *
service_description             PING
check_command            check_ping!100.0,20%!500.0,60%
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             根分区
check_command            check_local_disk!20%!10%!/
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             登录用户数
check_command            check_local_users!20!50
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             进程总数
check_command            check_local_procs!250!400!RSZDT
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             系统负荷
check_command            check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             交换空间利用率
check_command            check_local_swap!20!10
}
define service{
use                             local-service         ; Name of service template to use
host_name                       localhost
service_description             SSH
check_command            check_tcp!22!1.0!10.0
notifications_enabled        0
}
服务器监控客户端的配置:
保存退出后复制这个文件一份,作为nagios-client的监控模版文件
cp/usr/local/nagios/etc/objects/localhost.cfg /usr/local/nagios/etc/objects/nagios-client.cfg
vim /usr/local/nagios/etc/objects/nagios-client.cfg   修改完成后的配置如下
define host{
use                     linux-server
host_name               nagios-client
aliasnagios-client
address                 192.168.0.22
icon_image              server.gif
statusmap_image         server.gd2
2d_coords       500,200
3d_coords       500,200,100
}
define service{
use                             local-service         ; Name of service template to use
host_name                       *
service_description             PING
check_command           check_ping!100.0,20%!500.0,60%
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             boot分区
check_command           check_nrpe!check_sda1
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             根分区
check_command           check_nrpe!check_sda2
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             登录用户数
check_command           check_nrpe!check_users
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             进总程数
check_command           check_nrpe!check_total_procs
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             CPU平均负载
check_command           check_nrpe!check_load
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             虚拟内存
check_command           check_nrpe!check_swap
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             SSH
check_command           check_nrpe!check_ssh
notifications_enabled       0
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             僵死进程数
check_command           check_nrpe!check_zombie_procs
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             iostat
check_command           check_nrpe!check_iostat
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             mysql
check_command           check_nrpe!check_mysql
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             nginx
check_command           check_nrpe!check_nginx
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             memory
check_command           check_nrpe!check_mem
}
define service{
use                             local-service         ; Name of service template to use
host_name                       nagios-client
service_description             IP连接数
check_command           check_nrpe!check_ip_conn
}
DSC0009.jpg




直接把原来的邮件报警的两条命令中的/bin/mail修改为/usr/bin/mutt即可,如下图

加快nagios的报警时间设置:
1.修改模版文件:
vim /usr/local/nagios/etc/objects/templates.cfg
修改所有normal_check_interval项的值为1,既发现故障后1分钟就报警
修改所有check_interval项的值为1,即正常情况下每分钟检查一次
修改所有notification_interval  的值为20分钟         #在主机出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间
service nagios restart    重启nagios
DSC00010.jpg
DSC00011.jpg



DSC00012.jpg
DSC00013.jpg
DSC00014.jpg


DSC00015.jpg
DSC00016.jpg
DSC00017.jpg
DSC00018.jpg
DSC00019.jpg
DSC00020.jpg
DSC00021.jpg
DSC00022.jpg
DSC00023.jpg
DSC00024.jpg
DSC00025.jpg


测试告警:
DSC00026.jpg
DSC00027.jpg
DSC00028.jpg
试验完成!
附上软件包所需软件地址
http://down.iyunv.com/data/1007210
本文出自 “▁▁技术控ヽ” 博客,请务必保留此出处http://chenhao6.blog.iyunv.com/6228054/1323192

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155685-1-1.html 上篇帖子: pnp4nagios安装配置 下篇帖子: cacti+nagios 之cacti的搭建(
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表