Docker: Zabbix en Raspberry Pi

12 minutos de lectura

Sigo probando diferentes soluciones para la recolección y visualización de estadísticas de los diferentes dispositivos de mi red. Fue a raíz de escuchar un podcas de hardwareadictos, que decidi probar zabbix en mi Raspberry Pi 3, pero surgió un problema, y es que no existe una imagen par apoder crear un contenedor docker para arquitectura armv7, y sinceramente teniendo todos los servicios corriendo en doker, me daba una pereza tremenda tener que instalar y configúralo todo desde cero, apache, MariaDB, etc.

Navegando por el repositorio de zabbix puedes encontrar el fichero Dockerfile tanto del servidor con soporte para MySQL o PostgreSQL así como de la interfaz web para su administración, bien, teniendo el Dockerfile, lo demás es coser y cantar.

Mariadb

Aprovechando que ya tengo la base de datos mariadb corriendo en un contenedor, voy a utilizarlo para almacenar la metricas de zabbix,

Entramos en la shell del contenedor y creamos la base de datos, el usuario y le damos privilegios para manejarla.

 1docker exec -it mariadb bash
 2
 3mysql -uroot -p
 4
 5
 6create database zabbix character set utf8 collate utf8_bin;
 7
 8grant all privileges on zabbix.* to zabbix@localhost identified by 'YOUR_PASS';
 9grant all privileges on zabbix.* to zabbix@localhost identified by 'YOUR_PASS';
10
11GRANT USAGE ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';
12flush privilegies;
13
14exit;

Servidor zabbix-mysql

Vamos a construir el contenedor con soporte para MariaDB

Crea un fichero llamado docker-entrypoint.sh con el siguiente contenido:

  1
  2#!/bin/bash
  3
  4set -o pipefail
  5
  6set +e
  7
  8# Script trace mode
  9if [ "${DEBUG_MODE}" == "true" ]; then
 10    set -o xtrace
 11fi
 12
 13# Default directories
 14# User 'zabbix' home directory
 15ZABBIX_USER_HOME_DIR="/var/lib/zabbix"
 16# Configuration files directory
 17ZABBIX_ETC_DIR="/etc/zabbix"
 18
 19# usage: file_env VAR [DEFAULT]
 20# as example: file_env 'MYSQL_PASSWORD' 'zabbix'
 21#    (will allow for "$MYSQL_PASSWORD_FILE" to fill in the value of "$MYSQL_PASSWORD" from a file)
 22# unsets the VAR_FILE afterwards and just leaving VAR
 23file_env() {
 24    local var="$1"
 25    local fileVar="${var}_FILE"
 26    local defaultValue="${2:-}"
 27
 28    if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
 29        echo "**** Both variables $var and $fileVar are set (but are exclusive)"
 30        exit 1
 31    fi
 32
 33    local val="$defaultValue"
 34
 35    if [ "${!var:-}" ]; then
 36        val="${!var}"
 37        echo "** Using ${var} variable from ENV"
 38    elif [ "${!fileVar:-}" ]; then
 39        if [ ! -f "${!fileVar}" ]; then
 40            echo "**** Secret file \"${!fileVar}\" is not found"
 41            exit 1
 42        fi
 43        val="$(< "${!fileVar}")"
 44        echo "** Using ${var} variable from secret file"
 45    fi
 46    export "$var"="$val"
 47    unset "$fileVar"
 48}
 49
 50escape_spec_char() {
 51    local var_value=$1
 52
 53    var_value="${var_value//\\/\\\\}"
 54    var_value="${var_value//[$'\n']/}"
 55    var_value="${var_value//\//\\/}"
 56    var_value="${var_value//./\\.}"
 57    var_value="${var_value//\*/\\*}"
 58    var_value="${var_value//^/\\^}"
 59    var_value="${var_value//\$/\\\$}"
 60    var_value="${var_value//\&/\\\&}"
 61    var_value="${var_value//\[/\\[}"
 62    var_value="${var_value//\]/\\]}"
 63
 64    echo "$var_value"
 65}
 66
 67update_config_var() {
 68    local config_path=$1
 69    local var_name=$2
 70    local var_value=$3
 71    local is_multiple=$4
 72
 73    if [ ! -f "$config_path" ]; then
 74        echo "**** Configuration file '$config_path' does not exist"
 75        return
 76    fi
 77
 78    echo -n "** Updating '$config_path' parameter \"$var_name\": '$var_value'... "
 79
 80    # Remove configuration parameter definition in case of unset parameter value
 81    if [ -z "$var_value" ]; then
 82        sed -i -e "/^$var_name=/d" "$config_path"
 83        echo "removed"
 84        return
 85    fi
 86
 87    # Remove value from configuration parameter in case of double quoted parameter value
 88    if [ "$var_value" == '""' ]; then
 89        sed -i -e "/^$var_name=/s/=.*/=/" "$config_path"
 90        echo "undefined"
 91        return
 92    fi
 93
 94    # Use full path to a file for TLS related configuration parameters
 95    if [[ $var_name =~ ^TLS.*File$ ]]; then
 96        var_value=$ZABBIX_USER_HOME_DIR/enc/$var_value
 97    fi
 98
 99    # Escaping characters in parameter value
100    var_value=$(escape_spec_char "$var_value")
101
102    if [ "$(grep -E "^$var_name=" $config_path)" ] && [ "$is_multiple" != "true" ]; then
103        sed -i -e "/^$var_name=/s/=.*/=$var_value/" "$config_path"
104        echo "updated"
105    elif [ "$(grep -Ec "^# $var_name=" $config_path)" -gt 1 ]; then
106        sed -i -e  "/^[#;] $var_name=$/i\\$var_name=$var_value" "$config_path"
107        echo "added first occurrence"
108    else
109        sed -i -e "/^[#;] $var_name=/s/.*/&\n$var_name=$var_value/" "$config_path"
110        echo "added"
111    fi
112
113}
114
115update_config_multiple_var() {
116    local config_path=$1
117    local var_name=$2
118    local var_value=$3
119
120    var_value="${var_value%\"}"
121    var_value="${var_value#\"}"
122
123    local IFS=,
124    local OPT_LIST=($var_value)
125
126    for value in "${OPT_LIST[@]}"; do
127        update_config_var $config_path $var_name $value true
128    done
129}
130
131# Check prerequisites for MySQL database
132check_variables_mysql() {
133    : ${DB_SERVER_HOST:="mysql-server"}
134    : ${DB_SERVER_PORT:="3306"}
135    USE_DB_ROOT_USER=false
136    CREATE_ZBX_DB_USER=false
137    file_env MYSQL_USER
138    file_env MYSQL_PASSWORD
139
140    file_env MYSQL_ROOT_PASSWORD
141
142    if [ ! -n "${MYSQL_USER}" ] && [ "${MYSQL_RANDOM_ROOT_PASSWORD}" == "true" ]; then
143        echo "**** Impossible to use MySQL server because of unknown Zabbix user and random 'root' password"
144        exit 1
145    fi
146
147    if [ ! -n "${MYSQL_USER}" ] && [ ! -n "${MYSQL_ROOT_PASSWORD}" ] && [ "${MYSQL_ALLOW_EMPTY_PASSWORD}" != "true" ]; then
148        echo "*** Impossible to use MySQL server because 'root' password is not defined and it is not empty"
149        exit 1
150    fi
151
152    if [ "${MYSQL_ALLOW_EMPTY_PASSWORD}" == "true" ] || [ -n "${MYSQL_ROOT_PASSWORD}" ]; then
153        USE_DB_ROOT_USER=true
154        DB_SERVER_ROOT_USER="root"
155        DB_SERVER_ROOT_PASS=${MYSQL_ROOT_PASSWORD:-""}
156    fi
157
158    [ -n "${MYSQL_USER}" ] && [ "${USE_DB_ROOT_USER}" == "true" ] && CREATE_ZBX_DB_USER=true
159
160    # If root password is not specified use provided credentials
161    : ${DB_SERVER_ROOT_USER:=${MYSQL_USER}}
162    [ "${MYSQL_ALLOW_EMPTY_PASSWORD}" == "true" ] || DB_SERVER_ROOT_PASS=${DB_SERVER_ROOT_PASS:-${MYSQL_PASSWORD}}
163    DB_SERVER_ZBX_USER=${MYSQL_USER:-"zabbix"}
164    DB_SERVER_ZBX_PASS=${MYSQL_PASSWORD:-"zabbix"}
165
166    DB_SERVER_DBNAME=${MYSQL_DATABASE:-"zabbix"}
167}
168
169check_db_connect_mysql() {
170    echo "********************"
171    echo "* DB_SERVER_HOST: ${DB_SERVER_HOST}"
172    echo "* DB_SERVER_PORT: ${DB_SERVER_PORT}"
173    echo "* DB_SERVER_DBNAME: ${DB_SERVER_DBNAME}"
174    if [ "${DEBUG_MODE}" == "true" ]; then
175        if [ "${USE_DB_ROOT_USER}" == "true" ]; then
176            echo "* DB_SERVER_ROOT_USER: ${DB_SERVER_ROOT_USER}"
177            echo "* DB_SERVER_ROOT_PASS: ${DB_SERVER_ROOT_PASS}"
178        fi
179        echo "* DB_SERVER_ZBX_USER: ${DB_SERVER_ZBX_USER}"
180        echo "* DB_SERVER_ZBX_PASS: ${DB_SERVER_ZBX_PASS}"
181    fi
182    echo "********************"
183
184    WAIT_TIMEOUT=5
185
186    while [ ! "$(mysqladmin ping -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} -u ${DB_SERVER_ROOT_USER} \
187                --password="${DB_SERVER_ROOT_PASS}" --silent --connect_timeout=10)" ]; do
188        echo "**** MySQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
189        sleep $WAIT_TIMEOUT
190    done
191}
192
193mysql_query() {
194    query=$1
195    local result=""
196
197    result=$(mysql --silent --skip-column-names -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
198             -u ${DB_SERVER_ROOT_USER} --password="${DB_SERVER_ROOT_PASS}" -e "$query")
199
200    echo $result
201}
202
203create_db_user_mysql() {
204    [ "${CREATE_ZBX_DB_USER}" == "true" ] || return
205
206    echo "** Creating '${DB_SERVER_ZBX_USER}' user in MySQL database"
207
208    USER_EXISTS=$(mysql_query "SELECT 1 FROM mysql.user WHERE user = '${DB_SERVER_ZBX_USER}' AND host = '%'")
209
210    if [ -z "$USER_EXISTS" ]; then
211        mysql_query "CREATE USER '${DB_SERVER_ZBX_USER}'@'%' IDENTIFIED BY '${DB_SERVER_ZBX_PASS}'" 1>/dev/null
212    else
213        mysql_query "ALTER USER ${DB_SERVER_ZBX_USER} IDENTIFIED BY '${DB_SERVER_ZBX_PASS}';" 1>/dev/null
214    fi
215
216    mysql_query "GRANT ALL PRIVILEGES ON $DB_SERVER_DBNAME. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
217}
218
219create_db_database_mysql() {
220    DB_EXISTS=$(mysql_query "SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='${DB_SERVER_DBNAME}'")
221
222    if [ -z ${DB_EXISTS} ]; then
223        echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..."
224        mysql_query "CREATE DATABASE ${DB_SERVER_DBNAME} CHARACTER SET utf8 COLLATE utf8_bin" 1>/dev/null
225        # better solution?
226        mysql_query "GRANT ALL PRIVILEGES ON $DB_SERVER_DBNAME. * TO '${DB_SERVER_ZBX_USER}'@'%'" 1>/dev/null
227    else
228        echo "** Database '${DB_SERVER_DBNAME}' already exists. Please be careful with database COLLATE!"
229    fi
230}
231
232create_db_schema_mysql() {
233    DBVERSION_TABLE_EXISTS=$(mysql_query "SELECT 1 FROM information_schema.tables WHERE table_schema='${DB_SERVER_DBNAME}' and table_name = 'dbversion'")
234
235    if [ -n "${DBVERSION_TABLE_EXISTS}" ]; then
236        echo "** Table '${DB_SERVER_DBNAME}.dbversion' already exists."
237        ZBX_DB_VERSION=$(mysql_query "SELECT mandatory FROM ${DB_SERVER_DBNAME}.dbversion")
238    fi
239
240    if [ -z "${ZBX_DB_VERSION}" ]; then
241        echo "** Creating '${DB_SERVER_DBNAME}' schema in MySQL"
242
243        zcat /usr/share/doc/zabbix-server-mysql/create.sql.gz | mysql --silent --skip-column-names \
244                    -h ${DB_SERVER_HOST} -P ${DB_SERVER_PORT} \
245                    -u ${DB_SERVER_ROOT_USER} --password="${DB_SERVER_ROOT_PASS}"  \
246                    ${DB_SERVER_DBNAME} 1>/dev/null
247    fi
248}
249
250update_zbx_config() {
251    echo "** Preparing Zabbix server configuration file"
252
253    ZBX_CONFIG=$ZABBIX_ETC_DIR/zabbix_server.conf
254
255    update_config_var $ZBX_CONFIG "ListenPort" "${ZBX_LISTENPORT}"
256
257    update_config_var $ZBX_CONFIG "SourceIP" "${ZBX_SOURCEIP}"
258    update_config_var $ZBX_CONFIG "LogType" "console"
259    update_config_var $ZBX_CONFIG "LogFile"
260    update_config_var $ZBX_CONFIG "LogFileSize"
261    update_config_var $ZBX_CONFIG "PidFile"
262
263    update_config_var $ZBX_CONFIG "DebugLevel" "${ZBX_DEBUGLEVEL}"
264
265    update_config_var $ZBX_CONFIG "DBHost" "${DB_SERVER_HOST}"
266    update_config_var $ZBX_CONFIG "DBName" "${DB_SERVER_DBNAME}"
267    update_config_var $ZBX_CONFIG "DBSchema" "${DB_SERVER_SCHEMA}"
268    update_config_var $ZBX_CONFIG "DBUser" "${DB_SERVER_ZBX_USER}"
269    update_config_var $ZBX_CONFIG "DBPort" "${DB_SERVER_PORT}"
270    update_config_var $ZBX_CONFIG "DBPassword" "${DB_SERVER_ZBX_PASS}"
271
272    update_config_var $ZBX_CONFIG "HistoryStorageURL" "${ZBX_HISTORYSTORAGEURL}"
273    update_config_var $ZBX_CONFIG "HistoryStorageTypes" "${ZBX_HISTORYSTORAGETYPES}"
274
275    update_config_var $ZBX_CONFIG "DBSocket" "${DB_SERVER_SOCKET}"
276
277    update_config_var $ZBX_CONFIG "StartPollers" "${ZBX_STARTPOLLERS}"
278    update_config_var $ZBX_CONFIG "StartIPMIPollers" "${ZBX_IPMIPOLLERS}"
279    update_config_var $ZBX_CONFIG "StartPollersUnreachable" "${ZBX_STARTPOLLERSUNREACHABLE}"
280    update_config_var $ZBX_CONFIG "StartTrappers" "${ZBX_STARTTRAPPERS}"
281    update_config_var $ZBX_CONFIG "StartPingers" "${ZBX_STARTPINGERS}"
282    update_config_var $ZBX_CONFIG "StartDiscoverers" "${ZBX_STARTDISCOVERERS}"
283    update_config_var $ZBX_CONFIG "StartHTTPPollers" "${ZBX_STARTHTTPPOLLERS}"
284
285    update_config_var $ZBX_CONFIG "StartPreprocessors" "${ZBX_STARTPREPROCESSORS}"
286    update_config_var $ZBX_CONFIG "StartTimers" "${ZBX_STARTTIMERS}"
287    update_config_var $ZBX_CONFIG "StartEscalators" "${ZBX_STARTESCALATORS}"
288    update_config_var $ZBX_CONFIG "StartAlerters" "${ZBX_STARTALERTERS}"
289
290    update_config_var $ZBX_CONFIG "StartTimers" "${ZBX_STARTTIMERS}"
291    update_config_var $ZBX_CONFIG "StartEscalators" "${ZBX_STARTESCALATORS}"
292
293    update_config_var $ZBX_CONFIG "StartLLDProcessors" "${ZBX_STARTLLDPROCESSORS}"
294
295    : ${ZBX_JAVAGATEWAY_ENABLE:="false"}
296    if [ "${ZBX_JAVAGATEWAY_ENABLE}" == "true" ]; then
297        update_config_var $ZBX_CONFIG "JavaGateway" "${ZBX_JAVAGATEWAY:-"zabbix-java-gateway"}"
298        update_config_var $ZBX_CONFIG "JavaGatewayPort" "${ZBX_JAVAGATEWAYPORT}"
299        update_config_var $ZBX_CONFIG "StartJavaPollers" "${ZBX_STARTJAVAPOLLERS:-"5"}"
300    else
301        update_config_var $ZBX_CONFIG "JavaGateway"
302        update_config_var $ZBX_CONFIG "JavaGatewayPort"
303        update_config_var $ZBX_CONFIG "StartJavaPollers"
304    fi
305
306    update_config_var $ZBX_CONFIG "StartVMwareCollectors" "${ZBX_STARTVMWARECOLLECTORS}"
307    update_config_var $ZBX_CONFIG "VMwareFrequency" "${ZBX_VMWAREFREQUENCY}"
308    update_config_var $ZBX_CONFIG "VMwarePerfFrequency" "${ZBX_VMWAREPERFFREQUENCY}"
309    update_config_var $ZBX_CONFIG "VMwareCacheSize" "${ZBX_VMWARECACHESIZE}"
310    update_config_var $ZBX_CONFIG "VMwareTimeout" "${ZBX_VMWARETIMEOUT}"
311
312    : ${ZBX_ENABLE_SNMP_TRAPS:="false"}
313    if [ "${ZBX_ENABLE_SNMP_TRAPS}" == "true" ]; then
314        update_config_var $ZBX_CONFIG "SNMPTrapperFile" "${ZABBIX_USER_HOME_DIR}/snmptraps/snmptraps.log"
315        update_config_var $ZBX_CONFIG "StartSNMPTrapper" "1"
316    else
317        update_config_var $ZBX_CONFIG "SNMPTrapperFile"
318        update_config_var $ZBX_CONFIG "StartSNMPTrapper"
319    fi
320
321    update_config_var $ZBX_CONFIG "HousekeepingFrequency" "${ZBX_HOUSEKEEPINGFREQUENCY}"
322    update_config_var $ZBX_CONFIG "MaxHousekeeperDelete" "${ZBX_MAXHOUSEKEEPERDELETE}"
323    update_config_var $ZBX_CONFIG "SenderFrequency" "${ZBX_SENDERFREQUENCY}"
324
325    update_config_var $ZBX_CONFIG "CacheSize" "${ZBX_CACHESIZE}"
326
327    update_config_var $ZBX_CONFIG "CacheUpdateFrequency" "${ZBX_CACHEUPDATEFREQUENCY}"
328
329    update_config_var $ZBX_CONFIG "StartDBSyncers" "${ZBX_STARTDBSYNCERS}"
330    update_config_var $ZBX_CONFIG "HistoryCacheSize" "${ZBX_HISTORYCACHESIZE}"
331    update_config_var $ZBX_CONFIG "HistoryIndexCacheSize" "${ZBX_HISTORYINDEXCACHESIZE}"
332
333    update_config_var $ZBX_CONFIG "TrendCacheSize" "${ZBX_TRENDCACHESIZE}"
334    update_config_var $ZBX_CONFIG "ValueCacheSize" "${ZBX_VALUECACHESIZE}"
335
336    update_config_var $ZBX_CONFIG "Timeout" "${ZBX_TIMEOUT}"
337    update_config_var $ZBX_CONFIG "TrapperTimeout" "${ZBX_TRAPPERIMEOUT}"
338    update_config_var $ZBX_CONFIG "UnreachablePeriod" "${ZBX_UNREACHABLEPERIOD}"
339    update_config_var $ZBX_CONFIG "UnavailableDelay" "${ZBX_UNAVAILABLEDELAY}"
340    update_config_var $ZBX_CONFIG "UnreachableDelay" "${ZBX_UNREACHABLEDELAY}"
341
342    update_config_var $ZBX_CONFIG "AlertScriptsPath" "/usr/lib/zabbix/alertscripts"
343    update_config_var $ZBX_CONFIG "ExternalScripts" "/usr/lib/zabbix/externalscripts"
344
345    if [ -n "${ZBX_EXPORTFILESIZE}" ]; then
346        update_config_var $ZBX_CONFIG "ExportDir" "$ZABBIX_USER_HOME_DIR/export/"
347        update_config_var $ZBX_CONFIG "ExportFileSize" "{$ZBX_EXPORTFILESIZE}"
348    fi
349
350    update_config_var $ZBX_CONFIG "FpingLocation" "/usr/sbin/fping"
351    update_config_var $ZBX_CONFIG "Fping6Location"
352
353    update_config_var $ZBX_CONFIG "SSHKeyLocation" "$ZABBIX_USER_HOME_DIR/ssh_keys"
354    update_config_var $ZBX_CONFIG "LogSlowQueries" "${ZBX_LOGSLOWQUERIES}"
355
356    update_config_var $ZBX_CONFIG "StartProxyPollers" "${ZBX_STARTPROXYPOLLERS}"
357    update_config_var $ZBX_CONFIG "ProxyConfigFrequency" "${ZBX_PROXYCONFIGFREQUENCY}"
358    update_config_var $ZBX_CONFIG "ProxyDataFrequency" "${ZBX_PROXYDATAFREQUENCY}"
359
360    update_config_var $ZBX_CONFIG "SSLCertLocation" "$ZABBIX_USER_HOME_DIR/ssl/certs/"
361    update_config_var $ZBX_CONFIG "SSLKeyLocation" "$ZABBIX_USER_HOME_DIR/ssl/keys/"
362    update_config_var $ZBX_CONFIG "SSLCALocation" "$ZABBIX_USER_HOME_DIR/ssl/ssl_ca/"
363    update_config_var $ZBX_CONFIG "LoadModulePath" "$ZABBIX_USER_HOME_DIR/modules/"
364    update_config_multiple_var $ZBX_CONFIG "LoadModule" "${ZBX_LOADMODULE}"
365
366    update_config_var $ZBX_CONFIG "TLSCAFile" "${ZBX_TLSCAFILE}"
367    update_config_var $ZBX_CONFIG "TLSCRLFile" "${ZBX_TLSCRLFILE}"
368
369    update_config_var $ZBX_CONFIG "TLSCertFile" "${ZBX_TLSCERTFILE}"
370    update_config_var $ZBX_CONFIG "TLSKeyFile" "${ZBX_TLSKEYFILE}"
371
372    if [ "$(id -u)" != '0' ]; then
373        update_config_var $ZBX_CONFIG "User" "$(whoami)"
374    else
375        update_config_var $ZBX_CONFIG "AllowRoot" "1"
376    fi
377}
378
379prepare_server() {
380    echo "** Preparing Zabbix server"
381
382    check_variables_mysql
383    check_db_connect_mysql
384    create_db_user_mysql
385    create_db_database_mysql
386    create_db_schema_mysql
387
388    update_zbx_config
389}
390
391#################################################
392
393if [ "${1#-}" != "$1" ]; then
394    set -- /usr/sbin/zabbix_server "$@"
395    fi
396
397if [ "$1" == '/usr/sbin/zabbix_server' ]; then
398    prepare_server
399fi
400
401exec "$@"
402
403#################################################

Dale permisos de ejecución:

1chmod +x docker-entrypoint.sh

Crea el fichero Dockerfile

  1FROM alpine:3.11.2
  2
  3LABEL org.opencontainers.image.title="Zabbix server (MySQL)" \
  4      org.opencontainers.image.authors="Alexey Pustovalov <alexey.pustovalov@zabbix.com>" \
  5      org.opencontainers.image.vendor="Zabbix LLC" \
  6      org.opencontainers.image.url="https://zabbix.com/" \
  7      org.opencontainers.image.description="Zabbix server with MySQL database support" \
  8      org.opencontainers.image.licenses="GPL v2.0"
  9
 10STOPSIGNAL SIGTERM
 11
 12RUN set -eux && \
 13    addgroup -S -g 1000 zabbix && \
 14    adduser -S \
 15            -D -G zabbix \
 16            -u 999 \
 17            -h /var/lib/zabbix/ \
 18        zabbix && \
 19    adduser zabbix dialout && \
 20    mkdir -p /etc/zabbix && \
 21    mkdir -p /var/lib/zabbix && \
 22    mkdir -p /usr/lib/zabbix/alertscripts && \
 23    mkdir -p /var/lib/zabbix/enc && \
 24    mkdir -p /usr/lib/zabbix/externalscripts && \
 25    mkdir -p /var/lib/zabbix/mibs && \
 26    mkdir -p /var/lib/zabbix/modules && \
 27    mkdir -p /var/lib/zabbix/snmptraps && \
 28    mkdir -p /var/lib/zabbix/ssh_keys && \
 29    mkdir -p /var/lib/zabbix/ssl && \
 30    mkdir -p /var/lib/zabbix/ssl/certs && \
 31    mkdir -p /var/lib/zabbix/ssl/keys && \
 32    mkdir -p /var/lib/zabbix/ssl/ssl_ca && \
 33    chown --quiet -R zabbix:root /var/lib/zabbix && \
 34    mkdir -p /usr/share/doc/zabbix-server-mysql && \
 35    apk add --clean-protected --no-cache \
 36            tini \
 37            bash \
 38            fping \
 39            iputils \
 40            libcurl \
 41            libevent \
 42            libldap \
 43            libssh2 \
 44            libxml2 \
 45            mariadb-client \
 46            mariadb-connector-c \
 47            net-snmp-agent-libs \
 48            openipmi-libs \
 49            pcre \
 50            unixodbc && \
 51    rm -rf /var/cache/apk/*
 52
 53ARG MAJOR_VERSION=4.4
 54ARG ZBX_VERSION=${MAJOR_VERSION}.4
 55ARG ZBX_SOURCES=https://git.zabbix.com/scm/zbx/zabbix.git
 56
 57ENV TERM=xterm ZBX_VERSION=${ZBX_VERSION} ZBX_SOURCES=${ZBX_SOURCES} \
 58    MIBDIRS=/usr/share/snmp/mibs:/var/lib/zabbix/mibs MIBS=+ALL
 59
 60LABEL org.opencontainers.image.documentation="https://www.zabbix.com/documentation/${MAJOR_VERSION}/manual/installation/containers" \
 61      org.opencontainers.image.version="${ZBX_VERSION}" \
 62      org.opencontainers.image.source="${ZBX_SOURCES}"
 63
 64RUN set -eux && \
 65    apk add --no-cache --virtual build-dependencies \
 66            autoconf \
 67            automake \
 68            coreutils \
 69            curl-dev \
 70            libevent-dev \
 71            libssh2-dev \
 72            libxml2-dev \
 73            mysql-dev \
 74            net-snmp-dev \
 75            openipmi-dev \
 76            openldap-dev \
 77            pcre-dev \
 78            git \
 79            g++ \
 80            make \
 81            unixodbc-dev && \
 82    cd /tmp/ && \
 83    git clone ${ZBX_SOURCES} --branch ${ZBX_VERSION} --depth 1 --single-branch zabbix-${ZBX_VERSION} && \
 84    cd /tmp/zabbix-${ZBX_VERSION} && \
 85    zabbix_revision=`git rev-parse --short HEAD` && \
 86    sed -i "s/{ZABBIX_REVISION}/$zabbix_revision/g" include/version.h && \
 87    ./bootstrap.sh && \
 88    export CFLAGS="-fPIC -pie -Wl,-z,relro -Wl,-z,now" && \
 89    ./configure \
 90            --datadir=/usr/lib \
 91            --libdir=/usr/lib/zabbix \
 92            --prefix=/usr \
 93            --sysconfdir=/etc/zabbix \
 94            --enable-agent \
 95            --enable-server \
 96            --with-mysql \
 97# Does not support stable iksemel library
 98#            --with-jabber \
 99            --with-ldap \
100            --with-libcurl \
101            --with-libxml2 \
102            --with-net-snmp \
103            --with-openipmi \
104            --with-openssl \
105            --with-ssh2 \
106            --with-unixodbc \
107            --enable-ipv6 \
108            --silent && \
109    make -j"$(nproc)" -s dbschema && \
110    make -j"$(nproc)" -s && \
111    cp src/zabbix_server/zabbix_server /usr/sbin/zabbix_server && \
112    cp src/zabbix_get/zabbix_get /usr/bin/zabbix_get && \
113    cp src/zabbix_sender/zabbix_sender /usr/bin/zabbix_sender && \
114    cp conf/zabbix_server.conf /etc/zabbix/zabbix_server.conf && \
115    chown --quiet -R zabbix:root /etc/zabbix && \
116    cat database/mysql/schema.sql > database/mysql/create.sql && \
117    cat database/mysql/images.sql >> database/mysql/create.sql && \
118    cat database/mysql/data.sql >> database/mysql/create.sql && \
119    gzip database/mysql/create.sql && \
120    cp database/mysql/create.sql.gz /usr/share/doc/zabbix-server-mysql/ && \
121    cd /tmp/ && \
122    rm -rf /tmp/zabbix-${ZBX_VERSION}/ && \
123    apk del --purge --no-network \
124            build-dependencies && \
125    rm -rf /var/cache/apk/*
126
127EXPOSE 10051/TCP
128
129WORKDIR /var/lib/zabbix
130
131VOLUME ["/usr/lib/zabbix/alertscripts", "/usr/lib/zabbix/externalscripts", "/var/lib/zabbix/enc", "/var/lib/zabbix/mibs", "/var/lib/zabbix/modules"]
132VOLUME ["/var/lib/zabbix/snmptraps", "/var/lib/zabbix/ssh_keys", "/var/lib/zabbix/ssl/certs", "/var/lib/zabbix/ssl/keys", "/var/lib/zabbix/ssl/ssl_ca"]
133VOLUME ["/var/lib/zabbix/export"]
134
135COPY ["docker-entrypoint.sh", "/usr/bin/"]
136
137ENTRYPOINT ["/sbin/tini", "--", "/usr/bin/docker-entrypoint.sh"]
138
139USER zabbix
140
141CMD ["/usr/sbin/zabbix_server", "--foreground", "-c", "/etc/zabbix/zabbix_server.conf"]

Por ultimo construimos el contenedor

1
2docker build -t carlos/zabbix-server-mysql .
3
4docker run --name zabbix-server-mysql -e DB_SERVER_HOST="servidor-mysql" -e MYSQL_USER="usuario_zabbix" -e MYSQL_PASSWORD="contraseña" -d carlos/zabbix-server-mysql

Donde:

  • DB_SERVER_HOST dirección del servidor mysql

  • MYSQL_USER usuario con acceso a la base de datos

  • MYSQL_PASSWORD contraseña del usuario de la base de datos

Servidor zabbix-web

Vamos a construir el servidor web que va servir como interfaz para administrar zabbix.

Descárgate los ficheros docker-entrypoint.sh, Dockerfile y la carpeta conf desde mi repositorio y construye el contenedor

1
2docker run --name zabbix-web-nginx-mysql -e DB_SERVER_HOST="servidor-mysql" -e MYSQL_USER="usuario_zabbix" -e MYSQL_PASSWORD="contraseña" -e ZBX_SERVER_HOST="servidor_zabbix" -e PHP_TZ="Europe/Madrid" -d carlos/zabbix-web

Vemos como los dos contenedores estan arrancados y funcionando


Ya está, ahora accede al servidor a travea de http://ip/zabbix , usuario Admin contraseña zabbix


Tras ingresar

Repositorio

He subido todos los ficheros a mi repositorio por si prefieres descargarlos directamente y tan solo tengas que construir los contenedores.

Espero que te haya gustado, pasa un buen día… :penguin: