1. zabbix-proxy
1.1 zabbix-proxy安装
#创库
create database zabbix character set utf8 collate utf8_bin;
grant all on zabbix.* to zabbix@localhost identified by '123456';
#安装proxy
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com#https://mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
yum install zabbix-proxy-mysql -y
#导入表结构
zcat /usr/share/doc/zabbix-proxy-mysql-5.0.8/schema.sql.gz |mysql -uzabbix -p123456 zabbix
#配置
vim /etc/zabbix/zabbix_proxy.conf
Server=10.0.0.71
ServerPort=10051
Hostname=tj_proxy
....
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
#启动
systemctl start zabbix-proxy.service
systemctl enable zabbix-proxy.service
#检测
[root@web01 ~]# netstat -lntup|grep 10051
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 96337/zabbix_proxy
tcp6 0 0 :::10051 :::* LISTEN 96337/zabbix_proxy
1.2 使用proxy监控主机
创建代理
调通
agent配置
vim /etc/zabbix/zabbix_agentd.conf
Server=172.16.1.8
ServerActive=172.16.1.8
Hostname=172.16.1.9
systemctl restart zabbix-agent.service
2. 使用zabbix监控jvm
tomcat weblogic jboss resin
tomcat:
开启监控接口
#安装tomcat
rpm -ivh jdk-8u102-linux-x64.rpm
mkdir /app
tar xf apache-tomcat-8.0.27.tar.gz -C /app/
/app/apache-tomcat-8.0.27/bin/startup.sh
#增加一行
vim /app/apache-tomcat-8.0.27/bin/catalina.sh
CATALINA_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"
#重启生效
/app/apache-tomcat-8.0.27/bin/shutdown.sh
/app/apache-tomcat-8.0.27/bin/startup.sh
#检验
[root@web01 opt]# netstat -lntup|grep 12345
tcp6 0 0 :::12345 :::* LISTEN 100141/java
安装zabbix-java-gateway来监控jvm
yum install zabbix-java-gateway.x86_64 -y
systemctl start zabbix-java-gateway.service
systemctl enable zabbix-java-gateway.service
修改zabbix-server的配置文件
vim /etc/zabbix/zabbix_server.conf
JavaGateway=127.0.0.1
JavaGatewayPort=10052
StartJavaPollers=3
systemctl restart zabbix-server.service
添加监控
3.zabbix性能调优
性能不足的时候:经常出现误报,断图,还有其他报警
优化:
数据库优化:业务类型 写多读少
存储引擎:innodb --> tokudb
版本 mariadb 5.5 --> mysql 5.7
存储:机械硬盘--->固态硬盘
去掉无用的监控项
增加取值间隔,建议60s
减少数据的保留周期,重要监控项保留90d,其他的一律3d
mysql的分区和分表
zabbix-server服务优化
进程数
缓存大小
4.低级自动发现
新增一个监控项原型
增加一个UserParameter=xxxx[*],cmd $1
监控每块网卡的mac地址
[root@web01 zabbix_agent2.d]# cat zbx_net.conf
UserParameter=net_mac[*],ifconfig $1|awk '/ether /{print $$2}'
[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k net_mac[eth1]
00:0c:29:2e:16:18
低级自动发现规则:
4.1通过低级自动发现监控mysql多实例
准备环境mysql多实例
cp /etc/my.cnf /etc/my3307.cnf
vim /etc/my3307.cnf
[mysqld]
datadir=/data/3307/
socket=/data/3307/mysql.sock
port=3307
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/3307/mysqld.log
pid-file=/data/3307/mysqld.pid
sed '/^$/d' /etc/my3307.cnf
sed -i '/^$/d' /etc/my3307.cnf
cp /etc/my3307.cnf /etc/my3308.cnf
sed -i 's#3307#3308#g' /etc/my3308.cnf
cat /etc/my3308.cnf
mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
mysqld_safe --defaults-file=/etc/my3307.cnf &
mysqld_safe --defaults-file=/etc/my3308.cnf &
mysqladmin -uroot -h 127.0.0.1 -P 3307 password '123456'
mysql -uroot -p123456 -h 127.0.0.1 -P 3307
mysqladmin -uroot -h 127.0.0.1 -P 3308 password '123456'
mysql -uroot -p123456 -h 127.0.0.1 -P 3308
创建低级自动发现规则
#自定义规则的监控项
vim /etc/zabbix/zabbix_agent2.d/zbx_mysql.conf
UserParameter=mysql.port.discovery,/bin/bash /server/scripts/mysql_port_discovery.sh
#脚本
vim /server/scripts/mysql_port_discovery.sh
#!/bin/bash
#mysql low-level discovery
res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'`
port=($res)
printf '['
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"
fi
done
printf ']\n'
#重启
systemctl restart zabbix-agent2.service
#测试
[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql.port.discovery
[{"{#MYSQLPORT}":"3306"},{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"}]
创建监控项原型
vim zbx_mysql.conf
UserParameter=mysql_alive[*],mysqladmin -uroot -p123456 -h 127.0.0.1 -P $1 ping 2>/dev/null|grep -c alive
#测试
[root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k mysql_alive[3307]
1
5、zabbix的api讲解
api:应用程序接口
什么是调用api
怎么调用api,发起一个http请求
curl "http://apis.juhe.cn/simpleWeather/query?city=昌平&key=3dae6629acd8689e9b12f8bb4f"
{"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
curl -X POST -H 'Content-Type:application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://10.0.0.71/api_jsonrpc.php
返回结果;
{"jsonrpc":"2.0","result":"6d33c0a88b0d55d7860eea70005c5b5b","id":1}
token='6d33c0a88b0d55d7860eea70005c5b5b'
删除主机
curl -X POST -H 'Content-Type:application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
10372
],
"auth": "'$token'",
"id": 1
}' http://10.0.0.71/api_jsonrpc.php
返回结果
{"jsonrpc":"2.0","result":{"hostids":[10372]},"id":1}
创建主机
curl -X POST -H 'Content-Type:application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "10.0.0.8",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "10.0.0.8",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "17"
}
],
"templates": [
{
"templateid": "10357"
}
]
},
"auth": "'$token'",
"id": 1
}' http://10.0.0.71/api_jsonrpc.php
返回结果:
{"jsonrpc":"2.0","result":{"hostids":["10377"]},"id":1}
批量创建主机
[root@web01 ~]# cat piliang_create_host.sh
#!/bin/bash
token='6d33c0a88b0d55d7860eea70005c5b5b'
for n in `echo 10.0.0.{50..100}`
do
curl -X POST -H 'Content-Type:application/json-rpc' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'$n'",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "10.0.0.8",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "17"
}
],
"templates": [
{
"templateid": "10357"
}
]
},
"auth": "'$token'",
"id": 1
}' http://10.0.0.71/api_jsonrpc.php
done