1. zabbix-proxy

1.1 zabbix-proxy安装

  1. #创库
  2. create database zabbix character set utf8 collate utf8_bin;
  3. grant all on zabbix.* to zabbix@localhost identified by '123456';
  4. #安装proxy
  5. rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
  6. sed -i 's#http://repo.zabbix.com#https://mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
  7. yum install zabbix-proxy-mysql -y
  8. #导入表结构
  9. zcat /usr/share/doc/zabbix-proxy-mysql-5.0.8/schema.sql.gz |mysql -uzabbix -p123456 zabbix
  10. #配置
  11. vim /etc/zabbix/zabbix_proxy.conf
  12. Server=10.0.0.71
  13. ServerPort=10051
  14. Hostname=tj_proxy
  15. ....
  16. DBHost=localhost
  17. DBName=zabbix
  18. DBUser=zabbix
  19. DBPassword=123456
  20. #启动
  21. systemctl start zabbix-proxy.service
  22. systemctl enable zabbix-proxy.service
  23. #检测
  24. [root@web01 ~]# netstat -lntup|grep 10051
  25. tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 96337/zabbix_proxy
  26. tcp6 0 0 :::10051 :::* LISTEN 96337/zabbix_proxy

1.2 使用proxy监控主机

创建代理
image.png
image.png
调通
image.png
agent配置

  1. vim /etc/zabbix/zabbix_agentd.conf
  2. Server=172.16.1.8
  3. ServerActive=172.16.1.8
  4. Hostname=172.16.1.9
  5. systemctl restart zabbix-agent.service

image.png
image.png

2. 使用zabbix监控jvm

tomcat weblogic jboss resin
tomcat:

开启监控接口

  1. #安装tomcat
  2. rpm -ivh jdk-8u102-linux-x64.rpm
  3. mkdir /app
  4. tar xf apache-tomcat-8.0.27.tar.gz -C /app/
  5. /app/apache-tomcat-8.0.27/bin/startup.sh
  6. #增加一行
  7. vim /app/apache-tomcat-8.0.27/bin/catalina.sh
  8. 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"
  9. #重启生效
  10. /app/apache-tomcat-8.0.27/bin/shutdown.sh
  11. /app/apache-tomcat-8.0.27/bin/startup.sh
  12. #检验
  13. [root@web01 opt]# netstat -lntup|grep 12345
  14. tcp6 0 0 :::12345 :::* LISTEN 100141/java

安装zabbix-java-gateway来监控jvm

  1. yum install zabbix-java-gateway.x86_64 -y
  2. systemctl start zabbix-java-gateway.service
  3. systemctl enable zabbix-java-gateway.service

修改zabbix-server的配置文件

  1. vim /etc/zabbix/zabbix_server.conf
  2. JavaGateway=127.0.0.1
  3. JavaGatewayPort=10052
  4. StartJavaPollers=3
  5. systemctl restart zabbix-server.service

添加监控
image.png

image.png
image.png

3.zabbix性能调优

性能不足的时候:经常出现误报,断图,还有其他报警
image.png
优化:
数据库优化:业务类型 写多读少

  1. 存储引擎:innodb --> tokudb
  2. 版本 mariadb 5.5 --> mysql 5.7
  3. 存储:机械硬盘--->固态硬盘
  4. 去掉无用的监控项
  5. 增加取值间隔,建议60s
  6. 减少数据的保留周期,重要监控项保留90d,其他的一律3d
  7. mysql的分区和分表

zabbix-server服务优化

  1. 进程数
  2. 缓存大小

4.低级自动发现

  1. 新增一个监控项原型
  2. 增加一个UserParameter=xxxx[*],cmd $1
  3. 监控每块网卡的mac地址
  4. [root@web01 zabbix_agent2.d]# cat zbx_net.conf
  5. UserParameter=net_mac[*],ifconfig $1|awk '/ether /{print $$2}'
  6. [root@zabbix-server ~]# zabbix_get -s 10.0.0.8 -k net_mac[eth1]
  7. 00:0c:29:2e:16:18

image.png

低级自动发现规则:
image.png
image.png
image.png
image.png
image.png

4.1通过低级自动发现监控mysql多实例

准备环境mysql多实例

  1. cp /etc/my.cnf /etc/my3307.cnf
  2. vim /etc/my3307.cnf
  3. [mysqld]
  4. datadir=/data/3307/
  5. socket=/data/3307/mysql.sock
  6. port=3307
  7. user=mysql
  8. symbolic-links=0
  9. [mysqld_safe]
  10. log-error=/data/3307/mysqld.log
  11. pid-file=/data/3307/mysqld.pid
  12. sed '/^$/d' /etc/my3307.cnf
  13. sed -i '/^$/d' /etc/my3307.cnf
  14. cp /etc/my3307.cnf /etc/my3308.cnf
  15. sed -i 's#3307#3308#g' /etc/my3308.cnf
  16. cat /etc/my3308.cnf
  17. mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
  18. mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
  19. mysqld_safe --defaults-file=/etc/my3307.cnf &
  20. mysqld_safe --defaults-file=/etc/my3308.cnf &
  21. mysqladmin -uroot -h 127.0.0.1 -P 3307 password '123456'
  22. mysql -uroot -p123456 -h 127.0.0.1 -P 3307
  23. mysqladmin -uroot -h 127.0.0.1 -P 3308 password '123456'
  24. mysql -uroot -p123456 -h 127.0.0.1 -P 3308

创建低级自动发现规则

  1. #自定义规则的监控项
  2. vim /etc/zabbix/zabbix_agent2.d/zbx_mysql.conf
  3. UserParameter=mysql.port.discovery,/bin/bash /server/scripts/mysql_port_discovery.sh
  4. #脚本
  5. vim /server/scripts/mysql_port_discovery.sh
  6. #!/bin/bash
  7. #mysql low-level discovery
  8. res=`netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$5}'`
  9. port=($res)
  10. printf '['
  11. for key in ${!port[@]}
  12. do
  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
  20. done
  21. printf ']\n'
  22. #重启
  23. systemctl 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"}]

image.png
image.png
image.png
image.png
创建监控项原型

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

image.png
image.png
image.png
image.png
image.png

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'

删除主机
image.png


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