Skip to main content
Skip table of contents

Exporting PIDs of Docker Container

To implement the PIDs in Grafana, we need to deploy Push-gateway that is the utility provided by Prometheus. It is a metrics cache used by individual scripts as a target. A script is run periodically using systemd service and systemd timer to push the certain stats to Pushgateway. Prometheus then collects these metrics from Pushgateway and then we can plot these metrics in Grafana.

This setup is done at the Hybrid Chat Node(s).

Installing Pushgateway

Navigate to Prometheus directory on Hybrid Chat node and vi docker-compose.yml

version: '3.7'
    external: true
    image: prom/pushgateway
    restart: always
      - "9091:9091"
      - chat-solution

save this file and run docker-compose up -d to start the deployment.

Now move back to the /root/ and create a directory named pids by running mkdir /root/pids/ in this directory, create a file named by running nano and give it executable permissions by running chmod +x

Now edit the and paste the following code into this file.

z=$(docker stats --no-stream)
while read -r z
   var=$var$(awk '{print "pids{process=\""$2"\"}", $14z}');
done <<< "$z"
curl -X POST -H  "Content-Type: text/plain" --data "$var
" http://localhost:9091/metrics/job/top/instance/machine

Save this file and exit.

Now we need to execute this script as a Linux service by systemd utility and run it periodically so that we keep on getting updated stats. To do this, create a service file in systemd by running the following command.

vi /etc/systemd/system/pid_extract.service

Now paste the following code into the file we have just created.

Description=Logs system statistics to the systemd journal



save this file and create a new one by running:

vi /etc/systemd/system/pid_extract.timer

Paste the following code into the file.

Description=Logs some system statistics to the systemd journal



Save this file and exit.

Now run the following command to start and enable the service.

systemctl daemon-reload
systemctl start pid_extract.service
systemctl start pid_extract.timer
systemctl enable pid_extract.service
systemctl enable pid_extract.timer

We are now done with the setup at the Hybrid Chat node. Now we need to add the service in Prometheus set up to extract the metrics. 

Navigate to the VM where Prometheus is setup, Go to the directory, /prometheus/prometheus and open the prometheus.yml file. Add the following piece of code to the file after the alertmanager section.

  - job_name: 'pushgateway'
    scrape_interval: 45s
         - targets: ['<ip-address>:9091']

We need to provide the IP address of the Hybrid Chat VM where we have deployed Pushgateway.

Save this file and restart the Prometheus by the following command in the root directory of Prometheus.

docker-compose restart prometheus

That's it. You now have the stats showing up in Prometheus UI as well.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.