Automatic monitoring system for IPTV channels that detects status changes (ONLINE/OFFLINE) and sends email alerts.
- β Automatic monitoring every 2 seconds
- π§ Immediate email alerts
- π Real-time statistics (console output)
- π Bidirectional status change detection (ONLINE β OFFLINE)
- π‘οΈ systemd service for automatic execution
- π System logs and debugging
- π¨ Immediate detection of down channels on first run
- β Notification when channels are restored (ONLINE)
- Ubuntu 20.04 LTS
- PHP 7.4 or higher
- MySQL/MariaDB
- Composer (for PHP dependencies)
- Root permissions to install the service
/var/www/html/IPTV/
βββ canales_correo_mejorado.php # Main monitoring script (recommended version)
βββ monitor_canales.sh # Automatic execution script
βββ test_monitor.sh # Basic test script
βββ test_cambios.sh # Bidirectional test script
βββ iptv-monitor.service # systemd service configuration
βββ instalar_monitor.sh # Installation script
βββ composer.json # PHP dependencies
βββ composer.lock # PHP dependencies (lock)
βββ vendor/ # PHP libraries (PHPMailer)
βββ README.md # Documentation in English
βββ README ES.md # Documentation in Spanish
βββ (generated files: estado_canales.json, alertas_enviadas.json, monitor_log.txt)
# Update the operating system and packages
sudo apt update && sudo apt upgrade -y
# Install PHP and the required extensions for monitoring and sending emails
sudo apt install php php-mysql php-curl php-json php-mbstring -y
# Download and install Composer (PHP dependency manager)
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Open the file canales_correo_mejorado.php
and edit the following lines with your real data:
// Database configuration
$host = 'your-db-host'; // Database server address/IP
$port = 'your-db-port'; // Database port (default MySQL: 3306)
$dbname = 'your-db-name'; // Database name
$username = 'your-db-user'; // Database user
$password = 'your-db-password'; // Database password
// Email configuration
$correo_origen = 'your-email@example.com'; // Sender email (origin)
$correo_destino = 'destination-email@example.com'; // Recipient email (alerts)
$nombre_remitente = 'π‘ Your system name'; // Name that will appear as sender
$token_aplicacion = 'your-app-token'; // Application token (Gmail)
Note: Never upload your real data to a public repository.
# Enter the project directory
cd /var/www/html/IPTV/
# Install PHP dependencies (PHPMailer and others)
composer install
# Assign the www-data user and group to the directory (recommended for web servers)
sudo chown -R www-data:www-data /var/www/html/IPTV/
# Give execution permissions to the main monitoring script
sudo chmod +x /var/www/html/IPTV/monitor_canales.sh
# Run the installation script to register the systemd service
sudo bash /var/www/html/IPTV/instalar_monitor.sh
# Start the monitoring service
sudo systemctl start iptv-monitor
# Check the service status
sudo systemctl status iptv-monitor
# Enable the service to start automatically with the system
sudo systemctl enable iptv-monitor
# Run monitoring manually in the console
cd /var/www/html/IPTV/
./monitor_canales.sh
# Run the PHP monitoring script once (useful for testing)
cd /var/www/html/IPTV/
php canales_correo_mejorado.php
# Test immediate detection of down channels
chmod +x test_monitor.sh
./test_monitor.sh
# Test bidirectional detection (ONLINE β OFFLINE)
chmod +x test_cambios.sh
./test_cambios.sh
# View service logs in real time
sudo journalctl -u iptv-monitor -f
# View the last 100 lines of the log
sudo journalctl -u iptv-monitor -n 100
# Check the service status
sudo systemctl status iptv-monitor
- Email: Edit the variables in
canales_correo_mejorado.php
to set sender, recipient, and token. - Monitoring interval: Edit the
sleep
value inmonitor_canales.sh
(default: 2 seconds) to adjust the check frequency.
# Start the monitoring service
sudo systemctl start iptv-monitor
# Stop the service
sudo systemctl stop iptv-monitor
# Restart the service
sudo systemctl restart iptv-monitor
# View the service status
sudo systemctl status iptv-monitor
# Enable automatic start
sudo systemctl enable iptv-monitor
# Disable automatic start
sudo systemctl disable iptv-monitor
# View logs in real time
sudo journalctl -u iptv-monitor -f
# Check systemd logs for errors
sudo journalctl -u iptv-monitor -n 50
# Check permissions of the monitoring script
ls -la /var/www/html/IPTV/monitor_canales.sh
# Check that PHP is installed correctly
php --version
# Test the connection to the database manually
mysql -h your-db-host -P your-db-port -u your-db-user -p your-db-name
# example: mysql -h 192.168.18.1 -P 3306 -u user_iptv -p xtream_iptv
# Check that the port is open
# (useful for diagnosing network/firewall issues)
telnet your-db-host your-db-port
# example: telnet 192.168.18.1 3306
# Fix directory and script permissions
sudo chown -R www-data:www-data /var/www/html/IPTV/
sudo chmod +x /var/www/html/IPTV/monitor_canales.sh
# Check logs to identify the error
sudo journalctl -u iptv-monitor -f
# Check that the PHP file has no syntax errors
php -l /var/www/html/IPTV/canales_correo_mejorado.php
- Enable two-step verification on your Gmail account
- Generate an application password
- Use that password in the
$token_aplicacion
variable in the PHP script
- The script runs as the
www-data
user. - Logs are saved in Ubuntu's log system.
- The service restarts automatically if it fails.
- Status files are saved with appropriate permissions.
- Check the logs:
sudo journalctl -u iptv-monitor -f
- Verify the database connection
- Make sure PHP and extensions are installed
- Check file permissions
- The file
estado_canales.json
is created automatically - Emails are sent immediately when there are status changes
- The service restarts automatically if it fails
- Logs are rotated automatically by systemd
- Bidirectional detection: Detects both channels that go down (OFFLINE) and those that come back up (ONLINE)
- SMTP timeout reduced to 10 seconds for faster response
- Every 2 seconds: The database is queried and compared with the previous JSON
- Automatic update: The JSON file is updated on each run
Developed for CyberCode Labs π‘