You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3.0 KiB

Alias Tag Date DocType Hierarchy TimeStamp location Source CollapseMetaTable
Logwatch Telegram Bot
2022-03-10 https://github.com/fxkjd/logwatch-telegram-bot true

Parent:: Selfhosting, VPS Console Dialogue


name Save
type command
action Save current file
id Save

^button-GitHubfxkjdlogwatch-telegram-botSendNSave

GitHub - fxkjdlogwatch-telegram-bot Send logwatch results to your telegram account

title: Summary
collapse: open
Note Description

style: number

Logwatch Telegram Bot

This repository includes a guide to send the logwatch results to your Telegram Bot.


Installing

Prerequisites

The following need to be installed:

  • Logwatch
  • Curl

See Related Links for more information about how to install and configure logwatch.

Setting up

In Telegram

Create a Telegram Bot in order to obtain the BOT TOKEN (More info).

Start a conversation with your new Telegram Bot and obtain the CHAT_ID. You can obtain the CHAT_ID using the telegram API:

~~~bash
curl https://api.telegram.org/bot<BOT TOKEN>/getUpdates
~~~

Replace both of them in the #Logwatch sh script.

In your server

Create (if not already created) the /root/logwatch_bot/logs and /var/cache/logwatch/ folders.

Copy the logwatch.sh script in /root/logwatch_bot folder. Ensure it has execution permissions.

Edit the crontab.

~~~bash
crontab -e
~~~

Add the following line to send the logwatch results every day at 8.00 AM.

title: cronjob
~~~bash
0 8 \* \* \* /bin/bash /root/logwatch\_bot/logwatch.sh
~~~

Logwatch.sh

title: logwatch.sh
~~~bash
#!/bin/bash

# Credits to Neolot (original script: https://gist.github.com/Neolot/ef7fce2518a8ec71bbe09beb9381ee95)

# Config

day=$(date +%Y-%m-%d)

filename=/var/cache/logwatch/$day-logwatch.txt

token=<YOUR BOT TOKEN>

chat_id=<YOUR CHAT ID>

#Check if removed-but-not-purged

test -x /usr/share/logwatch/scripts/logwatch.pl || exit 0

#execute

/usr/sbin/logwatch --output file --filename $filename

chmod 644 $filename

mv $filename /root/logwatch_bot/logs/

curl -F chat_id="$chat_id" -F text="$(head /root/logwatch_bot/logs/$day-logwatch.txt -n 9)" https://api.telegram.org/bot$token/sendMessage >/dev/null 2>&1

curl -F chat_id="$chat_id" -F document=@"/root/logwatch_bot/logs/$day-logwatch.txt" https://api.telegram.org/bot$token/sendDocument >/dev/null 2>&1
~~~