Skip to content

Commit

Permalink
feat(docker): add some metrics abount docker
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smhagit committed May 2, 2021
1 parent c878bc4 commit 9080be8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ISP-RPi-mqtt-daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -612,6 +649,12 @@ def getLastInstallDate():
getLinuxVersion()
getFileSystemDrives()

if is_docker_installed:
getDockerMetrics()




# -----------------------------------------------------------------------------
# timer and timer funcs for ALIVE MQTT Notices handling
# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9080be8

Please sign in to comment.