1、搭建zabbix服务,实现监控linux和windows的内存,cpu,磁盘,网络等基础指标
-
zabbix部署网络拓扑图
环境说明
操作系统:Centos7
安装zabbix-release
node1:10.193.116.98 安装zabbix-server模块
node2:10.193.116.97 安装zabbix数据库模块
node3:10.193.116.96 安装zabbix-web模块
node4:10.193.116.95 安装zabbix-proxy模块
node5:10.193.116.94 安装zabbix-agent模块
A机房:zabbix-agent直接连接zabbix-server
B机房:zabbix-agent直接连接zabbix-proxy-
通过ansible进行部署
- 创建roles和hosts文件
mkdir -pv {zabbix-server,zabbix-agent,zabbix-web,zabbix-proxy,zabbix-mariadb}/{tasks,vars,templates,files,meta,defaults,handlers} hosts文件 [node1-zabbix-server] 10.193.116.98 [node2-zabbix-mariadb] 10.193.116.97 [node3-zabbix-web] 10.193.116.96 [node4-zabbix-proxy] 10.193.116.95 [all-zabbix-agent] 10.193.116.[94:97]
- zabbix-mariadb 角色
[root@ansible zabbix-mariadb]# tree . |-- defaults |-- files | |-- create.sql.gz | `-- my.cnf |-- handlers |-- meta |-- tasks | `-- main.yml |-- templates `-- vars main.yml内容 - name: install mariadb-server yum: name={{ item }} with_items: - mariadb-server - MySQL-python - name: copy conf copy: src=my.cnf dest=/etc/my.cnf backup=yes - name: start service service: name=mariadb state=started enabled=yes - name: create zabbix databases mysql_db: login_user=root name={{ db_name }} encoding=utf8 - name: mariadb conf mysql_user: login_user=root name={{ zabbix_name}} host={{ zabbix_hosts }} password={{ zabbix_passwd }} priv='{{ db_name }}.*:ALL' state=present tags: db playbook内容段 - hosts: node2-zabbix-mariadb remote_user: root vars: - db_name: zabbix - zabbix_name: zabbix - zabbix_hosts: 10.193.116.% - zabbix_passwd: zabbix roles: - zabbix-mariadb
- zabbix-server 角色
[root@ansible zabbix-server]# tree . |-- defaults |-- files |-- handlers |-- meta |-- tasks | `-- main.yml |-- templates | `-- zabbix_server.conf.j2 `-- vars main.yml内容 - name: install zabbix-server-mysql zabbix-get yum: name={{ item }} with_items: - zabbix-server-mysql - zabbix-get - mariadb - zabbix-agent - name: install conf template: src=zabbix_server.conf.j2 dest=/etc/zabbix/zabbix_server.conf - name: init zabbix_db shell: zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -h{{ DBHost }} -u{{ DBUser }} -p{{ DBPassword }} {{ DBName }} ignore_errors: yes - name: start service service: name=zabbix-server state=started enabled=yes - name: start agent service: name=zabbix-agent state=started enabled=yes zabbix_server.conf.j2中变量如下 DBHost={{ DBHost }} DBName={{ DBName }} DBUser={{ DBUser }} DBPassword={{ DBPassword }} playbook内容段 - hosts: node1-zabbix-server remote_user: root vars: - DBHost: 10.193.116.97 - DBName: zabbix - DBUser: zabbix - DBPassword: zabbix roles: - zabbix-server
- zabbix-web 角色
[root@ansible zabbix-web]# tree . |-- defaults |-- files | `-- zabbix.conf |-- handlers |-- meta |-- tasks | `-- main.yml |-- templates `-- vars main.yml内容 - name: install zabbix-server-web yum: name={{ item }} with_items: - zabbix-web-mysql - mariadb - name: httpd conf copy: src=zabbix.conf dest=/etc/httpd/conf.d/ - name: start service service: name=httpd state=started enabled=yes playbook内容段 - hosts: node3-zabbix-web remote_user: root roles: - zabbix-web
- zabbix-proxy 角色
[root@ansible zabbix-proxy]# tree . |-- defaults |-- files | `-- my.cnf |-- handlers |-- meta |-- tasks | `-- main.yml |-- templates | `-- zabbix_proxy.conf.j2 `-- vars main.yml内容 - name: install mariadb-server yum: name={{ item }} with_items: - mariadb-server - zabbix-proxy-mysql - zabbix-get - MySQL-python - name: copy conf copy: src=my.cnf dest=/etc/my.cnf backup=yes - name: start service service: name=mariadb state=started enabled=yes - name: create zabbix databases mysql_db: login_user=root name={{ DBName }} encoding=utf8 - name: mariadb conf mysql_user: login_user=root name={{ DBUser }} host={{ DBHost }} password={{ DBPassword }} priv='{{ DBName }}.*:ALL' state=present tags: db - name: init proxy_db shell: zcat /usr/share/doc/zabbix-proxy-mysql*/schema.sql.gz | mysql -h{{ DBHost }} -u{{ DBUser }} -p{{ DBPassword }} {{ DBName }} ignore_errors: yes - name: copy zabbix-proxy template: src=zabbix_proxy.conf.j2 dest=/etc/zabbix/zabbix_proxy.conf - name: start zabbix-proxy service: name=zabbix-proxy state=started enabled=yes zabbix_proxy.conf.j2中变量 Server={{ Server }} DBName={{ DBName }} DBUser={{ DBUser }} DBPassword={{ DBPassword }} playbook内容段 - hosts: node4-zabbix-proxy remote_user: root vars: - Server: 10.193.116.98 - DBHost: localhost - DBName: zabbix_proxy - DBUser: zabbix - DBPassword: zabbix roles: - zabbix-proxy
- zabbix-agent 角色
[root@ansible zabbix-agent]# tree . |-- defaults |-- files |-- handlers |-- meta |-- tasks | `-- main.yml |-- templates | `-- zabbix_agentd.conf.j2 `-- vars main.yml内容 - name: install zabbix-agent yum: name={{ item }} with_items: - zabbix-agent - name: agent conf template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf - name: start service service: name=zabbix-agent state=started enabled=yes zabbix_agentd.conf.j2中变量 Server={{ Server }} ServerActive={{ ServerActive }} Hostname={{ ansible_default_ipv4.address }} playbook内容段 - hosts: all-zabbix-agent remote_user: root vars: - ServerActive: 10.193.116.98 - Server: 10.193.116.98 roles: - zabbix-agent
- playbook
[root@ansible ansible]# vim zabbix.yaml - hosts: node2-zabbix-mariadb remote_user: root vars: - db_name: zabbix - zabbix_name: zabbix - zabbix_hosts: 10.193.116.% - zabbix_passwd: zabbix roles: - zabbix-mariadb - hosts: node1-zabbix-server remote_user: root vars: - DBHost: 10.193.116.97 - DBName: zabbix - DBUser: zabbix - DBPassword: zabbix roles: - zabbix-server - hosts: node3-zabbix-web remote_user: root roles: - zabbix-web - hosts: node4-zabbix-proxy remote_user: root vars: - Server: 10.193.116.98 - DBHost: localhost - DBName: zabbix_proxy - DBUser: zabbix - DBPassword: zabbix roles: - zabbix-proxy - hosts: all-zabbix-agent remote_user: root vars: - ServerActive: 10.193.116.98 - Server: 10.193.116.98 roles: - zabbix-agent
-
运行结果
-
将node5添加监控
配置--->主机-->填写主机名称-->选择群组-->填写接口IP-->选择proxy-->添加模板-->更新
-
查看node5监控数据
监测-->最新数据-->选择主机-->选择应用集
-
windows服务器监控(10.193.116.93)
- 网管下载Windows版本的zabbix-agent
地址:https://www.zabbix.com/cn/download_agents#tab:40LTS
-
解析安装包至Windows本地C盘
-
安装
cd "C:\zabbix_agents-4.0.7-win-amd64\bin"
zabbix_agentd.exe -i -c "C:\zabbix_agents-4.0.7-win-amd64\conf\zabbix_agentd.conf"
- 网管下载Windows版本的zabbix-agent
-
启动
命令行启动:
zabbix_agentd.exe -s -c "C:\zabbix_agents-4.0.7-win-amd64\conf\zabbix_agentd.conf"
或者通过服务管理启动:
-
测试是否获取数据
-
zabbix-web 添加Windows主机,并查看数据
2、搭建zabbix服务,监控nginx status
- node5 安装nginx,添加开启nginx状态监控
location /nginx_status {
# Turn on nginx stats
stub_status on;
# I do not need logs for stats
access_log off;
# Security: Only allow access from 192.168.1.100 IP #
allow 127.0.0.1/8;
deny all;
}
- 编写监控脚本
[root@node5 ~]# vim /usr/bin/nginx_status.sh
#!/bin/bash
#
host='127.0.0.1'
port='80'
statusurl='nginx_status'
active(){
curl -s http://${host}:${port}/${statusurl} |awk '/^Active/{print $3}'
}
accepts(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==3{print $1}'
}
handled(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==3{print $2}'
}
requests(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==3{print $3}'
}
reading(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==4{print $2}'
}
writing(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==4{print $4}'
}
waiting(){
curl -s http://${host}:${port}/${statusurl} |awk 'NR==4{print $6}'
}
case "$1" in
active)
active
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;
*)
echo $"Usage: $0 {active|accepts|handled|requests|reading|writing|waiting}"
exit 1
esac
- zabbix-agent 自定义key
[root@node5 ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
UserParameter=nginx.statu[*],/usr/bin/nginx_status.sh $1
重启zabbix-agent
[root@node5 ~]# systemctl restart zabbix-agent
-
zabbix-web页面添加nginx_status告警items
配置Nginx_requests来进行说明
首先配置监控项
配置-->模板-->监控项
接着进入进程配置页面
-
添加告警
配置-->模板-->触发器
名称:Nginx每秒请求超过100
表达式:
{Centos7-Nginx:nginx.statu[requests].last(#1)}>100
压测触发告警
安装ab压测工具
[root@node5 ~]# yum -y install httpd-tools
[root@node5 ~]# ab -n 5000 -c 20 http://127.0.0.1/nginx_status