#!/bin/bash # ## Installation and maintenance for LibreNMS readonly toolName="librenms" readonly toolUser="librenms" libreDeps='acl curl fping git graphviz imagemagick mtr-tiny nmap python3-dotenv python3-pymysql python3-redis python3-setuptools python3-systemd python3-pip rrdtool snmp snmpd whois' librePhpDeps=(cli curl fpm gd gmp mbstring mysql snmp xml zip) sq_aptOpt= phpVersion= seq_config() { #initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" if initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then info " Install: $LNMS_DIR" info " Backup: $LNMS_BU_DIR" else dry || return 1 fi return 0 } step_1_info() { echo "Updating apt"; } step_1_alias() { echo "install"; } step_1() { exe apt update } step_2_info() { echo "Installing $toolName dependencies:" echoinfo "$libreDeps" } step_2() { exe apt install $libreDeps ${sq_aptOpt} endReturn "Failed to install $toolName dependencies" } step_3_info() { echo "Installing PHP related packages:" fetchPhpVersion echoinfo "${librePhpDeps[@]/#/php${phpVersion}-}" } step_3() { fetchPhpVersion exe apt install "${librePhpDeps[@]/#/php${phpVersion}-}" ${sq_aptOpt} endReturn "Failed to install $toolName php dependencies" } step_4_info() { echo "Adding $toolName user ($toolUser)"; } step_4() { exe useradd $toolUser -d "$LNMS_DIR" -M -r -s "$(which bash)" saveReturn $? endReturn "Failed to create user $toolUser" } step_5_info() { echo "Clone $toolName git repository"; } step_5() { exe git clone https://github.com/librenms/librenms.git "$LNMS_DIR" # set permissions step fix } step_6_info() { echo "Installing $toolName using composer"; } step_6() { exe sudo -u librenms "${LNMS_DIR}/scripts/composer_wrapper.php" install --no-dev endReturn "Failed to install php dependencies" } step_10_info() { echo "Create mysql database for $toolName"; } step_10() { local libreDbOpt= if [ -n "$LNMS_DB_NAME" ] ; then libreDbOpt="-d $LNMS_DB_NAME" fi exe "${seq_origin}"/mysql.sh ${sqr_args} createdb --charset utf8 "$libreDbOpt" endReturn "Failed to create mysql database $LNMS_DB_NAME" } step_11_info() { echo "MariaDB configuration"; } step_11() { color green cat << SQLCONF_END Edit or create /etc/mysql/mariadb.conf.d/90-myconfig.cnf and add: ------------------------ [mysqld] innodb_file_per_table=1 lower_case_table_names=0 ------------------------ Restart mysql afterwards: service mysql restart SQLCONF_END } step_12_info() { echo "PHP fpm/cli timezone configuration"; } step_12() { color green fetchPhpVersion cat << PHPCONF_END Ensure date.timezone is set in php.ini to your preferred time zone. See http://php.net/manual/en/timezones.php for a list of supported timezones. Valid examples are: "America/New_York", "Europe/Berlin", "Etc/UTC". vi /etc/php/${phpVersion}/fpm/conf.d/90-custom_pi.ini vi /etc/php/${phpVersion}/cli/conf.d/90-custom_pi.ini ------------------------------------------- date.timezone = Europe/Berlin ------------------------------------------- Restart php-fpm afterwards: service php7.3-fpm restart PHPCONF_END } step_13_info() { echo "PHP fpm configuration"; } step_13() { color green cat << FPMCONF_END cp /etc/php/${phpVersion}/fpm/pool.d/www.conf /etc/php/${phpVersion}/fpm/pool.d/librenms.conf vi /etc/php/${phpVersion}/fpm/pool.d/librenms.conf # Change [www] to [librenms]: [librenms] # Change user and group to "librenms": user = librenms group = librenms # Change listen to a unique name: listen = /run/php-fpm-librenms.sock FPMCONF_END } step_14_info() { echo "Nginx configuration"; } step_14() { color green cat << NGINXCONF_END server { listen 80; server_name librenms.example.com; root $LNMS_DIR/html; index index.php; charset utf-8; gzip on; gzip_types text/css application/javascript text/javascript application/x-javascript image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon; location / { try_files \$uri \$uri/ /index.php?\$query_string; } location ~ [^/]\\.php(/|$) { fastcgi_pass unix:/run/php-fpm-librenms.sock; fastcgi_split_path_info ^(.+\\.php)(/.+)$; include fastcgi.conf; } location ~ /\\.(?!well-known).* { deny all; } } NGINXCONF_END } step_20_info() { echo "Create $toolName cron job"; } step_20_alias() { echo "cron"; } step_20() { local lnmsCronLoc="/etc/cron.d/librenms" info -n "Creating $lnmsCronLoc ... " exe cp "${LNMS_DIR}/librenms.nonroot.cron" "$lnmsCronLoc" && info -d "Ok" || info -d "Nok" } step_22_info() { echo "Enable lnms command completion"; } step_22_alias() { echo "cmdcompletion"; } step_22() { local lnmsLocalBin="/usr/local/bin/lnms" exep echo -e '"#!/usr/bin/env bash\nsudo -u '$toolUser' \"'$LNMS_DIR'/lnms\" \"\$@\""' \> "$lnmsLocalBin" exe chmod 744 "$lnmsLocalBin" exe cp "${LNMS_DIR}/misc/lnms-completion.bash" /etc/bash_completion.d/ } step_24_info() { echo "Copy logrotate config"; } step_24_alias() { echo "logrotate"; } step_24() { local lnmsLogrotLoc="/etc/logrotate.d/librenms" info -n "Creating $lnmsLogrotLoc ... " exe cp "${LNMS_DIR}/misc/librenms.logrotate" "$lnmsLogrotLoc" && info -d "Ok" || info -d "Nok" } step_26_info() { echo "Install nagios plugin to enable services"; } step_26_alias() { echo "services"; } step_26() { exe apt install monitoring-plugins info info " [$LNMS_DIR/config.php]" info -a " \$config['show_services'] = 1;" info -a info -a " [/etc/cron.d/librenms]" info -a " */5 * * * * librenms /opt/librenms/services-wrapper.py 1" } step_30_info() { echo "Backup ${toolName} web direcotry"; } step_30_alias() { echo "backup"; } step_30() { info "Backup install directory to ${LNMS_BU_DIR}" exe mkdir -p "$LNMS_BU_DIR" exep cd "${LNMS_DIR}"/.. "&&" tar czf "${LNMS_BU_DIR}/$(date +%Y%m%d)_${toolName}_web.tar.gz" "$(basename "$LNMS_DIR")" } step_31_info() { echo "Backup ${toolName} database"; } step_31_options() { echo "[daily|monthly(default)]"; } step_31_alias() { echo "backupdb"; } step_31() { case "${2:-}" in daily | Daily | DAILY) info "Daily backup..." exep mysqldump --single-transaction -u root "${LNMS_DB_NAME}" \| bzip2 -c \> "${LNMS_BU_DIR}/${toolName}_daily.sql.bz2" ;; *) exe mkdir -p "$LNMS_BU_DIR/monthly" info "Monthly backup..." exep mysqldump --single-transaction -u root "${LNMS_DB_NAME}" \| bzip2 -c \> "${LNMS_BU_DIR}/monthly/$(date +%Y%m%d)_${toolName}.sql.bz2" ;; esac } step_40_info() { echo "Switch $toolName installation to monthly stable"; } step_40() { cat << STABLE_EOF Add following to ${LNMS_DIR}/config.php -------------------------------------- \$config['update_channel'] = 'release'; -------------------------------------- Execute following command afterwards: cd $LNMS_DIR && git fetch --tags && git checkout \$(git describe --tags \$(git rev-list --tags --max-count=1)) STABLE_EOF } step_42_info() { echo "Fix librenms permission"; } step_42_alias() { echo "fix"; } step_42() { exe chown -R ${toolUser}: "$LNMS_DIR" exe chmod 771 "${LNMS_DIR}" exe setfacl -d -m g::rwx "${LNMS_DIR}/rrd" "${LNMS_DIR}/logs" "${LNMS_DIR}/bootstrap/cache/" "${LNMS_DIR}/storage/" exe setfacl -R -m g::rwx "${LNMS_DIR}/rrd" "${LNMS_DIR}/logs" "${LNMS_DIR}/bootstrap/cache/" "${LNMS_DIR}/storage/" } fetchPhpVersion() { if [ -n "${phpVersion}" ] ; then return 0 fi phpVersion="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')" } # shellcheck disable=SC2034 # Appears unused readonly sqr_minVersion=16 # shellcheck disable=SC1091 # Don't follow this source . /usr/local/bin/sequencer.sh