强哥带你学zabbix 5.01:什么是监控?2:常见的linux监控命令3:使用shell脚本来监控服务器4: zabbix的基础服务架构5: zabbix生产环境安装5.1:安装php运行环境5.2:安装数据库5.3:安装zabbix-server5.4: 安装zabbix-web6∶监控一台服务器主机7∶自定义监控项7.1:什么是监控项7.2 使用内置key监控nginx的状态7.3 使用自定义的监控项来监控nginx7.4 自定义带参数的监控项8∶自定义触发器9:自定义报警9.1 邮件报警9.2 微信报警(自定义脚本报警)9.3 报警升级10:自定义图形和grafana出图11∶自定义监控模板12:zabbix监控角度总结13: 用户访问量监控14:使用percona插件监控mysql15: snmp监控window和Linux16: zabbix自动发现和自动注册(主动)17: zabbix-agent主动和被动的区别18: zabbix版本升级19: zabbix分布式监控proxy20: zabbix监控java jvm原理21: zabbix性能优化22: zabbix低级自动发现23: zabbix api
监视和控制,
生活中的监控:事后追责
运维中的监控:事后追责,事前预警,性能分析,实时报警
cpu,内存,磁盘,网络
1top
2htop
3uptime
4free
5vmstat
6iostat
7df
8iftop
9nethogs
没有监控工具的时候,shell脚本+定时任务 监控服务器
x1[root@k8s ~]# cat mem_alter.sh
2
3
4MEM=`free -m|awk 'NR==2{print $NF}'`
5if [ $MEM -lt 100 ];then
6 echo "web服务器 192.168.2.104 可用内存不足,当前可用内存$MEM" | mail -s "web服务器内存不足" 296917342@qq.com
7fi
缺点:效率低,不能实现集中报警,不能分析历史数据
我只有一台云主机需要监控,适合shell脚本+定时任务
主机 :zabbix-server
ip地址: 10.0.0.71
操作系统版本: centos 7.6
要求php版本 7.2以上+
安装php第三方源
xxxxxxxxxx
31yum install epel-release -y
2rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
3yum install php72w-fpm php72w-gd.x86_64 php72w-bcmath.x86_64 php72w-xml.x86_64 php72w-mbstring.x86_64 php72w-ldap.x86_64 php72w-mysqlnd.x86_64 -y
安装nginx
xxxxxxxxxx
11yum install nginx -y
配置php-fpm和nginx
xxxxxxxxxx
301vim /etc/php-fpm.d/www.conf
2user = nginx
3group = nginx
4
5vim /etc/nginx/nginx.conf
6worker_processes 1;
7events {
8 worker_connections 1024;
9}
10http {
11 include mime.types;
12 default_type application/octet-stream;
13 sendfile on;
14 keepalive_timeout 65;
15 server {
16 listen 80;
17 server_name localhost;
18 location / {
19 root /html;
20 index index.php index.html index.htm;
21 }
22 location ~ \.php$ {
23 root /html;
24 fastcgi_pass 127.0.0.1:9000;
25 fastcgi_index index.php;
26 fastcgi_param SCRIPT_FILENAME /html$fastcgi_script_name;
27 include fastcgi_params;
28 }
29 }
30}
启动nginx和php-fpm
xxxxxxxxxx
61mkdir /html
2nginx -t
3systemctl start nginx
4systemctl enable nginx
5systemctl start php-fpm
6systemctl enable php-fpm
准备zabbix-web的php代码
xxxxxxxxxx
81wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.8.tar.gz
2tar xf zabbix-5.0.8.tar.gz
3cd zabbix-5.0.8/ui/
4cp -a * /html/
5chown -R nginx:nginx /html
6#解决首次访问 zabbix-web安装界面 error 500的错误
7mkdir /var/lib/php/session
8chown -R nginx:nginx /var/lib/php/session/
xxxxxxxxxx
81#解决方法:
2vim /etc/php.ini
3max_execution_time = 300
4max_input_time = 300
5post_max_size = 16M
6date.timezone = Asia/Shanghai
7
8systemctl restart php-fpm.service
xxxxxxxxxx
251#参数链接https://www.jianshu.com/p/dd7137c4efa5
2 120 tar xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
3 122 mkdir /app
4 123 mv mysql-5.7.32-linux-glibc2.12-x86_64 /app/mysql
5 125 vim /etc/profile
6 126 source /etc/profile
7 127 useradd mysql
8 128 mkdir /data/mysql -p
9 129 chown -R mysql.mysql /app/*
10 130 chown -R mysql.mysql /data/*
11 132 yum install libaio-devel -y
12 133 mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
13 138 vim /etc/my.cnf
14 139 vim /etc/systemd/system/mysqld.service
15 140 systemctl start mysqld.service
16 141 netstat -lntup
17 142 systemctl enable mysqld
18 143 mysql -uroot -p'j>jd&D;2(1_E'
19 146 mysqladmin -uroot -p'j>jd&D;2(1_E' password '1qaz@WSX'
20 147 mysql -uroot -p'1qaz@WSX'
21
22#创库授权
23create database zabbix character set utf8 collate utf8_bin;
24create user 'zabbix'@'localhost' identified by '123456';
25grant all privileges on zabbix.* to 'zabbix'@'localhost';
配置zabbix 5.0的源
xxxxxxxxxx
21rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
2sed -i 's#http://repo.zabbix.com#https://mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
安装zabbix-server
xxxxxxxxxx
11yum install zabbix-server-mysql -y
导入zabbix初始数据
xxxxxxxxxx
11zcat /usr/share/doc/zabbix-server-mysql-*/create.sql.gz|mysql -uzabbix -p123456 zabbix
配置zabbix-server
xxxxxxxxxx
61vim /etc/zabbix/zabbix_server.conf
2DBHost=localhost
3DBName=zabbix
4DBUser=zabbix
5DBPassword=123456
6DBSocket=/tmp/mysql.sock
启动zabbix-server
xxxxxxxxxx
31systemctl start zabbix-server.service
2systemctl enable zabbix-server.service
3netstat -lntup|grep 10051
解决方法:
xxxxxxxxxx
51vim /etc/php.ini
2pdo_mysql.default_socket= /tmp/mysql.sock
3mysqli.default_socket = /tmp/mysql.sock
4
5systemctl restart php-fpm
登录zabbix
用户名: Admin
密码:zabbix
调整语言为中文
6.1 监控zabbix-server
xxxxxxxxxx
41yum install zabbix-agent -y
2
3systemctl start zabbix-agent.service
4systemctl enable zabbix-agent.service
6.2 监控其他linux主机
添加监控前准备
xxxxxxxxxx
91#安装
2rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent2-5.0.8-1.el7.x86_64.rpm
3#配置
4vim /etc/zabbix/zabbix_agent2.conf
5Server=10.0.0.71
6
7#启动
8systemctl start zabbix-agent2.service
9systemctl enable zabbix-agent2.service
监控项:就是我们想要监控的指标,例如剩余内存,磁盘空间,服务的状态等等
每一个监控项,都有一个唯一的key,简洁明了(相当于shell脚本的变量名)
只需要安装zabbix-agent,默认就支持大量的监控项,但是linux模板并没有使用所有监控项 Template OS Linux by Zabbix agent主要监控了cpu,内存,磁盘,网卡,安全,它们都属于通用监控
应用集是监控项的分组
注意:添加监控项之前最好使用zabbix-get测试取值
xxxxxxxxxx
81#只在zabbix-server上安装
2yum install zabbix-get -y
3
4zabbix_get -s 127.0.0.1 -k proc.num[nginx]
5#注释
6-s 被监控主机的ip地址
7-p 端口
8-k 指定监控项的key
修改agent配置文件
xxxxxxxxxx
81#zabbix-agent
2vim /etc/zabbix/zabbix_agentd.conf
3
4#zabbix-agent2
5vim /etc/zabbix/zabbix_agent2.conf
6
7#增加一行
8UserParameter=nginx_status,netstat -lntp|grep -c nginx
重启agent生效
xxxxxxxxxx
11systemctl restart zabbix-agent2.service
使用zabbix-get测试取值
xxxxxxxxxx
21#只能在zabbix-server上执行
2zabbix_get -s 10.0.0.8 -k nginx_status
注意 zabbix-agent 取值的时候权限不足解决方法:
xxxxxxxxxx
311:使用sudo来执行
22:使用suid来执行
33:使用root用户来运行zabbix-agent
web界面添加监控项过程,同上
建议把agent自定义监控项独立成一个配置文件
xxxxxxxxxx
21vim /etc/zabbix/zabbix_agent2.d/zbx_nginx.conf
2UserParameter=nginx_status,netstat -lntp|grep -c nginx
好处:可以复用,管理更方便
对应有规律的监控项:
xxxxxxxxxx
31UserParameter=mysql_Uptime,echo 'show status;'|mysql -uroot|grep -w 'Uptime'|awk '{print $2}'
2UserParameter=mysql_Com_select,echo 'show status;'|mysql -uroot|grep -w 'Com_select'|awk '{print $2}'
3UserParameter=mysql_Com_delete,echo 'show status;'|mysql -uroot|grep -w 'Com_delete'|awk '{print $2}'
适合使用带参考的自定义监控项
xxxxxxxxxx
11UserParameter=mysql_define[*],echo 'show status;'|mysql -uroot|grep -w "$1"|awk '{print $$2}'
取值效果
xxxxxxxxxx
61[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql_define[Uptime]
2827
3[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql_define[Com_select]
41
5[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql_define[Com_insert]
60
触发器:设置一个报警条件
一个触发器至少对应一个监控项
触发器表达式的格式
例子1:
xxxxxxxxxx
41{10.0.0.8:vfs.file.cksum[/etc/passwd].diff()}>0
2主机:10.0.0.8
3key值:vfs.file.cksum[/etc/passwd]
4函数方法:diff() 对比两次监控项的值
例子2:
xxxxxxxxxx
41{10.0.0.8:proc.num.last()}/{10.0.0.8:kernel.maxproc.last()}*100>80
210.0.0.8:proc.num:当前运行进程
310.0.0.8:kernel.maxproc 系统最大允许进程的数量
4函数方法:last() 最新值
例子3:
xxxxxxxxxx
61{10.0.0.8:vm.memory.size[available].min(5m)}<{$MEMORY.AVAILABLE.MIN} and {10.0.0.8:vm.memory.size[total].last()}>0
2
3函数方法 mim(5m),max(5m),avg(5m)
4{$MEMORY.AVAILABLE.MIN}=20m
5and 同时
610.0.0.8:vm.memory.size[total] 最新的总内存大小
自定义触发器表达
单条件
xxxxxxxxxx
11{10.0.0.8:nginx_status.last()}=0
多条件
xxxxxxxxxx
11{10.0.0.8:disk_free.last()}<100M and {10.0.0.8:disk_free_per.last()}<3
配置发件人
配置收件人
配置触发器动作
触发报警
xxxxxxxxxx
11systemctl stop nginx.service
效果
准备好企业微信号
加入企业微信
使用微信关联企业微信
测试微信报警脚本
xxxxxxxxxx
141#修改脚本
2corpid='wxd074861951c67ba6'
3appsecret='QtraZrI936DZ0jZ3aSWTZ-lFVheAMgLmq3toM4B9U1A'
4agentid=1
5
6#安装python模板
7yum install python-pip -y
8pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
9
10#测试发送微信消息
11python weixin.py 'BuYuanTouLuXingShiDeCuiXianSheng' '天气真好' '阳光明媚,春暖花开!'
12#第一个参数,企业微信的用户
13#第二个参数,报警标题
14#第三个参数,报警内容
将脚本放在指定的位置
xxxxxxxxxx
61#查看报警脚本路径
2grep -Ev '^$|#' /etc/zabbix/zabbix_server.conf
3AlertScriptsPath=/usr/lib/zabbix/alertscripts
4
5mv weixin.py /usr/lib/zabbix/alertscripts/
6chmod +x /usr/lib/zabbix/alertscripts/weixin.py
配置发件人
xxxxxxxxxx
31{ALERT.SENDTO}
2{ALERT.SUBJECT}
3{ALERT.MESSAGE}
配置收件人
重新触发报警
xxxxxxxxxx
11systemctl stop nginx.service
注意权限问题
增加一个用户
分别为oldboy和admin配置不同的收件人
报警升级设置
效果
解决中文乱码
xxxxxxxxxx
91cd /html/
2cd assets/fonts/
3#上传中文字体
4rz -E
5
6[root@zabbix-server fonts]# ls
7DejaVuSans.ttf SIMHEI.TTF
8
9mv SIMHEI.TTF DejaVuSans.ttf
自定义图形
使用grafana给zabbix出图
安装grafana
xxxxxxxxxx
81wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.3.7-1.x86_64.rpm
2yum localinstall grafana-7.3.7-1.x86_64.rpm -y
3systemctl start grafana-server.service
4systemctl enable grafana-server.service
5
6#访问grafana
7http://10.0.0.8:3000
8默认账号密码:admin/admin
grafana的概念
xxxxxxxxxx
31a:插件,丰富原有功能plugin
2b:数据源 datasource
3c:dashboard 效果图
安装zabbix插件
xxxxxxxxxx
21grafana-cli plugins install alexanderzobnin-zabbix-app 4.1.2
2service grafana-server restart
启用zabbix插件
新建zabbix数据源
效果
导入dashboard
模板:定义好了,一些监控项,应用集,触发器,图形等资源
模板可以包含子模板
模板,可以导入导出,可以分享
自定义模板
xxxxxxxxxx
211#开启nginx监控页面
2 location = /nginx_status {
3 stub_status;
4 access_log off;
5 }
6
7#测试
8[root@web01 ~]# curl http://127.0.0.1/nginx_status
9Active connections: 1
10server accepts handled requests
11 7 7 7
12Reading: 0 Writing: 1 Waiting: 0
13
14#自定义监控项
15[root@web01 zabbix_agent2.d]# cat zbx_nginx.conf
16UserParameter=nginx_status,netstat -lntp|grep -c nginx
17UserParameter=nginx_accepts,curl http://127.0.0.1/nginx_status 2>/dev/null|awk 'NR==3{print $1}'
18UserParameter=nginx_active_con,curl http://127.0.0.1/nginx_status 2>/dev/null|awk 'NR==1{print $NF}'
19UserParameter=nginx_Reading,curl http://127.0.0.1/nginx_status 2>/dev/null|awk 'NR==4{print $2}'
20UserParameter=nginx_Writing,curl http://127.0.0.1/nginx_status 2>/dev/null|awk 'NR==4{print $4}'
21UserParameter=nginx_Waiting,curl http://127.0.0.1/nginx_status 2>/dev/null|awk 'NR==4{print $NF}'
制作模板
添加监控项
同样的方法,复制触发器,复制图形等资源
使用模板注意事项:
xxxxxxxxxx
71#开启监控页面
2#导入模板(兼容性)
3#把zabbix的取值配置文件,放入到指定目录/etc/zabbix/zabbix_agentd.d/
4#如果有取值脚本,需要把取值也放在对应的目录
5#zabbix-get调试取值
6#在目标主机链接刚导入的模板
7#验证,查看最新数据
xxxxxxxxxx
91物理层: 物理服务器 物理交换机
2物理服务器:cpu温度,主板温度,功率,电压,风扇转速 ipmitool
3物理交换机:接口流量 snmp模板
4
5系统层:cpu,负载,内存,磁盘,网络,系统 zabbix自带模板
6
7应用层:nginx/lvs/haproxy/php-fpm/mysql/pgsql/tomcat/nfs/glusterfs... 模板监控
8
9业务层:网站访问速度,用户访问量pv\uv\ip,用户活跃度 日活,周活,月活 订单量,利润
使用第三方统计平台
将统计分析的js代码添加到网站模板
自建统计平台
使用开源的统计平台piwik
yum安装的mysql监控方法
xxxxxxxxxx
161#导入模板(兼容性)
2#把zabbix的取值配置文件,放入到指定目录/etc/zabbix/zabbix_agentd.d/
3rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
4cd /var/lib/zabbix/percona/templates/
5cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agent2.d/
6systemctl restart zabbix-agent2.service
7yum install php-cli php-mysqlnd -y 或者php72w-cli php72w-mysqlnd
8
9vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
10数据库账号密码
11
12#zabbix-get调试取值
13zabbix_get -s 10.0.0.8 -k MySQL.Open-files
14
15#在目标主机链接刚导入的模板
16#验证,查看最新数据
其他方式安装的mysql监控方法
xxxxxxxxxx
181#导入模板(兼容性)
2#把zabbix的取值配置文件,放入到指定目录/etc/zabbix/zabbix_agentd.d/
3rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
4cd /var/lib/zabbix/percona/templates/
5cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agent2.d/
6systemctl restart zabbix-agent2.service
7yum install php-cli -y 或者php72w-cli
8
9vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
10$mysql_user = 'root';
11$mysql_pass = '123456';
12$mysql_socket = '/tmp/mysql.sock';
13
14#zabbix-get调试取值
15zabbix_get -s 10.0.0.8 -k MySQL.Open-files
16
17#在目标主机链接刚导入的模板
18#验证,查看最新数据
不能安装zabbix-agent的设备,都可以使用snmp监控
snmp协议: simple network manager protocol
OID:监控标准都有一个唯一的id,object id
xxxxxxxxxx
11总内存oid: .1.3.6.1.4.1.2021.4.5.0
MIB:
xxxxxxxxxx
11存储所有oid信息
snmp协议版本
xxxxxxxxxx
31v1
2v2c -c commuity 密码,口令
3v3
安装snmp服务端
xxxxxxxxxx
71yum install net-snmp -y
2vim /etc/snmp/snmpd.conf
3#第41行修改为
4com2sec notConfigUser default oldboy
5view systemview included .1
6
7systemctl start snmpd.service
安装snmp客户端,测试取值
xxxxxxxxxx
51yum install net-snmp-utils.x86_64 -y
2#snmpget 每次只能取一个值
3#snmpwalk 范围性的取值
4[root@zabbix-server yum.repos.d]# snmpwalk -v 2c -c oldboy 10.0.0.8 .1.3.6.1.2.1.25.2.2.0
5HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 995896 KBytes
新建主机,链接模板
自动发现
添加主机的规则
自定注册:
agent配置
xxxxxxxxxx
51vim /etc/zabbix/zabbix_agent2.conf
2Server=10.0.0.71
3ServerActive=10.0.0.71
4Hostname=10.0.0.8
5HostMetadata=web
验证:
xxxxxxxxxx
11systemctl restart zabbix-agent2.service
agent配置
xxxxxxxxxx
11[root@web02 templates]# vim /etc/zabbix/zabbix_agentd.conf
验证:
xxxxxxxxxx
11 systemctl restart zabbix-agent.service
ansible实现自动注册
xxxxxxxxxx
361[root@zabbix-server ~]# tail -3 /etc/ansible/hosts
2[agent]
310.0.0.7 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=db
410.0.0.8 ansible_ssh_user='root' ansible_ssh_pass='123456' HostMetadata=web
5
6[root@zabbix-server ~]# grep -Ev '^$|#' zabbix_agentd.conf.j2
7PidFile=/var/run/zabbix/zabbix_agentd.pid
8LogFile=/var/log/zabbix/zabbix_agentd.log
9LogFileSize=0
10Server={{ zabbix_server_ip }}
11ServerActive={{ zabbix_server_ip }}
12Hostname={{ ansible_default_ipv4.address }}
13HostMetadata={{ HostMetadata }}
14Include=/etc/zabbix/zabbix_agentd.d/*.conf
15
16[root@zabbix-server ~]# cat zabbix_agent.yml
17- hosts: agent
18 vars:
19 - zabbix_server_ip: 10.0.0.71
20 tasks:
21
22 - name: Install Zabbix Agent
23 yum:
24 name: https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.7-1.el7.x86_64.rpm
25 state: present
26
27 - name: Configure Zabbix Agent
28 template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
29 notify: Restart Zabbix Agent
30
31 - name: Start Zabbix Agent
32 service: name=zabbix-agent state=started enabled=yes
33
34 handlers:
35 - name: Restart Zabbix Agent
36 service: name=zabbix-agent state=restarted
被动:如果有100个监控项,zabbix-server对agent进行100次取值
主动:如果有100个监控项,agent主动向zabbix-server索要任务清单,根据清单采集所有监控项,一次性发送给zabbix-server
使用主动模式之前
使用主动模式之后
xxxxxxxxxx
121zabbix不同版本对php的要求
22.2 -->3.0 -->4.0 -->5.0
3php5.3 php5.4 php5.4 php7.2
4
5zabbix-server 升级
6安装新版本zabbix-release,
7安装新版本yum install zabbix-server-mysql -y
8重启服务systemctl restart zabbix-server
9
10zabbix-web php版本升级
11卸载旧版本yum remove php-* -y
12安装新版本yum install php72w-fpm php72w-gd.x86_64 php72w-bcmath.x86_64 php72w-xml.x86_64 php72w-mbstring.x86_64 php72w-ldap.x86_64 php72w-mysqlnd.x86_64 -y
安装proxy
xxxxxxxxxx
311#创库
2create database zabbix character set utf8 collate utf8_bin;
3grant all on zabbix.* to zabbix@localhost identified by '123456';
4
5#安装proxy
6rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
7sed -i 's#http://repo.zabbix.com#https://mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
8yum install zabbix-proxy-mysql -y
9
10#导入表结构
11zcat /usr/share/doc/zabbix-proxy-mysql-5.0.8/schema.sql.gz |mysql -uzabbix -p123456 zabbix
12
13#配置
14vim /etc/zabbix/zabbix_proxy.conf
15Server=10.0.0.71
16ServerPort=10051
17Hostname=tj_proxy
18....
19DBHost=localhost
20DBName=zabbix
21DBUser=zabbix
22DBPassword=123456
23
24#启动
25systemctl start zabbix-proxy.service
26systemctl enable zabbix-proxy.service
27
28#检测
29[root@web01 ~]# netstat -lntup|grep 10051
30tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 96337/zabbix_proxy
31tcp6 0 0 :::10051 :::* LISTEN 96337/zabbix_proxy
创建代理
调通
agent配置
xxxxxxxxxx
61vim /etc/zabbix/zabbix_agentd.conf
2Server=172.16.1.8
3ServerActive=172.16.1.8
4Hostname=172.16.1.9
5
6systemctl restart zabbix-agent.service
tomcat weblogic jboss resin
tomcat:
开启监控接口
xxxxxxxxxx
151#安装tomcat
2rpm -ivh jdk-8u102-linux-x64.rpm
3mkdir /app
4tar xf apache-tomcat-8.0.27.tar.gz -C /app/
5/app/apache-tomcat-8.0.27/bin/startup.sh
6
7#增加一行
8vim /app/apache-tomcat-8.0.27/bin/catalina.sh
9CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=10.0.0.8 -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
10#重启生效
11/app/apache-tomcat-8.0.27/bin/shutdown.sh
12/app/apache-tomcat-8.0.27/bin/startup.sh
13#检验
14[root@web01 opt]# netstat -lntup|grep 12345
15tcp6 0 0 :::12345 :::* LISTEN 100141/java
安装zabbix-java-gateway来监控jvm
xxxxxxxxxx
31yum install zabbix-java-gateway.x86_64 -y
2systemctl start zabbix-java-gateway.service
3systemctl enable zabbix-java-gateway.service
修改zabbix-server的配置文件
xxxxxxxxxx
51vim /etc/zabbix/zabbix_server.conf
2JavaGateway=127.0.0.1
3JavaGatewayPort=10052
4StartJavaPollers=3
5systemctl restart zabbix-server.service
添加监控
性能不足的时候:经常出现误报,断图,还有其他报警
优化:
数据库优化:业务类型 写多读少
xxxxxxxxxx
71存储引擎:innodb --> tokudb
2版本 mariadb 5.5 --> mysql 5.7
3存储:机械硬盘--->固态硬盘
4去掉无用的监控项
5增加取值间隔,建议60s
6减少数据的保留周期,重要监控项保留90d,其他的一律3d
7mysql的分区和分表
zabbix-server服务优化
xxxxxxxxxx
21进程数
2缓存大小
xxxxxxxxxx
91新增一个监控项原型
2增加一个UserParameter=xxxx[*],cmd $1
3
4监控每块网卡的mac地址
5[root@web01 zabbix_agent2.d]# cat zbx_net.conf
6UserParameter=net_mac[*],ifconfig $1|awk '/ether /{print $$2}'
7
8[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k net_mac[eth1]
900:0c:29:2e:16:18
通过低级自动发现监控mysql多实例
准备环境mysql多实例
xxxxxxxxxx
261cp /etc/my.cnf /etc/my3307.cnf
2vim /etc/my3307.cnf
3[mysqld]
4datadir=/data/3307/
5socket=/data/3307/mysql.sock
6port=3307
7user=mysql
8symbolic-links=0
9[mysqld_safe]
10log-error=/data/3307/mysqld.log
11pid-file=/data/3307/mysqld.pid
12
13sed '/^$/d' /etc/my3307.cnf
14sed -i '/^$/d' /etc/my3307.cnf
15cp /etc/my3307.cnf /etc/my3308.cnf
16sed -i 's#3307#3308#g' /etc/my3308.cnf
17cat /etc/my3308.cnf
18mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
19mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
20mysqld_safe --defaults-file=/etc/my3307.cnf &
21mysqld_safe --defaults-file=/etc/my3308.cnf &
22
23mysqladmin -uroot -h 127.0.0.1 -P 3307 password '123456'
24mysql -uroot -p123456 -h 127.0.0.1 -P 3307
25mysqladmin -uroot -h 127.0.0.1 -P 3308 password '123456'
26mysql -uroot -p123456 -h 127.0.0.1 -P 3308
创建低级自动发现规则
xxxxxxxxxx
261#自定义规则的监控项
2vim /etc/zabbix/zabbix_agent2.d/zbx_mysql.conf
3UserParameter=mysql.port.discovery,/bin/bash /server/scripts/mysql_port_discovery.sh
4#脚本
5vim /server/scripts/mysql_port_discovery.sh
6
7#mysql low-level discovery
8res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'`
9port=($res)
10printf '['
11for key in ${!port[@]}
12do
13 if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
14 printf '{'
15 printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"
16 else [[ "${key}" -eq "((${#port[@]}-1))" ]]
17 printf '{'
18 printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"
19 fi
20done
21printf ']\n'
22#重启
23systemctl restart zabbix-agent2.service
24#测试
25[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql.port.discovery
26[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]
创建监控项原型
xxxxxxxxxx
61vim zbx_mysql.conf
2UserParameter=mysql_alive[*],mysqladmin -uroot -p123456 -h 127.0.0.1 -P $1 ping 2>/dev/null|grep -c alive
3
4#测试
5[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql_alive[3307]
61
api:应用程序接口
什么是调用api
怎么调用api,常见的调用api,就是发起一个http请求
xxxxxxxxxx
21curl "http://apis.juhe.cn/simpleWeather/query?city=昌平&key=3dae6629acd8689e9b12f8bb4f"
2{"reason":"查询成功!","result":{"city":"昌平","realtime":{"temperature":"13","humidity":"15","info":"晴","wid":"00","direct":"西风","power":"2级","aqi":"67"},"future":[{"date":"2021-02-05","temperature":"1\/13℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"西风转西北风"},{"date":"2021-02-06","temperature":"-3\/13℃","weather":"多云","wid":{"day":"01","night":"01"},"direct":"西北风转南风"},{"date":"2021-02-07","temperature":"-5\/5℃","weather":"晴","wid":{"day":"00","night":"00"},"direct":"东南风转北风"},{"date":"2021-02-08","temperature":"-5\/5℃","weather":"多云转晴","wid":{"day":"01","night":"00"},"direct":"东南风转西北风"},{"date":"2021-02-09","temperature":"-3\/9℃","weather":"多云转晴","wid":{"day":"01","night":"00"},"direct":"东南风转西北风"}]},"error_code":0}
调用zabbix-api
获取token
xxxxxxxxxx
141curl -X POST -H 'Content-Type:application/json-rpc' -d '
2{
3 "jsonrpc": "2.0",
4 "method": "user.login",
5 "params": {
6 "user": "Admin",
7 "password": "zabbix"
8 },
9 "id": 1,
10 "auth": null
11}' http://10.0.0.71/api_jsonrpc.php
12返回结果;
13{"jsonrpc":"2.0","result":"6d33c0a88b0d55d7860eea70005c5b5b","id":1}
14token='6d33c0a88b0d55d7860eea70005c5b5b'
删除主机
xxxxxxxxxx
121curl -X POST -H 'Content-Type:application/json-rpc' -d '
2{
3 "jsonrpc": "2.0",
4 "method": "host.delete",
5 "params": [
6 10372
7 ],
8 "auth": "'$token'",
9 "id": 1
10}' http://10.0.0.71/api_jsonrpc.php
11返回结果
12{"jsonrpc":"2.0","result":{"hostids":[10372]},"id":1}
创建主机
xxxxxxxxxx
321curl -X POST -H 'Content-Type:application/json-rpc' -d '
2{
3 "jsonrpc": "2.0",
4 "method": "host.create",
5 "params": {
6 "host": "10.0.0.8",
7 "interfaces": [
8 {
9 "type": 1,
10 "main": 1,
11 "useip": 1,
12 "ip": "10.0.0.8",
13 "dns": "",
14 "port": "10050"
15 }
16 ],
17 "groups": [
18 {
19 "groupid": "17"
20 }
21 ],
22 "templates": [
23 {
24 "templateid": "10357"
25 }
26 ]
27 },
28 "auth": "'$token'",
29 "id": 1
30}' http://10.0.0.71/api_jsonrpc.php
31返回结果:
32{"jsonrpc":"2.0","result":{"hostids":["10377"]},"id":1}
批量创建主机
xxxxxxxxxx
371[root@web01 ~]# cat piliang_create_host.sh
2
3
4token='6d33c0a88b0d55d7860eea70005c5b5b'
5for n in `echo 10.0.0.{50..100}`
6do
7curl -X POST -H 'Content-Type:application/json-rpc' -d '
8{
9 "jsonrpc": "2.0",
10 "method": "host.create",
11 "params": {
12 "host": "'$n'",
13 "interfaces": [
14 {
15 "type": 1,
16 "main": 1,
17 "useip": 1,
18 "ip": "10.0.0.8",
19 "dns": "",
20 "port": "10050"
21 }
22 ],
23 "groups": [
24 {
25 "groupid": "17"
26 }
27 ],
28 "templates": [
29 {
30 "templateid": "10357"
31 }
32 ]
33 },
34 "auth": "'$token'",
35 "id": 1
36}' http://10.0.0.71/api_jsonrpc.php
37done