所有服务器的时间都会和NTP服务器进行时间同步
开源监控软件 cacti、nagios、zabbix、smokeping、open-falcon等
https://www.cacti.net/
https://github.com/Cacti/cacti
Cacti是基于LAMP平台展现的网络流量监测及分析工具,通过SNMP技术或自定义脚本从目标设备/主机获取监控指标信息;其次进行数据存储,调用模板将数据存到数据库,使用rrdtool存储和更新数据,通过rrdtool绘制结果图形;最后进行数据展现,通过Web方式将监控结果呈现出来,常用于在数据中心监控网络设备。
https://www.nagios.org/
Nagios用来监视系统和网络的开源应用软件,利用其众多的插件实现对本机和远端服务的监控,当被监控对象发生异常时,会及时向管理员告警,提供一批预设好的监控插件,用户可以之间调用,也可以自定义Shell脚本来监控服务,适合各企业的业务监控,可通过Web页面显示对象状态、日志、告警信息,分层告警机制及自定义监控相对薄弱。
官网地址:https://oss.oetiker.ch/smokeping/
配置方法:http://blogs.studylinux.net/?p=794
Smokeping是一款用于网络性能监测的开源监控软件,主要用于对IDC的网络状况,网络质量,稳定性等做检测,通过rrdtool制图方式,图形化地展示网络的时延情况,进而能够清楚的判断出网络的即时通信情况。
https://www.open-falcon.org/
https://github.com/XiaoMi/open-falcon
小米公司开源出来的监控软件open-falcon(猎鹰),监控能力和性能较强。
https://n9e.didiyun.com/
一款经过大规模生产环境验证的、分布式高性能的运维监控系统,由滴滴基于open-falcon二次开发后开源出来的分布式监控系统。
https://www.zabbix.com/cn/
目前使用较多的开源监控软件,可横向扩展、自定义监控项、支持多种监控方式、可监控网络与服务等。
针对容器环境的开源监控软件
使用比较多
监控宝(https://www.jiankongbao.com/)
听云(https://www.tingyun.com/)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hXHnIK3U-1605944334249)(C:\Users\cui\Desktop\课程截图\34.企业级监控服务Zabbix_图片\20200912202557.png)]
补全监控软件缺失的功能,在全国各地部署客户端,定时对公司的服务器进行访问,并把访问结果(响应时间,状态等)上传到服务器并进行分类,运维工程师可以登录并查看
https://www.zabbix.com/cn/features
Zabbix是一个企业级解决方案,支持实时监控数千台服务器,虚拟机和网络设备,采集百万级监控指标,适用于任何IT基础架构、服务、应用程序和资源的解决方案
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sYdNb7yQ-1605944334257)(C:\Users\cui\Desktop\课程截图\34.企业级监控服务Zabbix_图片\20200912202556.png)]
此图说明:
一个机房一个或者一个业务一个代理服务器,由代理去各个服务器和主机采集数据,一个代理可以采集多个服务器,采集后代理再汇报给zabbix server,zabbix server把数据写入数据库,运维工程师通过访问web服务器,调用数据库中记录的数据以进行监控。server和web可以在或者不在一个服务器上。
周期性时序数据
监控数据存储系统
历史数据: 每个监控项采集到的每个监控值,查询时可能会造成数据库负载过大,开发和测试也会查看
趋势数据: 趋势表里主要保留某个监控项一个小时内历史数据的最大值、最小值和平均值以及该监控项一个小时内所采集到的数据个数。
对比:查询一天数据就有如此差距
历史数据:80*1*12*24=23040
趋势数据:80*1*1*24=1920
可按照预定义的阈值等级实现分层报警,可以80%,不过要考虑基数,Zabbix用的好不好,就看阈值设置的好不好,需要慢慢优化
email,短信,微信,语音,故障自治愈 (zabbix通知服务器重启,需事先写好脚本,针对物理机和虚拟机)
初级运维 --> 中级运维/高级运维 --> 架构师 --> 总监 --> CTO
数据采集方式 zabbix-server,zabbix-proxy,zabbix-agent
zabbix database
zabbix web
grafana
host (host groups) <- templates #从模板继承告警配置
host -> items -> triggers -> action (条件-conditions, 操作-operations) #自定义告警配置
部署环境
服务器系统 ubuntu 18.04.4/Centos 7.x
主机类型 | IP地址 |
---|---|
zabbix server 编译安装 | 10.0.58.101 |
zabbix 主动代理/zabbix server apt安装 | 10.0.58.102 |
zabbix 被动代理 | 10.0.58.103 |
mysql master | 10.0.58.104 |
mysql slave | 10.0.58.105 |
web server1 | 10.0.58.106 |
web server2 | 10.0.58.107 |
最小化安装操作系统,然后安装常用依赖包
yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel
apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip
产品手册:https://www.zabbix.com/documentation/4.0/zh/manual
使用apt在ubuntu 安装zabbix 4.0.x版本
https://www.zabbix.com/cn/download?zabbix=4.0&os_distribution=ubuntu&os_version=18.04_bionic&db=mysql&ws=apache
# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
# dpkg -i zabbix-release_4.0-3+bionic_all.deb
# apt update
# apt install zabbix-server-mysql zabbix-frontend-php zabbix-agent
在mysql maser安装数据库并授权zabbix使用
[root@zabbix-mysql-master ~]# apt update
# apt update
# apt install mysql-server mysql-client #或者安装 mariadb-server mariadb-client
# vim /etc/mysql/mysql.conf.d/mysqld.cnf #修改监听地址
bind-address = 0.0.0.0
# systemctl restart mysql
[root@zabbix-mysql-master ~]#mysql #进入数据库创建账户并授权
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zabbix_server_apt character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix_server_apt.* to zabbix@"10.0.58.%" identified by 'qinghe.zabbix';
Query OK, 0 rows affected, 1 warning (0.00 sec)
在zabbix server初始化数据库
[root@zabbix-proxy-active ~]#mysql -uzabbix -pqinghe.zabbix -h10.0.58.104 #测试账户权限
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| zabbix_server_apt |
+--------------------+
2 rows in set (0.00 sec)
MySQL [(none)]> exit
[root@zabbix-proxy-active ~]#zcat /usr/share/doc/zabbix-server-mysql/create.sql.gz | mysql -uzabbix -pqinghe.zabbix -h10.0.58.104 zabbix_server_apt #初始化数据库
root@zabbix-server:~# vim /etc/zabbix/zabbix_server.conf
DBHost=10.0.58.104
DBName=zabbix_server_apt
DBUser=zabbix
DBPassword=qinghe.zabbix
DBPort=3306
systemctl restart zabbix-server zabbix-agent apache2
root@zabbix-server:~# vim /etc/zabbix/apache.conf
php_value date.timezone Asia/Shanghai
[root@zabbix-proxy-active ~]#systemctl restart zabbix-server zabbix-agent apache2
[root@zabbix-proxy-active ~]#systemctl enable zabbix-server zabbix-agent apache2
启动之后,zabbix server端口10051开启
#报错的界面
#正常的界面
说明:数据库配置和zabbix server配置最终会写在配置文件里,若不小心配错了,可以去配置文件里去改
[root@zabbix-proxy-active ~]#vim /usr/share/zabbix/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB;
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '10.0.58.104';
$DB['PORT'] = '3306';
$DB['DATABASE'] = 'zabbix_server_apt';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'qinghe.zabbix';
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';
$ZBX_SERVER = '10.0.58.102';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'zabbix server';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
#修改后重启
[root@zabbix-proxy-active ~]#systemctl restart apache2
略。。。。。。
编译安装zabbix server与agent
https://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable #下载地址
# pwd
/usr/local/src
# groupadd -g 1001 zabbix #创建zabbix用户和组
# useradd -u 1001 -g 1001 zabbix
# id zabbix
uid=1001(zabbix) gid=1001(zabbix) groups=1001(zabbix) #验证用户id
# wget https://cdn.zabbix.com/zabbix/sources/stable/4.0/zabbix-4.0.23.tar.gz
# tar xf zabbix-4.0.23.tar.gz
# cd zabbix-4.0.23/
Centos :
yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php phpbcmath php-mbstring mariadb mariadb-devel -y
Ubuntu
# apt update
# apt-get install apache2 apache2-bin apache2-data apache2-utils fontconfig-config fonts-dejavu-core fping libapache2-mod-php libapache2-mod-php7.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libfontconfig1 libgd3 libiksemel3 libjbig0 libjpeg-turbo8 libjpeg8 liblua5.2-0 libodbc1 libopenipmi0 libsensors4 libsnmp-base libsnmp30 libsodium23 libssh2-1 libtiff5 libwebp6 libxpm4 php-bcmath php-common php-gd php-ldap php-mbstring php-mysql php-xml php7.2-bcmath php7.2-cli php7.2-common php7.2-gd php7.2-json php7.2-ldap php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-readline php7.2-xml snmpd ssl-cert ttf-dejavu-core libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev openjdk-8-jdk curl libcurl4-openssl-dev
# ./configure --prefix=/apps/zabbix_server --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java
# make install
# apt update
# apt install mysql-server mysql-client
# vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
# systemctl restart mysql
[root@zabbix-mysql-master ~]#mysql #进入数据库创建账户并授权
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31-0ubuntu0.18.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database zabbix_server_yuan character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix_server_yuan.* to zabbix_yuan@"10.0.58.%" identified by 'qinghe.zabbix';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# apt install mysql-client #安装mysql客户端命令,用于测试zabbix 数据库账号权限
# mysql -uzabbix_yuan -pqinghe.zabbix -h10.0.58.104 #测试权限
# pwd
/usr/local/src/zabbix-4.0.23/database/mysql
# cd database/mysql/
[root@zabbix-server mysql]#mysql -uzabbix_yuan -pqinghe.zabbix -h10.0.58.104 zabbix_server_yuan < schema.sql
[root@zabbix-server mysql]#mysql -uzabbix_yuan -pqinghe.zabbix -h10.0.58.104 zabbix_server_yuan < images.sql
[root@zabbix-server mysql]#mysql -uzabbix_yuan -pqinghe.zabbix -h10.0.58.104 zabbix_server_yuan < data.sql
# vim /apps/zabbix_server/etc/zabbix_server.conf
[root@zabbix-server zabbix-4.0.23]#grep "^[a-Z]" /apps/zabbix_server/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log
DBHost=10.0.58.104
DBName=zabbix_server_yuan
DBUser=zabbix_yuan
DBPassword=qinghe.zabbix
DBPort=3306
Timeout=4
LogSlowQueries=3000
[root@zabbix-server ~]#/apps/zabbix_server/sbin/zabbix_server -c /apps/zabbix_server/etc/zabbix_server.conf
[root@zabbix-server ~]#tail -f /tmp/zabbix_server.log
38799:20200817:081211.891 server #24 started [trapper #5]
38801:20200817:081211.902 server #26 started [alert manager #1]
38802:20200817:081211.906 server #27 started [alerter #1]
38803:20200817:081211.907 server #28 started [alerter #2]
38804:20200817:081211.911 server #29 started [alerter #3]
38805:20200817:081211.918 server #30 started [preprocessing manager #1]
38806:20200817:081211.919 server #31 started [preprocessing worker #1]
38807:20200817:081211.925 server #32 started [preprocessing worker #2]
38808:20200817:081211.925 server #33 started [preprocessing worker #3]
https://www.zabbix.com/documentation/4.0/zh/manual/installation/install
[root@zabbix-server ~]#mkdir /var/www/html/zabbix
[root@zabbix-server zabbix-4.0.23]#pwd
/usr/local/src/zabbix-4.0.23
[root@zabbix-server zabbix-4.0.23]#cd frontends/php/
[root@zabbix-server php]#cp -a . /var/www/html/zabbix/
[root@zabbix-server ~]#apt-get install php-gettext php-xml php-net-socket php-gd php-mysql
[root@zabbix-server ~]#systemctl restart apache2
[root@zabbix-server ~]#vim /etc/php/7.2/apache2/php.ini
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Shanghai
[root@zabbix-server ~]#systemctl restart apache2
需要手动下载配置文件并上传至zabbix server的/var/www/html/zabbix/conf/zabbix.conf.php路径
手动上传到服务器指定路径并验证文件内容
[root@zabbix-server ~]#cat /var/www/html/zabbix/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB;
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '10.0.58.104';
$DB['PORT'] = '3306';
$DB['DATABASE'] = 'zabbix_server_yuan';
$DB['USER'] = 'zabbix_yuan';
$DB['PASSWORD'] = 'qinghe.zabbix';
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';
$ZBX_SERVER = '10.0.58.101';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'zabbix server yuan';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
配置文件上传成功后刷新web页面验证是否生效,然后在生效后点finish即可完成zabbix server 的初始化。
# /apps/zabbix_server/sbin/zabbix_agentd
通过service启动文件启动zabbix server与zabbix agent。
#先停止当前zabbix server进程
# pkill zabbix_server
[root@zabbix-server ~]#cat /lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
After=mysql.service
After=mysqld.service
After=mariadb.service
[Service]
#Environment="CONFFILE=/apps/zabbix_server/etc/zabbix_server.conf"
#EnvironmentFile=-/etc/default/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_server/run/zabbix_server.pid
KillMode=control-group
ExecStart=/apps/zabbix_server/sbin/zabbix_server -c /apps/zabbix_server/etc/zabbix_server.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
[root@zabbix-server ~]#vim /apps/zabbix_server/etc/zabbix_server.conf
PidFile=/apps/zabbix_server/run/zabbix_server.pid
[root@zabbix-server ~]#mkdir /apps/zabbix_server/run/
[root@zabbix-server ~]#id zabbix
uid=1001(zabbix) gid=1001(zabbix) groups=1001(zabbix)
[root@zabbix-server ~]#chown -R zabbix.zabbix /apps/zabbix_server/run/
# systemctl restart zabbix-server && systemctl enable zabbix-server
#先停止当前zabbix_agent进程
# pkill zabbix_agentd
[root@zabbix-server ~]#cat /lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
#Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf"
#EnvironmentFile=-/etc/default/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_server/run/zabbix_agentd.pid
KillMode=control-group
ExecStart=/apps/zabbix_server/sbin/zabbix_agentd -c /apps/zabbix_server/etc/zabbix_agentd.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
[root@zabbix-server ~]#vim /apps/zabbix_server/etc/zabbix_agentd.conf
PidFile=/apps/zabbix_server/run/zabbix_agentd.pid
# systemctl restart zabbix-agent && systemctl enable zabbix-agent
由于ubuntu系统目前未安装中文语言环境所以无法选择中文显示,如下
安装并设置中文简体语言环境
#安装简体中文语言环境
root@zabbix-server:~# sudo apt-get install language-pack-zh*
#增加中文语言环境变量
root@zabbix-server:~# sudo vim /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
LANG="zh_CN.UTF-8"
#重新设置本地配置
sudo dpkg-reconfigure locales
重启apache并验证wen页面
# systemctl restart apache2
当前系统有些监控项部分显示有乱码,使由于web界面显示为中文但是系统没有相关字体支持,因此需要相关字体的支持才能正常显示,如下
在Windows上找到控制面板–>字体–>楷体(或者其他个人喜欢的中文字体),然后将字体拷贝到windows系统其他目录,比如拷贝至windows当前用户的桌面。
将windows 字体文件上传至zabbix web目录,具体路径为/ZABBIX/WEB/PATH/assets/fonts/,如下
#apt安装的上传楷体字体文件到这里
[root@zabbix-proxy-active fonts]#pwd
/usr/share/zabbix/assets/fonts
# chown zabbix.zabbix ./* #更改权限为zabbix用户和组
[root@zabbix-proxy-active fonts]#ll
total 11520
drwxr-xr-x 2 root root 4096 Aug 17 07:04 ./
drwxr-xr-x 5 root root 4096 Aug 17 04:06 ../
lrwxrwxrwx 1 root root 38 Aug 17 04:06 graphfont.ttf -> /etc/alternatives/zabbix-frontend-font
-rw-r--r-- 1 root root 11787328 Mar 1 2019 simkai.ttf
#编译安装的上传楷体字体文件到这里
[root@zabbix-server ~]#cd /var/www/html/zabbix/assets/fonts/
[root@zabbix-server fonts]#ll
total 12260
drwxr-xr-x 2 cui cui 4096 Aug 17 08:47 ./
drwxr-xr-x 5 cui cui 4096 Jul 27 08:24 ../
-rw-r--r-- 1 cui cui 756072 Jul 20 08:19 DejaVuSans.ttf
-rw-r--r-- 1 root root 11787328 Mar 1 2019 simkai.ttf
#apt安装的
[root@zabbix-proxy-active zabbix]#pwd
/usr/share/zabbix
[root@zabbix-proxy-active zabbix]#vim include/defines.inc.php
#72 define('ZBX_GRAPH_FONT_NAME','graphfont'); // font file name
72 define('ZBX_GRAPH_FONT_NAME','simkai'); // font file name
#113 define('ZBX_FONT_NAME', 'graphfont');
113 define('ZBX_FONT_NAME', 'simkai');
#编译安装的如下
[root@zabbix-server ~]#vim /var/www/html/zabbix/include/defines.inc.php
#72 define('ZBX_GRAPH_FONT_NAME', 'DejaVuSans'); // font file name
72 define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
#113 define('ZBX_FONT_NAME', 'DejaVuSans');
113 define('ZBX_FONT_NAME', 'simkai');
通常不需要重启zabbix及apache,修改后的字体文件即可直接生效。
见 zabbix_server.conf 配置文件详解
# 监听端口
# ListenPort=10051
# 多IP地址情况下,与其他服务器通信使用的源IP地址,一般不用
# SourceIP=
# 日志类型,支持system、file和console,一般不用
# LogType=file
# 建议放在var/log/zabbix目录下,同时也要授权,属主属组改为zabbix
LogFile=/var/log/zabbix/zabbix_server.log
# 指定日志滚动写入大小,当达到指定大小时,会删除旧日志重新写入新日志
LogFileSize=1024
# 自定义日志级别,需要排错时可以改为4
# DebugLevel=3
# PID文件路径
PidFile=/run/zabbix/zabbix_server.pid
# 数据库路径
SocketDir=/run/zabbix
# 数据库端口
DBHost=10.0.58.104
# 数据库用户名称
# DBUser=
# 数据库用户密码
DBPassword=magedu.zabbix
# 数据库socket文件
# DBSocket=
# 数据库端口
DBPort=3306
# elasticsearch服务器地址,保存zabbix历史数据到ES里面,优化zabbix性能,新版本zabbix才支持
# HistoryStorageURL=
# elasticsearch索引类型
# HistoryStorageTypes=uint,dbl,str,log,text
# 将历史数据保存到不同的elasticsearch索引
# HistoryStorageDateIndex=0
# 定义实时导出触发器事件,监控项采集值,趋势数据的目录
# ExportDir=
# 定义每个导出文件的最大大小,一般不用设
# ExportFileSize=1G
# 预启动多少个实例(进程)数量
StartPollers=10
# IPMI收集数量
#StartIPMIPollers=10
# 预启动多少个进程用于处理zabbix agent数据
StartPreprocessors=3
# 不可达主机的轮询进程的初始实例启动数量
StartPollersUnreachable=1
# 预启动的Trappers进程数据
StartTrappers=5
# ICMP ping进程的初始实例数量
StartPingers=1
# 自动发现主机的进程数量,非常消耗性能,一般不用,有更好的方法
# StartDiscoverers=1
# http 进程数量
StartHTTPPollers=2
# 计时器实例数量,计时器用于计算问题的发生时间和步骤同步等
StartTimers=3
# escalators进程的初始实例数量,用于处理动作中的自动步骤的进程数量,一般不用
# StartEscalators=1
# 报警实例预启动数量
StartAlerters=3
# javagateway服务器地址,java pollers必须设置
# JavaGateway=
# javagateway端口,监控java服务
# JavaGatewayPort=10052
# Java轮询实例预启动数量
# StartJavaPollers=0
# 用于设置监控VMWARE。Esxi主机实例时使用,若为0则不启用,若要监控ESXI主机,此值最少为1,根据监控ESXI数量设置对应数值
# StartVMwareCollectors=0
# 监控vmware获取最新数据间隔频率,单位为秒
# VMwareFrequency=60
# 监控vmware获取性能数据间隔
# VMwarePerfFrequency=60
# vmware数据缓存大小,会占用zabbix server服务器内存
# VMwareCacheSize=8M
# 超时时间
# VMwareTimeout=10
# snmp触发器临时文件路径
# SNMPTrapperFile=/tmp/zabbix_traps.tmp
# SNMP触发器预启动数量
StartSNMPTrapper=1
# 监听地址
# ListenIP=0.0.0.0
# 多少小时清理一次代理端数据库的history数据
HousekeepingFrequency=1
# 每次最多删除历史数据的行数
MaxHousekeeperDelete=1000000
# 配置缓存的大小
CacheSize=128M
# Zabbix更新缓存数据的频率,单位为秒,范围是1-3600
CacheUpdateFrequency=300
# zabbix和数据库同步数据的进程数量,可以高一点
StartDBSyncers=10
# 历史数据的缓存大小,可以大点
HistoryCacheSize=128M,公司里2G
# 网络数据索引缓存的大小
HistoryIndexCacheSize=16M
# 用于设置划分多少系统共享内存用于存储计算出来的趋势数据,此参数指从一定程度上可影响数据库读压力,范围是128k-2G,公司里2G
TrendCacheSize=16M
# 历史值缓存的大小,用于缓存历史数据请求的共享内存大小
ValueCacheSize=16M
# 数据获取等待超时时间
# Timeout=3
# 触发器处理新数据的最长时间,单位是秒,可以稍微长点
TrapperTimeout=300
# 当主机不可达多少秒后,设置为主机不可用,单位是秒,范围是1-3600,不用特别长
UnreachablePeriod=30
# 当主机不可用了,多久检查一次该主机的可用性,单位是秒,范围是1-3600
UnavailableDelay=60
# 当主机不可到达了,多久检查一次该主机的可用性,单位是秒,范围是1-3600
UnreachableDelay=60
# 监控报警脚本路径,取决于编译时候的datadir参数
# AlertScriptsPath=${datadir}/zabbix/alertscripts
# 自定义脚本存储路径
# ExternalScripts=${datadir}/zabbix/externalscripts
# fping命令的位置
FpingLocation=/usr/bin/fping
#慢日志时间(以毫秒为单位),仅当DebugLevel设置为3,4或5时才可用,0 - 不记录慢查询,范围是1-3600000
LogSlowQueries=3000
# 临时目录
# TmpDir=/tmp
# 启用多少子进程与代理端通信,若代理较多可考虑加大此数值,范围是0-250
StartProxyPollers=3
#proxy被动模式下server多少秒同步配置文件至proxy,该参数仅用于被动模式下的代理,范围是1-3600*24*7
ProxyConfigFrequency=60
# 被动模式下,zabbix server间隔多少秒向proxy请求历史数据
ProxyDataFrequency=60
# 是否允许root启动zabbix
AllowRoot=1
# 设置zabbix启动用户
User=root
# 导入其他目录的配置文件
# Include=/usr/local/etc/zabbix_server.general.conf
# Include=/usr/local/etc/zabbix_server.conf.d/
# Include=/usr/local/etc/zabbix_server.conf.d/*.conf
实验配置:
[root@zabbix-server ~]#grep "^[a-Z]" /apps/zabbix_server/etc/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=1024
PidFile=/apps/zabbix_server/run/zabbix_server.pid
SocketDir=/apps/zabbix_server/
DBHost=10.0.58.104
DBName=zabbix_server_yuan
DBUser=zabbix_yuan
DBPassword=qinghe.zabbix
DBPort=3306
StartPollers=10
StartPreprocessors=3
StartPollersUnreachable=1
StartTrappers=5
StartPingers=1
StartHTTPPollers=1
StartTimers=3
StartAlerters=3
StartSNMPTrapper=1
HousekeepingFrequency=1
MaxHousekeeperDelete=1000000
CacheSize=128M
CacheUpdateFrequency=300
StartDBSyncers=10
HistoryCacheSize=128M
HistoryIndexCacheSize=16M
TrendCacheSize=16M
ValueCacheSize=16M
Timeout=30
TrapperTimeout=300
UnreachablePeriod=30
UnavailableDelay=60
UnreachableDelay=60
FpingLocation=/usr/sbin/fping
LogSlowQueries=3000
StartProxyPollers=3
ProxyConfigFrequency=60
ProxyDataFrequency=60
AllowRoot=1
User=root
学习通过apt/yum安装zabbix agent、对tomcat进行监控。
在其他linux服务器安装zabbix agent,然后添加到zabbix server以对其进行资源监控。
[root@zabbix-web1 src]#wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
[root@zabbix-web1 src]#dpkg -i zabbix-release_4.0-3+bionic_all.deb
[root@zabbix-web1 src]#apt update
[root@zabbix-web1 src]#apt install zabbix-agent
# vim /etc/zabbix/zabbix_agentd.conf
##### Passive checks related #被动检查相关配置
Server=10.0.58.101 #指向当前zabbix server
### Option: ListenPort
ListenPort=10050 #监听端口
### Option: StartAgents
StartAgents=3 #被动状态时默认启动的实例数(进程数),为0不监听任何端口
### Option: Hostname
Hostname=10.0.58.107 #区分大小写且在zabbix server唯一的值
[root@zabbix-web1 src]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.58.101
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=3
ServerActive=127.0.0.1
Hostname=10.0.58.106
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
# systemctl restart zabbix-agent
[root@zabbix-web1 src]#systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2020-08-17 16:07:00 UTC; 51s ago
Process: 22376 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 22377 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
Main PID: 22388 (zabbix_agentd)
Tasks: 6 (limit: 2290)
CGroup: /system.slice/zabbix-agent.service
├─22388 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─22390 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─22391 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─22392 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─22393 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─22394 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Aug 17 16:07:00 zabbix-web1 systemd[1]: Stopped Zabbix Agent.
Aug 17 16:07:00 zabbix-web1 systemd[1]: Starting Zabbix Agent...
Aug 17 16:07:00 zabbix-web1 systemd[1]: zabbix-agent.service: Can't open PID file /run/zabbix/zabbix_agent
Aug 17 16:07:00 zabbix-web1 systemd[1]: Started Zabbix Agent.
验证zabbix agent进程是否存在、端口是否监听以及日志是否有异常
[root@zabbix-web1 ~]#ps -ef | grep zabbix
zabbix 2118 1 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix 2120 2118 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix 2121 2118 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix 2122 2118 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix 2123 2118 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix 2124 2118 0 01:10 ? 00:00:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root 2127 2009 0 01:10 pts/0 00:00:00 grep --color=auto zabbix
[root@zabbix-web1 ~]#pstree -p 2120
zabbix_agentd(2120)
[root@zabbix-web1 ~]#pstree -p 2121
zabbix_agentd(2121)
[root@zabbix-web1 ~]#ss -tnl | grep 10050
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
[root@zabbix-web1 ~]#tail -n5 /var/log/zabbix/zabbix_agentd.log
2122:20200818:011018.156 agent #3 started [listener #2]
2120:20200818:011018.160 agent #1 started [collector]
2123:20200818:011018.161 agent #4 started [listener #3]
2124:20200818:011018.162 agent #5 started [active checks #1]
在zabbix web管理界面添加上一步安装了zabbix agent的linux主机
在zabbix对主机实现监控,可以通过添加模板或者添加监控项实现对指定的监控目标进行数据采集,通常采用先创建模板,然后再将模板关联至主机的方式,模板关联如下
选择符合当前主机监控项目的模板,如对linux系统监控可以关联zabbix 自带的Template OS Linux模板,如下在搜索框输入linux关键字后会自带匹配符合名称的模板,直接点击即可选择
需要等待几分钟(具体看模板中监控项的数据采集间隔时间)之后,主机的状态才会变成绿色或者红色,绿色表示zabbix server对zabbix agent有权限进行数据采集并且当前通信正常,红色则表示通信异常,通信异常会有多种原因,具体可以看相关日志或者到时候点击红色方框会有相关报错显示。
经过一段时间的数据采集后,验证zabbix server是否有刚添加完成主机的监控数据和图形,图形显示如下
至此,针对linux系统的zabbix agent安装、添加主机和监控就简单完成了。
注意:由于试验机器限制,10.0.58.102做一次快照,并且兼职 Java gateway服务器
学习如何通过java gateway实现对tomcat的指标进行数据采集和图形展示,如堆栈内存利用率、当前会话连接数、繁忙线程等。
下载地址:https://www.oracle.com/java/technologies/javase-downloads.html
# pwd
/apps
[root@zabbix-web1 apps]#tar xvf jdk-8u261-linux-x64.tar.gz
[root@zabbix-web1 apps]#ln -sv /apps/jdk1.8.0_261/ /apps/jdk
'/apps/jdk' -> '/apps/jdk1.8.0_261/'
[root@zabbix-web1 apps]#vim /etc/profile
export JAVA_HOME=/apps/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
[root@zabbix-web1 apps]#source /etc/profile
[root@zabbix-web1 apps]#java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
下载地址:https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.57/bin/
#解压tomcat压缩包
[root@zabbix-web1 ~]#cd /apps/
[root@zabbix-web1 apps]#tar xvf apache-tomcat-8.5.57.tar.gz
[root@zabbix-web1 apps]#ln -sv /apps/apache-tomcat-8.5.57 /apps/tomcat
'/apps/tomcat' -> '/apps/apache-tomcat-8.5.57'
#创建测试app
[root@zabbix-web1 apps]#cd apache-tomcat-8.5.57/
[root@zabbix-web1 tomcat]#mkdir webapps/linux41
[root@zabbix-web1 tomcat]#cat webapps/linux41/index.html
<h1>linux41 tomcat</h1>
#启动tomcat
[root@zabbix-web1 tomcat]#/apps/apache-tomcat-8.5.57/bin/catalina.sh start
[root@zabbix-web1 tomcat]#ss -nt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 36 10.0.58.106:22 10.0.0.1:9828
确认tomcat服务运行及访问正常
java gateway是一个独立于zabbix server和zabbix agent的组件,也就是java gateway可以是单独的一台服务器,但是也可以和zabbix server或者zabbix agent公用一台服务器,前提是端口不要配置冲突了。
本次使用一台单独的服务器,IP地址为10.0.58.102
[root@zabbix-proxy-active ~]#cd /usr/local/src/
[root@zabbix-proxy-active src]#wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
[root@zabbix-proxy-active src]#dpkg -i zabbix-release_4.0-3+bionic_all.deb
[root@zabbix-proxy-active src]#apt update
[root@zabbix-proxy-active src]#apt install zabbix-java-gateway
[root@zabbix-proxy-active src]#vim /etc/zabbix/zabbix_java_gateway.conf
[root@zabbix-proxy-active src]#grep "^[a-Z]" /etc/zabbix/zabbix_java_gateway.conf
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/var/run/zabbix/zabbix_java_gateway.pid"
START_POLLERS=10
TIMEOUT=30
[root@zabbix-proxy-active src]#systemctl restart zabbix-java-gateway
[root@zabbix-proxy-active src]#systemctl enable zabbix-java-gateway
[root@zabbix-proxy-active src]#lsof -i:10052 #验证java gateway端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 4995 zabbix 11u IPv6 46905 0t0 TCP *:10052 (LISTEN)
[root@zabbix-proxy-active src]#pstree -p 4995
java(4995)─┬─{
java}(4996)
├─{
java}(4997)
├─{
java}(4998)
├─{
java}(4999)
├─{
java}(5000)
├─{
java}(5001)
├─{
java}(5002)
├─{
java}(5003)
├─{
java}(5004)
├─{
java}(5005)
└─{
java}(5006)
#查看日志确保服务开启,养成良好习惯
[root@zabbix-proxy-active src]#tail -f /var/log/zabbix/zabbix_java_gateway.log
或者编译安装java gateway
# yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php phpbcmath php-mbstring mariadb-devel java-1.8.0-openjdk-devel -y
# ./configure --prefix=/usr/local/zabbix --enable-java --with-net-snmp --with-mysql --with-ssh2
#make install
#/usr/local/zabbix/sbin/zabbix_java/startup.sh #编译安装的java gateway启动方式
[root@zabbix-server ~]#vim /apps/zabbix_server/etc/zabbix_server.conf
JavaGateway=10.0.58.102 #监听地址
JavaGatewayPort=10052 #指定java gateway的服务器监听端口,如果是默认端口可以不写
StartJavaPollers=10 #启动多少个进程去轮训 java gateway
# systemctl restart zabbix-server
JMX在Java编程语言中定义了应用程序以及网络管理和监控的体系结构、设计模式、应用程序接口以及服务,通常使用JMX来监控系统的运行状态。
https://www.jianshu.com/p/8c5133cab858
[root@zabbix-web1 ~]#vim /apps/apache-tomcat-8.5.57/bin/catalina.sh
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote #启用远程监控JMX
-Dcom.sun.management.jmxremote.port=12345 #默认启动的JMX端口号,要和zabbix添加主机时候的端口一致即可
-Dcom.sun.management.jmxremote.authenticate=false #不使用用户名密码
-Dcom.sun.management.jmxremote.ssl=false #不使用ssl认证
-Djava.rmi.server.hostname=x.x.x.x" #tomcat主机自己的IP地址,不要写zabbix服务器的地址
如服务器IP是10.0.58.106,则配置如下
# vim /apps/apache-tomcat-8.5.57/bin/catalina.sh2
###########################
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=10.0.58.106"
# /apps/tomcat/bin/catalina.sh stop
# /apps/tomcat/bin/catalina.sh start
在windows安装windows版本的JDK,安装过程略,安装完成之后点击安装目录的jconsole.exe,比如C:\Program Files\Java\jdk1.8.0_221\bin\jconsole.exe,如下:
连接成功后会显示当前JMX数据
验证JMX状态
验证JMX数据
生产环境的JMX监控模板使用,主要添加了自定义监控项和阈值。
缺失中
将上一步骤导入的模板关联至被监控的tomcat服务器,然后取消关联并清理之前的JMX模板,如下
缺失
使用java客户端cmdline-jmxclient进行测试
测试能否获取到java 当前线程数和最大线程数
[root@zabbix-server ~]#java -jar cmdline-jmxclient-0.10.3.jar - 10.0.58.106:12345 'Catalina:name="http-nio-8080",type=ThreadPool' currentThreadCount
08/18/2020 03:32:04 +0000 org.archive.jmx.Client currentThreadCount: 10
# java -jar cmdline-jmxclient-0.10.3.jar - 10.0.58.106:12345 'Catalina:name="httpnio-8080",type=ThreadPool' maxThreads
12/14/2019 15:23:48 +0800 org.archive.jmx.Client maxThreads: 200
学习主动模式与被动模式工作原理,主动模式模板制作,然后添加主机并关联主动模式模板,然后验证主动模式监控数据和图形。
可以把被监控的一方想象成女生
主动模式可以监控两千以上的主机
无论是主动模式还是被动模式,都是站在zabbix agent角度来说的工作模式,比如被动模式,是说zabbix agent被动的接受zabbix server周期性发送过来的数据收集指令,在被动模式之下,zabbix server会根据主机关联的模板中的监控项和数据采集间隔时间,周期性的打开随机端口并向zabbix agent服务器的10050发起tcp连接,然后发送获取监控项数据的指令,即zabbix server发送什么指令那么zabbix agent就收集什么数据,zabbix server什么时候发送zabbix agent就什么时候采集,zabbix server不发送zabbix agent就一直不响应,所以zabbix agent也不用关心其监控项和数据采集周期间隔时间。
被动模式的优点就是配置简单,安装后即可使用,因此也成为zabbix 的默认工作模式,但是被动模式的最大问题就是会加大zabbix server的工作量,在数百甚至数千台服务器的环境下会导致zabbix server需要轮训向每个zabbix agent发送数据采集指令,如果zabbix server负载很高还会导致不能及时获取到最新数据,但由于无需其他复杂配置,被设置为了默认的工作方式。
主动模式是由zabbix agent主动向zabbix server的10051端口发起tcp连接请求,因此主动模式下必须在zabbix agent配置文件中指定zabbix server的IP或者主机名(必须可以被解析为IP地址),在连接到zabbix server之前zabbix agent是不知道自己要采集那些数据以及间隔多久采集一次数据的,然后在连接到zabbix server以后获取到自己的监控项和数据采集间隔周期时间,然后再根据监控项采集数据并返回给zabbix server,在主动模式下不再需要zabbix serve向zabbix agent发起连接请求,因此主动模式在一定程度上可减轻zabbix server打开的本地随机端口和进程数,在一定程度就减轻看zabbix server的压力。
[root@zabbix-web2 ~]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.58.101
StartAgents=3
ServerActive=10.0.58.101 #主动模式的zabbix server地址
Hostname=10.0.58.107
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
因为被动模式和主动模式监控的项目基本相同,所以可以根据被动模式的模板稍加修改成为主动模式的模板
修改名称然后添加
点击监控项进行修改
全选后批量更新
采集类型全部改为主动模式然后点击更新
其他项如更新间隔,数据保留时长根据公司情况修改
更新后生成主动模式模板如下
自动发现规则也要改成主动模式
使用10.0.58.107web2服务器来关联,添加主机时可以直接使用web1的主机来克隆,更高效
此时可以去修改配置文件里的日志级别,debug=4,查看更详细的日志,可以观察到主动模式启动是否成功
[root@zabbix-server ~]#netstat -tanlp | grep 107
tcp 0 0 10.0.58.101:10051 10.0.58.107:45666 TIME_WAIT -
tcp 0 0 10.0.58.101:10051 10.0.58.107:45662 TIME_WAIT -
tcp 0 0 10.0.58.101:10051 10.0.58.107:45660 TIME_WAIT -
tcp 0 0 10.0.58.101:10051 10.0.58.107:45664 TIME_WAIT -
tcp 0 0 10.0.58.101:10051 10.0.58.107:45658 TIME_WAIT -
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "agent.ping"
1 #1代表正常
https://www.zabbix.com/documentation/4.0/zh/manual/distributed_monitoring
zabbix 作为一个分布式监控系统(分布式监控解决方案),支持通过代理(proxy)收集zabbix agent的监控数据然后由zabbix proxy再把数据发送给zabbix server,也就是zabbix proxy 可以代替 zabbix server 收集监控数据,然后把数据汇报给 zabbix server,所以zabbix proxy可以在一定程度上分担了zabbix server 的数据收集压力,从而降低了数据的采集时间、也相应的增加了zabbix server的监控能力。
另外zabbix proxy也区分主动模式和被动模式,通信方式与zabbix server主动模式和被动模式一样,区别是zabbix proxy由于没有zabbix agent的配置,所以zabbix proxy在主动模式下要向zabbix server周期性的向zabbix server申请获取zabbix agent的监控项信息,但是zabbix proxy在被动模式下也是等待zabbix server的连接并接受zabbix server发送的监控项指令,然后再有zabbix proxy向zabbix agent发起请求获取数据。
功能 | zabbxy proxy | zabbix server |
---|---|---|
轻量级 | 是 | 相对重量级 |
图形 | 无 | 带图形控制界面 |
可以独立工作 | 是,可以独立采集数据并存储 | 是,即数据采集、存储、分析、展示于一体 |
易维护 | 是,配置完成后基本无需管理 | 维护也不难 |
独立数据库 | 保留少量最近数据 | 保留指定时间内的所有数据 |
报警通知 | 否,代理服务器不发送邮件通知 | 支持邮件、短信等告警机制 |
学习zabbix proxy工作原理
安装主动模式与被动模式的zabbix proxy服务器
配置zabbix gent工作模式为主动模式与被动模式
zabbix proxy的大版本必须要和zabbix server版本一致,否则会导致出现zabbix server与zabbix proxy不兼容问题,如下
https://www.zabbix.com/documentation/4.0/zh/manual/installation/install
zabbix 的不同安装方式
zabbix server:10.0.58.101
zabbix proxy 主动模式:10.0.58.102 #采用apt或者yum安装
zabbix proxy 被动模式:10.0.581.03 #采用编译安装
https://www.zabbix.com/documentation/4.0/zh/manual/installation/install_from_packages/debian_ubuntu
# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbixrelease_4.0-3+bionic_all.deb
# dpkg -i zabbix-release_4.0-3+bionic_all.deb
# apt update
#安装zabbix proxy
# yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php phpbcmath php-mbstring mariadb mariadb-devel java-1.8.0-openjdk-devel -y
# apt install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev curl libcurl4-openssl-dev
[root@zabbix-proxy-active ~]#apt install zabbix-proxy-mysql
#主动模式数据库导入:
[root@zabbix-proxy-active ~]#zcat /usr/share/doc/zabbix-proxy-mysql/schema.sql.gz | mysql -uproxy -h10.0.58.104 -p123456 zabbix_proxy_active
[root@zabbix-proxy-active ~]#systemctl restart zabbix-proxy
[root@zabbix-proxy-active ~]#systemctl enable zabbix-proxy
# useradd zabbix -s /usr/sbin/nologin
# apt install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev curl libcurl4-openssl-dev
[root@zabbix-proxy-passive zabbix-4.0.23]#pwd
/usr/local/src/zabbix-4.0.23
# ./configure --prefix=/apps/zabbix_proxy --enable-proxy --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
# make install
#创建zabbix proxy数据库
[root@zabbix-mysql-master ~]#mysql
mysql> create database zabbix_proxy_active character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix_proxy_active.* to proxy@'10.0.58.%' identified by '123456';
mysql> create database zabbix_proxy_passive character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix_proxy_passive.* to proxy@'10.0.58.%' identified by '123456';
验证:
[root@zabbix-proxy-active ~]#mysql -uproxy -h10.0.58.104 -p123456
MySQL [(none)]> show databases;
+----------------------+
| Database |
+----------------------+
| information_schema |
| zabbix_proxy_active |
| zabbix_proxy_passive |
+----------------------+
3 rows in set (0.00 sec)
#被动模式数据库导入
[root@zabbix-proxy-passive ~]#mysql -uproxy -p123456 -h10.0.58.104 zabbix_proxy_passive < /usr/local/src/zabbix-4.0.23/database/mysql/schema.sql
#说明:当创建Zabbix proxy数据库时不需要执行images.sql和data.sql的导入
#zabbix proxy启动service文件:
[root@zabbix-proxy-passive ~]#cat /lib/systemd/system/zabbix-proxy.service
[Unit]
Description=Zabbix Proxy
After=syslog.target
After=network.target
After=mysql.service
After=mysqld.service
After=mariadb.service
[Service]
#Environment="CONFFILE=/etc/zabbix/zabbix_proxy.conf"
#EnvironmentFile=-/etc/default/zabbix-proxy
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_proxy/run/zabbix_proxy.pid
KillMode=control-group
ExecStart=/apps/zabbix_proxy/sbin/zabbix_proxy -c /apps/zabbix_proxy/etc/zabbix_proxy.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
配置并使用被动模式(Passive Proxy Mode)的zabbix proxy收集zabbix agent监控数据。
zabbix server 10.0.58.101
被动模式服务器IP10.0.58.103
web服务器IP 10.0.58.107
# vim /apps/zabbix_proxy/etc/zabbix_proxy.conf
ProxyMode=1 #0为主动,1为被动
Server=10.0.58.101 #zabbix server服务器的地址或主机名
Hostname=magedu-jiege-proxy-active #代理服务器名称,需要与zabbix server添加代理时候的proxy name是一致的!
ListenPort=10051 #zabbix proxy监听端口
LogFile=/tmp/zabbix_proxy.log
PidFile=/apps/zabbix_proxy/run/zabbix_proxy.pid
EnableRemoteCommands=1 #允许zabbix server执行远程命令
DBHost=10.0.58.104 #数据库服务器地址
DBName=zabbix_proxy_active #使用的数据库名称
DBUser=proxy #连接数据库的用户名称
DBPassword=123456 #数据库用户密码
DBPort=3306 #数据库端口
ProxyLocalBuffer=720 #已经提交到zabbix server的数据保留时间
ProxyOfflineBuffer=720 #未提交到zabbix server的时间保留时间
HeartbeatFrequency=60 #心跳间隔检测时间,默认60秒,范围0-3600秒,被动模式不使用
ConfigFrequency=5 #间隔多少秒从zabbix server获取监控项信息
DataSenderFrequency=5 #数据发送时间间隔,默认为1秒,范围为1-3600秒,被动模式不使用
StartPollers=20 #启动的数据采集器数量
JavaGateway=10.0.58.104 #java gateway服务器地址,当需要监控java的时候必须配置否则监控不到数据
JavaGatewayPort=10052 #Javagatewa服务端口
StartJavaPollers=20 #启动多少个线程采集数据
CacheSize=2G #保存监控项而占用的最大内存
HistoryCacheSize=2G #保存监控历史数据占用的最大内存
HistoryIndexCacheSize=128M #历史索引缓存的大小
Timeout=30 #监控项超时时间,单位为秒f
LogSlowQueries=3000 #毫秒,多久的数据库查询会被记录到日志
实验:最终配置
[root@zabbix-server-passive zabbix-4.0.23 15]#grep "^[a-Z]" /apps/zabbix_proxy/etc/zabbix_proxy.conf
ProxyMode=1
Server=10.0.58.101
ServerPort=10051
Hostname=bj-pbs-magedu-passive
ListenPort=10051
LogFile=/tmp/zabbix_proxy.log
PidFile=/apps/zabbix_proxy/run/zabbix_proxy.pid
DBHost=10.0.58.104
DBName=zabbix_proxy_passive
DBUser=proxy
DBPassword=123456
ProxyLocalBuffer=360
ProxyOfflineBuffer=720
HeartbeatFrequency=60
ConfigFrequency=30
DataSenderFrequency=30
StartPollers=10
JavaGateway=10.0.58.102
JavaGatewayPort=10052
StartJavaPollers=10
Timeout=30
LogSlowQueries=3000
重启zabbix proxy服务并验证日志
[root@zabbix-proxy-passive ~ 5]#systemctl restart zabbix-proxy.service
[root@zabbix-proxy-passive ~ 6]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:10051 0.0.0.0:*
[root@zabbix-proxy-passive ~ 7]#tail -f /tmp/zabbix_proxy.log
2199:20200819:181123.150 proxy #27 started [poller #3]
2200:20200819:181123.172 proxy #28 started [poller #4]
2201:20200819:181123.198 proxy #29 started [poller #5]
2202:20200819:181123.222 proxy #30 started [poller #6]
2203:20200819:181123.240 proxy #31 started [poller #7]
2204:20200819:181123.271 proxy #32 started [poller #8]
2205:20200819:181123.289 proxy #33 started [poller #9]
2206:20200819:181123.314 proxy #34 started [poller #10]
2207:20200819:181123.336 proxy #35 started [unreachable poller #1]
2208:20200819:181123.356 proxy #36 started [icmp pinger #1]
管理–>agent代理程序–>创建代理
配置–>主机–>创建 主机
修改zabbix server向zabbix获取监控数据的频率等参数
### Option: StartProxyPollers
# Number of pre-forked instances of pollers for passive proxies.
#
# Mandatory: no
# Range: 0-250
# Default:
StartProxyPollers=20 #启用多少子进程与代理端通信,若代理较多可考虑加大此数值,范围是0-250
### Option: ProxyConfigFrequency
# How often Zabbix Server sends configuration data to a Zabbix Proxy in seconds.
# This parameter is used only for proxies in the passive mode.
#
# Mandatory: no
# Range: 1-3600*24*7
# Default:
ProxyConfigFrequency=60 #proxy被动模式下,server多少秒同步配置文件至proxy,该参数仅用于被动模式下的代理,范围是1-3600*24*7
### Option: ProxyDataFrequency
# How often Zabbix Server requests history data from a Zabbix Proxy in seconds.
# This parameter is used only for proxies in the passive mode.
#
# Mandatory: no
# Range: 1-3600
# Default:
ProxyDataFrequency=60 #被动模式下,zabbix server间隔多少秒向proxy请求历史数据
#若web服务器的/etc/zabbix/zabbix_agentd.conf配置文件中没有加入102和103,则检查不了
[root@zabbix-proxy-passive ~]#apt install zabbix-get
[root@zabbix-proxy-passive ~]#zabbix_get -s 10.0.58.106 -p 10050 -k "agent.ping"
zabbix_get [2281]: Check access restrictions in Zabbix agent configuration
root@zabbix-node3:~# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.58.101,10.0.58.102
ServerActive=127.0.0.1
Hostname=10.0.58.106
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
配置并使用主动模式(Active Proxy Mode)的zabbix proxy收集zabbix agent监控数据。
zabbix server 10.0.58.101
主动模式服务器IP 10.0.58.102
web服务器IP 10.0.58.107
# vim /etc/zabbix/zabbix_proxy.conf
ProxyMode=0 #0为主动,1为被动
Server=10.0.58.101 #zabbix server服务器的地址或主机名
Hostname=magedu-jiege-proxy-active #代理服务器名称,需要与zabbix server添加代理时候的proxy name是一致的!
ListenPort=10051 #zabbix proxy监听端口
LogFile=/tmp/zabbix_proxy.log
EnableRemoteCommands=1 #允许zabbix server执行远程命令
DBHost=10.0.58.104 #数据库服务器地址
DBName=zabbix_proxy_active #使用的数据库名称
DBUser=proxy #连接数据库的用户名称
DBPassword=123456 #数据库用户密码
DBPort=3306 #数据库端口
ProxyLocalBuffer=720 #已经提交到zabbix server的数据保留时间
ProxyOfflineBuffer=720 #未提交到zabbix server的时间保留时间
HeartbeatFrequency=60 #心跳间隔检测时间,默认60秒,范围0-3600秒,被动模式不使用
ConfigFrequency=5 #间隔多少秒从zabbix server获取监控项信息
DataSenderFrequency=5 #数据发送时间间隔,默认为1秒,范围为1-3600秒,被动模式不使用
StartPollers=20 #启动的数据采集器数量
JavaGateway=10.0.58.104 #java gateway服务器地址,当需要监控java的时候必须配置否则监控不到数据
JavaGatewayPort=10052 #Javagatewa服务端口
StartJavaPollers=20 #启动多少个线程采集数据
CacheSize=2G #保存监控项而占用的最大内存
HistoryCacheSize=2G #保存监控历史数据占用的最大内存
HistoryIndexCacheSize=128M #历史索引缓存的大小
Timeout=30 #监控项超时时间,单位为秒
LogSlowQueries=3000 #毫秒,多久的数据库查询会被记录到日志
实验配置:
[root@zabbix-server-active ~]#grep "^[a-Z]" /etc/zabbix/zabbix_proxy.conf
ProxyMode=0
Server=10.0.58.101
ServerPort=10051
Hostname=bj-pbs-qinghe-active
ListenPort=10051
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBHost=10.0.58.104
DBName=zabbix_proxy_active
DBUser=proxy
DBPassword=123456
ProxyLocalBuffer=360
ProxyOfflineBuffer=720
HeartbeatFrequency=60
ConfigFrequency=60
DataSenderFrequency=60
StartPollers=5
JavaGateway=10.0.58.102
JavaGatewayPort=10052
StartJavaPollers=10
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
CacheSize=8M
StartDBSyncers=4
HistoryCacheSize=16M
HistoryIndexCacheSize=4M
Timeout=30
ExternalScripts=/usr/lib/zabbix/externalscripts
FpingLocation=/usr/bin/fping
Fping6Location=/usr/bin/fping6
LogSlowQueries=3000
root@zabbix-proxy-active:~# systemctl restart zabbix-proxy
root@zabbix-proxy-active:~# systemctl enable zabbix-proxy
管理–>agent代理程序–>创建代理
修改或者新添加一台zabbix agent,并使用zabbix 主动模式代理服务器进行监控
使用主动模式proxy
验证当前主机和主动模式proxy 状态
需要修改zabbix agent中配置文件ServerActive的值为主动模式zabbix proxy。
# grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.58.101,10.0.58.103 #zabbix server与zabbix passive代理的地址
ServerActive=10.0.58.102
Hostname=10.0.58.106
Timeout=30
Include=/etc/zabbix/zabbix_agentd.d/*.conf
# systemctl restart zabbix-agent
zabbix agent与zabbix proxy与zabbix srver的交互过程
# vim /var/log/zabbix/zabbix_proxy.log
zabbix agent向zabbix proxy申请监控项:
36818:20200821:101604.873 End of zbx_tls_init_child()
36818:20200821:101604.873 __zbx_zbx_setproctitle() title:'listener #3 [waiting for connection]'
36819:20200821:101604.873 sending [{
"request":"active checks","host":"10.0.58.107"}]
36819:20200821:101604.873 before read
36819:20200821:101605.021 got [{
"response":"success","data":[{
"key":"kernel.maxfiles","delay":3600,"lastlogsize":0,"mtime":0}
#zabbix proxy向zabbix server发起请求获取主机监控项
21254:20200821:103311.092 In is_ip4() ip:'10.0.58.107'
21254:20200821:103311.092 End of is_ip4():SUCCEED
21254:20200821:103311.092 In get_hostid_by_host() host:'10.0.58.107' metadata:''
21254:20200821:103311.093 query [txnlev:0] [select h.hostid,h.status,h.tls_accept,h.tls_issuer,h.tls_subject,h.tls_psk_identity,a.host_metadata from hosts h left join autoreg_host a on a.proxy_hostid is null and a.host=h.host where h.host='10.0.58.107' and h.status in (0,1) and h.flags<>2 and h.proxy_hostid is null]
#收到zabbix server返回的监控项
21253:20200821:103232.754 Received [{
"globalmacro":{
"fields":["globalmacroid","macro","value"],"data":[[2,"{
$SNMP_COMMUNITY}","public"]]},"hosts":{
"fields":["hostid","host","status","available","ipmi_authtype","ipmi_privilege","ipmi_username","ipmi_password","ipmi_available","snmp_available","jmx_available","name","tls_connect","tls_accept","tls_issuer","tls_subject","tls_psk_identity","tls_psk"],"data":[[10050,"Template App Zabbix Agent",3,0,-1,2,"","",0,0,0,"Template App Zabbix Agent",1,1,"","","",""],[10271,"Template OS Linux-active-linux41",3,0,-1,2,"","",0,0,0,"Template OS Linux-active-linux41",1,1,"","","",""],[10272,"10.0.58.107",0,1,-1,2,"","",0,0,0,"10.0.58.107-qinghe-nginx-web2",1,1,"","","",""]]},"interface":{
"fields":["interfaceid","hostid","main","type","useip","ip","dns","port","bulk"],"data":[[4,10272,1,1,1,"10.0.58.107","","10050",1]]},"hosts_templates":{
"fields":["hosttemplateid","hostid","templateid"],"data":[[248,10271,10050],[249,10272,10271]]},"hostmacro":{
"fields":["hostmacroid","hostid","macro","value"],"data":[]}
#收到zabbix agent返回的监控数据
21254:20200821:103316.401 trapper got '{
"request":"agent data","session":"0be4b33368493b94e0fd41c85a9d95ea","data":[{
"host":"10.0.58.107","key":"kernel.maxfiles","value":"194374","id":1,"clock":1597977191,"ns":420097572},{
"host":"10.0.58.107","key":"kernel.maxproc","value":"131072","id":2,"clock":1597977191,"ns":420121548},{
"host":"10.0.58.107","key":"proc.num[,,run]","value":"1","id":3,"clock":1597977191,"ns":425521018},{
"host":"10.0.58.107","key":"proc.num[]","value":"176","id":4,"clock":1597977191,"ns":429143296},{
"host":"10.0.58.107","key":"system.boottime","value":"1597911406","id":5,"clock":1597977191,"ns":429369949},{
"host":"10.0.58.107","key":"system.cpu.intr","value":"3267298","id":6,"clock":1597977191,"ns":429498427},{
"host":"10.0.58.107","key":"system.cpu.load[percpu,avg15]","value":"0.010000","id":7,"clock":1597977191,"ns":429550322},{
"host":"10.0.58.107","key":"system.cpu.load[percpu,avg1]","value":"0.120000","id":8,"clock":1597977191,"ns":429567077},
TCP,全称Transfer Control Protocol,中文名为传输控制协议,它工作在OSI的传输层,提供面向连接的可靠传输服务,TCP的工作主要是建立连接,然后从应用层程序中接收数据并进行传输。TCP采用虚电路连接方式进行工作,在发送数据前它需要在发送方和接收方建立一个连接,数据在发送出去后,发送方会等待接收方给出一个确认性的应答,否则发送方将认为此数据丢失,并重新发送此数据。
在建立连接的时候,所谓的客户端与服务端是相对应的,即要看是谁主动连接的谁,如果A主动连接B那么A就是客户端而B是服务端,如果返过来B主动连接A,那么B就是客户端而A就成了服务端。
CLOSED: 端口默认是关闭状态。
LISTEN: 服务器程序开始监听一个端口,就是LISTEN状态。
SYN_RCVD: 三次握手的第二次握手后的端口状态,是收到了客户端发送的SYN_SENT数据包之后的状态,这个状态很短暂,正常在服务器上是很少看到的,除非服务器故意不发送最后一次握手数据包,服务器返回给客户端SYN确认之后就会将在自己的端口置为SYN_RCVD。
SYN_SENT :SYN_SENT状态表示客户端已发送SYN=1的请求连接报文,发送之后客户端就会将自己的端口状态置为SYN_SENT。
ESTABLISHED: 表示已经连接成功,客户端收到服务器的确认报文会回复服务器,然后就将端口置为ESTABLISHED,服务器第三次收到客户端的Ack确认就会将端口置为ESTABLISHED并开始传输数据。
FIN_WAIT_1: 出现在主动关闭方,FIN_WAIT_1状态实际上是当SOCKET在ESTABLISHED状态时,当任意一方想主动关闭连接,向对方发送了FIN=1的断开连接请求报文,此时该SOCKET即 进入到FIN_WAIT_1状态。而当对方回应ACK报文后,则进入到FIN_WAIT_2状态,当然在实际的正常情况下,无论对方何种情况下,都应该马 上回应ACK报文,所以FIN_WAIT_1状态一般是比较难见到的,而FIN_WAIT_2状态还有时常常可以用netstat看到。
FIN_WAIT_2: 出现在主动关闭方,当被动方回应FIN_WAIT_1的ACK报文后,则进入到FIN_WAIT_2状态
TIME_WAIT: 出现在主动关闭方,表示收到了对方的FIN请求关闭报文,并发送出了ACK报文,就等2*MSL(Max Segment Lifetime)后即可回到CLOSED可用状态了。如果FIN_WAIT_1状态下,收到了对方同时带FIN标志和ACK标志的报文时,可以直接进入到TIME_WAIT状态,而无须经过FIN_WAIT_2状态。
CLOSING : 这种状态比较特殊,实际情况中应该是很少见,属于一种比较罕见的例外状态。正常情况下,当你发送FIN报文后,按理来说是应该先收到(或同时收到)对方的 ACK报文,再收到对方的FIN报文。但是CLOSING状态表示你发送FIN报文后,并没有收到对方的ACK报文,反而却也收到了对方的FIN报文。什 么情况下会出现此种情况呢?其实细想一下,也不难得出结论 那就是如果双方几乎在同时close一个SOCKET的话,那么就出现了双方同时发送FIN报 文的情况,也即会出现CLOSING状态,表示双方都正在关闭SOCKET连接。
CLOSE_WAIT: 表示在等待关闭端口,这种状态存在于被动关闭的一方。
LAST_ACK: 是被动关闭方在主动关闭一方在发送FIN报文后,最后等待对方的ACK报文,当再次收到ACK报文后,也即可以进入到CLOSED可用状态了。
安装服务端与客户端将端口状态区分
[root@zabbix-web2 ~]#cat /etc/zabbix/zabbix_agentd.d/tcp_conn.sh
#!/bin/bash
#Author:Zhang ShiJie
tcp_conn_status(){
TCP_STAT=$1
ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' >
/tmp/tcp_conn.txt
TCP_NUM=$(grep "$TCP_STAT" /tmp/tcp_conn.txt | cut -d ' ' -f2)
if [ -z $TCP_NUM ];then
TCP_NUM=0
fi
echo $TCP_NUM
}
main(){
case $1 in
tcp_status)
tcp_conn_status $2;
;;
esac
}
main $1 $2
zabbix agent添加自定义监控项并调用脚本获取到监控项数据
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.conf
...............
297 UserParameter=tcp_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/tcp_conn.sh $1 $2
[root@zabbix-web2 ~]#chmod a+x /etc/zabbix/zabbix_agentd.d/tcp_conn.sh
[root@zabbix-web2 ~]#systemctl restart zabbix-agent #重启zabbix agent
监控项和值(shell脚本比较多)
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "tcp_status["tcp_status","TIME-WAIT"]"
12
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "tcp_status["tcp_status","ESTAB"]"
2
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "tcp_status["tcp_status","LISTEN"]"
19
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "tcp_status["tcp_status","FIN_WAIT_1"]"
0
配置-模板-导入
通过自定义监控模板对memcache进行监控
[root@zabbix-web2 ~]#vim /etc/memcached.conf nmap #ubuntu
# yum install memcached nmap #centos
# grep -v "#" /etc/memcached.conf | grep -v "^$"
-d
logfile /var/log/memcached.log
-m 512
-p 11211
-u memcache
-l 0.0.0.0
-P /var/run/memcached/memcached.pid
[root@zabbix-web2 ~]# systemctl restart memcached
[root@zabbix-web2 ~]# systemctl enable memcached
传递不同参数传递给脚本,并通过脚本获取监控项
# pwd
/etc/zabbix/zabbix_agentd.d
# cat memcache_monitor.sh
#!/bin/bash
#Author:Zhang ShiJie
memcached_status(){
M_PORT=$1
M_COMMAND=$2
echo -e "stats\nquit" | ncat 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND" | awk '{print $3}' #ubuntu使用ncat, 安装nmap
}
main(){
case $1 in
memcached_status)
memcached_status $2 $3
;;
esac
}
main $1 $2 $3
[root@zabbix-web2 zabbix_agentd.d]#chmod +x /etc/zabbix/zabbix_agentd.d/memcached_status.sh
[root@zabbix-web2 zabbix_agentd.d]#bash memcached_status.sh memcached_status 11211 curr_connections
1
# vim /etc/zabbix/zabbix_agentd.conf
298 UserParameter=memcache_status[*],/etc/zabbix/zabbix_agentd.d/memcached_status.sh "$1" "$2" "$3"
[root@zabbix-web2 ~]#systemctl restart memcached.service
[root@zabbix-web2 ~]#systemctl restart zabbix-agent.service
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "memcache_status["memcached_status","11211","curr_connections"]"
1
配置-模板-创建模板
配置-模板-magedu-memcache-监控项-创建监控项
配置-模板-magedu-memcache-触发器-创建触发器
点击表达式方框右侧的添加按钮,选择要对那个监控项设置触发器以及触发方式和值的大小,一个模板中可以有多个触发器,一个触发器是根据一个监控项的返回值对比预先设置的阈值,触发器就是监控项返回了不符合预定义的值范围后就进行触发下一步操作的警戒线。
配置-模板-magedu-memcache-图形-创建图形
在图形里面关联监控项,一个图形可以用多个监控项,不同的监控项会自动使用不同的颜色进行区分,也可以手动调整各监控项的颜色和图形类型。
配置-主机-选择主机-模板
#使用此命令在多个终端执行,使连接数和内存大小变化
[root@zabbix-web2 zabbix_agentd.d]#telnet 10.0.58.107 11211
Trying 10.0.58.107...
Connected to 10.0.58.107.
Escape character is '^]'.
set name1 0 0 3
cui
STORED
加触发器之前
加了触发器后
触发了报警
学习一个模板中创建多个监控项、多个触发器和多个图形
# apt install redis -y
# vim /etc/redis/redis.conf
bind 0.0.0.0
# systemctl restart redis
# systemctl enable redis
[root@zabbix-web2 ~]#(echo -en "INFO \r\n";sleep 1;) | ncat 127.0.0.1 "6379" |grep -w used_memory
used_memory:841272
[root@zabbix-web2 ~]#(echo -en "INFO \r\n";sleep 1;) | ncat 127.0.0.1 "6379" |grep -w used_memory |cut -d ":" -f2
841272
# pwd
/etc/zabbix/zabbix_agentd.d
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.d/redis_status.sh
#!/bin/bash
#Author: Cui Qinghe
redis_status(){
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO \r\n";sleep 1;) | ncat 127.0.0.1 "$R_PORT" > /tmp/redis_"$R_PORT".tmp
REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2)
echo $REDIS_STAT_VALUE
}
help(){
echo "${0} + redis_status + PORT + COMMAND"
}
main(){
case $1 in
redis_status)
redis_status $2 $3
;;
*)
help
;;
esac
}
main $1 $2 $3
[root@zabbix-web2 zabbix_agentd.d]#chmod +x redis_status.sh
[root@zabbix-web2 zabbix_agentd.d]#bash redis_status.sh redis_status 6379 used_memory
841272
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.conf
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_status.sh $1 $2 $3
[root@zabbix-web2 ~]#systemctl restart zabbix-agent
[root@zabbix-web2 ~]#rm -rf /tmp/redis_6379.tmp
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "redis_status["redis_status","6379","used_memory"]"
841272
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "redis_status["redis_status","6379","connected_clients"]"
1
配置-模板-创建模板
配置-模板-magedu-redis-monitor-template-监控项-创建监控项
说明:zabbix会对收到的数据自动进行处理
配置-模板-magedu-redis-monitor-template-触发器-创建触发器
需要将已用内存单位转换,从G转换为字节,例如当内存使用达2G时就进行触发,则换算方式如下
2*1024*1024*1024
配置-模板-magedu-redis-monitor-template-图形-创建图形
#在多个服务器模拟连接
[root@zabbix-server-passive ~]#telnet 10.0.58.107 6379
Trying 10.0.58.107...
Connected to 10.0.58.107.
Escape character is '^]'.
info
添加触发器后
脚本 nginx_status.sh
模板 nginx_status.yml
对nginx的活动连接和当前状态等运行状态进行监控
配置示例
location /nginx_status {
stub_status;
allow 10.0.58.0/16;
allow 127.0.0.1;
deny all;
}
状态页用于输出nginx的基本状态信息
输出信息示例
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections 当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
accepts 统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled 统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因
worker_connections限制等被拒绝的连接。
requests 统计总值,Nginx自启动后客户端发来的总的请求数。
如果是编译安装需要添加编译参数–with-http_stub_status_module
[root@zabbix-web2 ~]#cd /usr/local/src/
[root@zabbix-web2 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@zabbix-web2 src]#tar xvf nginx-1.18.0.tar.gz
[root@zabbix-web2 src]#cd nginx-1.18.0/
[root@zabbix-web2 nginx-1.18.0]#./configure --prefix=/apps/nginx --with-http_stub_status_module
[root@zabbix-web2 nginx-1.18.0]#make && make install
# vim /apps/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location /nginx_status {
stub_status;
allow 10.0.58.0/16;
allow 127.0.0.1;
[root@zabbix-web2 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@zabbix-web2 ~]#/apps/nginx/sbin/nginx
[root@zabbix-web2 ~]#curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
3 3 3
Reading: 0 Writing: 1 Waiting: 0 #前两个值越小越好
验证nginx
# pwd
/etc/zabbix/zabbix_agentd.d
# cat nginx_status.sh
#!/bin/bash
#Date:2016/11/11
#Author: Zhangshijie
nginx_status_fun(){
#函数内容
NGINX_PORT=$1 #端口,函数的第一个参数是脚本的第二个参数,即脚本的第二个参数是段端口号
NGINX_COMMAND=$2 #命令,函数的第二个参数是脚本的第三个参数,即脚本的第三个参数是命令
nginx_active(){
#获取nginx_active数量,以下相同,这是开启了nginx状态但是只能从本机看到
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
nginx_reading(){
#获取nginx_reading状态的数量
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
nginx_writing(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
nginx_waiting(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
nginx_accepts(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
nginx_handled(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
nginx_requests(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
esac
}
main(){
#主函数内容
case $1 in #分支结构,用于判断用户的输入而进行响应的操作
nginx_status) #当输入nginx_status就调用nginx_status_fun,并传递第二和第三个参数
nginx_status_fun $2 $3;
;;
*) #其他的输入打印帮助信息
echo $"Usage: $0 {nginx_status key}"
esac #分支结束符
}
main $1 $2 $3
[root@zabbix-web2 zabbix_agentd.d]#chmod +x nginx_status.sh
[root@zabbix-web2 zabbix_agentd.d]#bash nginx_status.sh nginx_status 80 active
1
[root@zabbix-web2 zabbix_agentd.d]#bash nginx_status.sh nginx_status 80 writing
1
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.conf
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1 $2 $3
[root@zabbix-web2 ~]#systemctl restart zabbix-agent
说明:关于配置文件
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.conf
Include=/etc/zabbix/zabbix_agentd.d/*.conf
#可以单独创建一个文件用于存UserParameter,需要注意的是,主配置文件和新建的配置文件内容不能重复
[root@zabbix-web2 zabbix_agentd.d]#ll /etc/zabbix/zabbix_agentd.d/*.conf
-rw-r--r-- 1 root root 423 Aug 23 23:53 /etc/zabbix/zabbix_agentd.d/all.conf
-rw-r--r-- 1 root root 1531 Jul 27 15:00 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
[root@zabbix-web2 zabbix_agentd.d]#vim all.conf
[root@zabbix-web2 zabbix_agentd.d]#cat all.conf
UserParameter=linux41[*],/bin/bash /etc/zabbix/zabbix_agentd.d/linux41.sh $1 $2 $3
UserParameter=qinghe,/usr/bin/python3 /etc/zabbix/zabbix_agentd.d/linux41.py
UserParameter=tcp_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/tcp_conn.sh $1 $2
UserParameter=memcache_status[*],/etc/zabbix/zabbix_agentd.d/memcached_status.sh "$1" "$2" "$3"
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_status.sh $1 $2 $3
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh $1 $2 $3
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.107 -p 10050 -k "nginx_status["nginx_status","80","active"]"
1
SNMP是英文"Simple Network Management Protocol"的缩写,中文意思是“简单网络管理协议,SNMP是一种简单网络管理协议,它属于TCP/IP五层协议中的应用层协议,用于网络管理的协议,SNMP主要用于网络设备的管理。
SNMP的基本思想 为不同种类的设备、不同厂家生产的设备、不同型号的设备,定义为一个统一的接口和协议,使得管理员可以是使用统一的外观面对这些需要管理的网络设备进行管理。通过网络,管理员可以管理位于不同物理空间的设备,从而大大提高网络管理的效率,简化网络管理员的工作。
SNMP协议是TCP/IP协议簇的一个应用层协议,在1988年被制定,并被Internet体系结构委员会(IAB,Internet Architecture Board)采纳作为一个短期的网络管理解决方案,由于SNMP的简单性,在Internet时代得到了蓬勃的发展,1992年发布了SNMPv2版本,以增强SNMPv1的安全性和功能,SNMP的协议版本目前有SNMP v1、SNMP v2c和SNMP v3三种版本,其具体差别如下
SNMP v1采用团体名(Community Name)认证,团体名用来定义SNMP NMS和SNMP Agent的关系,如果SNMP报文携带的团体名没有得到设备的认可,该报文将被丢弃,团体名起到了类似于密码的作用,用来限制SNMP NMS对SNMP Agent的访问。
SNMP v2c也采用团体名认证,它在兼容SNMP v1的同时又扩充了SNMP v1的功能,它提供了更多的操作类型(批量获取GetBulk和通知请求InformRequest)、支持更多的数据类型(Counter64等)、提供了更丰富的错误代码且能够更细致地区分错误。
SNMP v3提供了基于用户的安全模型(USM,User-Based Security Model)的认证机制,用户可以设置认证和加密功能,认证用于验证报文发送方的合法性,避免非法用户的访问,加密则是对NMS和Agent之间的传输报文进行加密,以免被窃听。通过有无认证和有无加密等功能组合,可以为SNMP NMS和SNMP Agent之间的通信提供更高的安全性。
SNMP网络元素分为NMS和Agent两种
NMS(Network Management Station,网络管理站)是运行SNMP客户端管理程序的工作站,能够提供非常友好的人机交互界面,方便网络管理员完成绝大多数的网络管理工作。
Agent是驻留在设备上的一个进程,负责接收、处理来自NMS的请求报文。在一些紧急情况下,如接口状态发生改变等,Agent也会主动通知NMS。
NMS是SNMP网络的管理者,Agent是SNMP网络的被管理者。NMS和Agent之间通过SNMP协议来交互管理信息。
SNMP管理进程与代理进程之间为了交互信息,定义了5种报文
一套完整的SNMP系统主要包括以下几个方面
所谓(MIB)管理信息库,就是所有代理进程包含的、并且能够被管理进程进行查询和设置的信息的集合。MIB是基于对象标识树的,对象标识是一个整数序列,中间以"."分割,这些整数构成一个树型结构,类似于 DNS或Unix的文件系统,MIB被划分为若干个组,如system、 interfaces、 a t(地址转换)和ip组等。iso . org . dod . internet .private . enterprises(1.3 .6 .1.4.1)这个标识,是给厂家自定义而预留的,比如华为的为1.3.6.1.4.1.2011,华三的为1.3.6.1.4.1.25506。
Centos 7.x服务器安装配置SNMP
[root@s6 ~]# yum install -y net-snmp / # apt-get install snmpd
[root@s6 ~]# vim /etc/snmp/snmpd.conf
# sec.name source community
com2sec notConfigUser default 123456 #第一步:设置团体认密码,默认为public,建议修改
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser #第二步:将团体名称notConfigUser 关联至组notConfigGroup
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1 #创建一个view视图,并对其授权可访问的OID范围
view systemview included .1. #自定义授权,否则zabbix server无法获取数据
access notConfigGroup “” any noauth exact systemview none none #将组notConfigGroup关联至systemview 从而完成对组的授权
[root@s6 ~]# systemctl restart snmpd
[root@centos7 ~]#ss -uanlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:161 *:* users:(("snmpd",pid=4049,fd=6))
如何测试OID :
snmpwalk是SNMP的一个工具,它使用SNMP的GET请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户。通过snmpwalk也可以查看支持SNMP协议(可网管)的设备的一些其他信息,比如cisco交换机或路由器IP地址、内存使用率等,也可用来协助开发SNMP功能。
要使用snmpwalk需要先按照net-snmp软件包中。
#centos7安装包如下
[root@s1 ~]# yum -y install net-snmp-utils
[root@s1 ~]# snmpwalk -h
USAGE: snmpwalk [OPTIONS] AGENT [OID]
–h:显示帮助。
–v:指定snmp的版本, 1或者2c或者3。
–c:指定连接设备SNMP密码。
–V:显示当前snmpwalk命令行版本。
–r:指定重试次数,默认为0次。
–t:指定每次请求的等待超时时间,单为秒,默认为3秒。
–l:指定安全级别:noAuthNoPriv|authNoPriv|authPriv。
–a:验证协议:MD5|SHA。只有-l指定为authNoPriv或authPriv时才需要。
–A:验证字符串。只有-l指定为authNoPriv或authPriv时才需要。
–x:加密协议:DES。只有-l指定为authPriv时才需要。
–X:加密字符串。只有-l指定为authPriv时才需要。
#Ubuntu安装包如下
[root@zabbix-server ~]#snap install net-snmp
测试能否通过SNMOP采集数据
[root@zabbix-server ~]#snmpwalk -v 2c -c 123456 10.0.58.108 1.3.6.1.2.1.1.1
iso.3.6.1.2.1.1.1.0 = STRING: "Linux centos7 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64"
[root@zabbix-server ~]#snmpwalk -v 2c -c 123456 10.0.58.108 1.3.6.1.2.1.1.5
iso.3.6.1.2.1.1.5.0 = STRING: "centos7"
CPU 负载:
1 minute Load: .1.3.6.1.4.1.2021.10.1.3.1
5 minute Load: .1.3.6.1.4.1.2021.10.1.3.2
15 minute Load: .1.3.6.1.4.1.2021.10.1.3.3
内存使用:
Total Swap Size: .1.3.6.1.4.1.2021.4.3.0
Available Swap Space: .1.3.6.1.4.1.2021.4.4.0
Total RAM in machine: .1.3.6.1.4.1.2021.4.5.0
Total RAM used: .1.3.6.1.4.1.2021.4.6.0
Total RAM Free: .1.3.6.1.4.1.2021.4.11.0
Total RAM Shared: .1.3.6.1.4.1.2021.4.13.0
Total RAM Buffered: .1.3.6.1.4.1.2021.4.14.0
Total Cached Memory: .1.3.6.1.4.1.2021.4.15.0
硬盘使用:
Path where the disk is mounted: .1.3.6.1.4.1.2021.9.1.2.1
Path of the device for the partition: .1.3.6.1.4.1.2021.9.1.3.1
Total size of the disk/partion (kBytes): .1.3.6.1.4.1.2021.9.1.6.1
Available space on the disk: .1.3.6.1.4.1.2021.9.1.7.1
Used space on the disk: .1.3.6.1.4.1.2021.9.1.8.1
Percentage of space used on disk: .1.3.6.1.4.1.2021.9.1.9.1
Percentage of inodes used on disk: .1.3.6.1.4.1.2021.9.1.10.1
系统信息:
sysDescr 1.3.6.1.2.1.1.1
sysObjectID 1.3.6.1.2.1.1.2
sysUpTime 1.3.6.1.2.1.1.3
sysContact 1.3.6.1.2.1.1.4
sysName 1.3.6.1.2.1.1.5
CPU 信息:
percentage of user CPU time: .1.3.6.1.4.1.2021.11.9.0
raw user cpu time: .1.3.6.1.4.1.2021.11.50.0
percentages of system CPU time: .1.3.6.1.4.1.2021.11.10.0
raw system cpu time: .1.3.6.1.4.1.2021.11.52.0
percentages of idle CPU time: .1.3.6.1.4.1.2021.11.11.0
raw idle cpu time: .1.3.6.1.4.1.2021.11.53.0
raw nice cpu time: .1.3.6.1.4.1.2021.11.51.0
使用zabbix 自带的SNMP,先修改团体名称,否则 会因为团体名不一致导致zabbix没有权限去获取被监控服务器的snmp利用率
监控MySQL连接数、主从同步、同步延迟等。
部署mysql 主从同步,需要配置不同的server id并开启binlog
MySQL 5.7.x:
[root@zabbix-mysql-master ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
server-id = 104
log-bin = /var/lib/mysql/master-log
MySQL 5.6.x:
# cat /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
server-id=10
log-bin=/data/mysql/master-log
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
[root@zabbix-mysql-master ~]#systemctl restart mysql
[root@zabbix-mysql-slave ~]#apt install -y mysql-server mysql-client
MySQL 5.7.x:
[root@zabbix-mysql-slave ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
server-id = 105
relay-log = /var/lib/mysql/relay-log
MySQL 5.6.x:
# cat /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
relay-log = /data/mysql
server-id=20
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock
[root@zabbix-mysql-slave ~]#systemctl restart mysql
在mysql master服务器授权账户并导出数据,然后scp到mysql backup服务器。
MySQL Master授权账户:
[root@zabbix-mysql-master ~]#mysql
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'qinghe'@'10.0.58.%' IDENTIFIED BY '123456';
[root@zabbix-mysql-master ~]#mysqldump --all-databases --single_transaction --flush-logs --master-data=2 --lock-tables > /opt/backup.sql
[root@zabbix-mysql-master ~]#scp /opt/backup.sql 10.0.58.105:/opt/
在MySQL Slave服务器导入数据开始同步数据,Position位置在sql文件
[root@zabbix-mysql-slave ~]#mysql < /opt/backup.sql
[root@zabbix-mysql-slave ~]#head -n30 /opt/backup.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='master-log.000002', MASTER_LOG_POS=154;
[root@zabbix-mysql-slave ~]#mysql
mysql> CHANGE MASTER TO MASTER_HOST='10.0.58.104',MASTER_USER='qinghe',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-log.000002',MASTER_LOG_POS=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.58.104
Master_User: qinghe
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000002
Read_Master_Log_Pos: 3481591
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 868984
Relay_Master_Log_File: master-log.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
官方文档及下载地址
插件地址:https://www.percona.com/doc/percona-monitoring-plugins/LATEST/zabbix/index.html
安装包下载地址:https://www.percona.com/downloads/
installation-instructions 安装教程:https://www.percona.com/doc/percona-monitoring-plugins/LATEST/zabbix/index.html
[root@zabbix-mysql-slave ~]# apt install zabbix-agent
[root@zabbix-mysql-slave ~]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.58.101,10.0.58.102
StartAgents=3
ServerActive=10.0.58.102
Hostname=10.0.58.105
Timeout=30
AllowRoot=1
User=root
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@zabbix-mysql-slave ~]# systemctl restart zabbix-agent
[root@zabbix-mysql-slave ~]#systemctl enable zabbix-agent
修改zabbix agent启动用户为root,包括zabbix agent配置文件和服务启动文件。
安装Percona软件包:
[root@zabbix-mysql-slave ~]#dpkg -i percona-zabbix-templates_1.1.8-1.artful_all.deb
[root@zabbix-mysql-slave ~]#cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@zabbix-mysql-slave ~]#systemctl restart zabbix-agent
安装php环境: 目前Percona与ubuntu 自带的php 7.2不兼容,需要安装php 5.6版本
[root@zabbix-mysql-slave ~]#add-apt-repository ppa:ondrej/php
[root@zabbix-mysql-slave ~]#apt-get -y update
[root@zabbix-mysql-slave ~]#apt install -y php5.6 php5.6-mysql
创建mysql认证文件:
[root@zabbix-mysql-slave ~]#cat /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php.cnf
<?php
$mysql_user = 'root';
$mysql_pass = '';
测试脚本能否获取数据:
[root@zabbix-mysql-slave ~]#/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
33
#监控项如下
[root@zabbix-mysql-slave ~]#cat /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf
UserParameter=MySQL.Sort-scan,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kt
UserParameter=MySQL.slave-stopped,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jh
UserParameter=MySQL.Com-replace,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jz
UserParameter=MySQL.innodb-lock-structs,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lp
UserParameter=MySQL.Com-load,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kg
UserParameter=MySQL.State-updating,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mi
UserParameter=MySQL.Aborted-clients,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ip
UserParameter=MySQL.innodb-lock-wait-secs,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ni
UserParameter=MySQL.Handler-read-key,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mr
UserParameter=MySQL.file-reads,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gy
UserParameter=MySQL.Query-time-count-12,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ox
UserParameter=MySQL.relay-log-space,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lj
UserParameter=MySQL.Threads-connected,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iu
UserParameter=MySQL.Qcache-lowmem-prunes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jp
UserParameter=MySQL.Binlog-cache-use,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lm
UserParameter=MySQL.State-freeing-items,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lt
UserParameter=MySQL.Query-time-count-10,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ov
UserParameter=MySQL.read-views,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gm
UserParameter=MySQL.Bytes-received,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ky
UserParameter=MySQL.os-waits,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hv
UserParameter=MySQL.Handler-commit,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mm
UserParameter=MySQL.Com-select,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jx
UserParameter=MySQL.Qcache-total-blocks,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh js
UserParameter=MySQL.Handler-read-prev,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mt
UserParameter=MySQL.Sort-rows,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ks
UserParameter=MySQL.Qcache-free-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jm
UserParameter=MySQL.pages-read,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gu
UserParameter=MySQL.Key-read-requests,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
UserParameter=MySQL.State-other,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ml
UserParameter=MySQL.Qcache-inserts,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jo
UserParameter=MySQL.State-none,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mk
UserParameter=MySQL.pending-normal-aio-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hp
UserParameter=MySQL.hash-index-cells-total,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nj
UserParameter=MySQL.pool-size,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gq
UserParameter=MySQL.pending-ibuf-aio-reads,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hl
UserParameter=MySQL.Handler-write,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ng
UserParameter=MySQL.innodb-sem-waits,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ny
UserParameter=MySQL.Handler-savepoint-rollback,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh my
UserParameter=MySQL.Query-time-total-01,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pg
UserParameter=MySQL.Query-time-total-00,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oz
UserParameter=MySQL.Table-locks-waited,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ig
UserParameter=MySQL.Handler-rollback,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mw
UserParameter=MySQL.unflushed-log,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lg
UserParameter=MySQL.Query-time-total-04,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pj
UserParameter=MySQL.Query-time-total-07,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pm
UserParameter=MySQL.Handler-savepoint,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mx
UserParameter=MySQL.Query-time-total-09,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh po
UserParameter=MySQL.Query-time-total-08,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pn
UserParameter=MySQL.Select-range-check,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ko
UserParameter=MySQL.Threads-running,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iw
UserParameter=MySQL.State-init,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lu
UserParameter=MySQL.Aborted-connects,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iq
UserParameter=MySQL.Handler-read-first,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mq
UserParameter=MySQL.Created-tmp-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ku
UserParameter=MySQL.Created-tmp-disk-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kv
UserParameter=MySQL.Select-full-range-join,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh km
UserParameter=MySQL.Connections,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iz
UserParameter=MySQL.Com-insert,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jw
UserParameter=MySQL.Query-time-total-11,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pq
UserParameter=MySQL.innodb-transactions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gl
UserParameter=MySQL.State-sorting-result,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mg
UserParameter=MySQL.State-statistics,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mh
UserParameter=MySQL.innodb-locked-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lo
UserParameter=MySQL.log-bytes-written,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh li
UserParameter=MySQL.innodb-log-buffer-size,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kz
UserParameter=MySQL.Select-full-join,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kl
UserParameter=MySQL.locked-transactions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh go
UserParameter=MySQL.Handler-read-rnd,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mu
UserParameter=MySQL.Handler-delete,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mn
UserParameter=MySQL.Query-time-total-13,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ps
UserParameter=MySQL.Query-time-total-10,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pp
UserParameter=MySQL.Key-buf-bytes-used,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oh
UserParameter=MySQL.Com-delete-multi,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kj
UserParameter=MySQL.Select-range,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kn
UserParameter=MySQL.pending-aio-log-ios,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hh
UserParameter=MySQL.ibuf-inserts,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hq
UserParameter=MySQL.State-copying-to-tmp-table,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lr
UserParameter=MySQL.Com-replace-select,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kk
UserParameter=MySQL.modified-pages,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gt
UserParameter=MySQL.Com-delete,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jy
UserParameter=MySQL.Threads-cached,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh it
UserParameter=MySQL.hash-index-cells-used,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nk
UserParameter=MySQL.uncheckpointed-bytes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nn
UserParameter=MySQL.Query-time-total-12,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pr
UserParameter=MySQL.Qcache-hits,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jn
UserParameter=MySQL.Questions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ju
UserParameter=MySQL.Qcache-queries-in-cache,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jr
UserParameter=MySQL.key-buffer-size,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oi
UserParameter=MySQL.total-mem-alloc,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nl
UserParameter=MySQL.spin-rounds,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hu
UserParameter=MySQL.ibuf-merged,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hr
UserParameter=MySQL.rows-inserted,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hw
UserParameter=MySQL.file-fsyncs,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gx
UserParameter=MySQL.Bytes-sent,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kx
UserParameter=MySQL.Query-time-total-03,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pi
UserParameter=MySQL.ibuf-merges,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hs
UserParameter=MySQL.Query-time-total-02,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ph
UserParameter=MySQL.pool-reads,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh qo
UserParameter=MySQL.history-list,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gk
UserParameter=MySQL.Query-time-total-05,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pk
UserParameter=MySQL.rows-updated,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hx
UserParameter=MySQL.max-connections,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ix
UserParameter=MySQL.free-pages,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gr
UserParameter=MySQL.Select-scan,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kp
UserParameter=MySQL.pending-aio-sync-ios,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hi
UserParameter=MySQL.recovery-system-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nw
UserParameter=MySQL.Query-time-total-06,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh pl
UserParameter=MySQL.innodb-sem-wait-time-ms,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nz
UserParameter=MySQL.thread-hash-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nx
UserParameter=MySQL.dictionary-cache-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nt
UserParameter=MySQL.ibuf-used-cells,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh no
UserParameter=MySQL.State-end,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ls
UserParameter=MySQL.slave-running,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jg
UserParameter=MySQL.pending-normal-aio-reads,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ho
UserParameter=MySQL.Innodb-row-lock-waits,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ok
UserParameter=MySQL.active-transactions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gp
UserParameter=MySQL.Sort-range,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kr
UserParameter=MySQL.spin-waits,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ht
UserParameter=MySQL.Slow-queries,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ii
UserParameter=MySQL.ibuf-cell-count,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nq
UserParameter=MySQL.Qcache-free-blocks,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jl
UserParameter=MySQL.Sort-merge-passes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kq
UserParameter=MySQL.thread-cache-size,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iy
UserParameter=MySQL.Key-write-requests,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gi
UserParameter=MySQL.pending-buf-pool-flushes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hj
UserParameter=MySQL.pending-log-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hn
UserParameter=MySQL.Com-update-multi,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kh
UserParameter=MySQL.State-login,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lw
UserParameter=MySQL.State-reading-from-net,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ly
UserParameter=MySQL.State-locked,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lv
UserParameter=MySQL.log-bytes-flushed,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lh
UserParameter=MySQL.ibuf-free-cells,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh np
UserParameter=MySQL.Qcache-not-cached,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jq
UserParameter=MySQL.pending-log-flushes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hm
UserParameter=MySQL.Max-used-connections,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ir
UserParameter=MySQL.State-sending-data,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lz
UserParameter=MySQL.rows-read,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hz
UserParameter=MySQL.lock-system-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nv
UserParameter=MySQL.Handler-read-rnd-next,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mv
UserParameter=MySQL.table-cache,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh io
UserParameter=MySQL.rows-deleted,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hy
UserParameter=MySQL.file-system-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nu
UserParameter=MySQL.file-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gz
UserParameter=MySQL.pending-chkp-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hk
UserParameter=MySQL.additional-pool-alloc,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nm
UserParameter=MySQL.current-transactions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gn
UserParameter=MySQL.Key-reads,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gh
UserParameter=MySQL.Handler-read-next,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ms
UserParameter=MySQL.Key-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gj
UserParameter=MySQL.Query-time-count-01,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh om
UserParameter=MySQL.pool-read-requests,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh qp
UserParameter=MySQL.Open-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ik
UserParameter=MySQL.Query-time-count-13,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oy
UserParameter=MySQL.Com-insert-select,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ki
UserParameter=MySQL.Query-time-count-11,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ow
UserParameter=MySQL.Query-time-count-03,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oo
UserParameter=MySQL.slave-lag,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jj
UserParameter=MySQL.Handler-update,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mz
UserParameter=MySQL.Created-tmp-files,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh kw
UserParameter=MySQL.Key-buf-bytes-unflushed,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh og
UserParameter=MySQL.State-preparing,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lx
UserParameter=MySQL.Binlog-cache-disk-use,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ll
UserParameter=MySQL.Slave-open-temp-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jk
UserParameter=MySQL.innodb-tables-in-use,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nh
UserParameter=MySQL.Threads-created,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh iv
UserParameter=MySQL.Slave-retried-transactions,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ji
UserParameter=MySQL.State-writing-to-net,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh mj
UserParameter=MySQL.pages-created,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gv
UserParameter=MySQL.Opened-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh il
UserParameter=MySQL.pages-written,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gw
UserParameter=MySQL.database-pages,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gs
UserParameter=MySQL.query-cache-size,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jt
UserParameter=MySQL.page-hash-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ns
UserParameter=MySQL.Innodb-row-lock-time,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oj
UserParameter=MySQL.Table-locks-immediate,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ih
UserParameter=MySQL.binary-log-space,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ln
UserParameter=MySQL.Com-update,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh jv
UserParameter=MySQL.Query-time-count-00,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ol
UserParameter=MySQL.adaptive-hash-memory,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh nr
UserParameter=MySQL.Query-time-count-02,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh on
UserParameter=MySQL.log-writes,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh hg
UserParameter=MySQL.Query-time-count-04,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh op
UserParameter=MySQL.Query-time-count-05,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh oq
UserParameter=MySQL.Query-time-count-06,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh or
UserParameter=MySQL.Query-time-count-07,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh os
UserParameter=MySQL.Query-time-count-08,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ot
UserParameter=MySQL.Query-time-count-09,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ou
UserParameter=MySQL.Open-files,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ij
UserParameter=MySQL.State-closing-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lq
UserParameter=MySQL.running-slave,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave
Percona模板中的监控项默认是五分钟收集一次监控项数据,会结合脚本检查agent上报错数据的文件的时间戳是否超过五分钟,脚本位置在/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh。
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.105 -p 10050 -k "MySQL.Key-read-requests"
33
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.105 -p 10050 -k "MySQL.Threads-connected"
1
编写脚本监控脚本MySQL主从同步及延迟
# apt install zabbix-agent
[root@zabbix-mysql-slave zabbix_agentd.d]#cat mysql_monitor.sh
#!/bin/bash
#Date:2016/11/11
#Author: Zhangshijie
Seconds_Behind_Master(){
NUM=`mysql -uroot -e "show slave status\G;" | grep "Seconds_Behind_Master:" | awk -F: '{print $2}'`
echo $NUM
}
master_slave_check(){
NUM1=`mysql -uroot -e "show slave status\G;" | grep "Slave_IO_Running" | awk -F: '{print $2}' | sed 's/^[ \t]*//g'`
#echo $NUM1
NUM2=`mysql -uroot -e "show slave status\G;" | grep "Slave_SQL_Running:" | awk -F: '{print $2}' | sed 's/^[ \t]*//g'`
#echo $NUM2
if test $NUM1 == "Yes" && test $NUM2 == "Yes";then
echo 50
else
echo 100
fi
}
main(){
case $1 in
Seconds_Behind_Master)
Seconds_Behind_Master;
;;
master_slave_check)
master_slave_check
;;
esac
}
main $1
[root@zabbix-mysql-slave zabbix_agentd.d]#chmod a+x mysql_monitor.sh
[root@zabbix-mysql-slave zabbix_agentd.d]#bash mysql_monitor.sh master_slave_check
50
[root@zabbix-mysql-slave zabbix_agentd.d]#pwd
/etc/zabbix/zabbix_agentd.d
[root@zabbix-mysql-slave zabbix_agentd.d]#cat all.conf
UserParameter=mysql_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/mysql_monitor.sh $1
[root@zabbix-mysql-slave zabbix_agentd.d]#systemctl restart zabbix-agent
#zabbix server测试获取数据
[root@zabbix-server ~]#/apps/zabbix_server/bin/zabbix_get -s 10.0.58.105 -p 10050 -k "mysql_status[master_slave_check]"
50
创建模板和MySQL主从同步的监控项、触发器和图形添加过程,其他监控项省略。
添加监控项
略
net.tcp.listen[80]
当zabbix 监控到指定的监控项异常的时候,通过指定的操作使故障自动恢复,通常是重启服务等一些简单的操作,也可以调用脚本执行比较复杂的操作。
设置监控项和触发器,新建动作,在触发条件里面添加操作,在远程主机通过zabbix 客户端执行命令
[root@zabbix-web2 ~]#vim /etc/zabbix/zabbix_agentd.conf
73 EnableRemoteCommands=1 #开启远程执行命令
287 UnsafeUserParameters=1 #允许远程执行命令的时候使用不安全的参数(特殊字符串)
[root@zabbix-web2 ~]# systemctl restart zabbix-agent
如果zabbix agent是使用zabbix用户启动的,那么要在zabbix 用户授权使用特权命令,负责有些命令zabbix没有权限执行,会导致定义好的自治愈策略因为权限拒绝为执行失败。
[root@zabbix-web2 ~]#vim /etc/sudoers
55 # Defaults !visiblepw #不强制使用tty
93 zabbix ALL = NOPASSWD: ALL #授权指定用户执行特殊命令不再需要密码,比如sudo等
配置–动作–创建动作
执行命令或者执行脚本
sudo /path/to/command
sudo /path/to/script.sh
sudo /apps/nginx/sbin/nginx
将被测试的服务手动停止运行,验证能否自动启动或重启,更多操作可以远程执行脚本。
如下 :
手动将Nginx、Tomcat等web服务停止后,验证zabbix agent能否自动启动或重启
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_7.1.4_amd64.deb
https://grafana.com/grafana/plugins/alexanderzobnin-zabbix-app
#grafana-cli plugins install alexanderzobnin-zabbix-app
installing alexanderzobnin-zabbix-app @ 3.10.4
from url: https://grafana.com/api/plugins/alexanderzobnin-zabbixapp/versions/3.10.4/download
into: /var/lib/grafana/plugins
✔ Installed alexanderzobnin-zabbix-app successfully
Restart grafana after installing plugins . <service grafana-server restart>
# systemctl restart grafana-server
设置–data source–add data source
在Others点击zabbix,即添加一个zabbix类型的数据源
http://10.0.58.101/zabbix/api_jsonrpc.php
Home-New dashboard
略~
7877
略
[root@kubernetes-master1 ~]# cat /etc/zabbix/zabbix_agentd.d/k8s_monitor.py
#!/usr/bin/env python
#coding:utf-8
#Author ZhangShiJie
import subprocess
success_list = []
error_list= []
def get_status():
obj = subprocess.Popen(("curl -sXGET http://10.20.15.209:8080/api/v1/nodes"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
data2 = data1.get('items')
#print data2
for i in data2:
data3 = i.get('status')
for i in data3.get('conditions'):
if i.get('reason') == 'KubeletReady':
if i.get('type') == "Ready":
if i.get('status') == 'True':
success_list.append(i.get('status'))
elif i.get('status') == 'False':
error_list.append(i.get('status'))
else:
break
else:
error_list.append(i.get('status'))
#pass
else:
error_list.append(i.get('status'))
def count_status():
if len(error_list) == 0:
print 50
else:
print 100
def main():
get_status()
count_status()
if __name__ == "__main__":
main()
# chmod a+x /etc/zabbix/zabbix_agentd.d/k8s_monitor.py
#cat /etc/zabbix/zabbix_agentd.d/mongodb_cluster_monitor.py
#!/bin/env python
#coding:utf-8
#Author: ZhangShiJie
import subprocess
success_list = []
error_list= []
def get_mongodb_status():
obj = subprocess.Popen(("echo 'rs.status()' | /usr/local/mongodb/bin/mongo -u user -p wswd --authenticationDatabase admin \
| grep health | awk -F':' '{print $2}' | awk -F',' '{print $1}'"),shell=True, stdout=subprocess.PIPE)
restful = obj.stdout.read()
data = restful.split()
for i in data:
if i == "1":
success_list.append(i)
else:
error_list.append(i)
def count_status():
if len(error_list) > 0:
print 100
else:
print 50
def main():
get_mongodb_status()
count_status()
if __name__ == "__main__":
main()
#chmod a+x /etc/zabbix/zabbix_agentd.d/redis_llen.py
[root@redis ~]#cat /etc/zabbix/zabbix_agentd.d/redis_llen.py
#!/usr/bin/env python
#coding:utf-8
#Author ZhangShijie
import redis
def redis_conn():
pool=redis.ConnectionPool(host="10.20.0.252",port=6379,db=0)
conn = redis.Redis(connection_pool=pool)
data = conn.llen('api4-nginx-accesslog')
print(data)
redis_conn()
[root@redis ~]# chmod a+x /etc/zabbix/zabbix_agentd.d/redis_llen.py
[root@elk-s1 ~]# vim /etc/zabbix/zabbix_agentd.d/els_status.py
#!/usr/bin/env python
#coding:utf-8
#Author ZhangShijie
import subprocess
false="false"
obj = subprocess.Popen(("curl -sXGET http://10.20.3.128:9200/_cluster/health?
pretty=true"),shell=True, stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
status = data1.get("status")
if status == "green":
print "100"
else:
print "50"
[root@elk-s1 ~]# chmod a+x /etc/zabbix/zabbix_agentd.d/els_status.py
#cat /etc/zabbix/zabbix_agentd.d/rabbit_cluster_monitor.py
#!/bin/env python
#coding:utf-8
#Author: ZhangShiJie
import subprocess
running_list = []
error_list = []
false="false"
true="true"
def get_status():
obj = subprocess.Popen(("curl -sXGET -u guest:guest http://10.20.3.171:15671/api/nodes"),shell=True,stdout=subprocess.PIPE)
data = obj.stdout.read()
data1 = eval(data)
for i in data1:
if i.get("running") == "true":
running_list.append(i.get("name"))
else:
error_list.append(i.get("name"))
def count_server():
if len(running_list) < 3: #可以判断错误列表大于0或者运行列表小于3,3未总计的节点数量
print 100 #100就是集群内有节点运行不正常了
else:
print 50 #50为所有节点全部运行正常
def main():
get_status()
count_server()
if __name__ == "__main__":
main()
#chmoa a+x /etc/zabbix/zabbix_agentd.d/rabbit_cluster_monitor.py
出现故障报警的时候,可以通过不同方式通知管理员进行故障处理,尽快恢复业务
[root@s1 ~]# vim /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts #报警脚本路径
ExternalScripts=/usr/lib/zabbix/externalscripts #外部脚本路径
通过企业邮箱、第三方服务商邮箱发送报警邮件通知运维工程师。
如果是QQ邮箱需要单独开启SMTP,其他邮箱具体联系服务商。
确认是已经打开状态,如果是未开启状态只要点击开启并根据提示进行相关验证即可。
QQ邮箱在第三方平台发送邮件不能直接使用QQ邮箱的登录密码,需要使用单独提供的授权码才可以登录,具体生成方式如下
安装提示使用绑定的手机发送配置邮件客户端到1069070069,然后点击我已发送。
将页面生成后返回的授权码妥善保存好,后期会使用此授权码进行登录验证。
报警媒介类型是一种给运维工程师发送消息通知的渠道,即当zabbix 的触发器触发一个事件后,怎么才能把这个事件通过某些方式通知给运维工程师呢?那么媒介类型就起到这样的作用,媒介类型创建好之后,需要在每个账户里面添加相应的收件配置,比如邮件类型的媒介类型要给zabbix账户添加邮箱,如果是微信类型的媒介类型那么就要在zabbix账户设置微信号,同样的到来,短信类型的媒介类型那就得给zabbix账户设置手机号用于接收报警消息
内容。
管理–>报警媒介类型–>创建报警媒介类型
设置参考 : https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371
动作是对zabbix 触发器触发后生成的事件的具体处理操作,可以是远程执行命令,也可以是发送通知给指定的管理员进行故障处理,发送命令是调用的上一步骤创建好的报警媒介类型。
配置–>动作–>创建动作
验证当前动作信息,并点击恢复操作,配置故障恢复的具体动作内容
分级发送警告:yuan是初级运维,Admin是架构师
将某个被监控的服务手动停止,验证能否收到zabbix 发送的报警通知。
在zabbix web界面,验证当事件触发后邮件通知也没有 发送成功。
到收件箱验证是否收到zabbix 发送的通知邮件
通知脚本见 api及通知脚本/send_sms.sh
#放到默认路径下
mv /root/send_sms.sh /apps/zabbix_server/share/zabbix/alertscripts
再加上可执行权限
[root@zabbix-server alertscripts]#cat send_sms.sh
#!/bin/bash
source /etc/profile
PHONE=$1
SUBJECT=$2
MESSAGE=$3
/usr/bin/curl -X "POST" "https://sms.yunpian.com/v2/sms/single_send.json" -H "content-type: application/x-www-form-urlencoded" -d "apikey=08d6496b9acef405f771fd318192ce03" -d "mobile=${PHONE}" -d "text=${MESSAGE}"
echo "${MESSAGE} 已经发送给 ${PHONE}" >> /tmp/sms.txt
[root@zabbix-server alertscripts]#pwd
/apps/zabbix_server/share/zabbix/alertscripts
服务端配置略,不同厂商配置不一样
获取密钥
{ALERT.SENDTO}–收件人媒介
{ALERT.SUBJECT}–通知主题
{ALERT.MESSAGE}–通知内容
名称 门户业务报警
默认接收人 业务报警{TRIGGER.STATUS}
默认信息 北京业务报警服务器:{HOST.NAME},IP:{HOSTNAME1},详情:{ITEM.NAME}:{ITEM.VALUE}
启用恢复信息
恢复主题 业务恢复
恢复信息 北京业务恢复服务器:{HOST.NAME},IP:{HOSTNAME1},详情:{ITEM.NAME}:{ITEM.VALUE}
#zabbix 宏变量使用及场景
https://www.zabbix.com/documentation/4.0/zh/manual/appendix/macros/supported_by_location
设置收件人的指定收件类型内容
定义动作细节
定义消息内容细节和收件人及发送方式
北京业务报警服务器:{HOST.NAME},IP:{HOSTNAME1},详情:{ITEM.NAME},{ITEM.VALUE}
配置恢复操作
验证内容
讲监控项手动停止,测试能否在出现故障后发送短信报警
微信企业账号注册见 api 及通知脚本/weixin.py及文档微信报警
https://work.weixin.qq.com/
打开企业微信官网注册账号,使用自己的手机号进行注册。
注册完成账号之后就可以扫码登录PC版web界面了,如下
在web界面创建一个应用,用于微信报警通知。
AgentID和Secret会在发送微信报警信息的时候调用
用户账户名称必须唯一,在发送微信报警信息的时候会调用
企业ID在发送微信报警信息的时候会调用
zabbix server实现微信通知基于python调用脚本实现且需要安装requests模块
简易教程 : https://work.weixin.qq.com/api/doc#90000/90003/90487
先获取token,token是通过corpid(企业ID)和corpsecret(应用 Secret)获取到的,然后通过API发送消息,根据官方文档可以看到,发送的消息其实就是一个post请求,请求方式如下 :
请求方式: POST(HTTPS)
请求地址: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN
post 参数为 access_token 和 消息体。
服务端API调用 :
https://work.weixin.qq.com/api/doc#90000/90135/90664
在zabbix server安装基础模块并编写pyhton脚本,python脚本通过调用企业微信的API实现自动发送通知消息,具体内容如下 :
# apt install python-pip
# pip install requests
# pwd
/apps/zabbix_server/share/zabbix/alertscripts
# cat wx.py
#!/usr/bin/python3.6
#coding:utf-8
#Author:Zhang ShiJie
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
corpid='企业ID'
appsecret="秘钥"
agentid="AgentID"
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
message=sys.argv[2] + "\n\n" +sys.argv[3]
params={
"touser": touser,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
[root@zabbix-server alertscripts]#python3 wx.py CuiQingHe "第二个参数是-主题" "第三个参数是-内容"
[root@zabbix-server alertscripts]#echo $?
0
[root@zabbix-server alertscripts]#python3 wx.py CuiQingHe "北京业务故障服务器" "北京业务故障服务器:10.0.58.101-Redis,IP:10.0.58.101,详情:CPU监控项,2"
[root@zabbix-server alertscripts]#echo $?
0
日志如下
[root@zabbix-web2 ~]#/apps/nginx/sbin/nginx -s stop
[root@zabbix-web2 ~]#ps -ef |grep nginx
root 43782 104774 0 16:15 pts/2 00:00:00 grep --color=auto nginx
#停了几分钟再开启
[root@zabbix-web2 ~]#/apps/nginx/sbin/nginx
通过脚本批量部署zabbix agent、通过API批量自动添加主机或者自动发现监控主机
通过脚本,实现数十上百台agent的自动化批量安装,可以通过源码或apt等方式安装。
官网下载源码包,通过自定义编译参数、自定义配置文件和监控脚本实现安装
见 zabbix-agent-onekey-install-4.0.X
#!/bin/bash
DIR=`pwd`
function sys_install(){
grep "Kernel" /etc/issue &> /dev/null
if [ $? -eq 0 ];then
useradd zabbix
yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel make -y
fi
grep "Ubuntu" /etc/issue &> /dev/null
if [ $? -eq 0 ];then
groupadd zabbix -g 2020 && useradd -m -s /usr/sbin/nologin -u 2020 -r -g zabbix zabbix
apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip make -y
fi
}
function zbx_install(){
cd ${DIR} && tar xvf zabbix-4.0.23.tar.gz && cd zabbix-4.0.23 && ./configure --prefix=/apps/zabbix_agent --enable-agent && make && make install && cd ..
mkdir /apps/zabbix_agent/pid
mkdir /apps/zabbix_agent/logs
\cp zabbix_agentd.conf /apps/zabbix_agent/etc/zabbix_agentd.conf
\cp zabbix_agentd.conf.d/* /apps/zabbix_agent/etc/zabbix_agentd.conf.d/
\cp zabbix-agent.service /lib/systemd/system/zabbix-agent.service
HOST_IP=`ifconfig eth0 | grep -w inet | awk '{print $2}'`
sed -i "s/Hostname=/Hostname=${HOST_IP}/g" /apps/zabbix_agent/etc/zabbix_agentd.conf
chown zabbix.zabbix /apps/zabbix_agent/ -R
systemctl daemon-reload && systemctl enable zabbix-agent && systemctl restart zabbix-agent
}
main(){
sys_install
zbx_install
}
main
[root@zabbix-web2 zabbix-4.0.23]#grep "^[a-Z]" /apps/zabbix_agent/etc/zabbix_agentd.conf
PidFile=/apps/zabbix_agent/pid/zabbix_agentd.pid
LogFile=/apps/zabbix_agent/logs/zabbix_agentd.log
EnableRemoteCommands=1
Server=10.0.58.101,10.0.58.102
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
ServerActive=10.0.58.102
Hostname=Zabbix server
RefreshActiveChecks=60
Timeout=30
Include=/apps/zabbix_agent/etc/zabbix_agentd.conf.d/*.conf
UnsafeUserParameters=1
[root@zabbix-web2 zabbix-4.0.23]#cp /etc/zabbix/zabbix_agentd.d/* /apps/zabbix_agent/etc/zabbix_agentd.conf.d/
[root@zabbix-web2 zabbix-4.0.23]#ll /apps/zabbix_agent/etc/zabbix_agentd.conf.d/
total 40
drwxr-xr-x 2 root root 4096 Aug 25 22:08 ./
drwxr-xr-x 3 root root 4096 Aug 25 22:05 ../
-rw-r--r-- 1 root root 506 Aug 25 22:08 all.conf
-rw-r--r-- 1 root root 31 Aug 25 22:08 linux41.py
-rwxr-xr-x 1 root root 26 Aug 25 22:08 linux41.sh*
-rwxr-xr-x 1 root root 273 Aug 25 22:08 memcached_status.sh*
-rwxr-xr-x 1 root root 2375 Aug 25 22:08 nginx_status.sh*
-rwxr-xr-x 1 root root 517 Aug 25 22:08 redis_status.sh*
-rwxr-xr-x 1 root root 460 Aug 25 22:08 tcp_conn.sh*
-rw-r--r-- 1 root root 1531 Aug 25 22:08 userparameter_mysql.conf
[root@zabbix-web2 ~]#cat /lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
#Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf"
#EnvironmentFile=-/etc/default/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_agent/pid/zabbix_agentd.pid
KillMode=control-group
ExecStart=/apps/zabbix_agent/sbin/zabbix_agentd -c /apps/zabbix_agent/etc/zabbix_agentd.conf
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
[root@zabbix-web2 ~]#chown -R zabbix.zabbix /apps/zabbix_agent/
[root@zabbix-web2 ~]#ll /apps/zabbix_agent/
total 36
drwxr-xr-x 9 zabbix zabbix 4096 Aug 25 22:00 ./
drwxr-xr-x 4 root root 4096 Aug 25 21:35 ../
drwxr-xr-x 2 zabbix zabbix 4096 Aug 25 21:35 bin/
drwxr-xr-x 3 zabbix zabbix 4096 Aug 25 22:05 etc/
drwxr-xr-x 3 zabbix zabbix 4096 Aug 25 21:35 lib/
drwxr-xr-x 2 zabbix zabbix 4096 Aug 25 22:00 logs/
drwxr-xr-x 2 zabbix zabbix 4096 Aug 25 21:59 pid/
drwxr-xr-x 2 zabbix zabbix 4096 Aug 25 21:35 sbin/
drwxr-xr-x 3 zabbix zabbix 4096 Aug 25 21:35 share/
[root@zabbix-web2 ~]#systemctl daemon-reload
[root@zabbix-web2 ~]#systemctl enable zabbix-agent.service
[root@zabbix-web2 ~]#systemctl restart zabbix-agent.service
#10.0.58.107编译安装zabbix-agent后,把配置文件传给10.0.7.108作为批量安装的模板使用
[root@zabbix-web2 ~]#scp -r /apps/zabbix_agent/etc/zabbix_agentd.conf* 10.0.7.108:/usr/local/src
[root@zabbix-web2 ~]#scp /lib/systemd/system/zabbix-agent.service 10.0.7.108:/usr/local/src
[root@redis-node8 src]#ll
total 17012
-rw-r--r-- 1 root root 1596 Aug 25 22:22 qinghe-abx-install_v4.0.23.sh
-rw-r--r-- 1 root root 17398767 Aug 17 15:21 zabbix-4.0.23.tar.gz
-rw-r--r-- 1 root root 13082 Aug 25 22:21 zabbix_agentd.conf
drwxr-xr-x 2 root root 182 Aug 25 22:21 zabbix_agentd.conf.d
-rw-r--r-- 1 root root 495 Aug 25 22:26 zabbix-agent.service
[root@redis-node8 src]#tar czvf zbx-onekye-install_v4.0.23.tar.gz ./
[root@redis-node8 ~]#scp zbx-onekye-install_v4.0.23.tar.gz 10.0.7.109:/opt/
[root@redis-node8 ~]#ssh 10.0.7.109 "cd /opt/ && tar xvf zbx-onekye-install_v4.0.23.tar.gz && bash qinghe-abx-install_v4.0.23.sh"
略
https://www.zabbix.com/documentation/4.0/zh/manual/api
通过API,实现完全自动化添加删除agent、关联模板等操作
见文本文档 Zabbix-API使用
1.获取token:
需要指定用的账户名 密码和id
id:zabbixadmin 密码:123456
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "USER",
"password": "PASSWD"
},
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
#返回的数据
{
"jsonrpc": "2.0",
"result": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}
2.查看指定主机信息:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"10.0.58.106"
]
}
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
3.获取所有主机列表:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
4.获取所有用户:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
5.获取单个模板信息:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"NGINX_Check_Statuc"
]
}
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://192.168.7.101/zabbix/api_jsonrpc.php | python -m json.tool
6.获取多个模板信息:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"tomcat-template-magedu-jiege",
"mysql-magedu-jiege"
]
}
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
7.基于脚本获取token
# cat token.py
#!/usr/bin/env python
#Author: Zhangshijie
# -*- coding:utf-8 -*-
import requests
import json
url = 'http://10.0.58.101/zabbix/api_jsonrpc.php'
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}
post_header = {'Content-Type': 'application/json'}
ret = requests.post(url, data=json.dumps(post_data), headers=post_header)
zabbix_ret = json.loads(ret.text)
if not zabbix_ret.has_key('result'):
print 'login error'
else:
print zabbix_ret.get('result')
8.通过API添加主机命令格式:
API添加主机为预先知道要添加的主机IP、预先安装并配置好zabbix agent、预先知道要关联的模板ID/组ID等信息,然后同API提交请求添加
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create", #定义方法,N多钟
"params": {
"host": "API Add Host Test", #自定义添加后的agent的名称
"interfaces": [
{
"type": 1, #类型为1表示agent,2是SNMP,3是IMPI,4是JMX
"main": 1, #主要接口
"useip": 1, #0是使用DNS,1是使用IP地址
"ip": "10.0.58.24", #添加的zabbix agent的IP地址
"dns": "",
"port": "10050" #agent端口
}
],
"groups": [
{
"groupid": "2" #添加到的组的ID
}
],
"templates": [
{
"templateid": "10001" #关联的模板的ID
}
]
},
"auth": "28fbd273d15b49d5d5ddf9a139839b41",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
8.1:通过API添加单台不带proxy的agent示例:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "API Add Host Test",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "10.0.58.24",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"auth": "8b20c44282bc5ba5d70d24ffe2c0e467",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
8.2 通过API添加主机-带proxy模式:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "172.18.0.109",
"proxy_hostid": "10274",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "10.0.58.109",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10270"
}
]
},
"auth": "8b20c44282bc5ba5d70d24ffe2c0e467",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
9.验证主机信息:
# curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"172.18.0.109"
]
}
},
"auth": "8b20c44282bc5ba5d70d24ffe2c0e467",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
10.通过脚本调用API快速添加:
root@zabbix-server:~# cat zabbix-add-host.sh
#!/bin/bash
IP="
172.31.100.105
172.31.100.106
172.31.100.107
172.31.100.108
"
for node_ip in ${IP};do
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "'${node_ip}'",
"name": "magedu-jiege-tomcat_'${node_ip}'",
"proxy_hostid": "10274",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "'${node_ip}'",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10270"
}
]
},
"auth": "8b20c44282bc5ba5d70d24ffe2c0e467",
"id": 1
}' http://10.0.58.101/zabbix/api_jsonrpc.php | python3 -m json.tool
done
http://blogs.studylinux.net/?p=705