Using MQTTWarn to monitor server or device

How do your servers talk to you?

Most of us monitor what our servers do, in some way or another. We might use Icinga/Nagios  for some tasks, or use any of what feels like a myriad different tools  for other tasks. I assume, that in most cases, people use e-mail, SMS,  etc. for sending out warning or critical alerts when they occur.

Each of these monitoring services needs to be configured to send  alerts via a particular channel (e.g. SMTP, SMS) to particular users,  and if the need to change these associations arises, most people have to  reconfigure their software in order to do so. For Icinga/Nagios, there are "recipes" on getting particular notification systems configured; that works, of course, but it‘s painful, error-prone, and the configuration is very static.

Some specialized tools have other mechanisms. Ansible for example, has a slew of notification modules  (some of which I even wrote), which can announce to your deployment  team what‘s going on while it runs. Here again, each time you use a  particular notification module in Ansible, you have to configure it to  notify a particular service‘s account. What if that changes? Do you  really want to change all the Ansible playbooks ...? No.

Let me show you what I mean by listing the source of an alert, its type, and its destination:

icinga       disks          [email protected] icinga       mail queue     [email protected] icinga       dnsbl          SMS:1234567890 ansible      playbook       twilio:john18 ...

So, when Icinga reports a problem with a DNS blacklist, an SMS will  be sent, and when Icina warns about a mail queue, an e-mail to Jane is  fired off.

Suppose we want to change that: in addition to having an e-mail sent  off, we wish to alert a bunch of mobile phones via Pushover. Or we want  to change the content of the message, etc. For most of us that means  chasing down all instances of the configured notification method, and  modifying / adding to that. No thanks. Really not.

Can we set up some sort of notification "bus" which services use to  send notifications to, and which then decides, using a central config,  what to do with those messages, maybe even reformat them on a  per-destination basis? Yes we can.

We created that recently. It‘s called mqttwarn, and it‘s pretty easy to set up. All you need is an MQTT broker, which is a lightweight "bus" for your network. Your services then "talk" to that broker (i.e. send it messages). mqttwarn  grabs those messages, and dispatches them on to a notification service  (SMTP, NNTP, Twilio, Pushover, HTTP, ...), stores them in files or in  MySQL, publishes them to Redis, pipes them to programs, etc. All  messages, some only (filtered by content, say).

I‘ll show you a small example to hopefully whet your appetite.

mqttwarn is configured in an ini-type file. The difficult part is setting up the so-called service targets which are notification services. I‘ll configure mqttwarn to provide a target called smtp:jplocal which delivers via SMTP, and another one called pushover:icinga which delivers a message to my mobile phone via Pushover.net.

[defaults] hostname  = ‘localhost‘ port      = 1883 ; name the service providers you will be using. launch   = smtp, pushover [config:pushover] targets = {     ‘icinga‘      : [‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx‘, ‘yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy‘],   } [config:smtp] server  =  ‘localhost:25‘ sender  =  "MQTTwarn <[email protected]>" starttls  =  False targets = {     ‘jplocal‘     : [ ‘[email protected]‘ ],     } [monitoring/+] targets = smtp:jplocal, pushover:icinga

When I launch mqttwarn, it will subscribe to the MQTT topic monitoring/+. (The + is a single-level wildcard which matches any one subtree.)

If I then publish a message to my MQTT broker, mqttwarn  will receive that message and act upon it. In this example I‘ll use a  command-line utility to do so, but we‘d normally configure our  monitoring system (or Ansible, etc.) to publish via MQTT, and there are  all manner of language bindings to accomplish that.

mosquitto_pub -t monitoring/warning -m ‘Disk utilization: 94%‘

As you‘ve probably guessed, -t specifies the topic name, and -m the payload to publish.

Do we have e-mail?

Date: Thu, 03 Apr 2014 09:26:36 +0200 From: MQTTwarn <[email protected]> To: [email protected] Subject: mqttwarn X-Mailer: mqttwarn Disk utilization: 94%

Simultaneously, mqttwarn notified the message via Pushover, and my phone alerts me accordingly:

Coming back to Ansible, if I wanted to post that same notification from a playbook, I would use the mqtt notification module. (The strange-looking quotes are necessary to protect the colon in the string.)

--- - hosts: all   tasks:   ...   - local_action: ‘mqtt                   topic=monitoring/warning                   payload="Disk utilization: 94%"‘

If I later change my mind and want to modify one of the targets, say,  I want to add another e-mail recipient, store messages, etc., I just  modify mqttwarn‘s  configuration file; no need to change the system which actually  initiated the notification alert (i.e. I don‘t have to alter my Icinga configuration).

Looking at Icinga (or Nagios), we can very easily add something similar. Consider your fugly notification module

define command{     command_name    notify-service-by-email     command_line    /usr/bin/printf "%b" "***** Icinga *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$     }

and now look at mine:

define command{     command_name notify-service-by-mqtt     command_line /usr/bin/notify-by-mqtt.py     }

The rather simple notify-by-mqtt program extracts the values I‘m interested in from the environment (note: requires setting enable_environment_macros=1 in icinga.cfg) and produces a JSON payload which is published via MQTT to mqttwarn. The payload looks like this:

{     "_type": "icinga",      "date": "2014-04-04",      "eventstarttime": "1396621975",      "hostdisplayname": "localhost",      "hostname": "localhost",      "hoststatetype": "HARD",      "servicedesc": "JPtest",      "servicedisplayname": "JPtest",      "serviceoutput": "file /tmp/f1: ENOENT",      "servicestate": "CRITICAL",      "servicestateid": "2",      "shortdatetime": "2014-04-04 16:38:25",      "time": "16:38:25",      "timet": "1396622305" }

On the receiving end, mqttwarn  can filter messages, and I change their formatting on a per/service  basis (e.g. I can be more verbose in an e-mail than in a tweet). mqttwarn can transform data on the fly, and it can apply templates for format the message.

Let me show you a simple example. I‘ll take the above mqttwarn configuration and add the title and format lines to it:

[monitoring/+] targets = smtp:jplocal, pushover:icinga title = {servicestate} {servicedisplayname} format = {time}: {hostname}:{servicedisplayname} is {servicestate}: {serviceoutput}

mqttwarn attempts to  decode JSON from the payload received over MQTT, and it can use the  JSON elements to populate format-type strings. The result as seen on my  phone via Pushover is like this:

Seen from bottom to top:

  1. our first example
  2. an Icinga alert with the raw JSON in it
  3. the final result with formatted title and body

We‘ve had a lot of very positive feedback on mqttwarn, and I‘m confident you‘ll be able to put it to good use!

时间: 2024-10-13 15:11:14

Using MQTTWarn to monitor server or device的相关文章

Microsoft SQL Server Version List(SQL Server 版本)

原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Service Packs (SP), Cumulative Updates (CU), patches, hotfixes and other builds of MS SQL Server 2014, 2012, 2008 R2, 2008, 2005, 2000, 7.0, 6.5 and 6.0 tha

Linux 网络流量查看 Linux ip traffic monitor

Network monitoring on Linux This post mentions some linux command line tools that can be used to monitor the network usage. These tools monitor the traffic flowing through network interfaces and measure the speed at which data is currently being tran

Android Screen Monitor同步手机屏幕到PC

有时候我们需要在PC上显示真机的屏幕,Android的ADM只能静态的监听一桢图像,所以在Google Code上,Google提供了一个Android Screen Monitor工具来实时显示手机屏幕: http://code.google.com/p/android-screen-monitor/   点击打开链接 介于大部分的朋友无法翻墙,这里提供下CSDN的下载链接 点击打开 System Requirements Ver.2.00 - 2.50 JRE(JDK) 5 or 6 and

LVS Load Balancing Linux Virtual Server

简介:Linux虚拟服务器(Linux Virtual Server. LVS),是一个由章文松开发的自由软件.利用KVS可以实现高可用的.可伸缩缩的Web, Mail, Cache和Medial等网络股务..井在此基 础上开发支持庞大用户数的,可伸缩的,高可用的电子商务应用.LVS1998年发展到现在,已经变得比较成熟,目前广泛应用在各种网络服务和电了商务应用 中.LVS具有很好的伸缩缩性.可靠性和管埋性,通过LVS要实现的最终目标是:利用linux 操作系统和LVS集群软件实现一个高可用.高

ArcGIS 10.3 for Server新特性介绍

ArcGIS10.3的正式版立即在美国Esri全球用户大会推出.中国的正式发行时间预计在Esri中国的用户大会.以下就将用户比較关心的ArcGIS 10.3 for Server的新特性给大家进行简单说明! ---------------------------------------------------------------------------------- 版权全部.文章同意转载.但必须以链接方式注明源地址,否则追究法律责任! 建议看到转载,请直接訪问正版链接获得最新的ArcGIS

如何编写LVS对Real Server的健康状态检测脚本

简介:Linux 虚拟服务器(Linux Virtual Server. LVS),是一个由章文松开发的自由软件.利用KVS可以实现高可用的.可伸缩缩的Web, Mail, Cache和Medial等网络股务..井在此基 础上开发支持庞大用户数的,可伸缩的,高可用的电子商务应用.LVS1998年发展到现在,已经变得比较成熟,目前广泛应用在各种网络服务和电了商务应用 中.LVS具有很好的伸缩缩性.可靠性和管埋性,通过LVS要实现的最终目标是:利用linux 操作系统和LVS集群软件实现一个高可用.

SQL Server 2008 R2 性能计数器详细列表(一)

原文:SQL Server 2008 R2 性能计数器详细列表(一) SQL Server Backup Device 计数器: 可监视用于备份和还原操作的 Microsoft SQL Server 备份设备 SQL Server Backup Device 计数器 说明 Device Throughput Bytes/sec 一个备份设备在备份或还原数据库时所用的读写操作的吞吐量(以每秒字节数表示).这一计数器只有在备份或还原操作执行时才存在. SQL Server Access Method

Goldengate monitor v11.1

JAVA 版本大于1.6以上即可 [[email protected] ~]# rpm -ivh jre-8u65-linux-x64.rpm Preparing...                ########################################### [100%] 1:jre1.8.0_65            ########################################### [100%] Unpacking JAR files...

Nagios Server通过NRPE监控client memory usage.

How to monitor server memory usage with Nagios Remote Plugin Executor (NRPE) Last updated on September 8, 2014 Authored by Sarmed Rahman 5 Comments In a previous tutorial, we have seen how we can set up Nagios Remote Plugin Executor (NRPE) in an exis