domoticz.sh
Code: Select all
#!/bin/sh
# Setup:
# Put domoticz.sh in usr/bin and make it executable: chmod 777 domoticz.sh
# put zomotcz in /etc/init.d/
# Run command /etc/init.d/zomoticz enable
# Manual start: /etc/init.d/zomoticz start
# Manual stop: /etc/init.d/zomoticz stop
# Aboute:
# Monitor your openwrt router
# Tested with Technicolor TG799vac firmware v17.2
# To get root on TG799: https://github.com/wuseman/TG799VAC-XTREME-17.2-MINT
# Or https://www.crc.id.au/hacking-the-technicolor-tg799vac-and-unlocking-features/
# Use at your own risk.
# Todo:
# Implement cpu load, ex: top -b -n1 | grep Cpu | sed -r 's@.+:\s([0-9\.]+).+@\1@'
# Version history
#
# Version 0.3 (211017)
# Implemented rx/tx stats sanitary check
#
# Version 0.2 (211017)
# Changed bytes to mb calculation
# Implemented Update free dns (if curl is suported!)
# Implemented RSSI Monitoring for connected wifi devices
# Implemented disk usage
# Implemented memory usage
# Fixed interface overflow
#
# version 0.1
# Orginal version
# Script settings:
DOMOTICZ="192.168.1.131" # Domotics Host IP
PORT="8080"
USER="xxxx"
PASS="xxxx"
disk_mount_point="/dev/sda1"
wan_rx_bytes_IDX="475" # Custom
wan_tx_bytes_IDX="474" # custom
mem_tot_IDX="466" # Custom
mem_free_IDX="467" # Custom
disk_free_IDX="470" # Text device
disk_used_IDX="471" # text device
disk_used_percentage_IDX="468" # Percentage device
disk_size_IDX="469" # Text device
Interface="eth4" # Name of the interface, eth4 is the wan port on TG799vac use: ubus call network.device status to check available interfaces
free_dns_curl="https://freedns.afraid.org/dynamic/update.php?xxxxxxxxxxxxxxxxxxxxxxxxx"
# END
# Device RSSI monitor settings (Only support for Broadcom chipset!):
RSSI_Device_1_Active="false"
RSSI_Device_1_IDX="100"
RSSI_Device_1_MAC="7A:1D:C7:BF:BD:CA"
RSSI_Device_2_Active="false"
RSSI_Device_2_IDX="100"
RSSI_Device_2_MAC="7A:1D:C7:BF:BD:CA"
RSSI_Device_3_Active="false"
RSSI_Device_3_IDX="100"
RSSI_Device_3_MAC="7A:1D:C7:BF:BD:CA"
RSSI_Device_4_Active="false"
RSSI_Device_4_IDX="100"
RSSI_Device_4_MAC="7A:1D:C7:BF:BD:CA"
# END
# Options:
Update_Free_DNS=1 # Update free dns if curl is suported!
Interface_Stats=1 # Activate interface stats
Memory_Stats=1 # Activate mem-stats
Disk_Stats=1 # Activate disk stats/usage
RSSI_Monitor=0 # Activate device RSSI monitoring
Intervall=300 # Cycle time between runs in sec
Debug=1 # Enable/disable debug
Syslog=1 # Enable/disable syslog
# End Options
# Dont edit below
FirstRun=1
wan_tx_bytes=0
wan_rx_bytes=0
R1_new=0
R1_old=0
# Functions:
# Calculator
calc() {
awk "BEGIN{print $*}";
}
RssiMonitor () { # Under construction!
# wl assoclist (get all devices connected)
# No arrays in ash :(
RSSI_Device_Val="null"
# Check device 1
if [ $RSSI_Device_1_Active = "true" ]; then RSSI_Device_Val=`wl rssi $RSSI_Device_1_MAC`;echo $RSSI_Device_Val
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${RSSI_Device_1_IDX}&svalue=${RSSI_Device_Val}"
fi
# Check device 2
if [ $RSSI_Device_2_Active = "true" ]; then RSSI_Device_Val=`wl rssi $RSSI_Device_2_MAC`;echo $RSSI_Device_Val
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${RSSI_Device_2_IDX}&svalue=${RSSI_Device_Val}"
fi
# Check device 3
if [ $RSSI_Device_3_Active = "true" ]; then RSSI_Device_Val=`wl rssi $RSSI_Device_3_MAC`;echo $RSSI_Device_Val
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${RSSI_Device_3_IDX}&svalue=${RSSI_Device_Val}"
fi
# Check device 4
if [ $RSSI_Device_4_Active = "true" ]; then RSSI_Device_Val=`wl rssi $RSSI_Device_4_MAC`;echo $RSSI_Device_Val
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${RSSI_Device_4_IDX}&svalue=${RSSI_Device_Val}"
fi
}
FreeDNS () {
if [ $FirstRun = 1 ]; then curl -k $free_dns_curl; fi
}
DiskStats () {
if [ $Debug = 1 ]; then echo "Debug (DiskStats)"; fi
# Size:
DiskSize=`df $disk_mount_point -h | grep $disk_mount_point | awk '{ print $2 }'`
# Used:
DiskUsed=`df $disk_mount_point -h | grep $disk_mount_point | awk '{ print $3 }'`
# Used in percentage:
DiskUsedInPercentage=`df $disk_mount_point -h | grep $disk_mount_point | awk '{ print $5 }'`
# Available/free
DiskFree=`df $disk_mount_point -h | grep $disk_mount_point | awk '{ print $4 }'`
#send to domoticz
if [ $FirstRun = 1 ]; then curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${disk_size_IDX}&svalue=${DiskSize}"; fi
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${disk_used_IDX}&svalue=${DiskUsed}"
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${disk_free_IDX}&svalue=${DiskFree}"
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${disk_used_percentage_IDX}&svalue=${DiskUsedInPercentage/%/}"
}
InterfaceStats () {
if [ $Debug = 1 ]; then echo "Debug (InterfaceStats)"; fi
T1_new=`cat /sys/class/net/$Interface/statistics/tx_bytes`
R1_new=`cat /sys/class/net/$Interface/statistics/rx_bytes`
# check virginity
if [ $FirstRun = 1 ]; then
if [ $Debug = 1 ]; then echo "Debug (firstrun)"; fi
else
echo "debug 2"
# detect rollover
if [ "$T1_new" -lt "$T1_old" ]; then
if [ $Debug = 1 ]; then echo "Debug (overflow)"; fi
wan_tx_bytes=$T1_new
wan_rx_bytes=$R1_new
else
# Calc values
wan_tx_bytes=`expr $T1_new - $T1_old`
wan_rx_bytes=`expr $R1_new - $R1_old`
#((wan_tx_bytes = $T1_new - $T1_old))
fi
fi
# convert to kb (binary 1024)
#wan_rx_bytes=$(expr $wan_rx_bytes / 1024)
#wan_tx_bytes=$(expr $wan_tx_bytes / 1024)
# or convert to mb (binary 1048576)
wan_rx_bytes=$(calc $wan_rx_bytes/1048576)
wan_tx_bytes=$(calc $wan_tx_bytes/1048576)
# Sanitary check
if awk "BEGIN {exit !($wan_tx_bytes < "0") || ($wan_rx_bytes < "0") || ($wan_tx_bytes >= "10000") || ($wan_rx_bytes >= "10000")}"; then
if [ $Debug = 1 ]; then echo "Sanitary check fail"; fi
else
# Send values to domoticz
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${wan_rx_bytes_IDX}&svalue=${wan_rx_bytes}"
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${wan_tx_bytes_IDX}&svalue=${wan_tx_bytes}"
# Send values to domoticz log
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=addlogmessage&message=RouterStats_rx:${wan_rx_bytes}&level=2"
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=addlogmessage&message=RouterStats_tx:${wan_tx_bytes}&level=2"
fi
# get value for next run
T1_old=`cat /sys/class/net/$Interface/statistics/tx_bytes`
R1_old=`cat /sys/class/net/$Interface/statistics/rx_bytes`
}
MemStats () {
if [ $Debug = 1 ]; then echo "Debug (MemStats)"; fi
mem_free=`free | grep Mem:| awk '{ print $4 }'`
mem_tot=`free | grep Mem:| awk '{ print $2 }'`
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${mem_free_IDX}&svalue=${mem_free}"
# Only send once
if [ $FirstRun = 1 ]; then
curl http://$USER:$PASS@$DOMOTICZ:$PORT/json.htm?"type=command¶m=udevice&idx=${mem_tot_IDX}&svalue=${mem_tot}"
fi
}
ScriptTimer(){
if [ $Debug = 1 ]; then
printf '\e[31m'
#printf '\e[1m'
echo ""
echo " Sleep for $Intervall""s"
echo ""
#echo "| |"
printf '|'
Intervall=$(expr $Intervall - 2)
#Sript indicator..
y=0
while [ "$y" -le "$Intervall" ] ; do
printf ' '
y=$(( $y + 1 ))
done
printf '|'
Intervall=$(expr $Intervall + 2)
echo ""
printf ' '
#Sript indicator..
y=0
while [ "$y" -le "$Intervall" ] ; do
printf '#'
y=$(( $y + 1 ))
sleep 1
done
printf '\e[0m'
echo ""
else
sleep $Intervall
fi
}
# Main:
while true; do
if [ $Syslog = 1 ]; then logger "Domoticz router stats: Running"; fi
if [ $Interface_Stats = 1 ]; then InterfaceStats; fi
if [ $Memory_Stats = 1 ]; then MemStats; fi
if [ $Disk_Stats = 1 ]; then DiskStats; fi
if [ $Update_Free_DNS = 1 ]; then FreeDNS; fi
if [ $RSSI_Monitor = 1 ]; then RssiMonitor; fi
ScriptTimer
FirstRun=0
done
exit 0
Code: Select all
#!/bin/sh /etc/rc.common
USE_PROCD=1
START=99
STOP=01
start_service() {
procd_open_instance
procd_set_param command /bin/sh "/usr/bin/domoticz.sh"
procd_close_instance
}