diff --git a/tg_bot/READMI.md b/tg_bot/READMI.md new file mode 100644 index 0000000..ee2b27b --- /dev/null +++ b/tg_bot/READMI.md @@ -0,0 +1,27 @@ +# Server Resource Monitoring Script with Telegram Notifications + +## Description +This script monitors server resources such as network usage, disk space, memory utilization, and CPU usage, and sends this information to a specified Telegram chat at regular intervals. + +## Installation +Before running the script, ensure you have the necessary utilities and Python libraries installed: + +```bash +# Ensure pip is installed +sudo apt update +sudo apt install screen python3-pip -y +pip3 install psutil +pip3 install pyTelegramBotAPI + +# Run command +screen +python3 fast_check.py +``` + +## Configuration +Make sure to update the following variables in the script: +- YOUR_TELEGRAM_BOT_TOKEN: Your Telegram bot token obtained from BotFather. +- YOUR_CHAT_ID: Your Telegram chat ID. + +## Customization +You can customize the script by adjusting the monitoring intervals, adding additional resource checks, or modifying the Telegram message format according to your specific requirements. diff --git a/tg_bot/fast_check.py b/tg_bot/fast_check.py new file mode 100644 index 0000000..1513de4 --- /dev/null +++ b/tg_bot/fast_check.py @@ -0,0 +1,63 @@ +import psutil +import socket +import time +import telebot + +def get_gb(size_bytes): + gb = size_bytes / (1024 ** 3) + return "{:.2f} GB".format(gb) + +def get_ip(): + try: + host_name = socket.gethostname() + host_ip = socket.gethostbyname(host_name) + return host_name, host_ip + except: + return "Unknown", "Unknown" + +def get_memory_info(): + memory_info = psutil.virtual_memory() + total_memory = get_gb(memory_info.total) + used_memory = memory_info.percent + return f"{used_memory}% ({total_memory})" + + +# 'YOUR_TELEGRAM_BOT_TOKEN' from BotFather +bot = telebot.TeleBot('') +chat_id = '' # NUMBER CHAT_ID + +while True: + # TOTAL INFO + network_info = psutil.net_io_counters() + disk_info = psutil.disk_usage('/') + memory_info = get_memory_info() + cpu_info = psutil.cpu_percent(interval=None, percpu=True) + + # NETWORK INFO MB/s + sent_speed = network_info.bytes_sent / (1024 ** 2) / 1800 # в MB/s + recv_speed = network_info.bytes_recv / (1024 ** 2) / 1800 # в MB/s + + # DISK INFO + disk_free_gb = get_gb(disk_info.free) + disk_free_percent = disk_info.percent + + # CPU INFO + cpu_count = psutil.cpu_count() + cpu_percent = sum(psutil.cpu_percent(interval=None, percpu=True)) / cpu_count + + # RAM INFO + memory_percent = memory_info + + # Hostname и IP + hostname, ip = get_ip() + + # Send Telegram + message = f"Hostname😊: {hostname}\nIP: {ip}\n\n" + message += f"Network usage 📡: \nSent: {sent_speed:.2f} MB/s\nReceived: {recv_speed:.2f} MB/s\n\n" + message += f"Free Spase 💾: {disk_free_gb} ({disk_free_percent}% Busy)\n\n" + message += f"Memory Usage 💭: {memory_percent}\n\n" + message += f"Cpu Usage 🧠: \nTotal: {cpu_count}\nConsumption: {cpu_percent}%" + + # Check every 30 min (sec) + bot.send_message(chat_id, message) + time.sleep(1800)