This repository was archived by the owner on Mar 31, 2026. It is now read-only.
forked from librenms/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-config.sh
More file actions
259 lines (227 loc) · 8.78 KB
/
Copy path03-config.sh
File metadata and controls
259 lines (227 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/with-contenv bash
# shellcheck shell=bash
set -e
# From https://github.com/docker-library/mariadb/blob/master/docker-entrypoint.sh#L21-L41
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(<"${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
TZ=${TZ:-UTC}
MEMORY_LIMIT=${MEMORY_LIMIT:-256M}
UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE:-16M}
CLEAR_ENV=${CLEAR_ENV:-yes}
FPM_PM_MAX_CHILDREN=${FPM_PM_MAX_CHILDREN:-15}
FPM_PM_START_SERVERS=${FPM_PM_START_SERVERS:-2}
FPM_PM_MIN_SPARE_SERVERS=${FPM_PM_MIN_SPARE_SERVERS:-1}
FPM_PM_MAX_SPARE_SERVERS=${FPM_PM_MAX_SPARE_SERVERS:-6}
OPCACHE_MEM_SIZE=${OPCACHE_MEM_SIZE:-128}
LISTEN_IPV6=${LISTEN_IPV6:-true}
REAL_IP_FROM=${REAL_IP_FROM:-"0.0.0.0/32"}
REAL_IP_HEADER=${REAL_IP_HEADER:-"X-Forwarded-For"}
LOG_IP_VAR=${LOG_IP_VAR:-remote_addr}
MAX_INPUT_VARS=${MAX_INPUT_VARS:-1000}
MEMCACHED_PORT=${MEMCACHED_PORT:-11211}
DB_PORT=${DB_PORT:-3306}
DB_NAME=${DB_NAME:-librenms}
DB_USER=${DB_USER:-librenms}
DB_TIMEOUT=${DB_TIMEOUT:-30}
LIBRENMS_BASE_URL=${LIBRENMS_BASE_URL:-/}
# Timezone
echo "Setting timezone to ${TZ}..."
ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime
echo ${TZ} >/etc/timezone
# PHP
echo "Setting PHP-FPM configuration..."
sed -e "s/@MEMORY_LIMIT@/$MEMORY_LIMIT/g" \
-e "s/@UPLOAD_MAX_SIZE@/$UPLOAD_MAX_SIZE/g" \
-e "s/@CLEAR_ENV@/$CLEAR_ENV/g" \
-e "s/@FPM_PM_MAX_CHILDREN@/$FPM_PM_MAX_CHILDREN/g" \
-e "s/@FPM_PM_START_SERVERS@/$FPM_PM_START_SERVERS/g" \
-e "s/@FPM_PM_MIN_SPARE_SERVERS@/$FPM_PM_MIN_SPARE_SERVERS/g" \
-e "s/@FPM_PM_MAX_SPARE_SERVERS@/$FPM_PM_MAX_SPARE_SERVERS/g" \
/tpls/etc/php83/php-fpm.d/www.conf >/etc/php83/php-fpm.d/www.conf
echo "Setting PHP INI configuration..."
sed -i "s|memory_limit.*|memory_limit = ${MEMORY_LIMIT}|g" /etc/php83/php.ini
sed -i "s|;date\.timezone.*|date\.timezone = ${TZ}|g" /etc/php83/php.ini
sed -i "s|;max_input_vars.*|max_input_vars = ${MAX_INPUT_VARS}|g" /etc/php83/php.ini
# OpCache
echo "Setting OpCache configuration..."
sed -e "s/@OPCACHE_MEM_SIZE@/$OPCACHE_MEM_SIZE/g" \
/tpls/etc/php83/conf.d/opcache.ini >/etc/php83/conf.d/opcache.ini
# Nginx
echo "Setting Nginx configuration..."
sed -e "s#@UPLOAD_MAX_SIZE@#$UPLOAD_MAX_SIZE#g" \
-e "s#@REAL_IP_FROM@#$REAL_IP_FROM#g" \
-e "s#@REAL_IP_HEADER@#$REAL_IP_HEADER#g" \
-e "s#@LOG_IP_VAR@#$LOG_IP_VAR#g" \
/tpls/etc/nginx/nginx.conf >/etc/nginx/nginx.conf
if [ "$LISTEN_IPV6" != "true" ]; then
sed -e '/listen \[::\]:/d' -i /etc/nginx/nginx.conf
fi
# SNMP
echo "Updating SNMP community..."
file_env 'LIBRENMS_SNMP_COMMUNITY' 'librenmsdocker'
sed -i -e "s/RANDOMSTRINGGOESHERE/${LIBRENMS_SNMP_COMMUNITY}/" /etc/snmp/snmpd.conf
# Init files and folders
echo "Initializing LibreNMS files / folders..."
mkdir -p /data/config /data/logs /data/monitoring-plugins /data/plugins /data/rrd /data/weathermap /data/alert-templates
if [ -d "${LIBRENMS_PATH}/html/plugins/Weathermap" ]; then
if [ -d "${LIBRENMS_PATH}/html/plugins/Weathermap/configs" ] && [ ! -L "${LIBRENMS_PATH}/html/plugins/Weathermap/configs" ]; then
rm -rf ${LIBRENMS_PATH}/html/plugins/Weathermap/configs
fi
if [ ! -L "${LIBRENMS_PATH}/html/plugins/Weathermap/configs" ]; then
ln -sf /data/weathermap ${LIBRENMS_PATH}/html/plugins/Weathermap/configs
fi
chown -h librenms:librenms ${LIBRENMS_PATH}/html/plugins/Weathermap/configs
find /data/weathermap ${LIBRENMS_PATH}/html/plugins/Weathermap/output \( ! -user librenms -o ! -group librenms \) -exec chown librenms:librenms {} +
fi
# cleanup bad symlink: https://github.com/librenms/docker/issues/294#issuecomment-1190389960
if [ -L "/data/weathermap/weathermap" ]; then
rm /data/weathermap/weathermap
fi
touch /data/logs/librenms.log
rm -rf ${LIBRENMS_PATH}/logs
rm -f ${LIBRENMS_PATH}/config.d/*
mkdir -p /etc/logrotate.d
touch /etc/logrotate.d/librenms
echo "Setting LibreNMS configuration..."
# Env file
if [ -z "$DB_HOST" ]; then
echo >&2 "ERROR: DB_HOST must be defined"
exit 1
fi
file_env 'DB_PASSWORD'
if [ -z "$DB_PASSWORD" ]; then
echo >&2 "ERROR: Either DB_PASSWORD or DB_PASSWORD_FILE must be defined"
exit 1
fi
cat >${LIBRENMS_PATH}/.env <<EOL
APP_URL=${LIBRENMS_BASE_URL}
DB_HOST=${DB_HOST}
DB_PORT=${DB_PORT}
DB_DATABASE=${DB_NAME}
DB_USERNAME=${DB_USER}
DB_PASSWORD="${DB_PASSWORD}"
EOL
# Config : Directories
cat >${LIBRENMS_PATH}/database/seeders/config/directories.yaml <<EOL
install_dir: '${LIBRENMS_PATH}'
log_dir: /data/logs
rrd_dir: /data/rrd
EOL
ln -sf /data/logs ${LIBRENMS_PATH}/logs
# Config : Server
cat >${LIBRENMS_PATH}/database/seeders/config/server.yaml <<EOL
own_hostname: '$(hostname)'
base_url: '${LIBRENMS_BASE_URL}'
EOL
# Config : User
cat >${LIBRENMS_PATH}/database/seeders/config/user.yaml <<EOL
user: librenms
group: librenms
EOL
# Config : Fping
cat >${LIBRENMS_PATH}/database/seeders/config/fping.yaml <<EOL
fping: /usr/sbin/fping
fping6: /usr/sbin/fping6
EOL
# Config : ipmitool
cat >${LIBRENMS_PATH}/database/seeders/config/ipmitool.yaml <<EOL
ipmitool: /usr/sbin/ipmitool
EOL
# Config : Disable autoupdate (set in config.php so it cannot be overridden in the webui)
cat >${LIBRENMS_PATH}/config.d/autoupdate.php <<EOL
<?php
\$config['update'] = 0;
EOL
# Config : Services
cat >${LIBRENMS_PATH}/database/seeders/config/services.yaml <<EOL
show_services: true
nagios_plugins: /usr/lib/monitoring-plugins
EOL
# Config : RRDCached, apply RRDCACHED_SERVER as php as it would be expected to change with the variable
if [ -n "${RRDCACHED_SERVER}" ]; then
cat >${LIBRENMS_PATH}/config.d/rrdcached.php <<EOL
<?php
\$config['rrdcached'] = "${RRDCACHED_SERVER}";
EOL
fi
cat >${LIBRENMS_PATH}/database/seeders/config/rrdtool.yaml <<EOL
rrdtool_version: "1.7.2"
EOL
# Config : Dispatcher
cat >${LIBRENMS_PATH}/database/seeders/config/dispatcher.yaml <<EOL
service_update_enabled: false
service_watchdog_enabled: false
EOL
# Check plugins
echo "Checking LibreNMS plugins..."
plugins=$(ls -l /data/plugins | egrep '^d' | awk '{print $9}')
for plugin in ${plugins}; do
if [ "${plugin}" == "Weathermap" ]; then
echo " WARNING: Plugin Weathermap cannot be overriden. Skipping..."
continue
fi
echo " Linking plugin ${plugin}..."
if [ -d "${LIBRENMS_PATH}/html/plugins/${plugin}" ]; then
rm -rf "${LIBRENMS_PATH}/html/plugins/${plugin}"
fi
ln -sf "/data/plugins/${plugin}" "${LIBRENMS_PATH}/html/plugins/${plugin}"
chown -h librenms:librenms "${LIBRENMS_PATH}/html/plugins/${plugin}"
done
# Fix perms
echo "Fixing perms..."
chown librenms:librenms /data/config /data/monitoring-plugins /data/plugins /data/rrd /data/weathermap /data/alert-templates
find /data/logs ${LIBRENMS_PATH}/composer* ${LIBRENMS_PATH}/config.d ${LIBRENMS_PATH}/bootstrap ${LIBRENMS_PATH}/logs ${LIBRENMS_PATH}/storage ${LIBRENMS_PATH}/vendor \( ! -user librenms -o ! -group librenms \) -exec chown librenms:librenms {} +
chmod ug+rw /data/logs /data/rrd ${LIBRENMS_PATH}/bootstrap/cache ${LIBRENMS_PATH}/storage ${LIBRENMS_PATH}/storage/framework/*
# Check additional Monitoring plugins
echo "Checking additional Monitoring plugins..."
mon_plugins=$(ls -l /data/monitoring-plugins | egrep '^-' | awk '{print $9}')
for mon_plugin in ${mon_plugins}; do
if [ -f "/usr/lib/monitoring-plugins/${mon_plugin}" ]; then
echo " WARNING: Official Monitoring plugin ${mon_plugin} cannot be overriden. Skipping..."
continue
fi
if [[ ${mon_plugin} != check_* ]]; then
echo " WARNING: Monitoring plugin filename ${mon_plugin} invalid. It must start with 'check_'. Skipping..."
continue
fi
if [[ ! -x "/data/monitoring-plugins/${mon_plugin}" ]]; then
echo " WARNING: Monitoring plugin file ${mon_plugin} has to be executable. Skipping..."
continue
fi
echo " Adding ${mon_plugin} Monitoring plugin"
ln -sf /data/monitoring-plugins/${mon_plugin} /usr/lib/monitoring-plugins/${mon_plugin}
done
# Check alert templates
echo "Checking alert templates..."
templates=$(ls -l /data/alert-templates | egrep '^-' | awk '{print $9}')
for template in ${templates}; do
if [ -f "${LIBRENMS_PATH}/resources/views/alerts/templates/${template}" ]; then
echo " WARNING: Default alert template ${template} cannot be overriden. Skipping..."
continue
fi
if [[ ${template} != *.php ]]; then
echo " WARNING: Alert template filename ${template} invalid. It must end with '.php'. Skipping..."
continue
fi
echo " Adding ${template} alert template"
ln -sf /data/alert-templates/${template} ${LIBRENMS_PATH}/resources/views/alerts/templates/${template}
done