Having systems that need a monitoring or alert about errors and fails you may use Telegram Bot. Next examples of using Telegram Bot help to watch the status of a long process, also to send lines from log, and even more, it is possible to send printscreens or also convert printscreens to text and then send it to your chat in Telegram.
This way you can monitor any process in your system with smartphone in any place.
1. Create Telegram bot and get its API key
Telegram has advanced API available with bots. Find official Telegram user @botfather if you wish to create own bot, for example my_status_monitor_bot. Bot father will also provide an API key of a new bot (do not share it).
2. Start chat with your bot or add it to group
Find your own new bot by user name @my_status_monitor_bot and send hello to it, or also you can add bot to a group. This way you'll be able to get chat_id.
3. Use API to get chat id
With method getUpdates from Telegram API you can see id of your chat with bot.
https://core.telegram.org/bots/api#getupdates
Just open page like https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/getupdates
Or use curl on Linux: curl https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/getupdates
where Y-O-U-R-A-P-I-K-E-Y is a key from step 1.
4. Send system info to Telegram chat using bot
Use API method sendMessage with JSON data: https://core.telegram.org/bots/api#sendmessage
Example 1. Send downloading file size to Telegram every 10 minutes:
while [ true ]; do size=$(stat --printf="%s" /media/user/flash/blockchain.raw); curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"chat_id\":\"1234567890\",\"text\":\"$size\"}" "https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/sendMessage"; sleep 600; done
JSON data is using here like this: {"chat_id":"1234567890","text":"$size"}. This means message for example '20500' (downloading file size) will be sent to chat id 12345678790.
chat_id: 1234567890 change to your chat id from previous step
This way you can leave you PC keep downloading large file and get file size info in Telegram.
Example 2. Send system logs to Telegram chat
sudo tail -n0 -f /var/log/messages | xargs -I {} curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"chat_id\":\"1234567890\",\"text\":\"{}\"}" "https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/sendMessage";
tail is checking log file and each new line (even if it comes every 0.1 second) will be sent to Telegram using curl. Special xargs variable '{}' contains data from pipe that tail gives. To filter log messages use awk. You also may check the group of /var/log/messages and add your usual user to that group for read a file without sudo:
# ll /var/log/messages
-rw-r----- 1 root adm 81171910 Aug 4 11:19 /var/log/messagesusermod -aG adm user
This added usual 'user' to group 'adm', the group of file '/var/log/messages'. And now let's filter and use only messages from Xorg like this:
awk '{IGNORECASE=1} /xorg/' /var/log/messages
tail -n0 -f /var/log/messages | awk '{IGNORECASE=1} /xorg/' | xargs -I {} curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"chat_id\":\"1234567890\",\"text\":\"{}\"}" "https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/sendMessage";
5. Extra: How to send log strings from console to Telegram. Screenshot to text
Run long time script like download blockchain or import/export database may take long time, printing logs to console. It is possible to send lines appearing on the screen to Telegram for convenient monitoring of process. You can send screen capture with method sendPhoto, but it is a bit complicated cause need first to upload image to web and only after that to telegram. Also it is possible to send text from command line log.
Install flameshot for capturing screen and tesseract for convert image to text.
apt-get install flameshot
apt-get install tesseract-ocr libtesseract-dev tesseract-ocr-eng
You can use any other utilities too. Next, tune the script for get last line or last few lines. This gets last two lines from terminal that have about 800x600 resolution on screen placed to left top corner, and tesseract converts it into text.
flameshot full --path ~/Pictures/scr; sleep 5; im=$(ls -t ~/Pictures/scr | head -1);
convert -extract 800x43+3+587 ~/Pictures/scr/$im ~/Pictures/scr/_last.png;
tesseract -l eng ~/Pictures/scr/_last.png ~/_last_ocr;
mess=$(grep -P '\d' ~/_last_ocr.txt); echo $mess;
Take a look on convert -extract 800x43+3+587 ... Here 800x43 is widthXheight of a part you want to cut from source image of screen capture. +3+587 this is indent from left and top borders.
When this exactly configured to get last lines you need, run loop to send log every 10 minutes, give delay for screen capture and image to text conversion with sleep:
while [ 1 ]; do flameshot full --path ~/Pictures/scr; sleep 5; im=$(ls -t ~/Pictures/scr | head -1); convert -extract 800x43+3+587 ~/Pictures/scr/$im ~/Pictures/scr/_last.png; sleep 5; tesseract -l eng ~/Pictures/scr/_last.png ~/_last_ocr; sleep 5; mess=$(cat ~/_last_ocr.txt); curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data "{\"chat_id\":\"1234567890\",\"text\":\"mess $mess\"}" "https://api.telegram.org/botY-O-U-R-A-P-I-K-E-Y/sendMessage"; sleep 600; date; done
This is very useful system monitoring with smartphone and Telegram.
Links
1. Telegram Bot API https://core.telegram.org/bots/api
2. More AWK examples https://youni.world/t/awk
3. The GNU AWK User's Guide https://www.gnu.org/software/gawk/manual/gawk.html
4. How to Do OCR from the Linux Command Line Using Tesseract https://www.howtogeek.com/682389/how-to-do-ocr-from-the-linux-command-l…