使用docker部署zabbix,自定义微信报警

it2023-01-19  50

docker部署zabbix

#使用的相关镜像: docker pull mysql:5.7 docker pull zabbix/zabbix-server-mysql:centos-latest # 采用centos内核的zabbix server镜像 docker pull zabbix/zabbix-web-nginx-mysql:latest docker pull zabbix/zabbix-java-gateway:latest #1、创建一个新的 Docker 网络 docker network create -d bridge zabbix_net #2、运行mysql 镜像,创建mysql容器。端口如果被占用,则修改为 -p 3310:3306 docker run -dit -p 3306:3306 --name zabbix-mysql --network zabbix_net --restart always -v /etc/localtime:/etc/localtime -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix123" -e MYSQL_ROOT_PASSWORD="root123" mysql:5.7 # MYSQL_DATABASE="zabbix" 在msql中创建的数据库的名 # MYSQL_USER="zabbix" 创建msql的登录账户名 # MYSQL_PASSWORD="zabbix123" 设置创建msql的登录账户的密码 # MYSQL_ROOT_PASSWORD="root123" 设置msql数据库root 的密码 # --network zabbix_net是将容器加入到zabbix_net网络中, # -v /etc/localtime:/etc/localtime是同步服务器和容器内部的时区, # --restart always设置自启动, # -e MYSQL_DATABASE="zabbix",创建环境变量。 # --name zabbix-mysql,给容器命名。 #3、运行zabbix-java-gateway镜像,创建zabbix-java-gateway容器。 docker run -v /etc/localtime:/etc/localtime -dit --restart=always --name=zabbix-java-gateway --network zabbix_net zabbix/zabbix-java-gateway:latest #4、运行zabbix-server-mysql镜像,创建zabbix-server-mysql容器。 # 首先创建数据卷zabbix-server-vol,通过命令 docker volume create zabbix-server-vol #启动zabbix-server-mysql容器。 #此处的以下内容与 运行mysql 镜像,创建mysql容器设置的内容要一致 # MYSQL_DATABASE="zabbix" # MYSQL_USER="zabbix" # MYSQL_PASSWORD="zabbix123" # MYSQL_ROOT_PASSWORD="root123" docker run -dit -p 10051:10051 --mount source=zabbix-server-vol,target=/etc/zabbix -v /etc/localtime:/etc/localtime -v /usr/lib/zabbix/alertscripts:/usr/lib/zabbix/alertscripts --name=zabbix-server-mysql --restart=always --network zabbix_net -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix123" -e MYSQL_ROOT_PASSWORD="root123" -e ZBX_JAVAGATEWAY="zabbix-java-gateway" zabbix/zabbix-server-mysql:centos-latest #5. 运行zabbix-web-nginx-mysql镜像,创建zabbix-web-nginx-mysql容器。 #此处的以下内容与 运行mysql 镜像,创建mysql容器设置的内容要一致 # MYSQL_DATABASE="zabbix" # MYSQL_USER="zabbix" # MYSQL_PASSWORD="zabbix123" # MYSQL_ROOT_PASSWORD="root123" # 端口被占用,修改 -p 80:8080 docker run -dit -p 8080:8080 -v /etc/localtime:/etc/localtime --name zabbix-web-nginx-mysql --restart=always --network zabbix_net -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_DATABASE="zabbix" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix123" -e MYSQL_ROOT_PASSWORD="root123" -e ZBX_SERVER_HOST="zabbix-server-mysql" zabbix/zabbix-web-nginx-mysql:latest

部署成功,在浏览器中输入http://IP(:8080)/zabbix,打开zabbix首页,其中用户名密码分别是admin/zabbix。

创建监控项以及触发器

创建新的应用集 创建监控项 创建触发器 使用py脚本发送报警信息到企业微信,所以需要有python环境。

自定义微信报警

安装python环境

# 进入zabbix-server中 # 直接以root用户进入docker容器中 docker exec -it --user root zabbix-server-mysql /bin/bash # 1.安装相应的编译工具 yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum install -y libffi-devel zlib1g-dev yum install zlib* -y # centos安装wget yum -y install wget # 2.下载安装包 wget wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz # 3.解压 tar -xvJf Python-3.7.2.tar.xz # 4.创建编译安装目录 mkdir /usr/local/python3 # 5.安装 cd Python-3.7.2 ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl #第一个指定安装的路径,不指定的话,安装过程中可能软件所需要的文件复制到其他不同目录,删除软件很不方便,复制软件也不方便. #第二个可以提高python10%-20%代码运行速度. #第三个是为了安装pip需要用到ssl,后面报错会有提到. make && make install # 6.创建软链接 ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3 # 7.验证是否成功 python3 -V pip3 -V # python: command not found # 解决: whereis python3 python3: /usr/lib/python3.6 /usr/lib64/python3.6 /usr/local/bin/python3 /usr/include/python3.6m /usr/local/python3 /usr/local/bin/python3 -V Python 3.7.2 # 找到了python3的路径 # 建立软连接 ln -s /usr/local/bin/python3 /usr/bin/python python -V Python 3.7.2 # 将py脚本放入zabbix脚本目录下 grep -Ev '^$|#' /etc/zabbix/zabbix_server.conf LogFile=/var/log/zabbix/zabbix_server.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_server.pid SocketDir=/var/run/zabbix DBHost=localhost DBName=zabbix DBUser=zabbix DBPassword=123456 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 AlertScriptsPath=/usr/lib/zabbix/alertscripts # 所有脚本存放的地方 ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000 cd /usr/lib/zabbix/alertscripts # 给执行权限 chmod +x weixin_alarm.py # zabbix执行时报错:error: import: command not found # 删除文件中所有空行 # 并且在最上方加一行: #!/usr/bin/python # zabbix执行时报错:/usr/bin/python^M: bad interpreter: No such file or directory vim weixin_alarm.py # 利用如下命令查看文件格式 :set ff # 格式为dos # 利用如下命令修改文件格式 :set ff=unix # 保存 退出 :wq # 解决

相关报错:

使用pip3安装软件报错:pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

解决办法:

首先安装 openssl-devel

sudo yum install openssl-devel

进入python安装包,重新编译

cd Python-3.7.2 ./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl make && make install Traceback (most recent call last): File "/usr/lib/zabbix/alertscripts/weixin_alarm.py", line 28, in <module> filemode = 'a' File "/usr/local/lib/python3.7/logging/__init__.py", line 1900, in basicConfig h = FileHandler(filename, mode) File "/usr/local/lib/python3.7/logging/__init__.py", line 1092, in __init__ StreamHandler.__init__(self, self._open()) File "/usr/local/lib/python3.7/logging/__init__.py", line 1121, in _open return open(self.baseFilename, self.mode, encoding=self.encoding) PermissionError: [Errno 13] Permission denied: '/tmp/weixin.log'

解决:

# 修改文件权限: cd /tmp chown zabbix:zabbix weixin.log # 修改py脚本权限 cd /usr/lib/zabbix/alertscripts chown zabbix:zabbix weixin_alarm.py # 修改权限保证报警脚本能被zabbix调用执行。

配置微信报警类型

创建媒体类型:

修改时间显示问题

docker exec -it zabbix-web-nginx-mysql /bin/bash find / -name zabbix.conf /etc/php7/php-fpm.d/zabbix.conf vi /etc/php7/php-fpm.d/zabbix.conf php_value[date.timezone] = Asia/Shanghai # 修改该行为Asia/Shanghai # 重新启动容器生效

logstash相关配置

# docker-compose.yml version: '3' services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1 #镜像名 hostname: elasticsearch container_name: elasticsearch #容器名 restart: always environment: #指定变量,相当于是直接在启动命令行上指定参数,具体能指定哪些参数要看该镜像支持哪些参数 - 'ES_JAVA_OPTS=-Xms1g -Xmx1g' #指定es的内存大小 ulimits: #生产环境需要配置的系统配置 memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 volumes: #挂载,格式为 本地卷:docker容器中卷 - ./elasticsearch/data:/usr/share/elasticsearch/data - ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml - ./elasticsearch/logs:/user/share/elasticsearch/logs ports: - 9200:9200 - 9300:9300 #es集群之间通信的端口,不可省略,否则,集群状态下会端口冲突 networks: #指定该容器使用的网络 - esnet kibana: image: docker.elastic.co/kibana/kibana:7.1.1 hostname: kibana container_name: kibana restart: always volumes: - ./kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml ports: - 5601:5601 networks: - esnet depends_on: - elasticsearch logstash: image: docker.elastic.co/logstash/logstash:7.1.1 hostname: logstash container_name: logstash restart: always volumes: - ./logstash/config:/usr/share/logstash/config - ./logstash/pipeline:/usr/share/logstash/pipeline ports: - 5044:5044 - 19009:8080 environment: LS_JAVA_OPTS: '-Xmx512m -Xms512m' networks: - esnet depends_on: - elasticsearch networks: esnet: # logstash/config目录下 # logstash.yml http.host: "0.0.0.0" # pipelines.yml - pipeline.id: main pipeline.workers: 1 path.config: "/usr/share/logstash/pipeline/logstash.conf" - pipeline.id: logout_to_zabbix pipeline.workers: 1 path.config: "/usr/share/logstash/pipeline/logout_to_zabbix.conf" # pipeline目录下新增 logout_to_zabbix.conf input{ kafka { bootstrap_servers => "xx.xx.xx.xx:xxxx" #kafka服务器地址 topics => "_system_log_collect" group_id => "zabbix_logs" codec => "json" #写入的时候使用json编码,因为logstash收集后会转换成json格式 } #stdin { # type => "stdin" # } } filter { json { source => "_source" } mutate { add_field =>["[zabbix_key]","oslogs1"] add_field =>["[zabbix_host]","Zabbix server"] add_field => ["parsed_message", "from %{[container][name]}, %{[fields][server]} message: %{[message]}"] } } output { if ("error" in [message] or "ERROR" in [message] or "ERR" in [message] or "Failed" in [message]) and "接口调用异常" not in [message] { zabbix { zabbix_host => "[zabbix_host]" zabbix_key => "[zabbix_key]" zabbix_server_host => "xxx.xxx.xxx.xxx" zabbix_server_port => "10051" zabbix_value => "parsed_message" } # stdout{ codec => rubydebug } } }

logstash中安装logstash-output-zabbix插件

docker exec -it logstash /bin/bash # 安装logstash-output-zabbix插件 /usr/share/logstash/bin/logstash-plugin install logstash-output-zabbix Validating logstash-output-zabbix Installing logstash-output-zabbix Installation successful # 安装成功 # 如果logstash容器无法启动,则修改logout_to_zabbix.conf为 input{ stdin { type => "stdin" } } output { stdout{ codec => rubydebug } } } # 启动后进入容器安装logstash-output-zabbix插件,安装成功后还原logout_to_zabbix.conf文件,重新启动docker-compose即可
最新回复(0)