Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify-by-Telegram #1001

Closed
luxzg opened this issue Feb 20, 2020 · 17 comments
Closed

Notify-by-Telegram #1001

luxzg opened this issue Feb 20, 2020 · 17 comments

Comments

@luxzg
Copy link

luxzg commented Feb 20, 2020

To skip the long story...

I am making small bash script to enable notifications to Telegram (instant messenger). But I am no developer... pulling whole git repository to add one file and then make a pull request is 2 days of work for me to remind myself how it's done.

Is it ok for me to publish the code here in this feature request, and possibly to get this included in future releases?

Basic script is functional (see screenshot below), but I hope to add more features to it tomorrow.

I'm using "notify-by-email" to provide me general directions, and I hope to make it a complete drop-in replacement.

Screenshot of notifications sent by Telegram bot:

It would make me happy to provide any kind of contribution to this awesome project

@sni
Copy link
Owner

sni commented Feb 20, 2020

Thanks. I am not sure if this is the right place for such thing. I am using telegram myself and its basically a simple curl command to send the notification to the telegram api.
Maybe a blog post or something would fit better.

@sni sni closed this as completed Mar 6, 2020
@Mohan-veera
Copy link

Mohan-veera commented Feb 16, 2023

@luxzg Can you please share me the bot code if possible.. You can share it on mohanveera.bolem@gmail.com

@luxzg
Copy link
Author

luxzg commented Feb 16, 2023

@Mohan-veera oh sure, as sni said before, it's basically one-liner. But let me get to work, I'll make it a small how-to and post right here as an answer. It was just so long ago, wouldn't want to write anything misleading from the top of my head. Btw, setup was done 3 years ago and it's still going strong, nagging us on Telegram every day with some alert :)

@luxzg
Copy link
Author

luxzg commented Feb 16, 2023

#1001

OK, this is based on my notes from 3 years ago, so it might have parts that have been modified by Telegram in the meantime. Likewise, it may get changed by Telegram at some point so... you know.

GET BOT

First, you need a Telegram bot.
Open Telegram app and look for BotFather:
https://telegram.me/BotFather
These are commands you need to send to BotFather (Telegram all-father of all bots) to start your own:

  • start chat with BotFather:
    /start
  • will list current set of available commands
    /newbot
  • will create a new bot, next it will ask for a name
    <enter something funny here>
  • now it will ask for a username, it must end in bot (can be upper or lower as much as I know, eg. Bot is also accepted), it did not accept dash in name I tried back then
    <myBotNameWithoutDashesBot>
  • you get success message which contains important information - USE THIS TOKEN TO ACCESS THE HTTP API - with some very long string, save that!
  • you can now modify your bot's picture and such, but that's personal preference
  • list your bots using following commands
    /mybots
  • it will list your bot and ask what you want, but you don't need to do anything else

GET TELEGRAM CHANNEL

Now that you have a bot, you also need a channel, since it's alerting, you probably want a private channel.
Open Telegram app again, and tap hamburger menu and pick New Group
Add people, and make sure to add your Bot as well, there's a search, type into it bot and pick one that you've just created.
I wanted to test this all, so I've only added my bot and me, until setup was done.
You'll want to name the group, and probably add an icon that'll yell it's an alert :)
When in need of adding more people, or bots, enter group in mobile app, tap group name, then pick option:

  • Add Members -> <pick your person or bot>

I've also made sure other members can't post to channel, to stop spam, to do that long-press each member one by one, Change permissions, and put all options to OFF
DO NOT remove messsage/media/links permissions from the BOT !! or it won't be able to post to group :)
When done with members, also set group permissions. When in group, tap group name again, and tap little pen icon in the upper right.
Make sure Group Type is set to Private, and under Permissions I've left only Messages, Media & Links, disabled the rest.
While I was doing that channel reflected my changes by informing that I've changed this group to supergroup.
This supergroup thing is important because it changes definition of group ID we will need to use in the alert script.

GET TELEGRAM GROUP ID

Now, as funny as my seem, open Telegram on your desktop PC's browser.
Open https://web.telegram.org/ and login there.
Open group you've just created and set all permissions.
Now copy the link from your browser's addressbar. It will look something like this: https://web.telegram.org/k/#-1xxxxxxxxx

  • (please note that link format has changed since 2020, this is current format, but it may change yet again in the future)

We need this 10-number ID (no letters), in all my group chats it is 10 numbers and starts with "1". Let's say it's 1234567890 for example.
To this number you add a -100 as prefix so you end up for example with -1001234567890.
THAT IS SUPERGROUP ID !!!

MAKE SURE YOU HAVE IT ALL

Now you have the only two important IDs needed for Telegram HTTP API:

  • bot API ID
  • supergroup ID

CREATE BASH/CURL SCRIPT

Now, depending on your version and such, the following paths may be different.
But in my Thruk installation on Ubuntu server I login, then:

  • get root rights
    sudo su
  • from there enter the plugins folder
    cd /omd/sites/monitoring/lib/nagios/plugins/
  • list all plugins
    ls -al
  • look in the list, if you have notify-by-email.pl there you're in the right place
  • you can copy existing file or create new one, but let's say we copy
    cp notify-by-email.pl notify-by-telegram-test.sh
  • then open new copy of the file
    nano notify-by-telegram-test.sh
  • you don't need this script actually, but if you scroll through it you'll see known keywords from Thruk such as:
    • SERVICEDESC
    • HOSTNAME
    • SERVICEOUTPUT
    • HOSTOUTPUT
    • and so on
  • actual Telegram notification script in my case (redacted due to security with all those xxxxxxxxxxxxx's) goes like this:
#!/bin/bash
TOKEN=10xxxxxxxx:AxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxZ
ENDPOINT="https://api.telegram.org/bot"$TOKEN"/sendMessage?chat_id=-1xxxxxxxxxxx9"
curl -s -X POST $ENDPOINT -F text="$1"
  • yes, really, that's it!
    • For TOKEN replace that with your Telegram Bot API token that BotFather gave you in that USE THIS TOKEN TO ACCESS HTTP API: xxxxxxxxxxxxx:xxxxxxxxxxxxx message
    • For chat_id use that -1001234567890 supergroup ID you've compiled by using group URL + -100 prefix
  • to test the script run this:
    /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-test.sh "If you're reading this in Telegram group then script is working!"
  • check the group, you should have a message from your bot
  • check the output in your Linux command line, you should get something like this (redacted again):
{"ok":true,"result":{"message_id":5161,"from":{"id":1xxxxxxxxx,"is_bot":true,"first_name":"xxxxxxxxxxxxxBot","username":"xxxxxxxxxxxBot"},"chat":{"id":-1001xxxxxxxxx,"title":"ITCrowd xxxxxxxxxxxxxxxxxx :)","type":"supergroup"},"date":1676549410,"text":"If you're reading this in Telegram group then script is working!"}}
  • so if you get "OK":true and you see the message your script is ok, your group is fine, and your bot is awesome! :)

SETUP NOTIFICATION SERVICE IN THRUK

We need to setup at least one test device/service with notification service that will trigger this script, and forward our message from Thruk.
Login to Thruk with admin user, then create these new commands and one contact:

  • Config Tool
  • Object Settings
    • Commands
      • Create a new command with:
        - command_name = host-notify-by-telegram
        - command_line =
        /bin/bash $USER1$/notify-by-telegram-test.sh "$NOTIFICATIONTYPE$ - $NOTIFICATIONCOMMENT$' $HOSTNAME$ $HOSTSTATE$ $HOSTADDRESS$ $SHORTDATETIME$ $HOSTOUTPUT$ $HOSTPERFDATA$ $HOSTACKAUTHOR$ $HOSTACKCOMMENT$ $HOSTDURATION$"
    • Commands
      • Create a new command with:
        - command_name = service-notify-by-telegram
        - command_line =
        /bin/bash $USER1$/notify-by-telegram-test.sh "$NOTIFICATIONTYPE$ - $NOTIFICATIONCOMMENT$' $HOSTNAME$ $HOSTSTATE$ $HOSTADDRESS$ $SHORTDATETIME$ $SERVICEDESC$ $SERVICESTATE$ $SERVICEOUTPUT$ $SERVICEPERFDATA$"
    • Contacts
      • Create a new contact with:
        - contact_name = telegram
        - alias = Telegram Bot
        - use = generic-contact
        - host_notification_commands = host-notify-by-telegram
        - service_notification_commands = service-notify-by-telegram

So now we have one command that handles host notifications, and one that handles service notifications, and those two are linked to the new Thruk contact.
Next we need to hook up this contact to our hosts, services, or groups.
While you can do that in any number of ways, I did it like this.

  • Contactgroups
    • contactgroup_name = generic-contactgroup
    • members = telegram
  • Services
    • name = generic-service
    • active_checks_enabled = Yes
    • contact_groups = generic-contactgroup
    • contacts = telegram
    • (might be overdoing it, because telegram contact is part of the contact group already, and then I call both group and add it separately, but nevermind that)

These generic groups usually handle all the hosts and services by default, but you should check their References for Incoming list.
In my case, I have generic-contactgroup with dozens of Incoming references from assorted hosts, and similary generic-service has bunch of Incoming References from assorted services (like NCPA HDD service check, or generic HTTPS checks and such).
If you open any specific service you want to join your Telegram channel with their spam ;D just open Services, find that service, and it should have use = generic-service listed (among other possible uses).
Same with hosts you'd like to get notifications from, open Hosts, find any specific host, and it should have contact_groups = generic-contactgroup listed.
Sure enough, you can just pick a single host for testing, open it in the Object settings - Hosts and once opened click add new attribute, then pick contacts, Finish and in the new field pick telegram from list of contacts.
Do something like disconnecting test host machine from network, and depending on your other notification/alerting settings, you should soon see a notification on your Telegram account.

That's it, you now have a blog post ;D If you get stuck feel free to type here, just as you've found this after 3 years maybe at least one other person (eventually) gets some use out of this :)

@luxzg
Copy link
Author

luxzg commented Feb 16, 2023

@Mohan-veera - I forgot to mention few things:

  • I've left media and links in Telegram group permissions because I can then use the same bot with Grafana alerts for some other stuff, and Grafana will send graphs and links with rest of notification message. Unfortunately I don't think Thruk can send images with graphs (yet? :) )
  • command_line of both Thruk commands created in this guide are just one option, you can mix and match assorted variables used by Thruk and get a more personalized message if you care about something specific, for me this is working fine as is, and you get pretty detailed error text on your Telegram (see image in first post). You can just make a simple "Host xyz is down" just the same, or list everything that Thruk supports
  • Bash script is simple on purpose, so you can modify all alert aspects on the Thruk/GUI side
  • likewise, you can reuse the same bash script, and the same contact, but create multiple other commands that have different message formats or additional text if you want to play around

If @sni or anyone else has better way to do some of these don't be shy 😁

@Mohan-veera
Copy link

@luxzg Thanks a lot for your doc which helps me to integrate the Thruk to the bot....

@sni
Copy link
Owner

sni commented Feb 20, 2023

  • Unfortunately I don't think Thruk can send images with graphs

The notification script is run by naemon, but you can utilize thruk to get the performance graph. There is the "thruk graph" cli tool.
We use that for ex. to append graphs to notification mails: https://github.com/ConSol/omd/blob/labs/packages/notifications-tt/skel/etc/mail-templates/notify-by-email.service.html.tpl#L135

@luxzg
Copy link
Author

luxzg commented Feb 20, 2023

  • Unfortunately I don't think Thruk can send images with graphs

The notification script is run by naemon, but you can utilize thruk to get the performance graph. There is the "thruk graph" cli tool. We use that for ex. to append graphs to notification mails: https://github.com/ConSol/omd/blob/labs/packages/notifications-tt/skel/etc/mail-templates/notify-by-email.service.html.tpl#L135

Hmm, this seems like it shouldn't be all that hard to use and adapt for Telegram. I'll give it a try. Thank you sni! If I end up victorious I'll post additional instructions here :)

@luxzg
Copy link
Author

luxzg commented Feb 22, 2023

Sorry @sni to bug you, but I can't figure out where is the correct binary or how to install this module.

I've found and tried this and get the error as below for everything except --help

#:/omd/sites/monitoring/bin# ./thruk
usage: ./thruk [<globaloptions>] <plugin> [<options>]
see --help for detailed help.
Can't locate Thruk/Utils.pm in @INC (you may need to install the Thruk::Utils module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at ./thruk line 107.
#:/omd/sites/monitoring/bin# ./thruk --help
NAME
    thruk - Command Line Utility for Thruk Monitoring Gui

SYNOPSIS
      Usage: thruk [globaloptions] <command> [commandoptions]
...

I've tried these other files, only 3 are executable:

find / -name 'thruk' -type f
/opt/omd/sites/monitoring/etc/logrotate.d/thruk
/opt/omd/sites/monitoring/etc/init.d/thruk <--- executable
/opt/omd/versions/3.20-labs-edition/lib/omd/scripts/post-update/thruk <--- executable
/opt/omd/versions/3.20-labs-edition/bin/thruk <--- executable
/opt/omd/versions/3.20-labs-edition/skel/etc/logrotate.d/thruk
/opt/omd/versions/3.20-labs-edition/skel/etc/init.d/thruk

These two have some errors:

/opt/omd/versions/3.20-labs-edition/bin/thruk -V
Can't locate Thruk/Config.pm in @INC (you may need to install the Thruk::Config module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /opt/omd/versions/3.20-labs-edition/bin/thruk line 86.

or

/opt/omd/sites/monitoring/etc/init.d/thruk -V
apache is not running, cannot start!

And "post-update" one has no output for either --help or -V or anything I've tried, probably best I don't touch it :)

I'd love to try the graphing tool with Telegram, and I've found the perl module (I guess) here:
https://github.com/sni/Thruk/blob/master/lib/Thruk/Utils/CLI/Graph.pm

But I don't see any documentation about how to include it properly in my installation

I use Ubuntu 18.04.4 and fairly old install of Thruk. Is it time to upgrade? :) Will it be included if I go with new version?

EDIT:
OMG ignore me, I need to update obviously, I have 3.20, Ubuntu repo has 4.70 and you've got 5.10 of OMD on the web cry seems I was gone from the office a bit too long...

EDIT 2:
OK, I'm on Ubuntu 20.04 and OMD Labs 5.10 finally, after 4 hours of updates, but still I have the same issue, so it wasn't version 3.20 issue :( I still need to figure out how to get CLI.pm perl module somehow :(

/opt/omd/versions/5.10-labs-edition/bin/thruk --list-commands
Can't locate Thruk/Config.pm in @INC (you may need to install the Thruk::Config module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /opt/omd/versions/5.10-labs-edition/bin/thruk line 124.
/opt/omd/versions/5.10-labs-edition/bin/thruk -a graph
Can't locate Thruk/Utils/CLI.pm in @INC (you may need to install the Thruk::Utils::CLI module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /opt/omd/versions/5.10-labs-edition/bin/thruk line 118.
/opt/omd/sites/monitoring/etc/init.d/thruk
apache is not running, cannot start!

@luxzg
Copy link
Author

luxzg commented Feb 24, 2023

So, I have installed a separate Ubuntu and only installed thruk and it's dependencies.
There, I can access thruk CLI commands by running /bin/thruk
Comparing folder structures I've found that this working CLI test install has perl modules here:

/usr/share/thruk/lib/Thruk/Utils

And my production install has them here:

/omd/versions/5.10-labs-edition/share/thruk/lib/Thruk/Utils

But I get this error as I mentioned in earlier comment:

/omd/versions/5.10-labs-edition/bin/thruk --local -a graph
Can't locate Thruk/Utils/CLI.pm in @INC (you may need to install the Thruk::Utils::CLI module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /omd/versions/5.10-labs-edition/bin/thruk line 118.

I'm assuimng @INC is some kind of include list, and that one of these should point to correct destination @DATADIR@/lib @THRUKLIBS@, but I get the error. I can just guess it's some kind of environmental variable missing or pointing to wrong directory.

So I tried simply copying files to one of other included folders:

cp -r /omd/versions/5.10-labs-edition/share/thruk/lib/Thruk /etc/perl/Thruk

And now I get a different error:

/omd/versions/5.10-labs-edition/bin/thruk --local -a graph
Can't locate Cpanel/JSON/XS.pm in @INC (you may need to install the Cpanel::JSON::XS module) (@INC contains: @DATADIR@/lib @THRUKLIBS@ /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /etc/perl/Thruk/Utils/CLI.pm line 17.
BEGIN failed--compilation aborted at /etc/perl/Thruk/Utils/CLI.pm line 17.
Compilation failed in require at /omd/versions/5.10-labs-edition/bin/thruk line 118.

So I'm fairly certain now that one of variables is pointing in wrong direction, and thus thruk binary can't find all of it's own files and modules. And that prevents using graph CLI command. Likewise, I don't know where to get other modules, and does it mean I should have Cpanel functionality setup... I hope not :)

@sni should I open ticket on OMD GitHub for this to get fixed or am I doing something wrong?

@luxzg
Copy link
Author

luxzg commented Feb 25, 2023

Additional progress, and new bumps on the road...

I've installed make and other build essentials, cpanminus, and then added Cpanel::JSON::XS and Date::Calc, in addition to copying Thruk modules to perl directory.

Now I get a new error:

'/etc/perl/Thruk/Config.pm/var/.' does not exist, make sure it exists and has proper user/groups/permissions at /etc/perl/Thruk/Config.pm line 461.
BEGIN failed--compilation aborted at /etc/perl/Thruk/Utils/CLI.pm line 25.
Compilation failed in require at /omd/versions/5.10-labs-edition/bin/thruk line 118.

Inside Config I've noticed this is happening if root is used, so I switched back to "monitoring" user.
su monitoring

And finally!!

/omd/versions/5.10-labs-edition/bin/thruk graph
Description:
    The graph command exports pnp/grafana graphs

Usage:
      Usage: thruk [globaloptions] graph <options>
...

So to conclude what I had to do to get the graph command to even run properly:

# Fix Perl requirements and includes for Thruk "graph" CLI utility

# 0) login to server via SSH or console, get root rights
sudo su

# 1) Will need "make", so we install the whole build package
apt-get install build-essential
	y
# check by re-running same command
#	output:		build-essential is already the newest version (12.8ubuntu1.1).

# 2) Will need Perl "App::cpanminus" to "get, unpack, build and install modules from CPAN" (https://metacpan.org/pod/App::cpanminus)
cpan App::cpanminus
	yes
	<Enter>
# check by re-running same command
#	output:		App::cpanminus is up to date (1.7046).

# 3) Will need "Cpanel::JSON::XS" Perl module, we get it from CPAN (https://metacpan.org/pod/Cpanel::JSON::XS)
cpanm Cpanel::JSON::XS
# check by re-running same command
#	output:		Cpanel::JSON::XS is up to date. (4.35)

# 4) Will need "Date::Calc" Perl module, we get it from CPAN (https://metacpan.org/pod/Date::Calc)
cpanm Date::Calc
# check by re-running same command
#	output:		Date::Calc is up to date. (6.4)

# 5) Copy Thruk Perl module files to "perl" folder, because that's certainly in @INC path, other workarounds could be possible/acceptable as well
cp -r /omd/versions/5.10-labs-edition/share/thruk/lib/Thruk /etc/perl/Thruk
# double check it
ll /etc/perl/
ll /etc/perl/Thruk/
# check for Graph.pm
ll /etc/perl/Thruk/Utils/CLI

# 6) Sudo to "monitoring" user, or whichever site you're using for this
su monitoring

# 7) Try "graph" command now
/omd/versions/5.10-labs-edition/bin/thruk graph

Output:

Description:
    The graph command exports pnp/grafana graphs

Usage:
      Usage: thruk [globaloptions] graph <options>

Options:
    help
            print help and exit

    format
            Output format, can be either 'png' or 'base64'. Default is png.

    width
            Width of the exported image in px. Default to 800.

    height
            Height of the exported image in px. Default to 300.

    host
            Hostname to export graph for

    service
            Service description to export graph for. If empty host graph will be exported.

    start
            Start timestamp used when collecting performance data. Defaults to yesterday.

    end
            End timestamp used when collecting performance data. Defaults to now.

    source
            Grafana panelId. Defaults to 1.

    output
            Write output to file, default is printing to stdout.

Examples:
    Export pnp host graph for localhost in base64 format.

      %> thruk graph --host='localhost' --service='_HOST_' --width=900 --height=200 --format=base64
# end output #

Now I have a new problem, though that could be related to my Kerberos auth setup for this server :-/

thruk graph --host='HOST51' --service='Check RDP port' --width=900 --height=200
[WARN] response: https://127.0.0.1:5000/monitoring/grafana/dashboard-solo/script/histou.js?host=HOST51&service=Check%20RDP%20port&panelId=1&from=1677239410000&to=1677325810000 401
[WARN] timeout while waiting for chart, export failed

Quest continues...

EDIT: after few more wourkarounds for authentication for localhost, hurray!!! It works!
I will do my best to finish this in Telegram and write a guide this weekend.
(But those issues are still there, so few manual installations are needed, unless I'm doing something wrong in the first place)

@sni
Copy link
Owner

sni commented Feb 25, 2023

If you get errors about missing modules, then you are using the wrong user. All commands need to run as site user. Cpanel::JSON::XS and Date::Calc are part of OMD and there is no need to install them twice. You just need to make sure you are using the site user.

@luxzg
Copy link
Author

luxzg commented Feb 25, 2023

Hm, it could be, I won't test now, since I finally got everything working. I'll assume having these modules twice won't hurt. I'm also pretty sure I tried with both root and "monitoring" (site user), but like I said, I spent too much time to go test that, as I'd need to uninstall everything and start again...

Anyway thanks for reply, when I will be setting up next monitoring with OMD/Thruk and Telegram, I'll make sure to try. (Or on the next major update of the server).

I've just finished setting up real production notifications on services, and it works really nice, thanks!

Below is what I've done to go from text-only notifications to text+graph on Telegram.

Telegram integration - WITH GRAPH IMAGES !

  • first go to Thruk Web UI, Object settings, Command

  • now create a new command from existing Telegram (text-only) command by cloning command "service-notify-by-telegram" into new command with settings:

    • command_name: service-notify-by-telegram-with-graph
    • command_line: /bin/bash $USER1$/notify-by-telegram-with-graph.sh "$NOTIFICATIONTYPE$ - $NOTIFICATIONCOMMENT$' $HOSTNAME$ $HOSTSTATE$ $HOSTADDRESS$ $SHORTDATETIME$ $SERVICEDESC$ $SERVICESTATE$ $SERVICEOUTPUT$ $SERVICEPERFDATA$" "$HOSTNAME$" "$SERVICEDESC$"
  • Apply, then save any changes and reload monitoring core through Thruk

  • now copy bash script for text-only notifications and make one for picture+text like this:

cd /omd/sites/monitoring/lib/nagios/plugins/
cp /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram.sh /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-with-graph.sh
  • edit the new file
    nano /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-with-graph.sh
  • and make it look like this:
#!/bin/bash

/omd/versions/5.10-labs-edition/bin/thruk graph --host="$2" --service="$3" --width=900 --height=200 --format=png --output=/omd/sites/monitoring/tmp/thruk/graphs/telegramPhoto.png
# img stored here by default:
#       /omd/sites/monitoring/tmp/thruk/graphs/<hostname>*<width>*<height>*.png
# img stored here with output set:
#       graph written to /omd/sites/monitoring/tmp/thruk/graphs/telegramPhoto.png

TOKEN=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxX

ENDPOINT="https://api.telegram.org/bot"$TOKEN"/sendPhoto?chat_id=-100xxxxxxxxxx"

curl -s -X POST $ENDPOINT -H 'Content-Type: multipart/form-data' -F caption="$1" -F photo=@"/omd/sites/monitoring/tmp/thruk/graphs/telegramPhoto.png"

rm /omd/sites/monitoring/tmp/thruk/graphs/telegramPhoto.png
rm /omd/sites/monitoring/tmp/thruk/graphs/*900-200.png
  • don't forget to set your own chat ID and bot API key, same logic as in text-only notifications
  • note those variables $2 and $3 in the script, in thruk command line, those are taken from the Thruk command, the last two arguments which are "$HOSTNAME$" "$SERVICEDESC$" - YOU NEED THESE UNTOUCHED or graph creation won't work correctly
  • notification text is first argument of the command and is denoted by the $1 in the script, you can change that, as that's just notification text, if you want to make it look differently
  • width/height is obvious I guess
  • default start/end period for the graph is 24 hours, with end being "now" and start being "24h earlier"

Test in CLI

  • need to be monitoring site user or it won't work!
    su monitoring

  • run command, make sure you type upper/lower case correctly and all that

/omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-with-graph.sh "This is a testing message with graph!" "HOST51" "NCPA Memory Virtual"
  • if it works, then future notifications will have correct image
  • you can then disable the text-only command, just remove it from the contact "telegram"

Big thanks!

To sni for making Thruk, and for pointing me in the right direction :)

@luxzg
Copy link
Author

luxzg commented Feb 25, 2023

Just a quick look at how notifications look in mobile app. Tnx again!

P.S. @Mohan-veera - if you want to try with images, now you can :)

@sni
Copy link
Owner

sni commented Feb 25, 2023

cp /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram.sh /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-with-graph.sh

That's not a good idea, the script will be gone after the next omd upate. Copy it to the sites local/lib/...

#1 rule in OMD, if you need root permissions for anything except creating or removing the site, you are doing it wrong.

@luxzg
Copy link
Author

luxzg commented Feb 25, 2023

cp /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram.sh /omd/sites/monitoring/lib/nagios/plugins/notify-by-telegram-with-graph.sh

That's not a good idea, the script will be gone after the next omd upate. Copy it to the sites local/lib/...

#1 rule in OMD, if you need root permissions for anything except creating or removing the site, you are doing it wrong.

Ah, thank you for the tips. I've had to move scripts manually on update from 3.20 to 4.60 to 5.10, but never actually lost them. But better safe than sorry, sure!

And I'll keep #1 rule in my mind from now on, thanks, will forward the info to my colleagues as well.

@luxzg
Copy link
Author

luxzg commented Mar 10, 2023

I've created a script to acknowledge a PROBLEM reported by a service to Telegram, via private message to Telegram bot. It's hack-ish and will probably improve... But if someone will need it post here and I'll post current working version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants