Membuat Notifikasi LOG File Server ke Telegram

1. Buat file konfigurasi bot telegram agar notifikasi file log kita ke kirim ke telegram
sudo nano /home/webserv2/notifikasi/tg-send.sh

CHAT_ID=Chat_ID_Telegram_Group_Bot (example : -344035569)
TOKEN=Token_Bot_telegram (example : 882206788:AAE1x6WHUvhumE-sBYlJqCj0jUQFIguPZOI)

curl -s "https://api.telegram.org/bot$TOKEN/sendMessage?chat_id=$CHAT_ID" --data-urlencode "text=[Log Website] $1"

kemudian save, dan ubah pengaturan chmod untuk file tadi.

chmod +x /home/webserv2/notifikasi/tg-send.sh


2. Buat file filter pattern untuk menyaring log yang akan di kirim ke notifikasi

sudo nano /home/webserv2/notifikasi/phperror.conf

type=Single
ptype=RegExp
pattern=WhatWeb
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=python
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=curl
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=webtech
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=404
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=302
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=sqlmap
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=AH01630
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

type=Single
ptype=RegExp
pattern=ssh2
desc=$0
action=shellcmd /home/webserv2/notifikasi/tg-send.sh "$0"

kemudian save


3. Buat file untuk menjalankan file notifikasi dan menentukan file log apa saja yang akan dimasukan ke notifikasi log tersebut

sudo /home/webserv2/notifikasi/run.sh

#!/bin/bash

sec -conf=/home/webserv2/notifikasi/phperror.conf -input=/var/log/apache2/other_vhosts_access.log -detach --bufsize=1
sec -conf=/home/webserv2/notifikasi/phperror.conf -input=/var/log/auth.log -detach --bufsize=1


kemudian save, dan ubah pengaturan chmod untuk file tadi.
chmod +x /home/webserv2/notifikasi/run.sh


3. Cara Menjalankan File notifikasi, agar memudahkan menjalankan lewat terminal
sudo nano /usr/bin/notifikasi

#!/bin/bash
bash /home/webserv2/notifikasi/run.sh

kemudian save, dan ubah pengaturan chmod untuk file tadi.
chmod +x /usr/bin/notifikasi

4. Cara Menjalankan autorun saat server hidup, buat sebuah file service  
sudo nano /etc/systemd/system/notifikasi.service


Description=File_Notifikasi_Log

Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/notifikasi
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target

save file tersebut, kemudian jalankan perintah di bawah ini.

sudo systemctl daemon-reload
sudo systemctl enable notifikasi.service
sudo systemctl start notifikasi.service
sudo reboot

notifikasi log yang terkirim telegram sudah automatis di jalankan server saat server hidup.
SELESAI.

Related Articles