Mender client inventory bash inside bash question

Hello everyone,
i’m looking into adding more attributes to the mender client inventory, to keep an eye that the necessary devices are connected.

i’m encountering that i am unable to use a bash inside a bash to generate my attributes, let me give an example

last line will ask bash to execute another script ‘keyboardconnect’

#!/bin/bash
set -ue
LC_ALL=C
export LC_ALL
grep ‘model name’ /proc/cpuinfo | uniq | awk -F’: ’ ’
// { printf(“cpu_model=%s\n”, $2);}

echo “kernel=$(cat /proc/version)”

cat /proc/meminfo | awk ’
/MemTotal/ {printf(“mem_total_kB=%d\n”, $2)}

echo “hostname=$(cat /etc/hostname)”

echo “keyboard-connected=$(./keyboardconnect)”

content keyboardconnect

#!/bin/bash
if [[ $(lsusb | grep 03f0:034a) ]]; then
echo “connected”
else
echo “not connected”
fi

this generates:

root@localhost:/usr/share/mender/inventory# ./mender-inventory-hostinfo
cpu_model=ARMv7 Processor rev 3 (v7l)
kernel=Linux version 4.19.75-v7l+ (dom@buildbot) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g84
60611)) #1270 SMP Tue Sep 24 18:51:41 BST 2019
mem_total_kB=3999784
hostname=localhost
keyboard-connected=connected

yet the attribute on mender stays empty
empty

but when keeping the keyboard script locally

#!/bin/bash
set -ue
keyboard1=“error”
if [[ $(lsusb|grep 03f0:034a) ]]; then
keyboard1=“connected”
else
keyboard1=“not connected”
fi

LC_ALL=C
export LC_ALL
grep ‘model name’ /proc/cpuinfo | uniq | awk -F’: ’ ’
// { printf(“cpu_model=%s\n”, $2);}

echo “kernel=$(cat /proc/version)”

cat /proc/meminfo | awk ’
/MemTotal/ {printf(“mem_total_kB=%d\n”, $2)}

echo “hostname=$(cat /etc/hostname)”

echo “keyboard-connected=${keyboard1}”

is giving the same output in terminal, but this one does work, could someone explain why this is the case?

Greets and thanks, Gene

Hi Gene,

I’m not certain but I have 2 suggestions.

  1. Your “cat /etc/hostname” bit is missing a “$”. It may work like this but I’m not sure what the side effects are.
  2. Just on a hunch, can you change the key value to be “keyboard_connected”? I’m not sure if I’ve ever tried an inventory key with a ‘-’ in the name.

Drew

hello @drewmoseley thank you for the reply

i tried the “-” and this worked without a problem.

my scripts by this time changed quite a with more succes, but i still could use you advice on this.

mender-inventory-cardreader which is a simple grep on lsusb of an device ID

#!/bin/bash
if [[ $(lsusb | grep 03f0:034a) ]]; then
	echo "cardreader=connected"
else
	echo "cardreader=not-connected"
fi

and this works fine, now i made another script where, only the ID of the device has been detected once, it wil give it status in the future (to prevent of giving an inventory of devices not meant for that mender client)

mender-inventory-printer ↓

#!/bin/bash
source mender-inventory-variables
echo "mender-inventory-variables=$active_printerM2"
#to test if importing variable works

if [[ "$active_printer" = "0"  ]];then
	if [[ $(lsusb | grep 04b8:0e15) ]]; then
	new_active_printer="1"
	sed -r -i.bak "s/active_printer=([[:graph:]]+)/active_printer=$new_active_printer/" mender-inventory-variables
#set variable active_keyboard to 1 in mender-inventory-variables
	source mender-inventory-variables
#reload the variables from mender-inventory-variables
	fi
fi

if [[ "$active_printerM2" = "1" ]]; then	
	if [[ $(lsusb | grep 04b8:0e15) ]]; then
		echo "Printer=connected"
	else
		echo "Printer=not_connected"
	fi
fi

when running this script manually it gives an normal output
(mender-inventory-variables=1 is just to test the IF)
but this script does not work/ give feedback to mender inventory, and is actually the original problem.

root@localhost:/usr/share/mender/inventory# ./mender-inventory-printer
mender-inventory-variables=1
Printer=connected

both scripts give the ‘attribute=value’ as described in the doc’s

Thanks for the help, Gene

I can’t answer why it works when you run it manually but I think the culprit is the line:

	if [[ $(lsusb | grep 04b8:0e15) ]]; then

That seems like it should be

    if lsusb | grep -q 04b8:0e15; then

or

   if [[ -z "$(lsusb | grep 04b8:0e15)" ]]; then

Drew

The problem is using of relative path names to invoke the scripts from within the script. Try using the full path name for the script instead. For example:

echo “keyboard-connected=$(./keyboardconnect)”

should be

echo “keyboard-connected=$(/full/path/to/location/of/keyboardconnect)”

hello @madisox @drewmoseley,

it seemed that the use of ‘source’ the use of AND (&&) operant made the right ‘output’ terminal wise, but not for mender. the workaround i ended doing is

if  [[ $(lsusb | grep 04b8:0e28 && grep active_printer_model3=1 mender-inventory-variables) ]]; then	
	echo "Printer=connected"
else
	echo "Printer=not_connected"
fi

if  [[ $(lsusb | grep 04b8:0e28 && grep active_printer_model3=0 mender-inventory-variables) ]]; then	
	active_printer_model3="1"
	sed -r -i.bak "s/active_printer_model3=([[:graph:]]+)/active_printer_model3=$new_active_printer_model3/" /usr/share/mender/inventory/mender-inventory-variables
fi

the use off full path (to change a variable) was also a solution
Thanks Gene