Compare commits

...
Sign in to create a new pull request.

20 commits
main ... main

Author SHA1 Message Date
hornet
8515e9c4a3 updoot readme v1.2 2024-12-27 21:51:18 +05:00
hornet
b09798cada updoot readme and version labels in the main file 2024-12-27 21:46:34 +05:00
hornet
e52703fe44 fixed authorization issue 2024-12-27 21:34:32 +05:00
hornet
1b52ceb881 Merge branch 'hax-dev' into dev 2024-10-25 18:20:39 +05:00
hornet
88b678da3b Merge branch 'hax-readme_updoot' into dev 2024-10-25 18:00:55 +05:00
hax
93fb4f93d0 Updoot README V1.1.0 :) 2024-10-25 00:43:54 +00:00
hax
b6792931dc Updoot secure system monitor with service management
Key Improvements:

Error Handling:
Used try-except blocks to catch errors from subprocesses and file operations, logging issues.

Thread Safety:
Introduced queue.Queue for thread-safe operations when handling ping results.

Subprocess Optimizations:
Used subprocess.run() for cleaner, more modern handling of subprocesses.
Avoided shell=True for security reasons unless absolutely necessary.

Service Management:
Improved service status checking by using systemctl is-active and using exit statuses for reliability.

User Authorization:
Checked user authorization in relevant commands like /restart, /reboot, and /ping.

Logging:
Introduced logging for all major operations to track activity and errors.

Polling Timeout:
Added timeouts and error handling to prevent the bot from hanging during long polling.

This updoot is bring more security, robustness, and scalability, ready to handle various edge cases that might occur in our system monitoring.

Signed-off-by: hax <hax@lainlounge.xyz>
2024-10-25 00:29:37 +00:00
hornet
e68739966a finished /reboot host and /restart host functions, added authorization for actions by telegram ID, added nginx to default services 2024-10-18 18:06:53 +05:00
hornet
0afffee697 new functions(WIP) - /reboot and /restart service 2024-10-17 15:51:21 +05:00
hornet
f28f291a49 updoot readme 2024-10-05 02:00:47 +05:00
HornetMaidan
7e3acb68ce updoot README 2024-10-05 01:41:05 +05:00
HornetMaidan
29cc8aedd4 horfix #4 (fucking insanity) 2024-10-05 01:38:52 +05:00
HornetMaidan
c0d02bb69a hotfix #3 (MY FUCKING GOD) 2024-10-05 01:32:33 +05:00
HornetMaidan
f45bffd25e hotfix #2 2024-10-05 01:24:00 +05:00
HornetMaidan
5d0ce49d0c hotfix 2024-10-05 01:08:41 +05:00
HornetMaidan
edb0641889 fixed the ping formatting 2024-10-05 00:53:58 +05:00
HornetMaidan
da49859b9d Merge branch 'main' of https://git.lainlounge.xyz/hornet/lainmonitor 2024-10-05 00:38:40 +05:00
HornetMaidan
a007405b4f add .gitignore 2024-10-05 00:35:18 +05:00
HornetMaidan
7d157e5136 added tailscale subnet availability check 2024-10-05 00:31:47 +05:00
hornet
186134db0c Merge pull request 'Proper README created' (#1) from hax/lainmonitor:main into main
Reviewed-on: http://192.168.0.66:3000/hornet/lainmonitor/pulls/1
Reviewed-by: hornet <hornet@lainlounge.xyz>
2024-10-04 19:29:01 +00:00
5 changed files with 262 additions and 115 deletions

2
.authorized_users Normal file
View file

@ -0,0 +1,2 @@
AUTHORIZED_USER_ID_1
AUTHORIZED_USER_ID_2

1
.env Normal file
View file

@ -0,0 +1 @@
YOUR_TOKEN_HERE

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
test.py
venv/

126
README.md
View file

@ -1,54 +1,71 @@
# LainMonitor
# lainmonitor
LainMonitor is a Telegram bot designed to monitor your system by providing real-time information about the system's status, services, and disk usage. It can also check connectivity to a specific Tailscale IP address.
LainMonitor is a Telegram bot designed to monitor your system, providing real-time updates on the systems status, essential services, and disk usage. It can also verify connectivity to a specific Tailscale IP address.
Current version: v1.2
## Features
- Retrieve system hostname, uptime, and status of essential services such as:
- Zerotier
- Prosody
- PostgreSQL
- Tailscale
- Check disk usage
- Ping a Tailscale IP to verify connectivity
- Use via Telegram commands like `/status`, `/ping`, and `/help`
### Key Features:
## Dependencies
- [Telebot](https://github.com/eternnoir/pyTelegramBotAPI) - A Python library for Telegram bot API.
Retrieve system information:
Hostname
Uptime
Status of critical services:
Zerotier
Prosody
PostgreSQL
Tailscale
nginx
Check disk usage
Ping a Tailscale IP for connectivity verification
Restart critical services
Reboot the host
Accessible via Telegram commands
### Prerequisites:
Python 3
Telebot — Python library for interacting with the Telegram bot API.
### Installation Guide:
Clone the repository:
## Installation
1. Clone this repository:
```bash
git clone https://git.lainlounge.xyz/hornet/lainmonitor.git
cd lainmonitor
```
2. Install the required Python library:
```bash
pip install pyTelegramBotAPI
```
3. Replace the placeholder in the code with your Telegram bot token:
```python
TOKEN = 'PLACE_YOUR_TOKEN_HERE'
```
4. Set up permissions for the bot to check system services (run as a user with `sudo` access).
RECOMMENDED: Create a virtual environment for python with:
```
python3 -m venv venv
source venv/bin/activate
```
Install dependencies:
## Usage
### Running Directly
You can run the bot directly using Python:
```bash
python3 lainmonitor.py
```
pip3 install -r requirements.txt
```
### Running as a Service
To run LainMonitor as a service, follow these steps:
1. Create a systemd service file:
```bash
Configure your bot token: Open the .env file and replace the placeholder with your Telegram bot token.
Configure authorized users: Open the .authorized_users file and replace the placeholders with Telegram user ID(s).
Set up service access: Ensure the bot can check system services by running it with sudo or appropriate permissions.
### Usage:
#### Running the Bot Manually:
You can run LainMonitor directly from the command line:
python3 lainmonitor.py
#### Running as a Systemd Service:
To run the bot as a systemd service, follow these steps:
Create a service file:
sudo nano /etc/systemd/system/lainmonitor.service
```
2. Add the following configuration:
```ini
Add the following configuration:
[Unit]
Description=LainMonitor Telegram Bot
After=network.target
@ -59,21 +76,24 @@ To run LainMonitor as a service, follow these steps:
[Install]
WantedBy=multi-user.target
```
3. Enable and start the service:
```bash
Enable and start the service:
sudo systemctl enable lainmonitor
sudo systemctl start lainmonitor
```
## Telegram Bot Commands
- `/start`: Initialize the bot and receive a welcome message.
- `/help`: Display available commands.
- `/status`: Get the system hostname, status, uptime, and the status of monitored services.
- `/ping`: Ping a Tailscale IP and return the connectivity status.
- `/reboot`: (Work in progress) Placeholder for a reboot command.
### Available Commands:
## Author
Created by **hornetmaidan**
/start — Initialize the bot and receive a welcome message.
/help — Display a list of available commands.
/status — Retrieve system hostname, uptime, and status of monitored services.
/ping — Ping a Tailscale IP and return connectivity status.
/restart hostname- Restart a specific service on a specified machine.
/reboot hostname — Placeholder for a system reboot command.
Feel free to contribute or suggest features!
### Contributions:
Created by hornetmaidan.
With Contributions from h@x.
Any new features and suggestions are welcome!

View file

@ -1,75 +1,197 @@
#description: telegram bot for monitoring the system
#dependencies: telebot
#usage: python3 lainmonitor.py | or run it as a service
#author: hornetmaidan
# --/usr/bin/env python3 -- #
# description: telegram bot for monitoring the system
# dependencies: telebot
# usage: python3 lainmonitor.py | or run it as a service
# author: hornetmaidan
# contributors: h@x
# version: 1.2
import os
import subprocess
import threading
import queue
from time import sleep
import telebot
#define the variables
status, hostname, uptime, zerotier, prosody, postgres, tailscale, disk, ping = 'unknown', 'unknown', 'unknown', 'unknown', 'unknown', 'unknown', 'unknown', 'unknown', 'unknown'
#telegram bot token
TOKEN = 'PLACE_YOUR_TOKEN_HERE'
import logging
#bot init
bot = telebot.TeleBot(TOKEN)
# Setup logging
logging.basicConfig(filename='lainmonitor.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
#get system info
def getinfo():
global status, hostname, uptime, zerotier, prosody, postgres, tailscale, disk
hostname = subprocess.check_output(['hostname']).decode().strip()
uptime = subprocess.check_output(['uptime', '-p']).decode().strip()
#systemd-only services
zerotier = subprocess.Popen("sudo systemctl status zerotier-one | grep 'Active'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip()
prosody = subprocess.Popen("sudo systemctl status prosody | grep 'Active'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip()
postgres = subprocess.Popen("sudo systemctl status postgresql | grep 'Active'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip()
tailscale = subprocess.Popen("sudo systemctl status tailscaled | grep 'Active'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip()
disk = subprocess.check_output(['df', '-h']).decode().strip()
if hostname == 'unknown':
# Load environment variables and config files securely
script_dir = os.path.dirname(os.path.realpath(__file__))
env_path = os.path.join(script_dir, '.env')
auth_users_path = os.path.join(script_dir, '.authorized_users')
# Load the token
try:
with open(env_path, 'r') as f:
token = f.read().strip()
except FileNotFoundError:
logging.error('Token file not found. Exiting...')
exit(1)
# Load the authorized users
try:
authorized_users = [str(line.strip()) for line in open(auth_users_path, 'r').readlines()]
except FileNotFoundError:
logging.error('Authorized users file not found. Exiting...')
exit(1)
# Initialize the bot
bot = telebot.TeleBot(token)
# Define status variables
status, hostname, uptime = 'unknown', 'unknown', 'unknown'
zerotier, prosody, postgres, tailscale, nginx, disk = ['unknown'] * 6
nodes, hostnames, threads = [], [], []
reach_queue = queue.Queue()
# Get basic system info
def get_system_info():
global hostname, uptime, zerotier, prosody, postgres, tailscale, nginx, disk
try:
hostname = subprocess.check_output(['hostname']).decode().strip()
uptime = subprocess.check_output(['uptime', '-p']).decode().strip()
services = ['zerotier-one', 'prosody', 'postgresql', 'tailscaled', 'nginx']
status_results = []
for service in services:
status_results.append(get_service_status(service))
zerotier, prosody, postgres, tailscale, nginx = status_results
disk = subprocess.check_output(['df', '-h']).decode().strip()
except subprocess.CalledProcessError as e:
logging.error(f"Error fetching system info: {e}")
status = 'offline'
else:
status = 'online'
return hostname, uptime, zerotier, prosody, postgres, tailscale, disk
#ping tailscale (change the IP address to the one you want or add more)
def check_tailscale():
global ping
ping = subprocess.Popen("ping TAILSCALE_IP -c 1 | grep '1 packets'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip()
if '1 received' in ping:
ping = 'connected'
# Helper function to get service status
def get_service_status(service):
try:
subprocess.run(['sudo', 'systemctl', 'is-active', '--quiet', service], check=True)
return f'{service} is active'
except subprocess.CalledProcessError:
return f'{service} is inactive/not present'
# Function to ping a Tailscale node
def ping_node(node, hostname):
try:
ping = subprocess.run(['ping', '-c', '1', node], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
reach_queue.put(f'{node}/{hostname} is reachable')
except subprocess.CalledProcessError:
reach_queue.put(f'{node}/{hostname} is unreachable')
# Check Tailscale nodes
def check_tailscale_nodes():
global nodes, hostnames, threads
try:
nodes_output = subprocess.check_output("tailscale status | grep '100'", shell=True).decode().strip()
nodes = [line.split()[0] for line in nodes_output.split('\n') if line]
hostnames = [line.split()[1] for line in nodes_output.split('\n') if line]
for node, hostname in zip(nodes, hostnames):
thread = threading.Thread(target=ping_node, args=(node, hostname))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
reach = []
while not reach_queue.empty():
reach.append(reach_queue.get())
return reach
except subprocess.CalledProcessError as e:
logging.error(f"Error checking Tailscale status: {e}")
return ['Error checking Tailscale status']
# Function to restart a service
def restart_service(service):
logging.info(f'Restarting {service}...')
try:
subprocess.run(['sudo', 'systemctl', 'restart', service], check=True)
sleep(3)
service_status = get_service_status(service)
status_message = f'{service} restarted! Status: {service_status}'
logging.info(status_message)
return status_message
except subprocess.CalledProcessError as e:
logging.error(f"Error restarting {service}: {e}")
return f'Error restarting {service}'
# Restart services menu
def restart_menu():
keyboard = [
[telebot.types.InlineKeyboardButton('zerotier-one', callback_data='zerotier-one')],
[telebot.types.InlineKeyboardButton('prosody', callback_data='prosody')],
[telebot.types.InlineKeyboardButton('postgresql', callback_data='postgresql')],
[telebot.types.InlineKeyboardButton('tailscaled', callback_data='tailscaled')],
[telebot.types.InlineKeyboardButton('nginx', callback_data='nginx')],
[telebot.types.InlineKeyboardButton('cancel', callback_data='cancel')]
]
reply_markup = telebot.types.InlineKeyboardMarkup(keyboard)
return reply_markup
# Callback query handler for service restart
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
service = call.data
if service != 'cancel':
status_message = restart_service(service)
bot.send_message(call.message.chat.id, status_message)
else:
ping = 'unreachable'
return ping
bot.edit_message_reply_markup(call.message.chat.id, call.message.message_id, reply_markup=None)
bot.send_message(call.message.chat.id, 'Canceled')
#debug handler
def check():
global status, hostname, uptime, zerotier, prosody, postgres, tailscale, disk
getinfo()
print('system status:', status)
print('hostname:', hostname)
print('uptime:', uptime)
print('zerotier:', zerotier)
print('prosody:', prosody)
print('postgres:', postgres)
print('tailscale:', tailscale)
print('disk:', disk)
return status, hostname, uptime, zerotier, prosody, postgres, tailscale, disk
# Reboot system function
def reboot():
logging.info('Rebooting system...')
subprocess.run(['sudo', 'reboot'], check=True)
#message handling
@bot.message_handler(commands=['start', 'help', 'status', 'reboot', 'ping'])
# Populate teh variables on first start
get_system_info()
# Message handlers
@bot.message_handler(commands=['start', 'help', 'status', 'restart', 'reboot', 'ping'])
def handle(message):
if message.text == '/start':
bot.reply_to(message, 'lainmonitor v1.0 --- standing by...')
elif message.text == '/help':
bot.reply_to(message, 'commands: /start, /help, /status, /reboot, /ping')
elif message.text == '/status':
check()
status_message = f'hostname: {hostname}\nsystem status: {status}\nuptime: {uptime}\nzerotier: {zerotier}\nprosody: {prosody}\npostgres: {postgres}\ntailscale: {tailscale}'
bot.reply_to(message, status_message)
bot.reply_to(message, f'filesystem info for {hostname}: \n\n{disk}')
elif message.text == '/reboot':
bot.reply_to(message, 'work in progress...')
elif message.text == '/ping':
check_tailscale()
bot.reply_to(message, f'ping status: {ping}')
user_id = str(message.from_user.id)
if user_id not in authorized_users:
bot.reply_to(message, 'You are not authorized for this action')
else:
if message.text == '/start':
bot.reply_to(message, 'lainmonitor v1.2 --- standing by...')
elif message.text == '/help':
bot.reply_to(message, 'commands: /start, /help, /status, /restart, /reboot, /ping')
bot.reply_to(message, 'commands: /start, /help, /status, /restart, /reboot, /ping')
elif message.text == '/status':
get_system_info()
status_message = (
f'hostname: {hostname}\n'
f'system status: {status}\n'
f'uptime: {uptime}\n'
f'zerotier: {zerotier}\n'
f'prosody: {prosody}\n'
f'postgres: {postgres}\n'
f'tailscale: {tailscale}\n'
f'nginx: {nginx}'
)
bot.reply_to(message, status_message)
bot.reply_to(message, f'Filesystem info for {hostname}:\n\n{disk}')
elif message.text == f'/restart {hostname}':
bot.send_message(message.chat.id, 'Select a service to restart:', reply_markup=restart_menu())
elif message.text == f'/reboot {hostname}':
bot.reply_to(message, f'Rebooting {hostname}...')
reboot()
elif message.text == '/ping':
reach = check_tailscale_nodes()
bot.reply_to(message, f'Ping status:\n\n{"\n".join(reach)}')
else:
pass
# Polling with timeout and error handling
try:
bot.polling(none_stop=True, timeout=60, long_polling_timeout=60)
except Exception as e:
logging.error(f'Polling error: {e}')
#polling
bot.polling()