从入门到精通
预中级
中级
进阶
机房巡检
ipmi
yum -y install OpenIPMI ipmitool
具体参考:https://www.ibm.com/developerworks/cn/linux/l-ipmi/
SNMP
具体参考:https://blog.csdn.net/HandsomeHong/article/details/80016249
安装snmp
yum -y install net-snmp net-snmp-libs net-snmp-utils
修改配置
[root@k8s-m1 ~]# grep '^[a-z]' /etc/snmp/snmpd.conf
com2sec admin default admin
group admin v2c admin
view admin included .1 80
access admin "" any noauth exact admin admin none
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root (configure /etc/snmp/snmp.local.conf)
dontLogTCPWrappersConnects yes
启动服务
systemctl start snmpd
netstat -lntpu |grep 161
udp 0 0 0.0.0.0:161 0.0.0.0:* 17023/snmpd
获取cpu内存
[root@k8s-m1 ~]# snmpwalk -v2c -c admin 10.201.1.145 .1.3.6.1.4.1.2021.10.1.3
UCD-SNMP-MIB::laLoad.1 = STRING: 0.01
UCD-SNMP-MIB::laLoad.2 = STRING: 0.04
UCD-SNMP-MIB::laLoad.3 = STRING: 0.05
[root@k8s-m1 ~]# snmpget -v2c -c admin 10.201.1.145 .1.3.6.1.4.1.2021.10.1.3.1
UCD-SNMP-MIB::laLoad.1 = STRING: 0.01
[root@k8s-m1 ~]# snmpget -v2c -c admin 10.201.1.145 .1.3.6.1.2.1.25.2.2.0
HOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 3880792 KBytes
OID参考文章:https://blog.csdn.net/buster2014/article/details/46925633
参考文章:https://www.cnblogs.com/anyux/p/8195541.html
进程:每个进程至少有一个线程,每个进程可以有多个线程。
负载:单位时间内运行队列中就绪等待的进程平均值
top
top - 14:44:13 up 5 days, 20:50, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 107 total, 2 running, 105 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.0 sy, 0.0 ni, 99.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3880792 total, 778556 free, 164584 used, 2937652 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 3125052 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
虚拟 物理 共享 占用cpu总时间
lscpu
[root@k8s-m1 ~]# lscpu
cat /proc/loadavg
cat /proc/loadavg
0.00 0.01 0.05 1/134 17383
sysstat包带的监控
yum -y install sysstat
vmstat 1 10
mpstat 1 10
虚拟内存、SWAP、物理内存
vmstat
vmstat 1 10
free
free -m
3.io
iotop
yum -y install iotop
iotop
测试io参考:https://elf8848.iteye.com/blog/2089055
网络io
iftop
[root@localhost ~]# iftop
[root@localhost ~]# iftop -i eth0 -F 10.201.1.32/24
nethogs
yum -y install nethogs
nethogs
IP地址 | 服务类型 |
---|---|
10.201.1.145 | server |
10.201.1.146 | agent |
rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum-config-manager --enable rhel-7-server-optional-rpms
yum install zabbix-server-mysql
yum install zabbix-web-mysql
yum -y install mariadb-server
systemctl enable mariadb.service
systemctl start mariadb.service
mysql_secure_installation
创建zabbix库和授权
mysql -uroot -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by '123456';
导入数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
修改httpd配置
ServerName 127.0.0.1:80 #95行取消注释
修改Zabbix server配置文件
vi /etc/zabbix/zabbix_server.conf
DBPassword=123456 #124行修改zabbix的密码
systemctl start httpd.service
systemctl enable httpd.service
systemctl start zabbix-server.service
systemctl enable zabbix-server.service
netstat -antpu |grep 10051
根据提示完成安装
提示需要修改时区
vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai #20行修改
用户名: Admin
密码:zabbix
yum install zabbix-agent
[root@k8s-n1 yum.repos.d]# 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.201.1.145 #指定server端
ServerActive=10.201.1.145 #指定server端
Include=/etc/zabbix/zabbix_agentd.d/*.conf
systemctl start zabbix-agent
systemctl enable zabbix-agent
netstat -antpu |grep 10050
设置完成后可以测试报警是否生效,关掉80端口
zabbix提供了自带的mysql监控模板,但是又很多缺陷,需要修改后,才能更好的使用
修改后需要设置变量,更加灵活的监控多实例的mysql服务
[root@k8s-n1 zabbix_agentd.d]# grep '^[A-Z]' /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=mysql.status[*],echo "show global status where Variable_name='$3';" | HOME=/var/lib/zabbix mysql -u$1 -p$2 -N | awk '{print $$2}'
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/var/lib/zabbix mysql -N'
UserParameter=mysql.ping[*],HOME=/var/lib/zabbix mysqladmin -u$1 -p$2 ping | grep -c alive
UserParameter=mysql.version,mysql -V
systemctl restart zabbix-agent
[root@k8s-n1 zabbix]# cat zabbix_linux_plugin.sh
#!/bin/bash
tcp_status_fun(){
TCP_STAT=$1
#netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp
ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp
TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2)
if [ -z $TCP_STAT_VALUE ];then
TCP_STAT_VALUE=0
fi
echo $TCP_STAT_VALUE
}
nginx_status_fun(){
NGINX_PORT=$1
NGINX_COMMAND=$2
nginx_active(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
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
}
memcached_status_fun(){
M_PORT=$1
M_COMMAND=$2
echo -e "stats\nquit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}'
}
redis_status_fun(){
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO \r\n";sleep 1;) | nc 10.201.1.146 "$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
}
main(){
case $1 in
tcp_status)
tcp_status_fun $2;
;;
nginx_status)
nginx_status_fun $2 $3;
;;
memcached_status)
memcached_status_fun $2 $3;
;;
redis_status)
redis_status_fun $2 $3;
;;
*)
echo $"Usage: $0 {tcp_status key|memcached_status key|redis_status key|nginx_status key}"
esac
}
main $1 $2 $3
[root@k8s-n1 zabbix_agentd.d]# cat zabbix-linux-plugin.conf
UserParameter=linux_status[*],/etc/zabbix/zabbix_linux_plugin.sh "$1" "$2" "$3"
systemctl restart zabbix-agent
百度网盘:https://pan.baidu.com/s/1rYyIdmE6MBKKDNGWzdUj6Q
密码:aaf0
[root@linux-node2 ~]# vim /etc/nginx/conf.d/nginx-status.conf
server {
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
yum -y install zabbix-java-gateway.x86_64
[root@k8s-m1 ~]# 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.pid"
START_POLLERS=5
vim /etc/zabbix/zabbix_server.conf
JavaGateway=10.201.1.145 #取消注释修改
JavaGatewayPort=10052 #取消注释修改
StartJavaPollers=5 #取消注释修改
systemctl restart zabbix-server.service
systemctl start zabbix-java-gateway
vim /usr/local/tomcat/bin/catalina.sh
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=1234 #配置端口
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.rmi.server.hostname=10.201.1.146" #指定本机IP地址
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
wget http://repo.typesafe.com/typesafe/repo/cmdline-jmxclient/cmdline-jmxclient/0.10.3/cmdline-jmxclient-0.10.3.jar
验证
[root@k8s-n1 ~]# java -jar cmdline-jmxclient-0.10.3.jar - 10.201.1.146:1234 java.lang:type=Memory
Attributes:
ObjectPendingFinalizationCount: ObjectPendingFinalizationCount (type=int)
HeapMemoryUsage: HeapMemoryUsage (type=javax.management.openmbean.CompositeData)
NonHeapMemoryUsage: NonHeapMemoryUsage (type=javax.management.openmbean.CompositeData)
Verbose: Verbose (type=boolean)
ObjectName: ObjectName (type=javax.management.ObjectName)
Operations:
gc: gc
Parameters 0, return type=void
[root@k8s-n1 ~]# java -jar cmdline-jmxclient-0.10.3.jar - 10.201.1.146:1234 java.lang:type=Memory NonHeapMemoryUsage
03/26/2019 09:15:36 +0800 org.archive.jmx.Client NonHeapMemoryUsage:
committed: 38191104
init: 2555904
max: -1
used: 36701248
可以看到已经获取到值了
win7 32 位java下载地址
百度网盘:https://pan.baidu.com/s/1rYyIdmE6MBKKDNGWzdUj6Q
密码:aaf0
打开jconsole,就可以弹出java的管理界面了
https://newcontinuum.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.0.25/zabbix-3.0.25.tar.gz
将里面的bin对应系统版本的文件全部复制出来,在到conf中复制一个win的配置文件
LogFile=c:\zabbix\zabbix_agentd.log #指定log存放路径
Server=10.201.1.145 #指定server端ip
ServerActive=10.201.1.145 #主动模式
Hostname=192.168.16.18 #指定本机主机名
cmd命令安装
C:\zabbix>zabbix_agentd.exe -c zabbix_agentd.win.conf -i
启动
C:\zabbix>zabbix_agentd.exe -c zabbix_agentd.win.conf -s
C:\zabbix>netstat -an
启用SNMP服务,并配置
两种方式IP和SNMP
使用这个插件可以监控到mysql很多详细参数大概200多个监控项
yum install -y https://www.percona.com/redir/downloads/percona-release/redhat/percona-release-0.1-3.noarch.rpm
yum install percona-zabbix-templates php php-mysql -y
rpm -ql percona-zabbix-templates
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@k8s-m1 ~]# cat /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh
#!/bin/sh
# The wrapper for Cacti PHP script.
# It runs the script every 5 min. and parses the cache file on each following run.
# Version: 1.1.8
#
# This program is part of Percona Monitoring Plugins
# License: GPL License (see COPYING)
# Copyright: 2018 Percona
# Authors: Roman Vynar
ITEM=$1
HOST=localhost
DIR=`dirname $0`
CMD="/usr/bin/php -q $DIR/ss_get_mysql_stats.php --host $HOST --items gg"
CACHEFILE="/tmp/$HOST-mysql_cacti_stats.txt"
if [ "$ITEM" = "running-slave" ]; then
# Check for running slave
RES=`HOME=~zabbix mysql -uzabbix -p123456 -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','` #只用修改这行,添加-u -p参数就行。指定用户和密码
if [ "$RES" = " Yes, Yes," ]; then
echo 1
else
echo 0
fi
exit
elif [ -e $CACHEFILE ]; then
# Check and run the script
TIMEFLM=`stat -c %Y /tmp/$HOST-mysql_cacti_stats.txt`
TIMENOW=`date +%s`
if [ `expr $TIMENOW - $TIMEFLM` -gt 300 ]; then
rm -f $CACHEFILE
$CMD 2>&1 > /dev/null
fi
else
$CMD 2>&1 > /dev/null
fi
# Parse cache file
if [ -e $CACHEFILE ]; then
cat $CACHEFILE | sed 's/ /\n/g; s/-1/0/g'| grep $ITEM | awk -F: '{print $2}'
else
echo "ERROR: run the command manually to investigate the problem: $CMD"
fi
vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
$mysql_user = 'zabbix'; #修改30行
$mysql_pass = '123456'; #修改31行
percona提供的xml模板是根据2.0写的,不适用4.0版本需要修改,我已经修改好了,下载地址:
百度网盘:https://pan.baidu.com/s/1rYyIdmE6MBKKDNGWzdUj6Q
密码:aaf0
打开php-fpm的pool配置文件,删除pm.status=指令的注释:
pm.status_path = /php-fpm_status
把如下配置添加到nginx配置文件:
server {
listen 10061;
location /php-fpm_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
重启php-fpm和nginx后,尝试执行如下命令测试:
curl http://127.0.0.1:10061/php-fpm_status
增加自定义key
cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/php-fpm_status.conf
UserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1:10061/php-fpm_status?xml" | grep "<$1>" | awk -F'>|<' '{print $$3}'
重启zabbix客户端
百度网盘:https://pan.baidu.com/s/1rYyIdmE6MBKKDNGWzdUj6Q
密码:aaf0
https://www.cnblogs.com/hy007x/p/7736403.html
https://github.com/mjwtc0722/zabbix-lvs-status
https://blog.51cto.com/3241766/2117521
被动模式:此模式为zabbix默认的工作模式,由zabbix server 向zabbix agent 发出指令获取数据,zabbix agent被动地去获取数据并返回给zabbix server,zabbix server会周期性地向agent索取数据。此模式的最大问题就是会增加zabbix server的工作量,在大量的服务器环境下,zabbix server不能及时获取到最新的数据。
主动模式:即由zabbix agent 主动采集数据并返回给zabbix server,不需要zabbix server 的另行干预,因此使用主动模式能在一定程序上减轻zabbix server的压力。
[root@k8s-n1 ~]# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0 #不做日志切分
StartAgents=0 #关闭被动模式
ServerActive=10.201.1.145 #指定server端IP地址
HostnameItem=system.hostname #server端一会需要填写这个地方的主机名,默认是获取本机主机名
Include=/etc/zabbix/zabbix_agentd.d/*.conf
systemctl restart zabbix-agent
先完全克隆这个模板,重命名为Template OS Linux active
批量更新items
将这个模板的连接的模板也改为主动模式,方法同上
将自动发现,也改为主动模式,并把里面的items改为主动
查看获取到的数据
zabbix获取数据时有时会出现超时,如果一些数据需要执行比较长的时间才能获取的话,那么zabbix会出现异常,考虑到这种情况,zabbix增加了Trapper功能,客户端自己提交数据给zabbix。
trapper是被监控主机主动发送数据给zabbix server,与主动模式的区别是不需要安装客户端;trapper方式发送数据是以主机名处理,不是IP地址,所以主机名要唯一。
tapper工作模式中,使用zabbix监控类型zabbix trapper(可以称为zabbix捕捉器),在zabbix服务器上必须有一个捕捉项目,然后需要配合zabbix_sender把数据推送给zabbix服务器,该程序由zabbix发行版自带
zabbix_sender是一个命令行工具,可以用来发送Zabbix服务器处理性能数据。该工具通常用于长时间运行的用户脚本,用于定期发送可用性和性能数据。
yum -y install zabbix-sender.x86_64
[root@k8s-n1 ~]# zabbix_sender
usage: zabbix_sender [-Vhv] {[-zpsI] -ko | [-zpI] -T -i -r} [-c ]
参数说明:
-c --config 配置文件绝对路径
-z --zabbix-server zabbix server的IP地址
-p --port zabbix server端口.默认10051
-s --host zabbix 管理界面指定的主机名
-I --source-address 源IP
-k --key 监控项的key
-o --value key值
-i --input-file 从文件里面读取hostname、key、value 一行为一条数据,使用空格作为分隔符,如果主机名带空格,那么请使用双引号包起来
-T --with-timestamps 一行一条数据,空格作为分隔符: ,配合 --input-file option,timestamp为unix时间戳
-r --real-time 将数据实时提交给服务器
-v --verbose 详细模式, -vv 更详细
创建主机
创建监控项
[root@k8s-n1 ~]# zabbix_sender -z 10.201.1.145 -s trapper -k trapper-test -o 133
info from server: "processed: 1; failed: 0; total: 1; seconds spent: 0.000113"
sent: 1; skipped: 0; total: 1
查看数据
通过zabbix_sender可以批量传递key值,可以创建一个文本,每行定义一个key值,可以使用不同的主机名以及key、key值。
创建一个文本
[root@k8s-n1 ~]# cat 1.txt
trapper trapper-test 10
trapper trapper-test 12
trapper trapper-test 15
trapper trapper-test 17
trapper trapper-test 19
trapper trapper-test 13
trapper trapper-test 16
trapper trapper-test 10
[root@k8s-n1 ~]# zabbix_sender -z 10.201.1.145 -s trapper -i 1.txt -vv
zabbix_sender [5007]: DEBUG: answer [{"response":"success","info":"processed: 8; failed: 0; total: 8; seconds spent: 0.000334"}]
info from server: "processed: 8; failed: 0; total: 8; seconds spent: 0.000334"
sent: 8; skipped: 0; total: 8
zabbix web端查看数据
假如有100台服务器,不想一台台主机去添加,能不能让zabbix自动添加主机呢?当然可以,网络发现便是满足这个需求的功能,当然前提条件是所有服务器都已经安装了agent或者snmp(其实也可以不用,鉴于我们大部分功能都用agent,所以请安装上agent),server端(由server端发起的)扫描配置好的ip段,自动添加host,自动给host link模板,自动加到主机组里等等。网络发现功能让我们能更快速的部署zabbix、简化zabbix管理、并且在经常变动的环境里面也不需要花太多的精力。
[root@k8s-n2 ~]# 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.201.1.145
ServerActive=10.201.1.145
HostnameItem=system.hostname
HostMetadata=web
Include=/etc/zabbix/zabbix_agentd.d/*.conf
自动注册由客户端主动发起,客户端必须安装并启动Agentd,否则无法被自动注册添加至主机列表。功能跟网络发现一样,但自动注册的效率比网络发现的高。
配置文件内容保持跟网络一样即可
1.删除k8s-n2
2.停用网络发现
3.添加自动注册动作规则
Zabbix的网络发现是指zabbix server通过配置好的规则,自动添加host,group,template
Zabbix的主动注册刚好和网络发现是相反的,功能基本一致。zabbix agent主动联系zabbix server,server自动添加host,group,template
以上两种方式都是发现host,添加host,而low-level discovery(低级自动发现)更加底层,用于发现item,trigger,graph等等
[root@linux-node2 ~]# mkdir /data/{3307,3308} -p
[root@linux-node2 ~]# chown -R mysql.mysql /data
[root@linux-node2 ~]# cp /etc/my.cnf /etc/my3307.cnf
[root@linux-node2 ~]# vim /etc/my3307.cnf
[mysqld]
datadir=/data/3307
socket=/data/3307/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port=3307
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/data/3307/mariadb.log
pid-file=/data/3307/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@linux-node2 ~]# cp /etc/my3307.cnf /etc/my3308.cnf
[root@linux-node2 ~]# vim /etc/my3308.cnf
[mysqld]
datadir=/data/3308
socket=/data/3308/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port=3308
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/data/3308/mariadb.log
pid-file=/data/3308/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf
[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &
[root@linux-node2 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf
[root@linux-node2 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &
[root@linux-node2 ~]# netstat -tulnp |grep mysql|awk -F "[ :]+" '{print $4}'
3307
3308
3306
vim discovery_mysql.sh
#!/bin/bash
#mysql low-level discovery
res=`sudo netstat -lntp|awk -F "[ :\t]+" '/mysqld/{print$4}'`
port=($res)
printf '{'
printf '"data":['
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"},"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf '{'
printf "\"{#MYSQLPORT}\":\"${port[${key}]}\"}"
fi
done
printf ']'
printf '}\n'
sh discovery_mysql.sh |python -m json.tool #采用json格式显示
{
"data": [
{
"{#MYSQLPORT}": "3307"
},
{
"{#MYSQLPORT}": "3308"
},
{
"{#MYSQLPORT}": "3306"
}
]
}
[root@linux-node2 zabbix_agentd.d]# vim discovery_mysql.conf
UserParameter=discovery_mysql,/bin/bash /etc/zabbix/zabbix_agentd.d/scripts/discovery_mysql.sh
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql #测试server端获取数据
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
{"data":[]}
#这里报错是因为netstat -tulnp的参数-p在执行时需要root的权限,这里使用suid的方式进行授权
[root@linux-node2 ~]# chmod u+s `which netstat`
[root@linux-node2 ~]# ll `which netstat`
-rwsr-xr-x. 1 root root 155000 8月 3 17:17 /usr/bin/netstat
*或者
sed -i '98a zabbix\tALL=(ALL)\tNOPASSWD: /bin/netstat' /etc/sudoers
sed -i 's@^Defaults requiretty@#Defaults requiretty@g' /etc/sudoers*
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k discovery_mysql
{"data":[{"{#MYSQLPORT}":"3307"},{"{#MYSQLPORT}":"3308"},{"{#MYSQLPORT}":"3306"}]}
[root@linux-node2 zabbix_agentd.d]# cat userparameter_mysql.conf
UserParameter=mysql.status[*],echo "show global status where Variable_name='$2';" | HOME=/var/lib/zabbix mysql -uroot -p123456 -h 127.0.0.1 -P $1 -N | awk '{print $$2}'
修改不同端口的mysql密码:
[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3306
[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3307
[root@linux-node2 zabbix_agentd.d]# mysqladmin -h 127.0.0.1 -uroot password '123456' -P3308
测试是否正常
[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3306 -e "use mysql;show GLOBAL VARIABLES like 'port';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3307 -e "use mysql;show GLOBAL VARIABLES like 'port';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3307 |
+---------------+-------+
[root@linux-node2 zabbix_agentd.d]# mysql -uroot -p123456 -h 127.0.0.1 -P3308 -e "use mysql;show GLOBAL VARIABLES like 'port';"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3308 |
+---------------+-------+
zabbix_get测试是否能正常获取数据:
[root@linux-node1 ~]# zabbix_get -s linux-node2 -k mysql.status[3306,Bytes_sent]
1808
这里直接在linux-node2上进行创建
"Configuration"-->"Host"-->"linux-node2"-->"Discovery"-->"Create discovery rule"-->"Add"
创建 Item prototypes,其实就是一个创建一个 item
Item prototypes (0)>>create Item prototypes,按照官方模板来配置。这里配5个展示
再查看Host中的item项,会多出以下监控项:
查看监控最新数据图表,即可看到3306、3307、3308的数据库状态信息:
转载于:https://blog.51cto.com/lingxudong/2367765