This commit is contained in:
hjelev
2022-03-19 01:12:48 +02:00
parent 1a422aeede
commit 9b665d3384
2 changed files with 64 additions and 62 deletions

View File

@@ -8,95 +8,96 @@ printm(){
} }
print_green(){ print_green(){
tput setaf 2; echo "$1" tput setaf 2; echo "$1"
tput sgr 0 tput sgr 0
} }
print_yellow(){ print_yellow(){
tput setaf 3; printf "$1" tput setaf 3; printf "$1"
tput sgr 0 tput sgr 0
} }
printm "Raspberry Pi MQTT monitor installer" printm "Raspberry Pi MQTT monitor installer"
check_and_install_pip(){ check_and_install_pip(){
cwd=$(pwd) cwd=$(pwd)
python=$(which python) python=$(which python)
pip=$(python -m pip --version 2>&1); pip=$(python -m pip --version 2>&1);
if [[ "$pip" == *"No"* ]]; then if [[ "$pip" == *"No"* ]]; then
echo "- Pip is not installed, installing it." echo "- Pip is not installed, installing it."
sudo apt install python-pip sudo apt install python-pip
else else
print_green "+ Found $pip" print_green "+ Found $pip"
fi fi
} }
install_requirements(){ install_requirements(){
printm "Installing requirements" printm "Installing requirements"
pip install -r requirements.txt pip install -r requirements.txt
} }
update_config(){ update_config(){
printf "\nCopy config.py.example to config.py\n" printf "\nCopy config.py.example to config.py\n"
cp src/config.py.example src/config.py cp src/config.py.example src/config.py
printm "MQTT settings" printm "MQTT settings"
printf "Enter mqtt_host: " printf "Enter mqtt_host: "
read HOST read HOST
sed -i "s/ip address or host/${HOST}/" src/config.py sed -i "s/ip address or host/${HOST}/" src/config.py
printf "Enter mqtt_user: " printf "Enter mqtt_user: "
read USER read USER
sed -i "s/username/${USER}/" src/config.py sed -i "s/username/${USER}/" src/config.py
printf "Enter mqtt_password: " printf "Enter mqtt_password: "
read PASS read PASS
sed -i "s/\"password/\"${PASS}/" src/config.py sed -i "s/\"password/\"${PASS}/" src/config.py
printf "Enter mqtt_port (default is 1883): " printf "Enter mqtt_port (default is 1883): "
read PORT read PORT
if [ -z "$PORT" ]; then if [ -z "$PORT" ]; then
PORT=1883 PORT=1883
fi fi
sed -i "s/1883/${PORT}/" src/config.py sed -i "s/1883/${PORT}/" src/config.py
printf "Enter mqtt_topic_prefix (default is rpi-MQTT-monitor): " printf "Enter mqtt_topic_prefix (default is rpi-MQTT-monitor): "
read TOPIC read TOPIC
if [ -z "$TOPIC" ]; then if [ -z "$TOPIC" ]; then
TOPIC=rpi-MQTT-monitor TOPIC=rpi-MQTT-monitor
fi fi
sed -i "s/rpi-MQTT-monitor/${TOPIC}/" src/config.py sed -i "s/rpi-MQTT-monitor/${TOPIC}/" src/config.py
printf "\nconfig.py is updated with provided settings\n" printf "\nconfig.py is updated with provided settings\n"
} }
set_cron(){ set_cron(){
printm "Setting Cronjob" printm "Setting Cronjob"
crontab -l > tempcron crontab -l > tempcron
if grep -q rpi-cpu2mqtt.py tempcron; then if grep -q rpi-cpu2mqtt.py tempcron; then
cronfound=$(grep rpi-cpu2mqtt.py tempcron) cronfound=$(grep rpi-cpu2mqtt.py tempcron)
print_yellow " There is already a cronjob running rpi-cpu2mqtt.py - skipping cronjob creation\n" print_yellow " There is already a cronjob running rpi-cpu2mqtt.py - skipping cronjob creation\n"
print_yellow " If you want the cronjob to be automatically created remove the line below from your\n cronjobs list and run the installer again.\n\n" print_yellow " If you want the cronjob to be automatically created remove the line below from your\n cronjobs list and run the installer again.\n\n"
echo " ${cronfound}" echo " ${cronfound}"
else else
printf "How often do you want the script to run in minutes? " printf "How often do you want the script to run in minutes? "
read MIN read MIN
echo "Adding the line below to your crontab" echo "Adding the line below to your crontab"
echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py" echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py"
echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py" >> tempcron echo "*/${MIN} * * * * ${python} ${cwd}/src/rpi-cpu2mqtt.py" >> tempcron
crontab tempcron crontab tempcron
fi fi
rm tempcron rm tempcron
} }
main(){ main(){
check_and_install_pip check_and_install_pip
install_requirements install_requirements
update_config update_config
set_cron set_cron
printm "Done" printm "Done"
} }
main main

View File

@@ -14,4 +14,5 @@ main(){
cd rpi-mqtt-monitor cd rpi-mqtt-monitor
bash install.sh bash install.sh
} }
main main