From 9080be874931ba82888e2f3f90dcdc3bfd92f778 Mon Sep 17 00:00:00 2001 From: smhagit Date: Sun, 2 May 2021 13:48:42 +0200 Subject: [PATCH] feat(docker): add some metrics abount docker this will get some data from docker cli and publish rpi_docker_container_running_count, rpi_docker_container_total_count, rpi_docker_network_total_count --- ISP-RPi-mqtt-daemon.py | 53 ++++++++++++++++++++++++++++++++++++++++++ README.md | 9 +++++++ 2 files changed, 62 insertions(+) diff --git a/ISP-RPi-mqtt-daemon.py b/ISP-RPi-mqtt-daemon.py index d230ac0..67e8ccb 100755 --- a/ISP-RPi-mqtt-daemon.py +++ b/ISP-RPi-mqtt-daemon.py @@ -209,6 +209,43 @@ def on_publish(client, userdata, mid): # Tuple (Hardware, Model Name, NbrCores, BogoMIPS, Serial) rpi_cpu_tuple = '' +is_docker_installed = True # ToDo: check automatically if docker is installed +rpi_docker_container_running_count = '' +rpi_docker_container_total_count = '' +rpi_docker_network_total_count = '' + + +# ----------------------------------------------------------------------------- +# collect some metrics from docker +# +def getDockerMetrics(): + global rpi_docker_container_running_count + global rpi_docker_container_total_count + global rpi_docker_network_total_count + + out = subprocess.Popen("docker ps -q | wc -l", + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdout, _ = out.communicate() + rpi_docker_container_running_count = stdout.decode('utf-8').split("\n")[0] + + out = subprocess.Popen("docker ps -a -q | wc -l", + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdout, _ = out.communicate() + rpi_docker_container_total_count = stdout.decode('utf-8').split("\n")[0] + + out = subprocess.Popen("docker network ls -q | wc -l", + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdout, _ = out.communicate() + rpi_docker_network_total_count = stdout.decode('utf-8').split("\n")[0] + + + # ----------------------------------------------------------------------------- # monitor variable fetch routines # @@ -612,6 +649,12 @@ def getLastInstallDate(): getLinuxVersion() getFileSystemDrives() +if is_docker_installed: + getDockerMetrics() + + + + # ----------------------------------------------------------------------------- # timer and timer funcs for ALIVE MQTT Notices handling # ----------------------------------------------------------------------------- @@ -855,6 +898,11 @@ def isPeriodTimerRunning(): RPI_CPU_BOGOMIPS = "cpu_bogomips" RPI_CPU_CORES = "cpu_number_of_cores" +RPI_DOCKER_CONTAINER_RUNNING_COUNT = "docker_container_running_count" +RPI_DOCKER_CONTAINER_TOTAL_COUNT = "docker_container_total_count" +RPI_DOCKER_NETWORK_TOTAL_COUNT = "docker_network_total_count" + + def send_status(timestamp, nothing): rpiData = OrderedDict() rpiData[SCRIPT_TIMESTAMP] = timestamp.astimezone().replace(microsecond=0).isoformat() @@ -870,6 +918,11 @@ def send_status(timestamp, nothing): rpiData[RPI_LOAD_5M] = float(rpi_load_5m) rpiData[RPI_LOAD_15M] = float(rpi_load_15m) + if is_docker_installed: + rpiData[RPI_DOCKER_CONTAINER_RUNNING_COUNT] = rpi_docker_container_running_count + rpiData[RPI_DOCKER_CONTAINER_TOTAL_COUNT] = rpi_docker_container_total_count + rpiData[RPI_DOCKER_NETWORK_TOTAL_COUNT] = rpi_docker_network_total_count + # DON'T use V1 form of getting date (my dashbord mech) #actualDate = datetime.strptime(rpi_last_update_date, '%y%m%d%H%M%S') #actualDate.replace(tzinfo=local_tz) diff --git a/README.md b/README.md index d9ad5b0..37a5638 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,15 @@ The monitored topic reports the following information: | `cpu` | lists the model of cpu, number of cores, etc. | | `memory` | shows the total amount of RAM in MB and the available ram in MB | | `throttle` | reports the throttle status value plus interpretation thereof | +| `docker_container_running_count` | number of running containers | +| `docker_container_total_count` | total number of containers (even stopped and exited) | +| `docker_network_total_count` | total number of docker networks | + +### Docker integration + +If docker is installed it will be automatically detected. To get data from the docker cli we must provide the RPI Monitor daemon the rights to use the docker cli. +`sudo usermod -aG docker daemon` + ## Prerequisites