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.

160 lines
3.0 KiB

---
Alias: ["Logwatch Telegram Bot"]
Tag: [""]
Date: 2022-03-10
DocType:
Hierarchy:
TimeStamp:
location:
Source: https://github.com/fxkjd/logwatch-telegram-bot
CollapseMetaTable: true
---
Parent:: [[Selfhosting]], [[VPS Console Dialogue|VPS command-line]]
---
 
```button
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
 
```ad-abstract
title: Summary
collapse: open
Note Description
```
 
```toc
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](https://core.telegram.org/bots#6-botfather)).
Start a conversation with your new Telegram Bot and obtain the `CHAT_ID`. You can obtain the `CHAT_ID` using the telegram API:
```ad-command
~~~bash
curl https://api.telegram.org/bot<BOT TOKEN>/getUpdates
~~~
```
Replace both of them in the [[#Logwatch sh]] script.
&emsp;
#### 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.
```ad-command
~~~bash
crontab -e
~~~
```
Add the following line to send the logwatch results every day at 8.00 AM.
```ad-command
title: cronjob
~~~bash
0 8 \* \* \* /bin/bash /root/logwatch\_bot/logwatch.sh
~~~
```
&emsp;
### Logwatch.sh
```ad-code
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
~~~
```
&emsp;
## Related Links
- [How To Install and Use Logwatch Log Analyzer and Reporter on a VPS](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-logwatch-log-analyzer-and-reporter-on-a-vps)
- [Logwatch - ArchWiki](https://wiki.archlinux.org/index.php/Logwatch)
- [OpenVPN logwatch script](https://gist.github.com/fxkjd/7162cc54fc8f3a3f3ba88ba258ce74c3)