Collectd not capturing the stats - Exec Plugin

Cannot see any metrics generated for this top script and i believe its something to do with the variable value handling which is incorrect

types.db
exec_top value1:GAUGE:U:U,value2:GAUGE:U:U


root@OpenWrt:/usr/libexec/collectd# COLLECTD_HOSTNAME=OpenWrt /usr/libexec/collectd/top.sh
PUTVAL "OpenWrt/exec-top/exec_top-grafana" interval=60 N:242.15:0.9
PUTVAL "OpenWrt/exec-top/exec_top-prometheus" interval=60 N:98.16:0.0
PUTVAL "OpenWrt/exec-top/exec_top-dnsmasq" interval=60 N:36.45:0.1
PUTVAL "OpenWrt/exec-top/exec_top-dnsmasq" interval=60 N:32.29:0.0
PUTVAL "OpenWrt/exec-top/exec_top-dnscrypt_proxy" interval=60 N:17.91:0.0

LoadPlugin exec
<Plugin exec>
        Exec "nobody:nogroup" "/usr/libexec/collectd/sqm_collectd.sh" "eth0"
        Exec "nobody:nogroup" "/usr/libexec/collectd/top.sh"
</Plugin>

Any help ? I tried changing the values.db as well still nothing

here is the script


root@OpenWrt:~# tail /overlay/upper/usr/share/collectd/types.db
# (required for the v5 upgrade target)
#
arc_counts              demand_data:COUNTER:0:U, demand_metadata:COUNTER:0:U, prefetch_data:COUNTER:0:U, prefetch_metadata:COUNTER:0:U
arc_l2_bytes            read:COUNTER:0:U, write:COUNTER:0:U
arc_l2_size             value:GAUGE:0:U
arc_ratio               value:GAUGE:0:U
arc_size                current:GAUGE:0:U, target:GAUGE:0:U, minlimit:GAUGE:0:U, maxlimit:GAUGE:0:U
mysql_qcache            hits:COUNTER:0:U, inserts:COUNTER:0:U, not_cached:COUNTER:0:U, lowmem_prunes:COUNTER:0:U, queries_in_cache:GAUGE:0:U
mysql_threads           running:GAUGE:0:U, connected:GAUGE:0:U, cached:GAUGE:0:U, created:COUNTER:0:U
exec-top                memory:GAUGE:0:U,cpu:GAUGE:0:U


root@OpenWrt:~# cat /usr/libexec/collectd/top.sh
#!/bin/sh

HOSTNAME="${COLLECTD_HOSTNAME:-OpenWrt}"
INTERVAL="${COLLECTD_INTERVAL:-15}"
TOP_N="${TOP_N:-5}"

# Validate TOP_N
TOP_N=$(printf "%.0f" "$TOP_N")
[ "$TOP_N" -lt 1 ] && TOP_N=1

while sleep "${INTERVAL%%.*}"; do
    /bin/ps -eo comm,rss,%cpu --sort=-rss | head -n $((TOP_N + 1)) | tail -n +2 | while read -r COMMAND RSS CPU; do
        CLEAN_COMMAND=$(echo "$COMMAND" | sed 's/[^a-zA-Z0-9]/_/g')
        RSS=${RSS//[!0-9]/}
        CPU=${CPU//[!0-9.]/}
        RSS=${RSS:-0}
        CPU=${CPU:-0}

        MEM=$(echo "scale=2; $RSS / 1024" | /usr/bin/bc)
        echo "PUTVAL \"$HOSTNAME/exec-top/${CLEAN_COMMAND}\" interval=$INTERVAL N:${MEM}:${CPU}"
    done
done

Is it intentional that you use hyphen instead of underscore?
Hyphen may either be a delimiter at some point, or is not recognised as a delimiter.

And here you used it mixed:

Ps. Is there a reason why you can't utilise the memory, cpu, processes plugins in the LuCI stats app?

I think exec-top is first layer and then it adds app name

exec-top{exec_top_grafana}

Anyways I can create the metric directly from command line?

Look at the sqm stats creation as the best real-life example.

That is the initial commit, and then are a few ones enhancing it later.

Looking at that, two pieces of advice:

  • Don't double use exec-top. Use different name for the overall routine, and then define variables clearly.
  • You likely need to define much more variables. But that is more uncertain comment as you only have one putval line.

I got it working with this setup

process_memory          value:GAUGE:0:U


#!/bin/sh
HOSTNAME="OpenWrt"
INTERVAL="${COLLECTD_INTERVAL:-15}"
TOP_N=5

# Convert INTERVAL to integer (strip decimals)
INTERVAL_INT="${INTERVAL%%.*}"

while sleep "$INTERVAL_INT"; do
  /bin/ps -eo comm,rss --sort=-rss | awk -v host="$HOSTNAME" -v intvl="$INTERVAL" '
    NR>1 && NR<='$(($TOP_N + 1))' {
      cmd = $1
      gsub(/[^a-zA-Z0-9]/, "_", cmd)
      printf "PUTVAL \"%s/exec-top/memory-%s\" interval=%s N:%.2f\n",
        host, cmd, intvl, $2/1024
    }'
done