Separate time schedule, calculate math, linux shell

I have multiple raspberry pi's and my goal is to send periodical activity email so I can know that this raspberry is alive.

I want to use the same image for every device.
It would be nice if somehow we can schedule this email to send in different time intervals for each raspberry pi.

Since my pi's do not have same IP address I will take fourth IP octet to calculate in which minute should send this email.

In this scenario i work with python email sending using less secure google account:
mkdir -p /var/zabbix/alertscripts/
wget https://gist.githubusercontent.com/superdaigo/3754055/raw/e28b4b65110b790e4c3e4891ea36b39cd8fcf8e0/zabbix-alert-smtp.sh -O/var/zabbix/alertscripts/zabbix-alert-smtp.py -q
sed -i "s/your.account@gmail.com/name.surname@gmail.com/" /var/zabbix/alertscripts/zabbix-alert-smtp.py
sed -i "s/your mail password/yoursecretpassword/" /var/zabbix/alertscripts/zabbix-alert-smtp.py
sed -i "s/Zabbix Alert/Alehandro/" /var/zabbix/alertscripts/zabbix-alert-smtp.py
At first i will seperate fourth octet from IP address:
fourth=$(ifconfig | grep "inet.*addr.*Bcast.*Mask" | sed "s/  Bcast.*$//g" | sed "s/^.*\.//g")
Since schedule will hapen in 1 hour interval i will devide this number with 60 seconds:
exactminute=$(expr $fourth % 60)
% means arithmetic remainder of $fourth divided by 60

Now i shedule task:
echo "`echo $exactminute` * * * * root /var/zabbix/alertscripts/zabbix-alert-smtp.py emailaddress@gmail.com \"$(hostname)\" \"$(uptime)\"">> /etc/crontab
This project nicely works together with:
Set dynamic hostname for Raspbian image

No comments: