Files
shell_sequencer/seqs/librenms.sh
Martin Winkler 8e63bbe7df Sequencer new option to open sequence config file when initialised using initSeqConfig
Sequences with exit in step_config, changed to return to support opening config file on first run
2021-03-19 01:32:59 +01:00

280 lines
8.1 KiB
Bash
Executable File

#!/bin/bash
#
## Installation and maintenance for LibreNMS
toolName="librenms"
toolUser="librenms"
phpVersion=
libreDeps='acl curl composer fping git graphviz imagemagick mtr-tiny nmap rrdtool snmp snmpd whois python3-dotenv python3-pymysql python3-redis python3-setuptools'
librePhpDeps='php${phpVersion}-cli php${phpVersion}-curl php${phpVersion}-fpm php${phpVersion}-gd php${phpVersion}-json php${phpVersion}-mbstring php${phpVersion}-mysql php${phpVersion}-snmp php${phpVersion}-xml php${phpVersion}-zip'
#librePhpDeps="composer php-cli-prompt php-composer-ca-bundle php-composer-semver php-composer-spdx-licenses php-json-schema php-psr-log php-symfony-console php-symfony-filesystem php-symfony-finder php-symfony-polyfill-mbstring php-symfony-process"
#librePackages="fping git graphviz imagemagick mtr-tiny nmap python-memcache python-mysqldb rrdtool snmp snmpd whoisi nagios-plugins"
# Get script working directory
# (when called from a different directory)
WDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd )"
CONFIG=0
SCRIPT_NAME=$(basename -- "$0")
SCRIPT_NAME=${SCRIPT_NAME%%.*}
CONFIG_FILE_NAME="${SCRIPT_NAME}.cfg"
CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example"
step_config() {
initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE"
if [ $? -eq 0 ] ; then
CONFIG=1
echoseq " Install: $LNMS_DIR"
echoseq " Backup: $LNMS_BU_DIR"
else
[ $DRY -eq 0 ] && return 1
fi
}
step_1_info() { echo "Updating apt"; }
step_1_alias() { ALIAS="install"; }
step_1() {
exe apt update
}
step_2_info() {
echo "Installing $toolname dependencies:"
echoinfo "$libreDeps"
}
step_2() {
exe apt install $libreDeps
endReturn -o $? "Failed to install $toolName dependencies"
}
step_3_info() {
echo "Installing PHP related packages:"
fetchPhpVersion
echoinfo `eval echo "$librePhpDeps"`
}
step_3() {
fetchPhpVersion
exe apt install `eval echo "$librePhpDeps"`
endReturn -o $? "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 $?
#exe usermod -a -G librenms www-data
#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 -o $? "Failed to install php dependencies"
}
step_10_info() { echo "Create mysql database for $toolName"; }
step_10() {
local libreDbOpt=
if [ ! -z "$LNMS_DB_NAME" ] ; then
libreDbOpt="-d $LNMS_DB_NAME"
fi
exe ${WDIR}/mysql.sh -q createdb --charset utf8 $libreDbOpt
endReturn -o $? "Failed to create mysql database $LNMS_DB_NAME"
}
step_11_info() { echo "MariaDB configuration"; }
step_11() {
outColor 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() {
outColor green
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/7.3/fpm/conf.d/90-custom_pi.ini
vi /etc/php/7.3/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() {
outColor green
cat << FPMCONF_END
cp /etc/php/7.3/fpm/pool.d/www.conf /etc/php/7.3/fpm/pool.d/librenms.conf
vi /etc/php/7.3/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() {
outColor 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() { ALIAS="cron"; }
step_20() {
echoseq -n " [I] Creating $lnmsCronLoc ... "
exe cp "${LNMS_DIR}/librenms.nonroot.cron" "$lnmsCronLoc" && echoseq "Ok" || echoseq "Nok"
}
lnmsCronLoc="/etc/cron.d/librenms"
step_22_info() { echo "Enable lnms command completion"; }
step_22_alias() { ALIAS="cmdcompletion"; }
step_22() {
exep 'echo -e "#!/bin/bash\nsudo -u '$toolUser' \"'$LNMS_DIR'/lnms\" \"\$@\"" > '"$lnmsLocalBin"
exe chmod 744 "$lnmsLocalBin"
exe cp "${LNMS_DIR}/misc/lnms-completion.bash" /etc/bash_completion.d/
}
lnmsLocalBin="/usr/local/bin/lnms"
step_24_info() { echo "Copy logrotate config"; }
step_24_alias() { ALIAS="logrotate"; }
step_24() {
echoseq -n " [I] Creating $lnmsLogrotLoc ... "
exe cp "${LNMS_DIR}/misc/librenms.logrotate" "$lnmsLogrotLoc" && echoseq "Ok" || echoseq "Nok"
}
lnmsLogrotLoc="/etc/logrotate.d/librenms"
step_26_info() { echo "Install nagios plugin to enable services"; }
step_26_alias() { ALIAS="services"; }
step_26() {
exe apt install monitoring-plugins
echoseq
echoseq " [$LNMS_DIR/config.php]"
echoseq "\$config['show_services'] = 1;"
echoseq
echoseq " [/etc/cron.d/librenms]"
echoseq " */5 * * * * librenms /opt/librenms/services-wrapper.py 1"
}
step_30_info() { echo "Backup ${toolName} web direcotry"; }
step_30_alias() { ALIAS="backup"; }
step_30() {
echoseq " [I] 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 [daily|monthly(default)]"; }
step_31_alias() { ALIAS="backupdb"; }
step_31() {
case "$2" in
daily|Daily|DAILY)
echoseq " [I] 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"
echoseq " [I] 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() { ALIAS="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 [ ! -z $phpVersion ] ; then
return 0
fi
phpVersion="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')"
}
# Sequence Revision
VERSION_SEQREV=12
. /usr/local/bin/sequencer.sh