Skip to content

add tg_bot directory fast easy monitoring #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tg_bot/READMI.md
Original file line number Diff line number Diff line change
@@ -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.
63 changes: 63 additions & 0 deletions tg_bot/fast_check.py
Original file line number Diff line number Diff line change
@@ -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)