Simple Application State Tracking

Based on dale3h’s hassctl

Gets the state of the apps specified in the list and publishes the results to MQTT.
I send over MQTT as another server reads states for redundancy.
(i.e. if HA goes down it notifies me)

check_app_state.sh

#!/bin/bash

mqtthost="localhost"  # mqtt broker address
mqtttopic="appcheck"  # first part of topic
mqtttopichost="vostro" # second part of topic

app=$1
ps_out=`ps -ef | grep $1 | grep -v 'grep' | grep -v $0`
result=$(echo $ps_out | grep "$1")
if [[ "$result" != "" ]];then
    /usr/bin/mosquitto_pub -h $mqtthost -q 0 -t $mqtttopic/$mqtttopichost/$app -m "Online"

else
    /usr/bin/mosquitto_pub -h $mqtthost -q 0 -t $mqtttopic/$mqtttopichost/$app -m "Offline"
fi

check_app_list.sh

#!/bin/bash

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh motion

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh appdaemon

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh influxdb

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh docker

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh glances

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh mosquitto

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh mysql

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh webmin

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh home-assistant@hass

bash /home/hass/.homeassistant/shell_scripts/check_app_state.sh homebridge

set up a cron job to run the check_app_list.sh every x minutes.

You will have to make an mqtt sensor for each app

- platform: mqtt
  state_topic: "appcheck/nana/imessage"
  name: "App iMessage"
  unit_of_measurement: ""
  qos: 0

hope this makes sense. rushing out the door.
this is a total hack job. I don’t know what I’m doing but trial end error I got it working and I’m learning . .
feel free to fix or point out anything I could have copied better . . . :wink:

Happy new year!

2 Likes

after some holiday libation, things suddenly became clearer.

I’ve reduced the muddle puck.

#!/bin/bash

# list of apps seperated by a space
apps="motion appdaemon influxdb docker glances mosquitto mysql webmin home-assistant@hass homebridge"

mqtthost="localhost"    # mqtt broker address
mqtttopic="appcheck"    # first part of topic
mqtttopichost="vostro"    # second part of topic
pos_mess="Online"    # positive message
neg_mess="Offline"    # negative message

for app in $apps
 do
   ps_out=`ps -ef | grep $app | grep -v 'grep' | grep -v $0`
   result=$(echo $ps_out | grep "$app")

   if [[ "$result" != "" ]];then
      #  echo $pos_mess
       /usr/bin/mosquitto_pub -h $mqtthost -q 0 -t $mqtttopic/$mqtttopichost/$app -m $pos_mess
   else
      #  echo $neg_mess
       /usr/bin/mosquitto_pub -h $mqtthost -q 0 -t $mqtttopic/$mqtttopichost/$app -m $neg_mess
   fi
done