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
Patrycjusz
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 abc.xyz.pl 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 https://core.telegram.org/bots/api#formatting-options) 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

#!/usr/bin/lua
-- 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")
handle:close()
local nemsalias = string.gsub(result, "\n", "")
local telegram_url = 'https://api.telegram.org/bot' .. 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
else
  emoji = "✅" -- White Heavy Check Mark
end
-- 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
   else 
     Service = "<b>Service:</b>\n" .. arg[9] .. '\n\n'                  --$SERVICEDESC$
     message = emoji .. '\n\n' .. Alias .. Date_Time .. Notification .. Host  .. Service ..State .. Address .. Info
   end

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
Post Reply