服务器 | ip | 规格 | 相关信息 |
---|---|---|---|
CentOS7.9 Linux localhost 3.10.0-1160.83.1.el7.x86_64 | 192.168.56.110 | 1c2g40GB | docker-23.0.1 mysql-8.0.27 zabbix-server-6.0.17 zabbix-agent2-6.0.17 grafana9.5 |
Ubuntu-20.04.6-LTS | 192.168.56.111 | 1c1g40GB | zabbix-agent2-6.0.17 |
需要服务器联网,同步宿主机与容器的时间
#宿主机时间同步
*/5 * * * * root /usr/sbin/ntpdate pool.ntp.org
#容器时间同步方式1
docker run 时添加 -e TZ="Asia/Shanghai" 参数
#容器时间同步方式2
自己打docker镜像,定制一下
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
后续会写Dockerfile或者docke-compers安装,下面操作这么一大串子命令看着就闹心,
docker真麻烦 已经封装了还要映射目录,
感觉docker/k8s就适合一些流量激增的那种类型网站 ,网站流量没那么大 不需要弹性伸缩等等是否不用docker/k8s??
业务如果没拆的当然还算直接跑服务器上比较好,如果拆成一个个功能模块那就需要docker/k8s了
微服务还算比较贴切,毕竟就算是服务中有了网关也还是需要通过网络进行调用不同的服务,如果跑在docker或者k8s在网络方面也算是一种隔离吧
my.cnf配置文件中直接指定某些参数
hostnamectl --static set-hostname zabbix_server
[root@ zabbix_server~]# docker pull mysql:8.0
[root@ zabbix_server~]# groupadd mysql
[root@ zabbix_server~]# useradd -gmysql -s /sbin/nologin -M mysql
[root@ zabbix_server~]# mkdir -p /data/mysql/{data,conf,logs,mysql-files,mysqld}
[root@ zabbix_server~]# chown -R mysql.mysql /data/mysql
[root@ zabbix_server~]# cd /data/mysql
[root@ zabbix_servermysql]# cat >> my.cnf << EOF
[mysqld]
\#永久修改时区
#default-time-zone = '+08:00'
log_timestamps = SYSTEM
max_connections = 1000
max-allowed-packet = 16M
max-connect-errors = 1000
lower_case_table_names = 1
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 64M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 336M
tmp-table-size = 32M
max-heap-table-size = 32M
thread-cache-size = 50
open-files-limit = 1024
\#这个变量被用于限制导入和导出的数据目录
secure-file-priv=/var/lib/mysql-files
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
pid-file=/var/run/mysqld/mysqld.pid
\#character-set-server=utf8mb3
\#collation-server=utf8mb3_bin
\#[client]
\#default-character-set=utf8
\#[mysql]
\#default-character-set=utf8
[client]
port = 3306
bind-address = 127.0.0.1,192.168.56.110
default-character-set = utf8mb4
EOF
[root@bogon mysql]# ll /data/mysql/
总用量 4
drwxr-xr-x 2 root root 6 5月 9 17:28 conf
drwxr-xr-x 2 root root 6 5月 9 17:11 data
drwxr-xr-x 2 root root 6 5月 9 17:28 logs
-rw-r--r-- 1 root root 635 5月 9 17:28 my.cnf
docker run --restart=always --privileged=true --net host -v /data/mysql/data:/var/lib/mysql:rw -v /data/mysql/logs/:/var/log/mysql:rw -v /data/mysql/conf/:/etc/mysql:rw -v /data/mysql/my.cnf:/etc/mysql/my.cnf:rw -v /data/mysql/mysql-files:/var/lib/mysql-files:rw -v /data/mysql/mysqld:/var/run/mysqld:rw --name mysql8 -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix" -e MYSQL_ROOT_PASSWORD="zabbix" -e ZBX_DBTLSCONNECT="required" -e TZ="Asia/Shanghai" -d mysql:8.0
–restart=always
restart参数值说明如下:
no - 容器不自动重启
on-failure - 容器退出状态不为0时自动重启
on-failure:[n] - 容器退出状态不为0时自动重启,最大尝试n次
always - 始终尝试自动重启
–privileged=true 使用该参数,container内的root拥有真正的root权限。
docker启动时直接指定某些参数
修改配置文件my.cnf
[root@ zabbix_server~]# cat /data/mysql/my.cnf
[mysqld]
#永久修改时区
#default-time-zone = '+08:00'
log_timestamps = SYSTEM
max_connections = 1000
max-allowed-packet = 16M
max-connect-errors = 1000
lower_case_table_names = 1
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 64M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 336M
tmp-table-size = 32M
max-heap-table-size = 32M
thread-cache-size = 50
open-files-limit = 1024
#这个变量被用于限制导入和导出的数据目录
secure-file-priv=/var/lib/mysql-files
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
pid-file=/var/run/mysqld/mysqld.pid
#character-set-server=utf8mb4
#collation-server=utf8mb4_bin
#[client]
#default-character-set=utf8
#[mysql]
#default-character-set=utf8
[client]
port = 3306
bind-address = 127.0.0.1,192.168.56.110
#default-character-set = utf8mb4
[root@localhost ~]#
启动
docker run --net host -v /data/mysql/data:/var/lib/mysql:rw -v /data/mysql/logs/:/var/log/mysql:rw -v /data/mysql/conf/:/etc/mysql:rw -v /data/mysql/my.cnf:/etc/mysql/my.cnf:rw -v /data/mysql/mysql-files:/var/lib/mysql-files:rw -v /data/mysql/mysqld:/var/run/mysqld:rw --name mysql8 -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix" -e MYSQL_ROOT_PASSWORD="zabbix" -e ZBX_DBTLSCONNECT="required" -e TZ="Asia/Shanghai" -d mysql:8.0 --restart unless-stopped \ --character-set-server=utf8 --collation-server=utf8_bin --default-authentication-plugin=mysql_native_password
–restart unless-stopped 除非停止,否则不会重启
详情参考zabbix6.0官方
https://www.zabbix.com/documentation/6.0/zh/manual/installation/containers
指定字符集 以及定认证插件插件
--character-set-server=utf8mb3 --collation-server=utf8mb3_bin \
--default-authentication-plugin=mysql_native_password
mysql> show variables like '%log_time%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| log_timestamps | UTC |
+----------------+-------+
UTC是世界统一时间,而我现在的系统为北京时间是东八区,比UTC早了8个小时,所以这里设置为SYSTEM
在[mysqld]下增加:log_timestamps=SYSTEM
docker restart mysql8
如何保证数据一致性,出现故障如何保障数据一致性
docker如何跨主机访问数据库(数据库直接部署在服务器上)呢
由于从mysql4.2中dump出来的数据,但是现在是mysql8.0版本
因数据量过大,导出时排除了 history 和 trend 数据,但需导出表结构
备份库
mysqldump -uroot -p --databases zabbix --ignore-table=zabbix.history --ignore-table=zabbix.history_log --ignore-table=zabbix.history_str --ignore-table=zabbix.history_text --ignore-table=zabbix.history_uint --ignore-table=zabbix.trends --ignore-table=zabbix.trends_uint > zabbix_dump.sql
备份表
mysqldump -d zabbix -uroot -p --tables history history_log history_str history_text history_uint trends trends_uint > zabbix_tables.sql
–ignore-table 备份库时忽略这个库中的某个表
所有我并没有修改字符集 直接将 zabbix_dump.sql zabbix_tables.sql数据导入到了mysql8.0,请注意,这里我mysql8的字符集是utf8mb4,当然数据库也是utf8mb4
如果你想修改,也可以看看下面操作,建议连带着mysql的官方文档也看看
修改数据库的默认字符集
SELECT @@character_set_database, @@collation_database;
alter database zabbix character set utf8mb4 collate utf8mb4_bin;
修改表 字段的字符集
原文: https://www.zabbix.com/forum/zabbix-troubleshooting-and-problems/401826-database-upgrade-failed-specified-key-was-too-long-max-key-length-is-3072-bytes
将每个表字符转换为 utf8,将排序规则转换为utf8_bin。不仅是数据库默认值。
SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;') FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'databasename';
character-set-server/default-character-set:服务器字符集,默认情况下所采用的。
character-set-database:数据库字符集。
character-set-table:数据库表字符集。
create database zabbix character set utf8mb4 collate utf8mb4_bin;
安装完zabbix6.0导入前,需确保mysqldump中每个表的DDL语句中参数如下(可采用 sed 批量调整)
原
ENGINE=InodDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
替换
sed -i 's/CHARSET=utf8/CHARSET=utf8mb4/g' zabbix_dump.sql
sed -i 's/COLLATE=utf8_bin/COLLATE=utf8mb4_bin/g' zabbix_dump.sql
sed -i 's/COLLATE\ utf8_bin/COLLATE\ utf8mb4_bin/g' zabbix_dump.sql
新
ENGINE=InodDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
背景:为了清晰的说明这个问题,说明一下用例背景:此用例想修改某张表的备注,但是其他的数据表都有collate = utf8_bin操作,但是这张表没有,因此研究了一下最终得到以下结论:
问题:collate = utf8_bin具体表示什么?
用例:alter table carrier_master comment '承运商信息维护表' collate = utf8_bin;
结论:
collate = utf8_bin表示以二进制的形式存储输入的每个字符,因此会导致数据会区分大小写(collate有核对的意思)大小写敏感。
(另外一种)
utf-8_general_ci表示一般性比较,不会有大小写的区分(case insensitive),大小写不敏感。
utf-8_general_cs也是一种,它区分大小写(case sensitive),既大小写敏感。
参考
https://www.zabbix.com/documentation/6.0/zh/manual/installation/containers
zabbix-mysql
zabbix-server
zabbix-web
zabbix-agent
#zabbix-server-mysql
docker pull zabbix/zabbix-server-mysql:6.0-centos-latest
#zabbix-nginx-mysql
docker pull zabbix/zabbix-web-nginx-mysql:6.0-centos-latest
#zabbix-agent
docker pull zabbix/zabbix-agent:6.0-centos-latest
[root@ zabbix_serverdata]# mkdir -p /data/zabbix/usr/lib/zabbix
[root@ zabbix_serverdata]# mkdir -p /data/zabbix/var/lib/zabbix
docker run --name zabbix-server-mysql -t \
-e DB_SERVER_HOST="127.0.0.1" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--net host \
-m=500m \
-v /data/zabbix/usr/lib/zabbix:/usr/lib/zabbix:rw \
-v /data/zabbix/var/lib/zabbix:/var/lib/zabbix:rw \
--restart unless-stopped \
-e TZ="Asia/Shanghai" \
-d zabbix/zabbix-server-mysql:6.0-centos-latest
由于我配置的主机网络比较简单,所有不再需要指定端口
-p 10050:10050
[root@ zabbix_server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f0d9beae4fbd zabbix/zabbix-server-mysql:6.0-centos-latest "/usr/bin/tini -- /u…" 7 minutes ago Restarting (1) 58 seconds ago zabbix-server-mysql
6143a903829a mysql:8.0 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes mysql8
query failed: [1419] You do not have the SUPER privilege and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable) [create trigger hosts_name_upper_insert before insert on hosts for each row set new.nam
在将函数或触发器导入MySQL数据库时,会出现以下错误:“您没有SUPER特权,并且启用了二进制日志记录(您可能想要使用不太安全的log_bin_trust_function_creators变量)”。
解决方案
有几种解决方法:
您需要为运行导入数据库的用户指定SUPER特权,并应用CREATE ROUTINE,ALTER ROUTINE,CREATE TRIGGER,ALTER TRIGGER,CREATE FUNCTION和ALTER FUNCTION特权;
如果要允许具有CREATE ROUTINE特权的MySQL服务器上的所有用户都可以创建此类功能,则可以通过两种方式指定log_bin_trust_function_creators选项:
通过在服务器启动时指定它,例如:-- log-bin-trust-function-creators = 1
通过SET GLOBAL语句将其设置为1 ,例如:
select @@log_bin_trust_function_creators;
SET GLOBAL log_bin_trust_function_creators = 1;
如果升级成功,就会看到 database upgrade fully completed 的字样,我重启了多次zabbix所有下图中没有
[root@ zabbix_server zabbix]# docker logs -f zabbix-server-mysql | grep "database upgrade fully completed"
7:20230510:093607.893 database upgrade fully completed
Zabbix历史的数据和趋势数据的数值(浮点型)数据类型支持的精度更高,需要手动更新数据库补丁。
database could be upgraded to use primary keys in history tables
database is not upgraded to use double precision values
MySQL的补丁地址为:
https://git.zabbix.com/projects/ZBX/repos/zabbix/raw/database/mysql/double.sql
#进入zabbix库执行命令
ALTER TABLE trends
MODIFY value_min DOUBLE PRECISION DEFAULT '0.0000' NOT NULL,
MODIFY value_avg DOUBLE PRECISION DEFAULT '0.0000' NOT NULL,
MODIFY value_max DOUBLE PRECISION DEFAULT '0.0000' NOT NULL;
ALTER TABLE history MODIFY value DOUBLE PRECISION DEFAULT '0.0000' NOT NULL;
[root@ zabbix_server zabbix]# docker logs zabbix-server-mysql
开启高可用,如果监控主机不是很多其实可以关闭掉
starting HA manager
HA manager started in active mode
复制 zabbix_server.conf到宿主机,并做修改,可有可无 看看自己环境是否需要优化
[root@ zabbix_server ~]# docker cp zabbix-server-mysql:/etc/zabbix/zabbix_server.conf /data/zabbix/
Successfully copied 27.65kB to /data/zabbix/
如果你的docker网络使用的是host,需要将ZBX_SERVER_HOST 指定为宿主机的ip地址
docker run --name zabbix-web-nginx-mysql -t \
-e ZBX_SERVER_HOST="zabbix-server-mysql" \
-e DB_SERVER_HOST="127.0.0.1" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
--net host \
--restart unless-stopped \
-e TZ="Asia/Shanghai" \
-d zabbix/zabbix-web-nginx-mysql:6.0-centos-latest
#由于我直接host网络,所有下面指定不需要
--network=zabbix-net \
-p 80:8080 \
详情参考
https://github.com/zabbix/zabbix-docker/blob/6.4/Dockerfiles/web-nginx-mysql/alpine/docker-entrypoint.sh
ln -sfT "$ZABBIX_ETC_DIR/nginx.conf" "$NGINX_CONFD_DIR/nginx.conf"
ln 源文件 目标文件
-s 软链接(符号链接)
-f 强制执行
-T 没有目标目录始终将LINK_NAME视为普通文件(强制将链接视为软链接(符号链接)。)
bash-4.4$ cat nginx_ssl.conf
server {
listen 8443 ssl http2;
listen [::]:8443 ssl http2;
server_name zabbix;
server_name_in_redirect off;
index index.php;
access_log /dev/fd/1 main;
error_log /dev/fd/2 error;
set $webroot '/usr/share/zabbix';
root $webroot;
large_client_header_buffers 8 8k;
client_max_body_size 10M;
ssl_certificate /etc/ssl/nginx/ssl.crt;
ssl_certificate_key /etc/ssl/nginx/ssl.key;
ssl_dhparam /etc/ssl/nginx/dhparam.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;
# intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
# HSTS (ngx_http_headers_module is required) (63072000 seconds)
add_header Strict-Transport-Security "max-age=63072000" always;
add_header Content-Security-Policy-Report-Only "default-src https:; script-src https: 'unsafe-eval' 'unsafe-inline'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /csp-report";
location =/nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location = /favicon.ico {
log_not_found off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# caching of files
location ~* \.ico$ {
expires 1y;
}
location ~* \.(js|css|png|jpg|jpeg|gif|xml|txt)$ {
expires 14d;
}
location ~ /(app\/|conf[^\.]|include\/|local\/|locale\/|vendor\/) {
deny all;
return 404;
}
location ~ ^/(status|ping)$ {
access_log off;
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $webroot$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
fastcgi_pass unix:/tmp/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $webroot$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout {FCGI_READ_TIMEOUT};
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}
}
http://192.168.56.110:8080/index.php
默认账户密码 admin/zabbix
Zabbix server is not running: the information displayed may not be current. Zabbix
docker run --name zabbix-web-nginx-mysql -t \
-e ZBX_SERVER_HOST="192.168.56.110" \
-e DB_SERVER_HOST="127.0.0.1" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="zabbix" \
--net host \
--restart unless-stopped \
-e TZ="Asia/Shanghai" \
-d zabbix/zabbix-web-nginx-mysql:6.0-centos-latest
重新启动后报错消失
我这边先在192.168.56.110上安装的4.2版本的,为了贴切我的生产环境,目前打算将zabbix-agent 4.2版本直接升级为zabbix-agent2 6.0.17版本
rpm -Uvh http://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.8-1.el7.x86_64.rpm
yum install zabbix-agent-4.2.8
#关掉zabbix-agent
[root@ zabbix_server~]# systemctl stop zabbix-agent && systemctl disable zabbix-agent
[root@ zabbix_server~]# mv /etc/zabbix /etc/zabbix_20230511_4.2
zabbix-agent2 支持的插件
https://www.zabbix.com/documentation/6.0/zh/manual/appendix/config/zabbix_agent2_plugins
https://www.zabbix.com/documentation/6.0/zh/manual/config/items/plugins
安装zabbix-agent2
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm
# yum clean all
[root@ zabbix_server ~]# yum install zabbix-agent2 zabbix-agent2-plugins-*
systemctl restart zabbix-agent2
systemctl enable zabbix-agent2
[root@ zabbix_server ~]# rpm -qa | grep zabbix-agent
zabbix-agent2-plugin-mongodb-6.0.17-release1.el7.x86_64
zabbix-agent-4.2.8-1.el7.x86_64
zabbix-agent2-plugin-postgresql-6.0.17-release1.el7.x86_64
zabbix-agent2-6.0.17-release1.el7.x86_64
或者下载包安装
wget http://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-agent2-6.0.17-release1.el7.x86_64.rpm
wget http://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-agent2-plugin-mongodb-6.0.17-release1.el7.x86_64.rpm
http://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-agent2-plugin-postgresql-6.0.17-release1.el7.x86_64.rpm
小测试
参考 https://blog.zabbix.com/docker-container-monitoring-with-zabbix/20175/
[root@zabbix_server plugins.d]# yum install -y zabbix-get
zabbix_get -s '127.0.0.1' -p 10050 -k 'agent.ping'
zabbix_get -s '192.168.56.110' -p 10050 -k 'system.hostname'
[root@zabbix_server ~]# zabbix_get -s '127.0.0.1' -p 10050 -k 'agent.ping'
1
[root@zabbix_server ~]# zabbix_get -s '192.168.56.110' -p 10050 -k 'system.hostname'
zabbix_get [3385]: Check access restrictions in Zabbix agent configuration
[root@zabbix_server ~]# zabbix_get -s '127.0.0.1' -p 10050 -k 'system.hostname'
zabbix_server
#获取与Docker相关的指标
[root@zabbix_server ~]# zabbix_get -s 127.0.0.1 -k docker.info
{"Id":"SG2K:AM4N:ROTF:QEYU:PAQ4:5XPX:DKKI:TUE4:J3LF:CGNN:6WL7:NK7L","Containers":4,"ContainersRunning":4,"ContainersPaused":0,"ContainersStopped":0,"Images":16,"Driver":"overlay2","MemoryLimit":true,"SwapLimit":true,"KernelMemory":false,"KernelMemoryTCP":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"PidsLimit":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIP6tables":true,"Debug":false,"NFd":44,"OomKillDisable":true,"NGoroutines":47,"LoggingDriver":"json-file","CgroupDriver":"cgroupfs","NEventsListener":0,"KernelVersion":"3.10.0-1160.83.1.el7.x86_64","OperatingSystem":"CentOS Linux 7 (Core)","OSVersion":"7","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","NCPU":1,"MemTotal":2984001536,"DockerRootDir":"/var/lib/docker","Name":"zabbix_server","ExperimentalBuild":false,"ServerVersion":"23.0.1","ClusterStore":"","ClusterAdvertise":"","DefaultRuntime":"runc","LiveRestoreEnabled":false,"InitBinary":"docker-init","SecurityOptions":["name=seccomp,profile=builtin"],"Warnings":null}
[root@zabbix_server ~]#
#低级发现密钥 – docker.containers.discovery[false] 来检查低级发现的结果
[root@zabbix_server ~]#
[root@zabbix_server ~]# zabbix_get -s 127.0.0.1 -k docker.containers.discovery[false]
[{"{#ID}":"0968ff7030a0398c19d41d7fd956f733968134c54d3f513ca9e7d7f785b3d18e","{#NAME}":"/zabbix-web-nginx-mysql"},{"{#ID}":"0a0a666f5591e7440355ca1db4ba2b28bbcf9ad2a16701eab9da05187c020a80","{#NAME}":"/zabbix-server-mysql"},{"{#ID}":"ca2fd9a967be94176cccf2be016dd132e371e929592d6fac2a74268d6ea20f3a","{#NAME}":"/grafana"},{"{#ID}":"998f432c9531e44cdb8ac8f6edf9f24a1a02ca32bd0ff5154e2df306193bc4b4","{#NAME}":"/mysql8"}]
官方文档配置解析:https://www.zabbix.com/documentation/6.0/zh/manual/appendix/config/zabbix_agent2
[root@ zabbix_serverzabbix]# cat /etc/zabbix/zabbix_agent2.conf |grep -vE '^$|^#'
PidFile=/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname= zabbix_server_agent2
Include=/etc/zabbix/zabbix_agent2.d/*.conf
PluginSocket=/run/zabbix/agent.plugin.sock
ControlSocket=/run/zabbix/agent.sock
Include=./zabbix_agent2.d/plugins.d/*.conf
[root@ zabbix_server zabbix]# ll /etc/zabbix/zabbix_agent2.d/plugins.d/
总用量 44
-rw-r--r-- 1 root root 1591 4月 24 19:55 ceph.conf
-rw-r--r-- 1 root root 366 4月 24 19:55 docker.conf
-rw-r--r-- 1 root root 1113 4月 24 19:55 memcached.conf
-rw-r--r-- 1 root root 592 4月 24 19:55 modbus.conf
-rw-r--r-- 1 root root 2346 4月 24 20:43 mongodb.conf
-rw-r--r-- 1 root root 174 4月 24 19:55 mqtt.conf
-rw-r--r-- 1 root root 2101 4月 24 19:55 mysql.conf
-rw-r--r-- 1 root root 1700 4月 24 19:55 oracle.conf
-rw-r--r-- 1 root root 3189 4月 24 20:45 postgresql.conf
-rw-r--r-- 1 root root 870 4月 24 19:55 redis.conf
-rw-r--r-- 1 root root 388 4月 24 19:55 smart.conf
https://www.zabbix.com/documentation/6.0/zh/manual/config/items/itemtypes/zabbix_agent/zabbix_agent2
插件配置解析
https://www.zabbix.com/documentation/6.0/zh/manual/appendix/config/zabbix_agent2_plugins
由于zabbix升级后并不会更新模板库,需要自行手动更新
zabbix官方模板库
https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates
zabbix用户加入到docker组,否则无法监控
[root@zabbix_server ~]# usermod -aG docker zabbix
[root@zabbix_server ~]# id zabbix
uid=990(zabbix) gid=987(zabbix) 组=987(zabbix),994(docker)
[root@zabbix_server ~]# systemctl restart zabbix-agent2
、
zabbix-agent2的插件目录下配置redis1的连接信息
[root@zabbix_server plugins.d]# cat /etc/zabbix/zabbix_agent2.d/plugins.d/redis.conf | grep -vE '^#|^$'
[root@zabbix_server ~]# cat /etc/zabbix/zabbix_agent2.d/plugins.d/redis.conf | grep -vE '^#|^$'
Plugins.Redis.KeepAlive=300
Plugins.Redis.Sessions.zabbix_server_agent2.Uri=tcp://127.0.0.1:6379
Plugins.Redis.Sessions.zabbix_server_agent2.Password=redis123456
Plugins.Redis.Sessions..Uri = 地址
SessionName 等于 zabbix_server_agent2,后续还需要修改web页面中默认的监控模版中关于redis的宏
SessionName 的名称不能使用数字以及连接符 "- ";可以使用下划线,最好与主机名称一致
zabbix官方模板库
https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates
zabbix-agent2配置文件参考
https://www.zabbix.com/documentation/6.0/zh/manual/appendix/config/zabbix_agent2_plugins/redis_plugin
修改为redis插件配置中对应的SessionName 的名字 即 zabbix_server_agent2
其他服务和上述监控docker服务时一样, 下载对应的模版,直接监控即可,有的可能也需要修改继承模版中的宏
只是如何定制化就比较麻烦了
https://www.zabbix.com/cn/download?zabbix=6.0&os_distribution=ubuntu&os_version=20.04&components=agent_2&db=&ws=
#Install Zabbix repository
wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb
dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb
apt update
# Install Zabbix agent2
apt install zabbix-agent2 zabbix-agent2-plugin-*
#修改配置文件
root@ubuntu-demo:~# cat /etc/zabbix/zabbix_agent2.conf | grep -vE '^$|^#'
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=192.168.56.110
ServerActive=192.168.56.110
Hostname=192.168.56.111
Include=/etc/zabbix/zabbix_agent2.d/*.conf
PluginSocket=/run/zabbix/agent.plugin.sock
ControlSocket=/run/zabbix/agent.sock
Include=./zabbix_agent2.d/plugins.d/*.conf
systemctl restart zabbix-agent2
systemctl enable zabbix-agent2
Synchronizing state of zabbix-agent2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable zabbix-agent2
在 Ubuntu 中,你可以使用hostnamectl命令编辑系统主机名以及相关设置。这个工具识别三种不同的主机名:
static - 传统主机名。它存储在/etc/hostname文件中,并且可以被用户设置
pretty - 一个自由形态的 UTF8 主机名,用来代表用户。例如: Linuxize’s desktop。
transient - 由 kernel 维护的动态主机名。 在运行过程中,DHCP 或者 mDNS 服务器可以改变 transient 主机名。默认情况下,它和 static 主机名一模一样。
静态主机名被存储在/etc/hostname,并且 pretty 主机名被存储在/etc/machine-info文件
如果你在一个云实例上运行 Ubuntu,并且安装了cloud-init软件包,你也可以编辑/etc/cloud/cloud.cfg文件。这个软件包由云服务器厂商提供,通常默认被安装,并且它可以被用来处理云服务器实例的初始化。
root@ubuntu-demo:~# hostname
ubuntu-demo
root@ubuntu-demo:~# hostnamectl
Static hostname: ubuntu-demo
Pretty hostname: ubuntu_demo
Icon name: computer-vm
Chassis: vm
Machine ID: 587eae0dc1b44cc09dcf6673d15a00f6
Boot ID: 87bdda6441054244ab574fc2317b7e33
Virtualization: oracle
Operating System: Ubuntu 20.04.6 LTS
Kernel: Linux 5.4.0-148-generic
Architecture: x86-64
root@ubuntu-demo:~#
root@ubuntu-demo:~# cat /etc/hosts
127.0.0.1 localhost
127.0.0.1 ubuntu-demo ubuntu_redis5_test
root@ubuntu-demo:~# apt install redis
#修改密码
root@ubuntu-demo:~# cat /etc/redis/redis.conf |grep -vE '^$|^#' | grep pass
requirepass foobared
root@ubuntu-demo:~# cat /etc/redis/redis.conf |grep -vE '^$|^#' | grep bind
bind 127.0.0.1 192.168.56.111
root@ubuntu-demo:~# systemctl start redis
root@ubuntu-demo:~# systemctl enable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable redis-server
root@ubuntu-demo:~# cat /etc/zabbix/zabbix_agent2.d/plugins.d/redis.conf | grep -vE '^$|^#'
Plugins.Redis.KeepAlive=300
Plugins.Redis.Sessions.ubuntu_redis5_test.Uri=tcp://127.0.0.1:6379
Plugins.Redis.Sessions.ubuntu_redis5_test.Password=ubuntu123456
zabbix会自动将这个继承宏 添加到主机宏
这里我只是截图了俩个仪表盘的监控数据
经纬度查询 https://jingweidu.bmcx.com/
openstreetmap 官网 https://www.openstreetmap.de/
在该网站获取地图.png http://openwhatevermap.xyz/
北京
经度 116.23128
纬度 40.22077
添加仪表盘
注意:坐标别写反了 先写纬度,再写经度
在管理—— 一般 —— 地理地图 可以修改地图的获取来源
默认 图源获取 https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png
官网 https://www.openstreetmap.de/
http://openwhatevermap.xyz/ 在该网站获取地图.png
添加该url
https://{s}.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png
批量添加主机
https://www.zabbix.com/documentation/6.0/zh/manual/api/reference/host/create
#!/bin/bash
#批量添加zabbix主机
#登陆
token=`echo $json | grep result | awk -F'"' '{print $10}'`
#批量添加主机
for ip in `cat /root/host.txt`
do
curl -s -X POST -H 'Content-Type: application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "Linux server",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.3.1",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "50"
}
],
"tags": [
{
"tag": "Host name",
"value": "Linux server"
}
],
"templates": [
{
"templateid": "20045"
}
],
"macros": [
{
"macro": "{$USER_ID}",
"value": "123321"
},
{
"macro": "{$USER_LOCATION}",
"value": "0:0:0",
"description": "latitude, longitude and altitude coordinates"
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "038e1d7b1735c6a5436ee9eae095879e",
"id": 1
}
}' http://192.168.81.250/zabbix/api_jsonrpc.php | python -m json.tool
done
执行脚本
192.169.56.110
[root@zabbix_server ~]# docker pull grafana/grafana:9.5.0
启动容器
如果是升级,请先备份 宿主机上 /var/lib/docker/volumes/grafana 以及/etc/grafana
宿主机上创建grafana用户,并指定UID,并且加入root组,这样也不需要给容器root权限了
官方参考:https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/#migrate-to-v51-or-later
[root@zabbix_server ~]#useradd -r -s/sbin/nologin -M grafana -u 472 -G 0
[root@zabbix_server ~]# docker run --user 472 --name grafana -d --net host -m=500M --restart unless-stopped --mount type=volume,source=grafana,target=/var/lib/grafana:rw -v /etc/grafana:/etc/grafana:rw -e ZBX_SERVER_HOST="192.168.56.110" -e TZ="Asia/Shanghai" grafana/grafana:9.5.0
可以看到目录权限为grafana,这样也不需要给容器root权限了
http://192.168.56.110:3000/
admin/admin
http://192.168.56.110:8080/api_jsonrpc.php