Problem parsrsing output channel of exec 2 binding

Hi,

currently I am migration from the exec 1 to the exec 2 binding.

I have a shell script, which outputs a single number. With the exec 1 binding I used the following item to retrieve this number:

//Number Network_TCP_Count		"TCP Con. Count: [%d]"              <chart> 	(gNetwork, gNetworkMonitor) 	{ exec="<[/usr/local/bin/netmonitor.sh --tcpcount:1800000:REGEX((.*?))]", expire="1h10m, -1" }

This worked without problems

I converted this to the following Thing and item:

Thing exec:command:netmonitor      [ command="/usr/local/bin/netmonitor.sh --tcpcount", interval=1800, timeout=30, autorun=false, transform="REGEX(((.*?))" ]

Number Network_TCP_Count_Str    "TCP Con. Count: [%s]"              <chart> 	                                { channel="exec:command:netmonitor:output" }

With this I noticed several problems:

  1. When the bash script does not print a newline character after the number the item Network_TCP_Count_Str will be empty, that of course is not a real problem.

  2. There is a lot of additional text going into the item. When I print its value, I get:

232
(Es konnten nicht alle Prozesse identifiziert werden; Informationen über
nicht-eigene Processe werden nicht angezeigt; Root kann sie anzeigen.)
(Es konnten nicht alle Prozesse identifiziert werden; Informationen über
nicht-eigene Processe werden nicht angezeigt; Root kann sie anzeigen.)

“232” is the number I am interested in. The additional text roughly translates into “Not all process could be identified, information about none own processes are not shown, root can show them”.

I am using openhab on Linux and openhab is executed as user “openhab”, so the warning makes sense but I do not know, why the text is inserted into the output, it is not printed by my script.

As a workaround I tried to change the regex to only fetch the number, I tried several variants which, according to reges101 should be fine:

transform="REGEX((\\d+))"
transform="REGEX(([0-9]+))" 
transform="REGEX((\\d+?))" 
transform="REGEX(([0-9]+?))" 

(I am not sure why the ? is needed in the original regex, but without the resulting string is empty)

The ultimate goal is to convert the result into a number, as far as I understand it I will need a rule for that. As a workaround I will parse the string in this rule, but there must be a more elegant resolution to the problem?

Thanks for your help!

Juelicher

would you mind adding the source of that script of yours? Sounds interesting and might help to replicate the problem and that eventually might assist solving your issue. :slight_smile:

Not at all, it is only a two-liner:

#!/bin/bash

NetCount=$(netstat -p tcp | wc -l)

echo $NetCount

Because the out Channel gets populated with everything written to standard error and standard out while the command was running.

In openHAB, the REGEX must match the entire String and the first group (i.e. first set of parens) is what get’s returned. So use something like REGEX((\d+).*)

One other thing that may cause problems is I believe the out Channel only supports String Items, not Number Items.

You are right, when running the script as user openhab the additional message is printed to stderr. I tried that before posting here, but as the shell of my openhab user is set to /bin/false, a simple su openhab had no affect, which I did not notice.

Thanks, I will try this.

Yes, I found out about this when trying to find out why this was not working after switching to the exec 2 binding. I have created a string item which holds the output of the command and a rule to convert it to a Number. Currently this rule ignores the error message, which works, but I will try out the regex approch again.

A small addition: As I now know, what the problem is, redirecting stderr of the netstat command gets rid of the error message

NetCount=$(netstat -p tcp 2> /dev/null | wc -l)

Maybe the fact, that the output channel contains stderr and stdout of the command should be added to the documentation of the exec binding?

1 Like