监控服务器Nagios之四 报警及扩展

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

一、邮件报警

    Nagios有一个很强大的功能就是邮件、短信报警,当监控的服务有故障(根据设定)的时候,就会通知你,这可以极大地提升工作效率。

1、  安装sendmail组件

1
#yum –y install sendmail* mailx

重启sendmail服务,并开机启动

1
#service sendmail  restart;chkconfig  sendmail on

发送测试邮件,测试sendmail的可用性

1
2
#echo “ok” |mail  -s  “title”  
654001593@qq.com

2、  邮件报警设置

编辑/usr/local/nagios/etc/objects下的contact.cfg文件

1
2
3
4
5
6
define contact{
        contact_name                 nagiosadmin                   
use                                    generic-contact               
alias                          Nagios Admin                   
email                        654001593@qq.com,yang9596@126.com     
        }

只需要把email项改为自己定义的邮箱即可,可以定义多个。

3、  邮件分组

    如果说公司有上百台监控的设备,而你只负责其中的一部分,其他设备有问题了,也不停的给你发短信,是不是很烦躁?那么我们可以使用邮件分组,好处就在于我们可以只接受规定的服务器报警邮件。

我们可以定义多个contact(联系人),然后再用contact  group(联系组)对各contact进行分组。

例如管理网络的有两人,管理服务器的有两个人,我们就可以定义两个contactgroup,然后定义四个管理员的contact,如下例是当前我正在使用的contact.cfg,服务器管理员有两名,网络管理员有两名:

contact.cfg的配置

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
#######################################################################################
  #######################################################################################
  ##############   NETWORK ADMINISTRATOR MEMBERS
  #######################################################################################
  #######################################################################################
define contact{
         contact_name                               net_jacken
         use                                        generic-contact
         alias                                       net_jacken
         service_notification_period                24x7
         host_notification_period                   24x7
         service_notification_options               w,u,c,r,f,s
         host_notification_options                  d,u,r,f,s
         service_notification_commands              notify-service-by-email                           
         host_notification_commands                 notify-host-by-email
        email                                       net_jacken@ test .com
         }
  
define contact{
         contact_name                               net_tom
         use                                        generic-contact
         alias                                       net_tom
         service_notification_period                24x7
         host_notification_period                   24x7
         service_notification_options               w,u,c,r,f,s
         host_notification_options                  d,u,r,f,s
         service_notification_commands              notify-service-by-email
         host_notification_commands                 notify-host-by-email
         email                                      net_tom@ test .com
         }
  
#######################################################################################
  #######################################################################################
  ##############   SYSTEM ADMINISTRATOR MEMBERS
  #######################################################################################
  #######################################################################################
  
define contact{
         contact_name                              sa_mark
         use                                       generic-contact
         alias                                      sa_mark
         service_notification_period               24x7
         host_notification_period                  24x7
         service_notification_options              w,u,c,r,f,s
         host_notification_options                 d,u,r,f,s
         service_notification_commands             notify-service-by-email
         host_notification_commands                notify-host-by-email
         email                                     sa_mark@ test .com
         }
  
define contact{
         contact_name                               sa_bob
         use                                        generic-contact
         alias                                       sa_bob
         service_notification_period                24x7
         host_notification_period                   24x7
         service_notification_options               w,u,c,r,f,s
         host_notification_options                  d,u,r,f,s
         service_notification_commands              notify-service-by-email
         host_notification_commands                 notify-host-by-email
         email                                      sa_bob@ test .com
         }
#######################################################################################
  #######################################################################################
  ##############   NETWORK ADMINISTRATOR GROUP
  #######################################################################################
  #######################################################################################
  define contactgroup{
         contactgroup_name                         network
         alias                                      network
         members                                   net_jacken,net_tom
         }
#######################################################################################
  #######################################################################################
  ##############   SYSTEM ADMINISTRATOR GROUP
  #######################################################################################
  #######################################################################################
  define contactgroup{
         contactgroup_name                         system
         alias                                      system
         members                                   sa_mark,sa_bob
         }

4、  主机及监控内容的配置以主机192.168.1.4为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vim   /usr/local/nagios/etc/objects/192 .168.1.4.cfg
define host{
         use                                    linux-server
         host_name                              client-02
         alias                                   192.168.1.4
         address                                192.168.1.4
         }
  define service{
         use                                    generic-service         
         host_name                              client-02
         service_description                    PING
         check_command                          check_ping!100.0,20%!500.0,60%
         contact_groups                         network
         }
  define service{
         use                                    generic-service
         host_name                              client-02
         service_description                    Uptime
         check_command                          check_nt!UPTIME
         contact_groups                         system
          }

    如上面配置所示,当监控主机的ping出现问题的时候,nagios就会查看contact.cfg中定义的联系人组network中的联系人的信息,然后读取各联系人的邮件地址,这样的话,网络中出现故障时就可以直接给net_jackennet_tom二人发邮件了;同理,当服务器出现问题的时候就会给system组的相关人员发送邮件了

短信通知

    其实短信通知就和nagios服务器没太大关系,就是相当于别人给你发一个邮件,你可以用手机查看而已,这里不再详细描述。

注册后登录mail.139.com---右上角设置手机通知---接收方式……….

不过有时候可能会识别垃圾邮件,设置---邮箱安全----白名单……….

二、监控Web关键字

    试问如果有一天网站突然挂了,或者主页被被人攻击了,那么怎么有效地收到通知并及时处理呢?这时可以通过Nagios监控Web关键字来解决。

可以使用默认监控命令check_http命令+相关的参数来实现,如下:

command.cfg添加如下关键词监控命令:check_http_word,参数解析:-I指定IP或者主机名,-u指定URL-p指定端口,-s指定关键词。

这里要监控的是192.168.1.2主机上的由Discuz搭建的论坛。

1、  修改commands.cfg

1
2
3
4
5
6
7
[root@nagios ~] # vim/usr/local/nagios/etc/objects/commands.cfg
找到”check_http”修改如下:
# 'check_http' command definition
define  command {
        command_name    check_http_word
        command_line    $USER1$ /check_http-I  $HOSTADDRESS$   -s $ARG1$
        }

2、  创建监控主机配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#cd  /usr/local/nagios/etc/objects
#cp localhost.cfg  192.168.1.3.cfg
#echo > 192.168.1.3.cfg
#vim 192.168.1.3.cfg
#添加以下内容
define host{
        use                    linux-server
        host_name              192.168.1.3
        alias                   192.168.1.3
        address                192.168.1.3
        }
define hostgroup{
        hostgroup_name  192.168.1.3
        alias            Linux Servers
        members         192.168.1.3
        }
define service{
        use                             local -service
        host_name                      192.168.1.3
        service_description            web_words_monitor
        check_command                  check_http_word!192.168.1.3!X3.2
        notifications_enabled           1                    #关键字X3.2(论坛版本)
        }

3、  修改nagios.cfg文件

1
2
3
4
# vim /usr/local/nagios/etc/nagios.cfg
#加入下边一行
cfg_file= /usr/local/nagios/etc/objects/192 .168.1.3.cfg
#service nagios restart

4、  验证

已经ok

wKiom1U9G_KDJTlbAADdHQyCGxw955.jpg

客户端192.168.1.3停止httpd

1
2
3
[root@lamp ~] # /etc/init.d/httpd stop
停止 httpd:                                              [确定]
[root@lamp ~] #

再看监控端

wKioL1U9HVbyDiS4AACx2fIY3ac674.jpg

客户端192.168.1.3启动httpd

1
2
3
[root@lamp ~] # /etc/init.d/httpd start
正在启动 httpd:                                           [确定]
[root@lamp ~] #

再看监控端

wKiom1U9G_KT9UE6AADTt968IUc316.jpg

注释:我们这里是关闭httpd服务,模拟关键字消失,即使服务正常,如果所监控的关键字消失,依旧会报警。

三、监控Mysql主从

环境为 Nagios服务端IP192.168.1.2

Mysql主服务器IP192.168.1.3

Mysql从服务器IP192.168.1.4

现在Mysql为同步状态。

1、  Nagios服务器上定义command.cfg

1
2
3
4
5
6
# vim /usr/local/nagios/etc/objects/ commands.cfg
添加以下内容
define  command {
        command_name   check_mysql_slave
        command_line  $USER1$ /check_mysql-H  $HOSTADDRESS$ -S -uroot -phi -H $ARG1$
  }

2、  定义监控项目

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
# cd  /usr/local/nagios/etc/objects/
# touch mysql.cfg
# chown nagios:nagios mysql.cfg
#vim mysql.cfg
添加以下内容
define host{
        use                    linux-server
        host_name               mysql
        alias                    mysql_master
        address                192.168.1.3
        }
define hostgroup{
        hostgroup_name          mysql
        alias                    Mysql Servers
        members                 mysql
        }
  
  
define service{
        use                             local -service
        host_name                      mysql
        service_description            Monitor_Mysql_Slave
        check_command                  check_mysql_slave!192.168.1.4
        notifications_enabled           1
        }

如果没有check_mysqlNagios安装mysql-devel然后重新编译Nagios-plugins

3、  更新nagios.cfg配置文件

1
2
3
# vim /usr/local/nagios/etc/nagios.cfg
添加一行
cfg_file= /usr/local/nagios/etc/objects/mysql .cfg

4、  Mysql服务器授权

1
2
3
4
5
6
7
mysql>  grant  all  on  *.*  to   root@ '192.168.1.2'  identified  by  'hi' ;
Query OK, 0  rows  affected (0.00 sec)
  
mysql> flush  privileges ;
Query OK, 0  rows  affected (0.00 sec)
  
mysql>

5、  验证

wKioL1U9HVeC_d5NAAO9-cg3xE4296.jpg

192.168.1.4从服务上关闭mysql服务

#service mysqld stop

wKiom1U9G_WDCBtbAABxrZaby7Q106.jpg

wKioL1U9HVmgjl2aAAHVau2Cod4575.jpg

Mysql主服务挂掉的时候依旧会报警


本文转自Jacken_yang 51CTO博客,原文链接:http://blog.51cto.com/linuxnote/1638905,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
弹性计算 监控 数据可视化
ecs自定义监控
ecs自定义监控
22 1
|
1月前
|
Arthas 弹性计算 运维
阿里云ECS监控服务
阿里云ECS监控服务
401 2
|
1月前
|
弹性计算 监控 数据可视化
ECS网络流量监控
ECS网络流量监控
54 2
|
1月前
|
弹性计算 运维 监控
ECS监控与管理
ECS监控与管理
33 5
|
26天前
|
弹性计算 运维 监控
ECS资源监控
ECS资源监控涉及CPU、内存、磁盘I/O、网络流量、系统负载和进程的关键指标,通过云服务商控制台、监控服务、API与SDK、运维工具进行实时监控和告警设置。支持历史数据查询、事件监控,以及使用Windows资源监视器和Linux系统工具进行操作系统层面监控。全面监控确保ECS实例稳定运行、资源有效利用和问题及时处理。如需特定云服务商的指导,请询问。
28 3
|
29天前
|
弹性计算 运维 监控
ecs监控与评估
阿里云ECS提供全面的监控与评估解决方案,包括云监控服务(实时资源指标、告警)、ECS实例详情页(运行状态查看)、资源负载评估(综合性能得分)、ECS Insight(多维度分析)、报警配置、流量管理优化(负载均衡、CDN)。这些工具帮助用户有效管理资源、识别潜在风险、优化性能,确保云服务稳定高效。
17 1
|
1月前
|
监控 JavaScript 安全
监控内网电脑软件设计与实现:基于Node.js的服务器端架构分析
在当今信息技术高度发达的时代,监控内网电脑的需求日益增长。企业需要确保网络安全,个人用户也需要监控家庭网络以保护隐私和安全。本文将介绍一种基于Node.js的服务器端架构,用于设计和实现监控内网电脑软件。
90 0
|
1月前
|
弹性计算 监控 网络协议
ECS操作系统监控
ECS操作系统监控
16 2
|
4月前
|
Prometheus 监控 Cloud Native
助力工业物联网,工业大数据之服务域:服务器性能监控Prometheus及项目总结【三十五】
助力工业物联网,工业大数据之服务域:服务器性能监控Prometheus及项目总结【三十五】
33 1
|
5月前
|
监控 Java
游戏服务器的内存监控日志
游戏服务器的内存监控日志
60 0

热门文章

最新文章