Telegram notifications for service check_ssh not working

Found something amiss in NEMS? Confirm first that you are running the latest version, and then post your bug report here.
Post Reply
Junior Member
Posts: 1
Joined: Wed Jan 06, 2021 6:16 am

Telegram notifications for service check_ssh not working

Post by Patrycjusz »

Hi everyone,
I found bug in Telegram notification module. In my case I didn't get notifications for service check_ssh on server with non standard port. So I started debugging ;)

It turned out that Telegram API returns error:

Code: Select all

HTTP/1.1 400 Bad Request
{"ok":false,"error_code":400,"description":"Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 162"}
After inspecting file /usr/local/nagios/libexec/notify-by-telegram.lua I found parameter parse_mode=Markdown is sent. Referring to Telgram API documentation (see this will interpret _ char as italic text start and another underscore char as italic text end, so it turned out that service name was the problem.

I changed parse_mode=Markdown parameter to parse_mode=HTML because it was faster (and in my opinion better) than writing custom escape character function for markdown mode. I also added couple HTML related minor changes.

Modified /usr/local/nagios/libexec/notify-by-telegram.lua file:

Code: Select all

-- Big thanks to:
--   baggins for the original development for NEMS 1.3
--   Kaganishu for helping with documentation and improvements for NEMS 1.5
--   NickTheGreek for contributing his findings to help improve functionality for NEMS 1.5

-- VERSION 1.5.6

https = require('ssl.https')
local handle = io.popen("/usr/local/bin/nems-info alias")
local result = handle:read("*a")
local nemsalias = string.gsub(result, "\n", "")
local telegram_url = '' .. arg[1] .. '/sendMessage?'
local chat_id = '-' .. arg[2]:gsub('%g', '')

-- UTF-8 Emojis Based on State
if string.find(arg[3]:lower(), "problem") then
  emoji = "⚠️" -- Warning Sign
elseif string.find(arg[3]:lower(), "flappingstart") then
  emoji = "⚠️" -- Warning Sign
  emoji = "✅" -- White Heavy Check Mark
-- Extras (may use later)
-- emoji = "🚫" -- No Entry Sign
-- emoji = "❓" -- Question Mark

   Notification = "<b>Notification Type:</b>\n" .. arg[3] ..'\n\n'    --$NOTIFICATIONTYPE$
   Host = "<b>Host:</b>\n" .. arg[4] ..'\n\n'                         --$HOSTNAME$
   State = "<b>State:</b>\n" .. arg[5] ..'\n\n'                       --$HOSTSTATE$/$SERVICESTATE$
   Address = "<b>Address:</b>\n" .. arg[6] ..'\n\n'                   --$HOSTADDRESS$
   Info = "<b>Info:</b>\n"  .. arg[7] ..'\n\n'                        --$HOSTOUTPUT$/"$SERVICEOUTPUT$"
   Date_Time = "<b>Date/Time:</b>\n" .. arg[8] ..'\n\n'               --$LONGDATETIME$
   Alias = "<b>Reporting NEMS Server:</b>\n" .. nemsalias .. '\n\n'   --NEMS Server Alias

   if (#arg == 8) then --assumes this since 8 arguments have been passed
     message = emoji .. '\n\n' .. Alias .. Date_Time .. Notification .. Host ..State .. Address .. Info
     Service = "<b>Service:</b>\n" .. arg[9] .. '\n\n'                  --$SERVICEDESC$
     message = emoji .. '\n\n' .. Alias .. Date_Time .. Notification .. Host  .. Service ..State .. Address .. Info

local data_str = 'parse_mode=HTML&chat_id=' .. chat_id .. '&text=' .. message..''  
local res, code, headers, status = https.request(telegram_url, data_str)
Tested on:
NEMS Platform: Raspberry Pi 4
NEMS Version Running: 1.5.2
Junior Member
Posts: 3
Joined: Mon Jan 18, 2021 10:50 am

Re: Telegram notifications for service check_ssh not working

Post by MiskoR »

Hi Patrycjusz,

I also has an issue with any Telegram notification - they are not being sent and I know that bot is working well as I am receiving notifications via the same bot from another monitoring app.

I've made the change that you proposed and now waiting for event to triger it.

Meanwhile, can you let me know how did you debug this module? Where did you picked up that error?

User avatar
Robbie Ferguson
Posting Freak
Posts: 835
Joined: Wed Mar 07, 2012 3:23 pm
Location: Ontario, Canada

Re: Telegram notifications for service check_ssh not working

Post by Robbie Ferguson »

Thank you for the detailed assessment. Just note that having modified the LUA code directly, you may lose your changes since NEMS will overwrite them during an update.

What I'd love to see is your support.nems file when experiencing the problem. That way I can create a proper patch for your scenario.

Post Reply