A story about how to inventory your OpenWrt embedded devices with simple tools

Some weeks ago we had to inventory a bunch of embedded devices running OpenWrt. For first we looked at available solutions on the Internet like Puppet, collectd and so on but our requirements were so tightened that it was difficult to make those tools working. In fact the devices we had to inventory had very low resources in terms of flash memory space and available memory and we didn’t want to take all of those libraries and overhead for a simple task.

So we started to search a better and smart solution capable of fitting our purposes. The simple tool we could use for that was the shell scripting language, that didn’t require any other library or dependency. In order to be smart, we wanted to use the json format to talk with the api. But, you know, it’s difficult to format a shell output in json.

So, we searched the Internet and we found a nice tool, jq (https://stedolan.github.io/jq/) whose purpose is to provide lightweight and flexible command-line json processor. So we joined the power of shell scripting with this nice tool and the result was very cool!

# Few lines as example :-)
mac=$(cat /sys/class/net/eth0/address | tr -d ‘:’ | tr ‘a-z’ ‘A-Z’)
# Retrieve data
processes=$(ps | tail -n +2 | sed ‘s/^/ /’ | jq -cR ‘[splits(“ +”)] | {pid: .[1], user: .[2], vsz: .[3], stat: .[4], name: .[5], args: .[6:]}’ | jq -s .)
openwrt=$(cat /etc/openwrt_release | awk ‘
BEGIN { FS=”=”; print “{“ }
{ printf “%s\”%s\”:%s”, sep, $1, $2; sep=”, “ }
END { print “ } “ }’)

Just few lines of code to serve our needs! Jq is a real swiss army knife with many options, it is small sized and it requires few dependencies. So it fitted perfectly for our work. In those few lines we had a perfect well-defined json output with all information we wanted to collect.

The server part collected all of those information to store them in a database. For the first prototype we implemented it with some php scripts, very easy work.

We ended up with an inventory infrastructure that matched our needs for a start. Yes, it could be improved but for now it serves our goal very nice and it’s a simple and flexible solution. It’s important to avoid to use or code something that overcome your needs.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *