This commit is contained in:
2022-04-18 11:21:20 +08:00
commit 45a7af638f
210 changed files with 8997 additions and 0 deletions

161
zabbix/ADD/ccmd Executable file
View File

@@ -0,0 +1,161 @@
#!/bin/bash
##################################################
# Mount file #
# - /etc/my.cnf #
# - /etc/zabbix/zabbix_server.conf #
# Mount dir #
# - /etc/nginx/http.d #
# - /var/log/mysql #
# - /var/lib/mysql #
# - /var/lib/mysql-bin #
# - /var/log/nginx #
# - /var/log/php7 #
# - /var/log/zabbix #
##################################################
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
PIDS=
GOT_SIGTERM=
LOG_DIR='/var/log/mysql'
DATA_DIR='/var/lib/mysql'
BINLOG_DIR='/var/lib/mysql-bin'
INIT_FLAG=${INIT_FLAG:-}
SOCK_FILE='/run/mysqld/mysqld.sock'
PID_FILE='/run/mysqld/mysqld.pid'
function Print {
local file=/dev/null
[ '-f' = "$1" ] && file=$2 && shift && shift
date +"[%F %T] $*" | tee -a $file
}
function Quit {
local running
while running= ; do
pkill -f sleep && running=1 && Print killing sleep ...
pkill -f nginx && running=1 && Print killing nginx ...
pkill -f php-fpm7 && running=1 && Print killing php-fpm7 ...
pkill -f zabbix_server && running=1 && Print killing zabbix_server ...
pkill -f mysqld && running=1 && Print killing mysqld ...
[ -z "$running" ] && break
sleep 1
done
Print Container stopped.
test -n "$GOT_SIGTERM"
}
function RestoreConf {
! ls /etc/nginx/conf.d/*.conf 2>/dev/null | grep -Eq '(zabbix|zbx)' \
&& Print Restore /etc/nginx/conf.d/zabbix.conf ... \
&& cp /usr/share/zabbix/nginx.conf /etc/nginx/conf.d/zabbix.conf
[ ! -e /etc/zabbix/zabbix_proxy.conf ] \
&& Print Restore /etc/zabbix/zabbix_proxy.conf ... \
&& cp /usr/share/zabbix/zabbix_proxy.conf /etc/zabbix/zabbix_proxy.conf
[ ! -e /etc/zabbix/zabbix_server.conf ] \
&& Print Restore /etc/zabbix/zabbix_server.conf ... \
&& cp /usr/share/zabbix/zabbix_server.conf /etc/zabbix/zabbix_server.conf
return 0
}
function SideCar {
local day= last_day=$(date +%d)
local md5= last_md5=$(find /etc/nginx/ -type f -name "*.conf" \
| xargs -I ^ md5sum ^ | md5sum)
while sleep 10; do
day=$(date +%d) \
&& [ "$day" != "$last_day" ] \
&& last_day=$day \
&& find /var/log/nginx/ -type f -name "*.log" \
| xargs -I ^ mv ^ ^.$(date +%F -d yesterday) \
&& nginx -s reopen
md5=$(find /etc/nginx/ -type f -name "*.conf" | xargs -I ^ md5sum ^ \
| md5sum) \
&& [ "$md5" != "$last_md5" ] \
&& last_md5=$md5 \
&& nginx -tq \
&& Print Reload nginx conf ... \
&& nginx -s reload
done
}
function InitDB {
rm -f $SOCK_FILE $PID_FILE
chown -R mysql.mysql $LOG_DIR $BINLOG_DIR $DATA_DIR
if [ ! -d "$DATA_DIR/mysql" ]; then
Print Install database ...
mysql_install_db --user=mysql > /dev/null
INIT_FLAG=1
fi
}
function StartProc {
Print Start mysql ...
mysqld -u mysql &
PIDS="$PIDS $!"
while sleep 1; do
[ -e $SOCK_FILE ] && break || echo -n .
[ ! -e /proc/$! ] && echo && Print unexpected error! && exit
done
echo
if [ -n "$INIT_FLAG" ]; then
Print Secure database ...
mysql_secure_installation <<< "$(echo -e '\nn\nn\n\n\n\n\n')" > /dev/null
Print Create zabbix db and user ...
mysql -e "CREATE DATABASE zabbix DEFAULT CHARSET UTF8 COLLATE UTF8_BIN"
mysql -e "CREATE USER zabbix@localhost"
mysql -e "GRANT ALL ON zabbix.* TO zabbix@localhost"
Print Import zabbix schema.sql ...
mysql -Dzabbix < /usr/share/zabbix/database/mysql/schema.sql
Print Import zabbix images.sql ...
mysql -Dzabbix < /usr/share/zabbix/database/mysql/images.sql
Print Import zabbix data.sql ...
mysql -Dzabbix < /usr/share/zabbix/database/mysql/data.sql
if sql_files="$(ls $DATA_DIR/init_sql/*.sql 2>/dev/null)"; then
Print Import the sql files ...
for sql_file in $sql_files; do
Print Importing $sql_file ...
mysql < $sql_file
done
Print Imported all sql files successfully.
fi
fi
Print MySQL is ready for connections.
RestoreConf
Print Start php ...
php-fpm7 -F -y /etc/php7/php-fpm.conf &
PIDS="$PIDS $!"
Print Start zabbix ...
zabbix_server -f &
PIDS="$PIDS $!"
Print Start nginx ...
nginx -g 'daemon off;pid /run/nginx/nginx.pid;' &
PIDS="$PIDS $!"
Print Start nginx sidecar ...
SideCar &
PIDS="$PIDS $!"
}
function Main {
local pid=
InitDB
StartProc
trap "GOT_SIGTERM=1; Print Got SIGTERM ..." SIGTERM
while [ -z "$GOT_SIGTERM" ] && sleep 1; do
for pid in $PIDS; do
[ ! -e /proc/$pid ] && Print Unexpected error! && exit
done
done
}
# Start here
Main

View File

@@ -0,0 +1,23 @@
# 部署 zabbix
- 根据实际环境修改
- docker-compose.yml
- 创建目录
```
grep '\<source:' docker-compose.yml | cut -d: -f2 | xargs mkdir -p
```
- 启动
```
docker-compose up -d
```
- 查看容器日志,直至出现 "Starting Zabbix Server. Zabbix 5.0.9 (revision {ZABBIX_REVISION}).",表示 zabbix server 启动完成
```
docker-compose logs -f zabbix
```
- 浏览器访问 http://x.x.x.x/zabbix/,打开 zabbix 安装界面__不要做任何修改一直点击 "下一步"__直至出现登陆界面
- 默认用户名和密码: Admin/zabbix

View File

@@ -0,0 +1,28 @@
version: "3.7"
services:
zabbix:
image: harbor.colben.cn/general/zabbix
container_name: zabbix
restart: "on-failure"
stop_grace_period: 1m
ports:
- 80:80
- 10051:10051
volumes:
- type: bind
source: ./mysql/db
target: /var/lib/mysql
- type: bind
source: ./mysql/log
target: /var/log/mysql
- type: bind
source: ./php7/log
target: /var/log/php7
- type: bind
source: ./nginx/log
target: /var/log/nginx
- type: bind
source: ./zabbix/log
target: /var/log/zabbix

57
zabbix/Dockerfile Normal file
View File

@@ -0,0 +1,57 @@
ARG ARCH
FROM harbor.colben.cn/general/nginx-php$ARCH
MAINTAINER Colben colbenlee@gmail.com
ADD --chown=root:root /ADD/ /opt/
RUN apk update \
&& apk add --no-cache mariadb mariadb-client \
zabbix-mysql zabbix zabbix-webif zabbix-setup \
&& rm -rf /var/cache/apk/* /etc/my.cnf* /etc/mysql/* \
&& echo -e '[mysqld]\n'> /etc/my.cnf \
&& echo -e '[client-server]\n\
socket = /run/mysqld/mysqld.sock\n\
[mysqld]\n\
datadir = /var/lib/mysql\n\
pid-file = /run/mysqld/mysqld.pid\n\
log-error = /var/log/mysql/error.log\n\
character-set-server = utf8mb4\n\
default-storage-engine = innodb\n\
slow-query-log = TRUE\n\
slow-query-log-file = /var/log/mysql/slow.log\n\
lower-case-table-names = 1\n\
' > /etc/mysql/my.cnf \
&& sed -i 's/stty/#stty/' /usr/bin/mysql_secure_installation \
&& mkdir -p /var/log/mysql /var/lib/mysql-bin /run/mysqld \
&& chown -R mysql.mysql /var/log/mysql /var/lib/mysql-bin /run/mysqld \
&& chmod -R 0777 /usr/share/webapps/zabbix/conf \
&& sed -i '/^#* *AllowRoot *=/cAllowRoot=1' /etc/zabbix/zabbix_server.conf \
&& mv /etc/zabbix/* /usr/share/zabbix/ \
&& echo -e 'server {\n\
listen 80;\n\
server_name zabbix.colben.cn;\n\
access_log /var/log/nginx/access-zabbix.log main;\n\
error_log /var/log/nginx/error-zabbix.log;\n\
location /zabbix/ {\n\
root /usr/share/webapps;\n\
index index.php index.html index.html;\n\
}\n\
location ~ ^/zabbix/.+\.php$ {\n\
root /usr/share/webapps;\n\
access_log /var/log/nginx/access-zabbix.log main;\n\
index index.php index.html index.html;\n\
expires -1s;\n\
include fastcgi_params;\n\
try_files $uri =404;\n\
fastcgi_pass unix:/var/lib/php7/phpfpm.sock;\n\
fastcgi_index index.php;\n\
fastcgi_param PATH_INFO $fastcgi_path_info;\n\
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n\
fastcgi_split_path_info ^(.+\.php)(/.+)$;\n\
break;\n\
}\n\
location / {\n\
return 403;\n\
}\n\
}\n\
' > /usr/share/zabbix/nginx.conf
CMD ["/opt/ccmd"]

19
zabbix/README.md Normal file
View File

@@ -0,0 +1,19 @@
# 构建 zabbix 镜像
## 定制
- 安装 mariadb zabbix
## 外挂目录和文件
- /etc/my.cnf: mysql 配置文件
- /var/lib/mysql-bin: mysql binlog 目录
- /var/lib/mysql: mysql 数据目录
- /var/log/mysql: mysql 日志目录
- /var/log/php7: php7 日志目录
- /etc/nginx/http.d: nginx http 配置目录
- /var/log/nginx: nginx 日志目录
- /etc/zabbix/zabbix_server.conf: zabbix server 配置文件
- /var/log/zabbix: zabbix 日志目录
## 案例 1
- [Demo/SingleNode/](Demo/SingleNode/): 部署 zabbix

67
zabbix/zabbix.sh Executable file
View File

@@ -0,0 +1,67 @@
#!/bin/bash
#=========================================
# Author : colben
#=========================================
set -euo pipefail
export LANG=en_US.UTF-8
trap Quit EXIT
[ 'x86_64' == "$(uname -m)" ] && ARCH='' || ARCH="-$(uname -m)"
ROOT_DIR="$(cd $(dirname $0) && pwd)"
IMAGE="harbor.colben.cn/general/$(basename ${0%.sh})$ARCH:latest"
if [ -t 0 ]; then
function Print { echo -e "\033[36;1m$(date +'[%F %T]')\033[32;1m $*\033[0m"; }
function Warn { echo -e "\033[36;1m$(date +'[%F %T]')\033[33;1m $*\033[0m"; }
function Error { echo -e "\033[36;1m$(date +'[%F %T]')\033[31;1m $*\033[0m"; exit 1; }
else
function Print { echo -e "$(date +'[%F %T INFO]') $*"; }
function Warn { echo -e "$(date +'[%F %T WARN]') $*"; }
function Error { echo -e "$(date +'[%F %T ERROR]') $*"; exit 1; }
fi
function Quit {
local exitCode=$?
[ 0 -ne $exitCode ] && Error Failed to build or push image!
[ -z "${END:-}" ] && echo && Error Interrupted manually!
Print Succeeded to build and push image.
}
function YesOrNo {
Warn $*
local sw=
while :; do
read -p '(Yes/No/Quit) ' -n1 sw
[[ "$sw" =~ ^Y|y$ ]] && echo && return 0
[[ "$sw" =~ ^N|n$ ]] && echo && return 1
[[ "$sw" =~ ^Q|q$ ]] && echo && exit 0
[ -n "$sw" ] && echo
done
}
function Update {
:
}
function Build {
local yn
cd $ROOT_DIR
docker images --format='{{.Repository}}:{{.Tag}}' | grep "^$IMAGE$" \
&& Warn Removing image $IMAGE ... \
&& docker rmi $IMAGE
Warn Building image: $IMAGE ...
docker build --force-rm --build-arg ARCH="$ARCH" -t $IMAGE .
YesOrNo Push image: $IMAGE? && docker push $IMAGE
}
function Main {
Update
Build
END=1
}
# Start here
Main