1
0
Fork 0
mirror of https://github.com/librenms/docker.git synced 2026-01-23 10:15:22 +00:00

Merge pull request #2 from connectitnet/master

Add support for distributed pollers and related functionality
This commit is contained in:
CrazyMax 2018-09-24 11:24:18 +02:00 committed by GitHub
commit 7e1aab98aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 195 additions and 34 deletions

5
.gitignore vendored
View file

@ -2,5 +2,8 @@
/.idea
/*.iml
# Visual Studio Code
/.vscode
# App
/.dev
/.dev

View file

@ -16,14 +16,61 @@ LABEL maintainer="CrazyMax" \
org.label-schema.schema-version="1.0"
RUN apk --update --no-cache add \
bash binutils ca-certificates coreutils curl fping git graphviz imagemagick mtr \
mysql-client net-snmp net-snmp-tools nginx nmap openssl python2 py-mysqldb rrdtool runit \
shadow supervisor syslog-ng tzdata util-linux whois \
php7 php7-cli php7-ctype php7-curl php7-fpm php7-gd php7-json php7-mcrypt php7-memcached php7-mbstring php7-mysqli \
php7-opcache php7-openssl php7-pdo php7-pdo_mysql php7-phar php7-posix php7-session php7-simplexml php7-snmp \
php7-tokenizer php7-xml php7-zip \
bash \
binutils \
ca-certificates \
coreutils \
curl \
fping \
git \
graphviz \
imagemagick \
mtr \
mysql-client \
net-snmp \
net-snmp-tools \
nginx \
nmap \
openssl \
php7 \
php7-cli \
php7-ctype \
php7-curl \
php7-fpm \
php7-gd \
php7-json \
php7-mbstring \
php7-mcrypt \
php7-memcached \
php7-mysqli \
php7-opcache \
php7-openssl \
php7-pdo \
php7-pdo_mysql \
php7-phar \
php7-posix \
php7-session \
php7-simplexml \
php7-snmp \
php7-tokenizer \
php7-xml \
php7-zip \
py-mysqldb \
py2-pip \
python2 \
rrdtool \
runit \
shadow \
supervisor \
syslog-ng \
tzdata \
util-linux \
whois \
&& pip install python-memcached \
&& sed -i -e "s/;date\.timezone.*/date\.timezone = UTC/" /etc/php7/php.ini \
&& rm -rf /var/cache/apk/* /var/www/* /tmp/*
&& rm -rf /var/cache/apk/* /var/www/* /tmp/* \
&& setcap cap_net_raw+ep /usr/bin/nmap \
&& setcap cap_net_raw+ep /usr/sbin/fping
ENV LIBRENMS_VERSION="1.43" \
LIBRENMS_PATH="/opt/librenms" \

View file

@ -44,14 +44,30 @@ If you are interested, [check out](https://hub.docker.com/r/crazymax/) my other
* `UPLOAD_MAX_SIZE` : Upload max size (default `16M`)
* `OPCACHE_MEM_SIZE` : PHP OpCache memory consumption (default `128`)
* `LIBRENMS_POLLER_THREADS` : Threads that `poller-wrapper.py` runs (default `16`)
* `LIBRENMS_SNMP_COMMUNITY` : Your community string (default `librenmsdocker`)
* `LIBRENMS_POLLER_INTERVAL` : Interval in minutes at which `poller-wrapper.py` runs (defaults to `5`) [docs](https://docs.librenms.org/#Support/1-Minute-Polling/)
* `LIBRENMS_DISTRIBUTED_POLLER_ENABLE` : Enable distributed poller functionality
* `LIBRENMS_DISTRIBUTED_POLLER_NAME` : Optional name of poller (defaults to hostname)
* `LIBRENMS_DISTRIBUTED_POLLER_GROUP` : By default, all hosts are shared and have the poller_group = 0. To pin a device to a poller, set it to a value greater than 0 and set the same value here. One can also specify a comma separated string of poller groups. The poller will then poll devices from any of the groups listed. [docs](https://docs.librenms.org/#Extensions/Distributed-Poller/#distributed-poller)
* `LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_HOST` : Memcached server for poller synchronization (Defaults to `$MEMCACHED_HOST`)
* `LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_PORT` : Port of memcached server (Defaults to `$MEMCACHED_PORT`)
* `LIBRENMS_CRON_DISCOVERY_ENABLE` : Enable LibreNMS discovery for this container cronjobs (default `true`)
* `LIBRENMS_CRON_DAILY_ENABLE` : Enable LibreNMS daily script for this container cronjobs (default `true`)
* `LIBRENMS_CRON_ALERTS_ENABLE` : Enable LibreNMS alerts generation for this container cronjobs (default `true`)
* `LIBRENMS_CRON_BILLING_ENABLE` : Enable LibreNMS billing polling for this container cronjobs (default `true`)
* `LIBRENMS_CRON_BILLING_CALCULATE_ENABLE` : Enable LibreNMS billing for this container cronjobs (default `true`)
* `LIBRENMS_CRON_CHECK_SERVICES_ENABLE` : Enable LibreNMS service checks for this container cronjobs (default `true`)
* `LIBRENMS_CRON_POLLER_ENABLE` : Enable LibreNMS polling for this container cronjobs (default `true`)
* `LIBRENMS_SNMP_COMMUNITY` : This container's SNMP v2c community string (default `librenmsdocker`)
* `DB_HOST` : MySQL database hostname / IP address
* `DB_PORT` : MySQL database port (default `3306`)
* `DB_NAME` : MySQL database name (default `librenms`)
* `DB_USER` : MySQL user (default `librenms`)
* `DB_PASSWORD` : MySQL password (default `librenms`)
* `DB_TIMEOUT` : Time in seconds after which we stop trying to reach the MySQL server (useful for clusters, default `30`)
* `MEMCACHED_HOST` : Hostname / IP address of a Memcached server
* `MEMCACHED_PORT` : Port of the Memcached server
* `RRDCACHED_HOST` : Hostname / IP address of a RRDcached server
* `RRDCACHED_PORT` : Port of the RRDcached server
### Volumes

View file

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
function runas_librenms() {
su - librenms -s /bin/sh -c "$1"
@ -12,13 +12,55 @@ MEMORY_LIMIT=${MEMORY_LIMIT:-"256M"}
UPLOAD_MAX_SIZE=${UPLOAD_MAX_SIZE:-"16M"}
OPCACHE_MEM_SIZE=${OPCACHE_MEM_SIZE:-"128"}
MEMCACHED_PORT=${MEMCACHED_PORT:-"11211"}
RRDCACHED_PORT=${RRDCACHED_PORT:-"42217"}
LIBRENMS_POLLER_THREADS=${LIBRENMS_POLLER_THREADS:-"16"}
LIBRENMS_SNMP_COMMUNITY=${LIBRENMS_SNMP_COMMUNITY:-"librenmsdocker"}
LIBRENMS_POLLER_INTERVAL=${LIBRENMS_POLLER_INTERVAL:-"5"}
LIBRENMS_DISTRIBUTED_POLLER_ENABLE=${LIBRENMS_DISTRIBUTED_POLLER_ENABLE:-false}
LIBRENMS_DISTRIBUTED_POLLER_NAME=${LIBRENMS_DISTRIBUTED_POLLER_NAME:-$(hostname -f)}
LIBRENMS_DISTRIBUTED_POLLER_GROUP=${LIBRENMS_DISTRIBUTED_POLLER_GROUP:-'0'}
LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_HOST=${LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_HOST:-"${MEMCACHED_HOST}"}
LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_PORT=${LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_PORT:-"${MEMCACHED_PORT}"}
LIBRENMS_CRON_DISCOVERY_ENABLE=${LIBRENMS_CRON_DISCOVERY_ENABLE:-true}
LIBRENMS_CRON_DAILY_ENABLE=${LIBRENMS_CRON_DAILY_ENABLE:-true}
LIBRENMS_CRON_ALERTS_ENABLE=${LIBRENMS_CRON_ALERTS_ENABLE:-true}
LIBRENMS_CRON_BILLING_ENABLE=${LIBRENMS_CRON_BILLING_ENABLE:-true}
LIBRENMS_CRON_BILLING_CALCULATE_ENABLE=${LIBRENMS_CRON_BILLING_CALCULATE_ENABLE:-true}
LIBRENMS_CRON_CHECK_SERVICES_ENABLE=${LIBRENMS_CRON_CHECK_SERVICES_ENABLE:-true}
LIBRENMS_CRON_POLLER_ENABLE=${LIBRENMS_CRON_POLLER_ENABLE:-true}
DB_PORT=${DB_PORT:-"3306"}
DB_NAME=${DB_NAME:-"librenms"}
DB_USER=${DB_USER:-"librenms"}
DB_PASSWORD=${DB_PASSWORD:-"asupersecretpassword"}
DB_TIMEOUT=${DB_TIMEOUT:-"30"}
# 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"
}
# Timezone
echo "Setting timezone to ${TZ}..."
@ -53,6 +95,7 @@ sed -e "s/@UPLOAD_MAX_SIZE@/$UPLOAD_MAX_SIZE/g" \
# 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
@ -77,6 +120,11 @@ if [ -z "$DB_HOST" ]; then
>&2 echo "ERROR: DB_HOST must be defined"
exit 1
fi
file_env 'DB_PASSWORD'
if [ -z "$DB_PASSWORD" ]; then
>&2 echo "ERROR: Either DB_PASSWORD or DB_PASSWORD_FILE must be defined"
exit 1
fi
cat > ${LIBRENMS_PATH}/config.d/database.php <<EOL
<?php
\$config['db_host'] = '${DB_HOST}';
@ -112,7 +160,7 @@ if [ ! -z "${MEMCACHED_HOST}" ]; then
<?php
\$config['memcached']['enable'] = true;
\$config['memcached']['host'] = '$MEMCACHED_HOST';
\$config['memcached']['port'] = 11211;
\$config['memcached']['port'] = $MEMCACHED_PORT;
EOL
fi
@ -120,10 +168,35 @@ fi
if [ ! -z "${RRDCACHED_HOST}" ]; then
cat > ${LIBRENMS_PATH}/config.d/rrdcached.php <<EOL
<?php
\$config['rrdcached'] = "${RRDCACHED_HOST}:42217";
\$config['rrdcached'] = "${RRDCACHED_HOST}:${RRDCACHED_PORT}";
\$config['rrdtool_version'] = '1.7.0';
EOL
fi
# Config : Ditributed poller
if [ ! -z "${LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_HOST}" -a ! -z "${RRDCACHED_HOST}" -a $LIBRENMS_DISTRIBUTED_POLLER_ENABLE = true ]; then
cat > ${LIBRENMS_PATH}/config.d/distributed_poller.php <<EOL
<?php
\$config['distributed_poller'] = true;
\$config['distributed_poller_name'] = '$LIBRENMS_DISTRIBUTED_POLLER_NAME';
\$config['distributed_poller_group'] = '$LIBRENMS_DISTRIBUTED_POLLER_GROUP';
\$config['distributed_poller_memcached_host'] = '$LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_HOST';
\$config['distributed_poller_memcached_port'] = $LIBRENMS_DISTRIBUTED_POLLER_MEMCACHED_PORT;
EOL
fi
# Fix perms
echo "Fixing permissions..."
chown -R librenms. ${DATA_PATH} \
${LIBRENMS_PATH}/config.d \
${LIBRENMS_PATH}/bootstrap \
${LIBRENMS_PATH}/storage
chmod ug+rw ${DATA_PATH}/logs \
${DATA_PATH}/rrd \
${LIBRENMS_PATH}/bootstrap/cache \
${LIBRENMS_PATH}/storage \
${LIBRENMS_PATH}/storage/framework/*
# Sidecar cron container ?
if [ "$1" == "/usr/local/bin/cron" ]; then
echo ">>"
@ -131,17 +204,52 @@ if [ "$1" == "/usr/local/bin/cron" ]; then
echo ">>"
# Init
if [ -z "$CRONTAB_PATH" ]; then
>&2 echo "ERROR: CRONTAB_PATH must be defined"
exit 1
fi
rm -rf ${CRONTAB_PATH}
mkdir -m 0644 -p ${CRONTAB_PATH}
touch ${CRONTAB_PATH}/librenms
# Add crons
# Add crontab
cat ${LIBRENMS_PATH}/librenms.nonroot.cron > ${CRONTAB_PATH}/librenms
sed -i -e "s/ librenms //" ${CRONTAB_PATH}/librenms
sed -i -e "s/poller-wrapper.py 16/poller-wrapper.py ${LIBRENMS_POLLER_THREADS}/g" ${CRONTAB_PATH}/librenms
if [ $LIBRENMS_CRON_DISCOVERY_ENABLE != true ]; then
sed -i "/discovery.php/d" ${CRONTAB_PATH}/librenms
fi
# Fix perms
echo "Fixing permissions..."
if [ $LIBRENMS_CRON_DAILY_ENABLE != true ]; then
sed -i "/daily.sh/d" ${CRONTAB_PATH}/librenms
fi
if [ $LIBRENMS_CRON_ALERTS_ENABLE != true ]; then
sed -i "/alerts.php/d" ${CRONTAB_PATH}/librenms
fi
if [ $LIBRENMS_CRON_BILLING_ENABLE != true ]; then
sed -i "/poll-billing.php/d" ${CRONTAB_PATH}/librenms
fi
if [ $LIBRENMS_CRON_BILLING_CALCULATE_ENABLE != true ]; then
sed -i "/billing-calculate.php/d" ${CRONTAB_PATH}/librenms
fi
if [ $LIBRENMS_CRON_CHECK_SERVICES_ENABLE != true ]; then
sed -i "/check-services.php/d" ${CRONTAB_PATH}/librenms
fi
sed -i "/poller-wrapper.py/d" ${CRONTAB_PATH}/librenms
if [ $LIBRENMS_CRON_POLLER_ENABLE = true ]; then
cat >> ${CRONTAB_PATH}/librenms <<EOL
*/${LIBRENMS_POLLER_INTERVAL} * * * * /opt/librenms/cronic /opt/librenms/poller-wrapper.py ${LIBRENMS_POLLER_THREADS}
EOL
fi
# Fix crontab perms
echo "Fixing crontab permissions..."
chmod -R 0644 ${CRONTAB_PATH}
elif [ "$1" == "/usr/sbin/syslog-ng" ]; then
echo ">>"
@ -152,30 +260,17 @@ elif [ "$1" == "/usr/sbin/syslog-ng" ]; then
mkdir -p ${DATA_PATH}/syslog-ng /run/syslog-ng
chown -R librenms. ${DATA_PATH}/syslog-ng /run/syslog-ng
else
# Fix perms
echo "Fixing permissions..."
chown -R librenms. ${DATA_PATH} \
${LIBRENMS_PATH}/config.d \
${LIBRENMS_PATH}/bootstrap \
${LIBRENMS_PATH}/storage
chmod ug+rw ${DATA_PATH}/logs \
${DATA_PATH}/rrd \
${LIBRENMS_PATH}/bootstrap/cache \
${LIBRENMS_PATH}/storage \
${LIBRENMS_PATH}/storage/framework/*
echo "Waiting database..."
waitdb_timeout=30
echo "Waiting ${DB_TIMEOUT}s for database to be ready..."
counter=1
while ! ${dbcmd} -e "show databases;" > /dev/null 2>&1; do
sleep 1
counter=`expr $counter + 1`
if [ ${counter} -gt ${waitdb_timeout} ]; then
if [ ${counter} -gt ${DB_TIMEOUT} ]; then
>&2 echo "ERROR: Failed to connect to database on $DB_HOST"
exit 1
fi;
done
echo "Database up!"
echo "Database ready!"
counttables=$(echo 'SHOW TABLES' | ${dbcmd} "$DB_NAME" | wc -l)