From fdc1d797cdf3ebcac11df911c6ea270a96f384f8 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 28 Oct 2019 17:27:07 +0100 Subject: Add mem module --- modules/mem.inc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 modules/mem.inc diff --git a/modules/mem.inc b/modules/mem.inc new file mode 100644 index 0000000..9a94a17 --- /dev/null +++ b/modules/mem.inc @@ -0,0 +1,25 @@ +#!/bin/ash + +# Get everything from /proc/meminfo suitable for eval to import into environment +# (Filters a few values due to invalid chars) +# With no arguments, returns everything, otherwise only those given as arguments +mem_env() { + local a e + if [ "$#" -eq 0 ]; then + e='[a-zA-Z0-9]+' + else + # Set empty first in case a value is missing + # TODO Within awk script (collect, check, then output) + for a in "$@"; do + echo "$a=" + done + e="(${*// /|})" + fi + awk '{a=substr($1, 1, length($1)-1); if (a ~ /^'"$e"'$/ && $2 ~ /^[0-9]+$/) print a "=" $2}' "/proc/meminfo" +} + +# Get a single entry from /proc/meminfo +mem_get() { + awk '{if ($1 == "'"$1"':") { print $2; exit; }}' "/proc/meminfo" +} + -- cgit v1.2.3-55-g7522