Implementing Supervisor Process Monitoring with Open Telemetry

Supervisor Process Monitoring with Open Telemetry

In this blog, I’ll Walk you through how I set up a custom monitoring system for Supervisor-managed processes like Nginx and Apache2, this setup will allow you to track the health and performance of processes running under Supervisor in real time. 

What Is Supervisor?  

Supervisor is a process control system that allows users to manage and monitor numerous processes. 

Step 1 Install Supervisor 

  • you can install it using this command: 
sudo apt update 
sudo apt install supervisor

Step 2 Configure Supervisor for Nginx and Apache 

  • Once Supervisor is installed, you’ll need to create configuration files for Nginx and Apache. 

For  Nginx 

  • Create a file in this path in this format   /etc/supervisor/conf.d/nginx.conf/. 
One-line Summary for NGINX (Supervisor Configuration) 
    • This Supervisor configuration enables effective management of the NGINX service by running it in the foreground, ensuring automatic startup, handling unexpected failures through automatic restarts, and directing logs to specified output and error files.
[program:nginx]
command=/usr/sbin/nginx -g "daemon off;" 
autostart=true 
autorestart=true 
stderr_logfile=/var/log/nginx.err.log 
stdout_logfile=/var/log/nginx.out.log

For Apache 

  • Create another file on this path in this format /etc/supervisor/conf.d/apache2.conf. 
One-line Summary for Apache (Supervisor Configuration): 
  • This Supervisor configuration enables effective management of the Apache service by running it in the foreground, ensuring automatic startup, handling failures through automatic restarts, and storing standard output and error logs in specified files. 
[program:apache2]
command=/usr/sbin/apachectl -D FOREGROUND
autostart=true
autorestart=true
stderr_logfile=/var/log/apache2.err.log
stdout_logfile=/var/log/apache2.out.log

Step 3 Reload Supervisor and Start Services  

      • Once the configurations are created, updated, we need to reload Supervisor to apply these changes and start both services. 
sudo supervisorctl reread 
sudo supervisorctl update 
sudo supervisorctl start nginx 
sudo supervisorctl start apache2 

Step 4 Check the Status of Nginx and Apache. 

      • Now check if the Supervisor is managing Nginx and Apache or not. If it does you will see the output like below. 
sudo supervisorctl status 
      • You should see output like:
nginx     RUNNING   pid  1234, uptime  0:01:10apache2   RUNNING   pid  5678, uptime  0:01:10

Step 5 Create a Python Script for Monitoring with Open Telemetry and Prometheus. 

      • I have created a Python script to gather performance metrics of the processes under Supervisor. 
What does this Script do? 
      1. Monitors the status of nginx and apache2 processes. 
      2. Collects metrics like CPU usage, memory usage, uptime, and the number of threads. 
      3. Exposes the data in Prometheus-compatible format at http://{server_ip}:9876/metrics. 
      4. This script automatically monitors critical processes (like nginx, apache2) using Open Telemetry. 
      5. It collects real-time metrics such as: 
Python Script to Export Supervisor Metrics: 

I’ve developed a Python script that uses Open Telemetry to collect custom metrics from a supervisor-managed application, including: 

      • Process status (up/down) 
      • Uptime in seconds 
      • CPU usage in percent 

These metrics are exposed over an HTTP endpoint in a Prometheus-compatible format. 

You can find the complete script hosted on GitHub: 

https://github.com/kumaribhanu/supervisor-python-script

Step 6 Install Required Libraries for Python Script. 

      • Now to run the script, you’ll need to install a few Python libraries. 
pip install opentelemetry-sdk opentelemetry-exporter-prometheus prometheus-client psutil 

Step 7 Run the Python Script 

      • Once the libraries are installed, run the script. 
python3 test2.py 

Step 8 Access Prometheus Metrics 

      • In your browser to view the live metrics of your Nginx and Apache processes. 
      • open the following URL in your browser. 

http://localhost:9001/metrics .

      • Replace <localhost> with the actual server IP or domain name. 
      • You should see output like 
supervisord_process_up{process_name="nginx"} 1
supervisord_process_cpu_percent{process_name="apache2"} 4.5

Step 9: Visualize Supervisor Metrics in Grafana 

Here are some recommended metrics to visualize in Grafana, along with their explanation and benefits. 

Metric Name Description Why Monitor It?
supervisord_process_up Indicates whether the process is currently running (1) or not (0). Helps quickly detect if a critical process has stopped or crashed.
supervisord_process_uptime_seconds Shows how long the process has been running in seconds. Useful to track process restarts or unexpected downtime.
supervisord_process_cpu_percent Displays the CPU usage percentage of the process. Detects performance bottlenecks or abnormal CPU consumption.
supervisord_process_memory_rss_bytes Tracks the Resident Set Size (actual memory used in RAM) by the process. Helps identify memory leaks or inefficient memory usage.
supervisord_process_threads Shows the number of active threads within the process. Useful to monitor threading issues or unexpected thread growth.

Conclusion

This Blog helps us to create a comprehensive monitoring system for Supervisor-managed processes like Nginx and Apache2 using Open Telemetry and Prometheus. By visualizing key metrics in Grafana, you can easily track the health and performance of your services in real time. This proactive monitoring helps identify and address issues such as high CPU usage, memory leaks, or downtime, ensuring that your system remains reliable and performs optimally. 

 

Author: Bhanu Kumari

I’m Bhanu Kumari, a DevOps Engineer at Opstree Solutions, where I specialize in automating infrastructure and streamlining deployment pipelines to enhance software delivery processes. With experience in tools like Jenkins, Docker, Kubernetes, and Terraform, I focus on building scalable and reliable systems that support rapid development cycles. I am passionate about bridging the gap between development and operations teams, ensuring seamless integration and continuous delivery.

Leave a Reply