Pertama install telebot watchdog di server
sudo pip install telebot watchdog
create file python
import re
import telebot
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler# Telegram Bot Configuration
BOT_TOKEN = 'kode_bot_token'
CHAT_ID = 'kode_chat_id' # Replace with your Telegram user ID or group ID# Path to the Apache2 Access Log
LOG_FILE = '/var/log/apache2/other_vhosts_access.log' #sesuaikan dengan alamat lognya# Initialize Telegram Bot
bot = telebot.TeleBot(BOT_TOKEN)# Event Handler for Monitoring the Log File
class LogHandler(FileSystemEventHandler):
def __init__(self, log_file):
self.log_file = log_file
self.file = open(log_file, 'r')
self.file.seek(0, 2) # Start reading at the end of the filedef on_modified(self, event):
if event.src_path == self.log_file:
new_lines = self.file.readlines()
for line in new_lines:
if self.is_relevant_status_code(line):
self.notify_telegram(line)def is_relevant_status_code(self, log_line):
# Extract HTTP status code using regex
match = re.search(r'"\s(\d{3})\s', log_line)
if match:
status_code = int(match.group(1))
# Return True for the specified status codes
return status_code in [400, 401, 403, 404, 301, 302, 500, 505]
return Falsedef notify_telegram(self, log_entry):
try:
bot.send_message(
CHAT_ID,
"*HARAP WASPADA:*\n{}".format(log_entry),
parse_mode="Markdown"
)
except Exception as e:
print(f"Failed to send message to Telegram: {e}")# Set Up and Start the Log Monitor
def monitor_log_file():
event_handler = LogHandler(LOG_FILE)
observer = Observer()
observer.schedule(event_handler, path=LOG_FILE, recursive=False)
observer.start()
print(f"Monitoring {LOG_FILE} for status codes 400, 401, 403, 404, 301, 302, 500, 505...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()if __name__ == "__main__":
monitor_log_file()
save file tersebut dengan nama misalkan : apache_status_monitor.py
chmod +x apache_status_monitor.py
kemudian coba jalankan dengan perintah
python3 apache_status_monitor.py
setelah itu coba test dengan membuka alamat yang salah di website kita.