Skip to content

Commit

Permalink
Add fully scripted influxdb + grafana installation
Browse files Browse the repository at this point in the history
Closes #366
Partly resolves #437

This patch aims to script the installation instructions found:
https://community.openhab.org/t/influxdb-grafana-persistence-and-graphing/13761
  • Loading branch information
EliasGabrielsson committed Nov 17, 2018
1 parent ecf59c4 commit 2ad7cf3
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 39 deletions.
10 changes: 8 additions & 2 deletions functions/openhab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,14 @@ openhab_is_installed() {
}

openhab_is_running() {
#TODO
exit 1
if [ `systemctl is-active openhab2` != "active" ]; then return 1; fi
if [ -r /etc/default/openhab2 ]; then
. /etc/default/openhab2
fi
# Read and set openHAB variables set in /etc/default/ scripts
if [ -z "${OPENHAB_HTTP_PORT}" ]; then OPENHAB_HTTP_PORT=8080; fi
if [ -z "${OPENHAB_HTTPS_PORT}" ]; then OPENHAB_HTTPS_PORT=8443; fi
return 0;
}

# The function has one non-optinal parameter for the application to create a tile for
Expand Down
270 changes: 233 additions & 37 deletions functions/packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -406,62 +406,258 @@ speedtest_cli_setup() {
fi
}

## Function for installing and configure InfluxDB and Grafana while also integrate it to openHAB.
## The function can be invoked either INTERACTIVE with userinterface UNATTENDED.
##
## When called UNATTENDED it will install both InfluxDB and Grafana on the local system.
##
## influxdb_grafana_setup()
##

influxdb_grafana_setup() {
FAILED=0
introtext="This will install InfluxDB and Grafana. Soon this procedure will also set up the connection between them and with openHAB. For now, please follow the instructions found here:
local FAILED=0
local text_intro="This will install and configure InfluxDB and Grafana. Soon this procedure will also set up the connection between them and with openHAB. For now, please follow the instructions found here:
\nhttps://community.openhab.org/t/13761/1"
failtext="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue."
successtext="Setup successful. Please continue with the instructions you can find here:\n\nhttps://community.openhab.org/t/13761/1"
local text_fail="Sadly there was a problem setting up the selected option. Please report this problem in the openHAB community forum or as a openHABian GitHub issue."
local text_success="Setup successful. Please continue with the instructions you can find here:\n\nhttps://community.openhab.org/t/13761/1"

echo "$(timestamp) [openHABian] Setting up InfluxDB and Grafana... "
distributor=$(lsb_release -si | awk '{print tolower($0)}')
if ! [ $distributor = "ubuntu" ]; then #since we only have ubuntu and debian as supported OS at the moment
distributor="debian" # replacing e.g. raspbian with debian
fi
codename=$(lsb_release -sc)
if [ -n "$INTERACTIVE" ]; then
if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$introtext" 15 80) then echo "CANCELED"; return 0; fi
fi
if ! (whiptail --title "Description, Continue?" --yes-button "Continue" --no-button "Back" --yesno "$text_intro" 15 80) then echo "CANCELED"; return 0; fi
fi

local influxdb_address
local influxdb_admin_username
local influxdb_admin_password
local influxdb_openhab_username
local influxdb_openhab_password
local influxdb_grafana_username
local influxdb_grafana_password
local influxdb_database_name
local grafana_admin_password
local openhab_integration=false
if [ -n "$INTERACTIVE" ]; then
local text_influxDB_intro="A new InfluxDB instance can be installed locally on the openhabian system or an already running InfluxDB instance can be used."
if ! (whiptail --title "InfluxDB" --yes-button "Install locally" --no-button "Use existing instance" --yesno "$text_influxDB_intro" 15 80) then
local text_influxDB_configure="Shall a new user and database be configured on the InfluxDB instance automatically or shall existing existing ones be used?"
if ! (whiptail --title "InfluxDB" --yes-button "Configure automatically" --no-button "Use existing database" --yesno "$text_influxDB_configure" 15 80) then
# Existing InfluxDB - Manual configuration
influxdb_database_name=$(whiptail --title "InfluxDB" --inputbox "OpenHAB need to use a specific InfluxDB database. Please enter a configured InfluxDB database name:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_openhab_username=$(whiptail --title "InfluxDB" --inputbox "OpenHAB need write/read access to previous defined database. Please enter an InfluxDB username for OpenHAB:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_openhab_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_openhab_username:\"" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_grafana_username=$(whiptail --title "InfluxDB" --inputbox "Grafana need read access to previous defined database. Please enter an InfluxDB username for Grafana:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_grafana_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_grafana_username\":" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
else
# Existing InfluxBD - Automatic configuration
influxdb_admin_username=$(whiptail --title "InfluxDB" --inputbox "An InfluxDB admin account must be used for automatical database configuration. Please enter a username: " 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_admin_password=$(whiptail --title "InfluxDB" --passwordbox "Password for InfluxDB account \"$influxdb_admin_username\":" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
fi
# Influx DB server address
local influxdb_returncode=0
local text_influxdb_address="Enter InfluxDB instance adress: [protocol:address:port] \n eg. https://192.168.1.100:8086"
while [ "$influxdb_returncode" != "204" ]
do
influxdb_address=$(whiptail --title "InfluxDB" --inputbox "$text_influxdb_address" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
influxdb_returncode=`curl -s -o --max-time 6 --insecure /dev/null -w "%{http_code}" "$influxdb_address"/ping | sed 's/^0*//'`
text_influxdb_address="Can't establish contact to InfluxDB instance. Please retry to enter InfluxDB instance adress: [protocol:address:port] \n eg. https://192.168.1.100:8086"
done
else
# Local InfluxDB
local text_influxDB_admin_password="The local InfluxDB installation needs a password for the \"admin\" account. Enter a password:"
local matched=false
local password_check
while [ "$matched" = false ]; do
influxdb_admin_password=$(whiptail --title "InfluxDB - Admin Account" --passwordbox "$text_influxDB_admin_password" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - Admin Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_admin_password" = "$password_check" ] && [ ! -z "$influxdb_admin_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
fi

if [ -z "$influxdb_openhab_username" ]; then # is empty
influxdb_database_name="openhab_db"
influxdb_openhab_username="openhab"
influxdb_grafana_username="grafana"
local matched=false
local password_check
while [ "$matched" = false ]; do
influxdb_openhab_password=$(whiptail --title "InfluxDB - OpenHAB Account" --passwordbox "An openHAB specific InfluxDB user will be created \"openhab\". Please enter a password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - OpenHAB Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_openhab_password" = "$password_check" ] && [ ! -z "$influxdb_openhab_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
matched=false
while [ "$matched" = false ]; do
influxdb_grafana_password=$(whiptail --title "InfluxDB - Grafana Account" --passwordbox "A Grafana specific InfluxDB user will be created \"grafana\". Please enter a password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "InfluxDB - Grafana Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$influxdb_grafana_password" = "$password_check" ] && [ ! -z "$influxdb_grafana_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done
fi

# Local Grafana
local text_grafana_admin_password="The local Grafana installation needs a password for the \"admin\" account. Enter a password:"
local matched=false
local password_check
while [ "$matched" = false ]; do
grafana_admin_password=$(whiptail --title "Grafana - Admin Account" --passwordbox "$text_grafana_admin_password" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
password_check=$(whiptail --title "Grafana - Admin Account" --passwordbox "Please confirm the password:" 15 80 3>&1 1>&2 2>&3)
if [ $? = 1 ]; then echo "CANCELED"; return 0; fi
if [ "$grafana_admin_password" = "$password_check" ] && [ ! -z "$grafana_admin_password" ]; then
matched=true
else
$(whiptail --title "Authentication Setup" --msgbox "Password mismatched or blank... Please try again!" 15 80 3>&1 1>&2 2>&3)
fi
done

cond_redirect apt -y install apt-transport-https
cond_echo ""
echo "InfluxDB... "
cond_redirect wget -O - https://repos.influxdata.com/influxdb.key | apt-key add - || FAILED=1
echo "deb https://repos.influxdata.com/$distributor $codename stable" > /etc/apt/sources.list.d/influxdb.list || FAILED=1
cond_redirect apt update || FAILED=1
cond_redirect apt -y install influxdb || FAILED=1
cond_redirect systemctl daemon-reload
cond_redirect systemctl enable influxdb.service
cond_redirect systemctl start influxdb.service
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
cond_echo ""

echo "Grafana... "
if is_pi; then
if is_pione || is_pizero || is_pizerow; then GRAFANA_REPO_PI1="-rpi-1b"; fi
echo "deb https://dl.bintray.com/fg2it/deb${GRAFANA_REPO_PI1} $codename main" > /etc/apt/sources.list.d/grafana-fg2it.list || FAILED=2
cond_redirect apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 379CE192D401AB61 || FAILED=2
if openhab_is_running; then
local text_openHAB_integration="OpenHAB can use InfluxDB for persistant storage. Shall InfluxDB be configured with OpenHAB?
(A new config file for openHAB will be created with basic settings.)"
if (whiptail --title "OpenHAB integration, Continue?" --yes-button "Yes" --no-button "No" --yesno "$text_openHAB_integration" 15 80) then openhab_integration=true ; fi
else
cond_echo "OpenHAB is not running. InfluxDB and grafana openHAB integration is skipped..."
fi
fi

local dist="debian"
local codename="stretch"
if is_ubuntu; then
dist="ubuntu"
codename=$(lsb_release -sc)
elif is_debian; then
dist="debian"
codename=$(lsb_release -sc)
fi

if [ -z "$influxdb_address" ]; then # is empty, install a InfluxDB database
cond_echo ""
echo "Installing InfluxDB..."
influxdb_address="http://localhost:8086"
influxdb_admin_username="admin"
if [ ! -f /etc/influxdb/influxdb.conf ]; then
cond_redirect apt -y install apt-transport-https
cond_redirect wget -O - https://repos.influxdata.com/influxdb.key | apt-key add - || FAILED=1
echo "deb https://repos.influxdata.com/$dist $codename stable" > /etc/apt/sources.list.d/influxdb.list || FAILED=1
cond_redirect apt update || FAILED=1
cond_redirect apt -y install influxdb || FAILED=1
cond_redirect systemctl daemon-reload
sleep 2
cond_redirect systemctl enable influxdb.service
sleep 2
cond_redirect systemctl restart influxdb.service
sleep 10
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
echo -n "Configure InfluxDB admin account... "; echo -n ""
curl --insecure $influxdb_address/query --data-urlencode "q=CREATE USER admin WITH PASSWORD '$influxdb_admin_password' WITH ALL PRIVILEGES" || FAILED=1
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
echo -n "Configure listen on localhost only... "; echo -n ""
cond_redirect sed -i -e '/^# Determines whether HTTP endpoint is enabled./ { n ; s/^# enabled = true/enabled = true/ }' /etc/influxdb/influxdb.conf
cond_redirect sed -i 's/# bind-address = ":8086"/bind-address = "localhost:8086"/g' /etc/influxdb/influxdb.conf
cond_redirect sed -i 's/# auth-enabled = false/auth-enabled = true/g' /etc/influxdb/influxdb.conf
cond_redirect systemctl restart influxdb.service
sleep 10
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
else
echo "SKIPPED"
cond_echo "InfluxDB already installed. Using http://localhost:8086"
fi
cond_echo ""
fi

if [ ! -z "$influxdb_admin_username" ]; then # is set, configure database and application users
echo -n "Setup of inital influxdb database and InfluxDB users... "
echo -n ""
influxdb_database_name="openhab_db"
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE DATABASE $influxdb_database_name" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE USER openhab WITH PASSWORD '$influxdb_openhab_password'" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=CREATE USER grafana WITH PASSWORD '$influxdb_grafana_password'" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=GRANT ALL ON openhab_db TO openhab" || FAILED=1
curl --user $influxdb_admin_username:$influxdb_admin_password --insecure $influxdb_address/query --data-urlencode "q=GRANT READ ON openhab_db TO grafana" || FAILED=1
if [ $FAILED -eq 1 ]; then echo -n "FAILED "; else echo -n "OK "; fi
fi

echo -n "Installing Grafana..."
# TODO: Update with ARM repository when ready; https://github.com/grafana/grafana/issues/12761
if is_pione || is_pizero || is_pizerow || is_pitwo; then
cond_redirect wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.3.4_armhf.deb || FAILED=2
cond_redirect apt update || FAILED=2
cond_redirect apt-get install -y adduser libfontconfig
cond_redirect dpkg -i grafana_5.3.4_armhf.deb
elif is_pithree || is_pithreeplus || is_pine64; then
cond_redirect wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.3.4_arm64.deb || FAILED=2
cond_redirect apt update || FAILED=2
cond_redirect apt-get install -y adduser libfontconfig
cond_redirect dpkg -i grafana_5.3.4_arm64.deb
else
cond_redirect wget -O - https://packagecloud.io/gpg.key | apt-key add - || FAILED=2
echo "deb https://packagecloud.io/grafana/stable/$distributor/ $codename main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
if [ $distributor = "ubuntu" ]; then #remove this loop when grafana ubuntu packages are available
echo "deb https://packagecloud.io/grafana/stable/debian/ jessie main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
echo "deb https://packagecloud.io/grafana/stable/$dist/ $codename main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
if [ $dist = "ubuntu" ]; then #remove this loop when grafana ubuntu packages are available
echo "deb https://packagecloud.io/grafana/stable/debian/ stretch main" > /etc/apt/sources.list.d/grafana.list || FAILED=2
fi
cond_redirect apt update || FAILED=2
cond_redirect apt -y install grafana || FAILED=2
fi
cond_redirect apt update || FAILED=2
cond_redirect apt -y install grafana || FAILED=2
cond_redirect systemctl daemon-reload
cond_redirect systemctl enable grafana-server.service
cond_redirect systemctl start grafana-server.service
sleep 10
if [ $FAILED -eq 2 ]; then echo -n "FAILED "; else echo -n "OK "; fi
cond_echo ""

echo "Connecting (TODO)... "
#TODO
echo -n "Updating Grafana admin password..."
curl --user admin:admin --header "Content-Type: application/json" --request PUT --data "{\"password\":\"$grafana_admin_password\"}" http://localhost:3000/api/admin/users/1/password || FAILED=2
if [ $FAILED -eq 2 ]; then echo -n "FAILED "; else echo -n "OK "; fi

echo "Adding openHAB dashboard tile... "
echo -n "Updating Grafan configuration..."
cond_redirect sed -i -e '/^# disable user signup \/ registration/ { n ; s/^;allow_sign_up = true/allow_sign_up = false/ }' /etc/grafana/grafana.ini
cond_redirect sed -i -e '/^# enable anonymous access/ { n ; s/^;enabled = false/enabled = true/ }' /etc/grafana/grafana.ini
cond_redirect systemctl restart grafana-server.service
sleep 10

echo -n "Connection Grafana to InfluxDB..."
curl --user admin:$grafana_admin_password --request POST http://localhost:3000/api/datasources \
--header "Content-Type: application/json" \
--data '{"name": "openhab_home", "type": "influxdb", "url": "http://localhost:8086", "password": "'"$influxdb_grafana_password"'", "user": "'"$influxdb_grafana_user"'", "database": "'"$influxdb_database_name"'", "access": "proxy", "basicAuth":true, "basicAuthUser":"'"$influxdb_grafana_user"'", "basicAuthPassword":"'"$influxdb_grafana_password"'", "withCredentials":false}'

echo -n "Adding openHAB dashboard tile for Grafana... "
dashboard_add_tile grafana || FAILED=4

if [ "$openhab_integration" = true ]; then
echo -n "Adding install InfluxDB with database configuration to OpenHAB"
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" "http://localhost:$OPENHAB_HTTP_PORT/rest/extensions/influxdb/install"
cond_redirect touch /etc/openhab2/services/influxdb.cfg
echo "url=$influxdb_address" >> /etc/openhab2/services/influxdb.cfg
echo "user=$influxdb_openhab_username" >> /etc/openhab2/services/influxdb.cfg
echo "password=$influxdb_openhab_password" >> /etc/openhab2/services/influxdb.cfg
echo "db=$influxdb_database_name" >> /etc/openhab2/services/influxdb.cfg
echo "retentionPolicy=autogen" >> /etc/openhab2/services/influxdb.cfg
fi

if [ -n "$INTERACTIVE" ]; then
if [ $FAILED -eq 0 ]; then
whiptail --title "Operation Successful!" --msgbox "$successtext" 15 80
Expand Down Expand Up @@ -700,4 +896,4 @@ or just reboot the system.
whiptail --title "Operation Failed!" --msgbox "$failtext" 10 60
fi
fi
}
}

2 comments on commit 2ad7cf3

@tylerbinski
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi There, Nice work, I am testing this on RPI3 and will share testing results shortly.

Suggestion for line 419 - update the message text "Soon this procedure will also set up the connection between them and with openHAB. For now, please follow the instructions found here:
\nhttps://community.openhab.org/t/13761/1" ... Since the new setup this code will in fact attempt to configure the connections.

@tylerbinski
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There appears to be an issue with Grafana installation on RaspberryPi3B.

$ dpkg -i grafana_5.3.4_arm64.deb dpkg: error processing archive grafana_5.3.4_arm64.deb (--install): **package architecture (arm64) does not match system (armhf)** Errors were encountered while processing: grafana_5.3.4_arm64.deb

System Details
Release = Raspbian GNU/Linux 9 (stretch)
Kernel = Linux 4.14.79-v7+
Platform = Raspberry Pi 3 Model B Rev 1.2
openHAB 2.3.0-1 (Release Build)

sudo dpkg --print-architecture
armhf

uname -a
Linux openHABianPi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux

cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4

Perhaps a code update around lines 606 to add an architecture check and include that in the if statement to determine which package to download/install.

  local architecture=$(dpkg --print-architecture)
  if architecture="armhf" || is_pione || is_pizero || is_pizerow || is_pitwo; then

Please sign in to comment.