Linux services through sitemapt

I want to see the status of a linux service on my sitemap (fe is service knxd active or not).
And when it’s not running, it would be really nice to start it (fe service knxd restart)?
A kind of ‘light switch’ would be nice…

Any idea what’s the best approach is?

ps if the services could be used as item, it would be a really nice thing. This way, I can use it also in fe rule that triggers a pushover alert.

Presumably this service is listening on a network port? If so you can use the Network binding using a servicedevice Thing type. I do this for a number of my services. Here is my Thing to monitor whether InfluxDB is running:

network:servicedevice:argusinfluxdb     [ hostname="argus", port=8086, retry=5, timout=5000, refreshInterval=10000 ]

If it is not listening on a network port you will probably need to use the SNMP binding or executeCommandLine/Exec binding and a script that you can run from OH to query for the status of the service and returns it to OH where you can act upon it.

To restart the service you will definitely have to use executeCommandLine/Exec binding to issue the command. You will probably have to add the openhab user to sudoers and give it permission to call service.

Or you could use this:

For the moment, I followed the network binding solution since the network binding is already in place:

Check status

  • I didn’t had a listener port for knxd (=testcase), but wrote a small bash script that runs netcat with port 12345 when knxd is running. The bash scripts runs every 2 minutes throug a cron, this to allow knxd to be stable enough.

bash script:

#!/bin/sh 
SERVICE=knxd;
SERVICE2=netcat; 
CURRENT_DATE=$(date '+%d/%m/%Y-%H:%M:%S')

if 
        ps ax | grep -v grep | grep $SERVICE > /dev/null 
then 
        echo "$CURRENT_DATE : $SERVICE service running, everything is fine" >> /var/log/syslog
        netcat -lk 12345 & 
else 
        echo "$CURRENT_DATE : $SERVICE is not running!" >> /var/log/syslog 
        pkill netcat
fi
  • With a thing and item, I can now easily show the status in my sitemap.

GUI thing:

sitemap:

Group item=Server {
   Frame label="KNX Kopppeling" {
      Text item=MietjeKNX_Online label="KNX Online [MAP(status.map):%d]"
      }
   }
  • And with a small rule, I’m getting pushover messages when the service has stopped.

rule:

rule "KNXFail"
when
        Item MietjeKNX_Online changed to 0
then
        logInfo("OH", "KNX crashte, en een berichtje werd gestuurd")
        pushover ("KNX faalde zopas bij OpenHAB")
end 

Restart

  • With a EXEC/REGEX thing and item, I can restart the service.

GUI Thing:

sitemap:

Group item=Server {
   Frame label="KNX Kopppeling" {
      Switch item=KnxRestartItem label="USB<>KNX" mappings=[ON="Herstart"] 
      }
   }
  • Just a try-and-error was the permissions of openhab vs sudo :wink:

ps I Liked the idea about the snmp, and when I’ve got 5 minutes, will certainly try that approach… Also because I never used it with openHAB, and could be nice to migrate some things from cacti towards openHAB this way. So much to do, so little time…