Skip to content

Instantly share code, notes, and snippets.

@fcolista
Last active February 8, 2017 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcolista/4af50f37512c7570d642aeab85d69d7d to your computer and use it in GitHub Desktop.
Save fcolista/4af50f37512c7570d642aeab85d69d7d to your computer and use it in GitHub Desktop.
AlpineLinux: Icingaweb2 database config script
#!/bin/sh
#
# Script for automatic Database setup for Icingaweb2, designed for Alpine Linux (https://www.alpinelinux.org)
# (c) 2017 Francesco Colista
# Email: fcolista@alpinelinux.org
#
configure_icinga2() {
db=$1
dbcaps=$(echo $db | cut -c1 | tr a-z A-Z)$(echo $db | cut -c2-)
if ! [ -f /etc/icinga2/features-enabled/ido-$db ]; then
echo "Looks that IDO feature is not enable for $db. I'm enabling it in your behalf.."
icinga2 feature enable ido-$db
rc-service icinga2 restart
fi
echo " ==> Updating $dbcaps DB configuration for Icinga2 ..."
cat>/etc/icinga2/features-enabled/ido-$db.conf<<EOF
/**
* The db_ido_$db library implements IDO functionality
* for $dbcaps.
*/
library "db_ido_$db"
object Ido${dbcaps}Connection "ido-$db" {
user = "$DBUSER"
password = "$DBPASS"
host = "localhost"
database = "$DBNAME"
}
EOF
}
apk info -q -e icinga2 || apk add icinga2
apk info -q -e icingaweb2 || apk add icingaweb2
echo
read -p "Choose a database name you want to create for Icinga: " DBNAME
read -p "Choose an username you want to create for Icinga: " DBUSER
read -p "Choose a password you want to create for Icinga: [warning: will be echoed]: " DBPASS
read -p "Choose the DB you want to use: [M]ysql|[M]ariaDB or [P]ostgreSQL : " DB
echo
while true; do
case $DB in
M*|m*) DBTYPE="mysql"
apk info -q -e mariadb || apk add mariadb mariadb-client
! [ -d /var/lib/mysql ] && rc-service mariadb setup
rc-service mariadb status || rc-service mariadb start
sql="CREATE DATABASE $DBNAME;GRANT SELECT, INSERT, UPDATE, DELETE, DROP, CREATE VIEW, INDEX, EXECUTE ON $DBNAME.* TO $DBUSER@localhost IDENTIFIED BY '"$DBPASS"';FLUSH PRIVILEGES;"
mysql -u root -e "$sql";
mysql -u root $DBNAME < /usr/share/icinga2-ido-mysql/schema/mysql.sql
break ;;
P*|p*) DBTYPE="pgsql"
apk info -q -e postgresql || apk add postgresql
rc-service postgresql status || rc-service postgresql start
psql -U postgres -c "CREATE ROLE $DBUSER WITH CREATEDB LOGIN PASSWORD '"$DBPASS"';"
psql -U postgres -c "CREATE DATABASE $DBNAME WITH OWNER $DBUSER ;"
psql -U $DBUSER -W -d $DBNAME < /usr/share/icinga2-ido-pgsql/schema/pgsql.sql
break ;;
*) echo -e "\nWrong choice. Choose the DB you want to use: [M]ysql|[M]ariaDB or [P]ostgreSQL : " ;;
esac
done
configure_icinga2 $DBTYPE && echo -e "\nConfiguration $DBTYPE is done. Please restart Icinga.\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment