Thanks! that did help me get started.
I ended up writing a crontab script that does both the check and the power-cycle instead of triggering a custom event, because that felt like a better fit in my use-case, but the payload in the script can be easily adapted to generate custom events. I'm posting it here so others may benefit:
Code: Select all
#!/bin/bash
# Monitor log for occurrence of events in the last minute and trigger
# a payload when they occurred (for example a power cycle)
# use this in a cron job, but run it less then once every minute
# or the payload may get triggered twice...
# setup
host=127.0.0.1
port=8080
triggeridx=6
payload="type=command¶m=switchlight&idx=$triggeridx&switchcmd=Off"
error="Error: YouLess: Error connecting to: 192.168."
# the next string needs to be url-encoded: https://www.url-encode-decode.com/
logmsg="YouLess+was+power-cycled+by+MonitorLog"
tstamp=$(date '+%F %H:%M')
# see how many matches we had in the last minute
matches=$(curl -s "http://$host:$port/json.htm?type=command¶m=getlog&loglevel=4" | grep "$error" | grep "$tstamp" | wc -l)
if [ "$matches" != "0" ];
then
curl -s "http://$host:$port/json.htm?$payload"
curl -s "http://$host:$port/json.htm?type=command¶m=addlogmessage&message=$logmsg"
fi
I'm running this every 5 mins in a cronjob, and it works well because a failing YouLess results in an error every 10 seconds. If the error you're interested in is more sporadic, the grep "logic" may need some tweaks, e.g., check what happened in the last hour (just remove %M from tstamp). Note that if you make that change, you should run it less than once an hour to avoid being triggered twice by the same error (there are ways around this, for example by storing the lastlogtime of every run in a persistent variable and only get the log entries from after that time).