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"}
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)
NEMS Platform: Raspberry Pi 4
NEMS Version Running: 1.5.2