如何使用Alerta在CentOS 7上监视Nagios警报

介绍

Alerta是一个Web应用程序,用于整合和重新生成来自多个监控系统的警报,并在单个屏幕上进行可视化。 Alerta可以与诸如Nagios,Zabbix,Sensu,InfluxData Kapacitor等许多知名监控工具集成。

在本教程中,您将设置Alerta并将其配置为显示来自Nagios (通用的开源监控系统)的通知。

先决条件

要遵循本教程,您将需要:

第1步 - 安装Nagios-to-Alerta事件代理模块

您可以使用Nagios Event Broker(NEB)模块扩展Nagios的功能。 NEB是Nagios的事件集成机制,NEB模块是可以将其他服务与Nagios集成的共享库。 在此步骤中,我们将Nagios安装到Alerta Gateway ,即将向Alerta发送通知的NEB模块。

以非root用户身份登录到Nagios服务器:

ssh sammy@your_nagios_server_ip

Nagios到Alerta Gateway没有预配置的系统包,因此您必须从源代码构建它。 为此,您需要安装一些开发工具和文件。 您还需要安装Git,以便您可以从GitHub获取源代码。

yum install -y git curl gcc make libcurl-devel

在安装了先决条件的情况下,使用Git从项目的GitHub存储库中克隆源代码:

git clone https://github.com/alerta/nagios-alerta.git

然后更改到新的nagios-alerts目录:

cd nagios-alerta

然后使用make编译nagios-alerta模块:

make nagios4

您会看到以下输出:

Outputcd ./src && make nagios4
make[1]: Entering directory `/root/nagios-alerta/src'
gcc -fPIC -g -O2 -DHAVE_CONFIG_H -I../include -I../include/nagios4 -lcurl -o alerta-neb.o alerta-neb.c -shared  -lcurl
make[1]: Leaving directory `/root/nagios-alerta/src'

如果看到不同的内容,请确保您已安装所有先决条件。

现在运行安装任务:

sudo make install

您将看到此输出,表明该模块已安装在/usr/lib/nagios

Outputcd ./src && make install
make[1]: Entering directory `/root/nagios-alerta/src'
[ -d /usr/lib/nagios ] || mkdir /usr/lib/nagios
install -m 0644 alerta-neb.o /usr/lib/nagios
make[1]: Leaving directory `/root/nagios-alerta/src'

安装模块后,我们可以配置Nagios来使用这个新模块。

第2步 - 配置Nagios-to-Alerta模块

让我们配置Nagios发送通知消息给Alerta。

首先,在Nagios主配置文件中启用新安装的Alerta代理模块。 在编辑器中打开Nagios配置文件:

sudo vi /usr/local/nagios/etc/nagios.cfg

查找包含broker_module指令的部分:

/usr/local/nagios/etc/nagios.cfg
...
# EVENT BROKER MODULE(S)
# This directive is used to specify an event broker module that should
# by loaded by Nagios at startup.  Use multiple directives if you want
# to load more than one module.  Arguments that should be passed to
# the module at startup are separated from the module path by a space.
#
[...]
#broker_module=/somewhere/module1.o
#broker_module=/somewhere/module2.o arg1 arg2=3 debug=0
...

要配置Alerta模块,您需要提供两个强制参数:

将此行添加到文件中以配置Alerta集成:

/usr/local/nagios/etc/nagios.cfg
...
broker_module=/usr/lib/nagios/alerta-neb.o http://your_alerta_server_ip/api key=ALERTA_API_KEY
...

还有一些额外的可选参数也可以指定:

  • env :指定环境名称。 默认环境名称为“ Production
  • hard_only :前进仅在硬状态。 您可以在Nagios文档中找到有关Nagios状态类型的更多信息。 将其设置为1以启用此模式。
  • 调试 : - 启用模块的调试模式。 将其设置为1以启用此模式。

要指定所有这些选项,请改用此行:

/usr/local/nagios/etc/nagios.cfg
...
broker_module=/usr/lib/nagios/alerta-neb.o http://your_alerta_server_ip/api key=ALERTA_API_KEY env=Production hard_only=1 debug=1
...

保存文件并退出编辑器。

为了根据环境和服务名称来识别警报,您需要使用Nagios 自定义对象变量来设置环境和服务名称。 为此,请在配置中使用_Environment_Service变量。 现在我们来配置它们。

打开默认的Nagios主机对象配置文件,您可以在/usr/local/nagios/etc/objects/目录中找到该配置文件:

sudo vi /usr/local/nagios/etc/objects/localhost.cfg

我们会将此主机的所有警报标记为生产警报,我们将调用默认服务Nagios 查找以下主机定义:

/usr/local/nagios/etc/objects/localhost.cfg
...
define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }

...

_Environment_Service值添加到配置中:

/usr/local/nagios/etc/objects/localhost.cfg
...
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        _Environment            Production
        _Service                Nagios
        }
...

现在将所有与系统partitio空间相关联的事件标记为系统警报。 找到定义如何检查可用空间的文件的这一部分:

/usr/local/nagios/etc/objects/localhost.cfg
...
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Root Partition
        check_command                   check_local_disk!20%!10%!/
        }
...

修改它以将其与System服务相关联:

/usr/local/nagios/etc/objects/localhost.cfg
...
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Root Partition
        check_command                   check_local_disk!20%!10%!/
        _Service                        System
        }
...

保存文件并退出编辑器。 重新启动Nagios应用这些新设置:

sudo systemctl restart nagios.service

通过检查其状态来确保服务正在运行:

systemctl status nagios.service

您会看到以下输出:

Output...
Jul 01 08:44:31 nagios nagios[8914]: [alerta] Initialising Nagios-Alerta Gateway module, v3.4.1
Jul 01 08:44:31 nagios nagios[8914]: [alerta] debug is off
Jul 01 08:44:31 nagios nagios[8914]: [alerta] states=Hard/Soft
Jul 01 08:44:31 nagios nagios[8914]: [alerta] Forward service checks, host checks and downtime to http://your_alerta_server_ip/api
Jul 01 08:44:31 nagios nagios[8914]: Event broker module '/usr/lib/nagios/alerta-neb.o' initialized successfully.
Jul 01 08:44:31 nagios nagios[8914]: Successfully launched command file worker with pid 8920

现在Nagios将在任何系统或服务关闭时立即发送通知。 让我们生成一个测试事件。

第3步 - 生成测试警报以验证Nagios-Alerta集成

让我们生成一个测试警报,以确保所有连接。 默认情况下,Nagios跟踪服务器上可用磁盘空间的大小。 我们将创建一个足够大的临时文件来触发Nagios的文件系统使用警报。

首先,确定在Nagios服务器上有多少可用空间。 您可以使用df命令找出:

df -h

您会看到如下输出:

Output    Filesystem      Size  Used Avail Use% Mounted on
    /dev/vda1        20G  3.1G   16G   17% /

看看可用空间的大小。 在这种情况下,可用空间是16GB 你的可用空间可能不同。

使用fallocate命令创建一个占用可用磁盘空间的80%以上的文件,该文件应足以触发警报:

fallocate -l 14G /tmp/temp.img

在几分钟内,Nagios将触发有关可用磁盘空间量的警报,并将通知消息发送给Alerta。 您将在Alerta仪表板中看到此新通知:

Alerta显示Nagios的可用空间警报

现在您知道警报正在运行,请删除您创建的临时文件,以便您可以回收磁盘空间:

rm -f /tmp/temp.img

一段时间后,Nagios将发送恢复信息。 然后警报将从主要的Alerta仪表板消失,但您可以通过选择关闭来查看所有关闭的事件。

Alerta的关闭警报

您可以点击事件行查看更多详细信息。

结论

在本教程中,您已将Nagios配置为向另一台运行Alerta的服务器发送通知。

Alerta为您提供了一个方便的地方跟踪许多系统的警报。 例如,如果您的基础设施的某些部分使用Nagios,而其他部分使用Zabbix,则可以将两个系统的通知合并为一个面板。

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏