Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): add some metrics about docker #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = int(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 = int(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 = int(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