#!/bin/bash OUTPUT="/var/lib/node_exporter/textfile_collector/shm_usage.prom" TMP=$(mktemp) echo "# HELP container_shm_usage_bytes Shared memory usage in bytes" > $TMP echo "# TYPE container_shm_usage_bytes gauge" >> $TMP echo "# HELP container_shm_size_bytes Shared memory total size in bytes" >> $TMP echo "# TYPE container_shm_size_bytes gauge" >> $TMP while IFS= read -r line; do pid=$(echo "$line" | awk '{print $1}') container=$(echo "$line" | awk '{print $2}' | tr -d '/') if [ "$pid" = "0" ] || [ -z "$pid" ]; then continue; fi df_out=$(nsenter -t "$pid" -m df -B1 /dev/shm 2>/dev/null | tail -1) total=$(echo "$df_out" | awk '{print $2}') used=$(echo "$df_out" | awk '{print $3}') if [ -n "$used" ] && [ -n "$total" ]; then echo "container_shm_usage_bytes{name=\"$container\"} $used" >> $TMP echo "container_shm_size_bytes{name=\"$container\"} $total" >> $TMP fi done < <(docker ps -q | xargs docker inspect --format '{{.State.Pid}} {{.Name}}' 2>/dev/null) mv $TMP $OUTPUT chmod 644 $OUTPUT