├── README.md ├── README_cn.md ├── README_fa.md ├── check_traffic.py └── config.yml /README.md: -------------------------------------------------------------------------------- 1 | # VM Traffic Monitor 2 | 3 | [**简体中文**](README_cn.md) | [**فارسی**](README_fa.md) 4 | 5 | This script monitors the remaining traffic of VMs on Vultr, DigitalOcean, and Linode. If the remaining traffic is zero, it shuts down the VM and sends a notification via Telegram. 6 | 7 | ## Features 8 | 9 | - Monitors VM traffic on Vultr, DigitalOcean, and Linode. 10 | - Shuts down VM when traffic is depleted. 11 | - Sends notification via Telegram. 12 | 13 | ## Setup 14 | 15 | 1. Clone the repository 16 | ```sh 17 | git clone https://github.com/yourusername/vm-traffic-monitor.git 18 | 19 | cd vm-traffic-monitor 20 | ``` 21 | 22 | 2. Install dependencies 23 | ```sh 24 | pip install requests pyyaml python-telegram-bot 25 | ``` 26 | 27 | 3. Configure the config.yaml 28 | 29 | 4. Add execution permissions to the script 30 | ```sh 31 | chmod +x check_traffic.py 32 | ``` 33 | 34 | 5. Set up a cron job to run the script periodically 35 | ```sh 36 | crontab -e 37 | ``` 38 | Add the following line to run the script every hour: 39 | ```sh 40 | 0 * * * * /usr/bin/python3 /path/to/check_traffic.py >> /path/to/check_traffic.log 2>&1 41 | 42 | ``` 43 | 44 | ## Running as a Daemon(Optional) 45 | To run the script as a daemon, you can use systemd on Linux: 46 | 47 | 1. Create a systemd service file 48 | ```sh 49 | sudo nano /etc/systemd/system/vm-traffic-monitor.service 50 | ``` 51 | 2. Add the following content to the service file 52 | ```ini 53 | [Unit] 54 | Description=VM Traffic Monitor 55 | After=network.target 56 | 57 | [Service] 58 | ExecStart=/usr/bin/python3 /path/to/check_traffic.py 59 | WorkingDirectory=/path/to 60 | StandardOutput=file:/var/log/vm-traffic-monitor.log 61 | StandardError=file:/var/log/vm-traffic-monitor.log 62 | Restart=always 63 | 64 | [Install] 65 | WantedBy=multi-user.target 66 | 67 | ``` 68 | 3. Reload systemd and start the service 69 | ```sh 70 | sudo systemctl daemon-reload 71 | sudo systemctl start vm-traffic-monitor 72 | sudo systemctl enable vm-traffic-monitor 73 | ``` 74 | -------------------------------------------------------------------------------- /README_cn.md: -------------------------------------------------------------------------------- 1 | # VM Traffic Monitor 2 | 3 | 此脚本可以监控Vultr、DigitalOcean和Linode上的VPS剩余流量。如果剩余流量为零,它会关机VPS并通过Telegram发送通知。 4 | 5 | 功能 6 | 7 | - 监控Vultr、DigitalOcean和Linode上的VPS流量。 8 | - 流量耗尽时关闭VPS。 9 | - 通过Telegram发送通知。 10 | 11 | 设置 12 | 13 | 1. 克隆仓库 14 | ```sh 15 | git clone https://github.com/yourusername/vm-traffic-monitor.git 16 | 17 | cd vm-traffic-monitor 18 | ``` 19 | 20 | 2. 安装依赖 21 | ```sh 22 | pip install requests pyyaml python-telegram-bot 23 | ``` 24 | 25 | 3. 配置config.yaml 26 | 27 | 4. 添加脚本执行权限 28 | ```sh 29 | chmod +x check_traffic.py 30 | ``` 31 | 32 | 5. 设置定时任务定期运行脚本 33 | ```sh 34 | crontab -e 35 | ``` 36 | 添加以下行以每小时运行一次脚本: 37 | ```sh 38 | 0 * * * * /usr/bin/python3 /path/to/check_traffic.py >> /path/to/check_traffic.log 2>&1 39 | 40 | ``` 41 | 42 | ## 作为守护进程运行(可选) 43 | 44 | 要将脚本作为守护进程运行,可以在Linux上使用systemd: 45 | 46 | 1. 创建一个systemd服务文件 47 | ```sh 48 | sudo nano /etc/systemd/system/vm-traffic-monitor.service 49 | ``` 50 | 51 | 2. 添加以下内容到服务文件 52 | ```ini 53 | [Unit] 54 | Description=VM Traffic Monitor 55 | After=network.target 56 | 57 | [Service] 58 | ExecStart=/usr/bin/python3 /path/to/check_traffic.py 59 | WorkingDirectory=/path/to 60 | StandardOutput=file:/var/log/vm-traffic-monitor.log 61 | StandardError=file:/var/log/vm-traffic-monitor.log 62 | Restart=always 63 | 64 | [Install] 65 | WantedBy=multi-user.target 66 | 67 | ``` 68 | 69 | 3. 重新加载systemd并启动服务 70 | ```sh 71 | sudo systemctl daemon-reload 72 | sudo systemctl start vm-traffic-monitor 73 | sudo systemctl enable vm-traffic-monitor 74 | ``` -------------------------------------------------------------------------------- /README_fa.md: -------------------------------------------------------------------------------- 1 | ## VM Traffic Monitor 2 | 3 | VPS ترافیک مانیتور 4 | 5 | این اسکریپت ترافیک باقی مانده VPS در Vultr، DigitalOcean و Linode را نظارت می‌کند. اگر ترافیک باقی مانده صفر باشد، VM را خاموش کرده و از طریق تلگرام اعلان می‌فرستد. 6 | 7 | ویژگی‌ها 8 | 9 | - نظارت بر ترافیک VPS در Vultr، DigitalOcean و Linode. 10 | - خاموش کردن VPS هنگامی که ترافیک تمام می‌شود. 11 | - ارسال اعلان از طریق تلگرام. 12 | 13 | تنظیمات 14 | 15 | 1. کلون کردن مخزن 16 | ```sh 17 | git clone https://github.com/yourusername/vm-traffic-monitor.git 18 | 19 | cd vm-traffic-monitor 20 | ``` 21 | 22 | 2. نصب وابستگی‌ها 23 | ```sh 24 | pip install requests pyyaml python-telegram-bot 25 | ``` 26 | 27 | 3. پیکربندی config.yaml 28 | 29 | 4. افزودن مجوز اجرا به اسکریپت 30 | ```sh 31 | chmod +x check_traffic.py 32 | ``` 33 | 34 | 5. تنظیم یک کرون جاب برای اجرای دوره‌ای اسکریپت 35 | ```sh 36 | crontab -e 37 | ``` 38 | اضافه کردن خط زیر برای اجرای اسکریپت هر ساعت: 39 | ```sh 40 | 0 * * * * /usr/bin/python3 /path/to/check_traffic.py >> /path/to/check_traffic.log 2>&1 41 | 42 | ``` 43 | 44 | ## اجرای به عنوان یک دیمن (اختیاری) 45 | 46 | برای اجرای اسکریپت به عنوان یک دیمن، می‌توانید از systemd در لینوکس استفاده کنید: 47 | 48 | 1. ایجاد یک فایل سرویس systemd 49 | ```sh 50 | sudo nano /etc/systemd/system/vm-traffic-monitor.service 51 | ``` 52 | 53 | 2. افزودن محتوای زیر به فایل سرویس 54 | ```ini 55 | [Unit] 56 | Description=VM Traffic Monitor 57 | After=network.target 58 | 59 | [Service] 60 | ExecStart=/usr/bin/python3 /path/to/check_traffic.py 61 | WorkingDirectory=/path/to 62 | StandardOutput=file:/var/log/vm-traffic-monitor.log 63 | StandardError=file:/var/log/vm-traffic-monitor.log 64 | Restart=always 65 | 66 | [Install] 67 | WantedBy=multi-user.target 68 | 69 | ``` 70 | 71 | 3. بارگذاری مجدد systemd و شروع سرویس 72 | ```sh 73 | sudo systemctl daemon-reload 74 | sudo systemctl start vm-traffic-monitor 75 | sudo systemctl enable vm-traffic-monitor 76 | ``` -------------------------------------------------------------------------------- /check_traffic.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import telegram 3 | import yaml 4 | 5 | with open("config.yaml", 'r') as config_file: 6 | config = yaml.safe_load(config_file) 7 | 8 | API_KEYS = config['api_keys'] 9 | TELEGRAM_TOKEN = config['telegram']['token'] 10 | TELEGRAM_CHAT_ID = config['telegram']['chat_id'] 11 | API_URLS = config['api_urls'] 12 | INSTANCE_IDS = config['instance_ids'] 13 | 14 | def check_traffic_and_shutdown(): 15 | for provider, api_key in API_KEYS.items(): 16 | headers = { 17 | 'Authorization': f'Bearer {api_key}', 18 | 'Content-Type': 'application/json' 19 | } 20 | response = requests.get(API_URLS[provider], headers=headers) 21 | if response.status_code == 200: 22 | data = response.json() 23 | remaining_traffic = parse_traffic_data(provider, data) 24 | if remaining_traffic <= 0: 25 | for instance_id in INSTANCE_IDS[provider]: 26 | shutdown_vm(provider, api_key, instance_id) 27 | send_notification(provider) 28 | else: 29 | print(f"Failed to get data from {provider}: {response.status_code}") 30 | 31 | def parse_traffic_data(provider, data): 32 | if provider == 'vultr': 33 | return data['account']['balance'] 34 | elif provider == 'digital_ocean': 35 | return data['droplets'][0]['disk'] 36 | elif provider == 'linode': 37 | return data['data']['transfer_pool']['quota'] 38 | return 0 39 | 40 | def shutdown_vm(provider, api_key, instance_id): 41 | if provider == 'vultr': 42 | shutdown_url = f'https://api.vultr.com/v2/instances/{instance_id}/halt' 43 | elif provider == 'digital_ocean': 44 | shutdown_url = f'https://api.digitalocean.com/v2/droplets/{instance_id}/actions' 45 | elif provider == 'linode': 46 | shutdown_url = f'https://api.linode.com/v4/linode/instances/{instance_id}/shutdown' 47 | 48 | headers = { 49 | 'Authorization': f'Bearer {api_key}', 50 | 'Content-Type': 'application/json' 51 | } 52 | response = requests.post(shutdown_url, headers=headers) 53 | if response.status_code in [200, 202, 204]: 54 | print(f"{provider} VM {instance_id} shut down successfully.") 55 | else: 56 | print(f"Failed to shut down {provider} VM {instance_id}: {response.status_code}") 57 | 58 | def send_notification(provider): 59 | send_telegram(provider) 60 | 61 | def send_telegram(provider): 62 | bot = telegram.Bot(token=TELEGRAM_TOKEN) 63 | bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=f"The {provider} VMs have been shut down due to no remaining traffic.") 64 | 65 | if __name__ == "__main__": 66 | check_traffic_and_shutdown() 67 | -------------------------------------------------------------------------------- /config.yml: -------------------------------------------------------------------------------- 1 | api_keys: 2 | vultr: 'your_vultr_api_key' 3 | digital_ocean: 'your_digital_ocean_api_key' 4 | linode: 'your_linode_api_key' 5 | 6 | telegram: 7 | token: 'your_telegram_bot_token' 8 | chat_id: 'your_telegram_chat_id' 9 | 10 | api_urls: 11 | vultr: 'https://api.vultr.com/v2/account' 12 | digital_ocean: 'https://api.digitalocean.com/v2/droplets' 13 | linode: 'https://api.linode.com/v4/account/settings' 14 | 15 | instance_ids: 16 | vultr: ['instance_id_1', 'instance_id_2'] 17 | digital_ocean: ['instance_id_1', 'instance_id_2'] 18 | linode: ['instance_id_1', 'instance_id_2'] 19 | --------------------------------------------------------------------------------