9.5、zabbix高级操作(1)
阅读原文时间:2023年07月11日阅读:2

在zabbix-web中删除之前所有监控的主机;

1、zabbix自动发现配置:

zabbix-server通过扫描指定范围的ip地址发现zabbix-agent并自动添加监控主机,适用于zabbix-agent的被动模式,会增加zabbix-server的负担;

(1)zabbix-agent参数配置:

[root@slave-node1 ~]# vim /etc/zabbix/zabbix_agentd.conf

Server=172.16.1.90,172.16.1.91

ListenPort=10050

ListenIP=172.16.1.91

StartAgents=3

Timeout=30

[root@slave-node1 ~]# systemctl restart zabbix-agent.service

(2)修改zabbix-server所在服务器的/etc/hosts/文件:

[root@controller-node1 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.1.91 slave-node1

说明:如果不修改的话会导致主动发现添加的监控主机名为ip地址而不是被监控主机实际的主机名称;

(3)在zabbix-web添加自动发现规则:

1)

2)

3)

4)

5)

2、zabbix主动注册:

zabbix-agent向zabbix-server发送主机信息,zabbix-server根据主机信息完成zabbix-agent监控主机的添加,适用

于zabbix-agent的主动模式,会减轻zabbix-server的负担;

删除上面实验已添加的自动发现的主机,并关闭自动发现规则;

(1)zabbix-agent参数配置:

[root@slave-node1 ~]# vim /etc/zabbix/zabbix_agentd.conf

StartAgents=0

#完全关闭zabbix-agent的被动模式,关闭监听的ip地址和10050端口;

ServerActive=172.16.1.90

#开启zabbix-agent主动模式,指向zabbix-server的IP地址;

HostnameItem=system.hostname

#zabbix-agent主动模式下指定添加监控主机的名称为服务器的名称;

HostMetadata=web

#zabbix-agent主动模式下主动注册的主机元素;

Timeout=30

[root@slave-node1 ~]# systemctl restart zabbix-agent.service

(2)创建zabbix-agent主动模式所需的Template OS Linux Active模板:

1)

2)

3)

4)

5)

6)

7)

8)

9)

10)

11)

(3)创建Template OS linux Active 链接的模板 Template App Zabbix Agent 为主动模式:

1)

2)

3)

4)

5)

6)

7)

8)

9)

10)

11)

12)

(4)创建自动注册规则:

1)添加服务器群组:

2)

3)

4)

(5)查看自动注册的主机:

(6)查看注册主机最新数据:

3、zabbix低级自动发现:

可以自动创建item,trigger,图形,在这里使用zabbix-agent的主动方式(被动也可);

(1)模板中自带的自动发现规则,以文件系统为例:

1)[root@slave-node1 ~]# zabbix_agentd -p | grep vfs.fs.discovery

vfs.fs.discovery [s|{"data":[{"{#FSNAME}":"/","{#FSTYPE}":"rootfs"},{"{#FSNAME

}":"/sys","{#FSTYPE}":"sysfs"},{"{#FSNAME}":"/proc","{#FSTYPE}":"proc"},{"{#FSNAME}":"/dev","{#FSTYPE}":"devtmpfs"},{"{#FSNAME}":"/sys/kernel/security","{#FSTYPE}":"securityfs"},{"{#FSNAME}":"/dev/shm","{#FSTYPE}":"tmpfs"},{"{#FSNAME}":"/dev/pts","{#FSTYPE}":"devpts"},{"{#FSNAME}":"/run","{#FSTYPE}":"tmpfs"},{"{#FSNAME}":"/sys/fs/cgroup","{#FSTYPE}":"tmpfs"},{"{#FSNAME}":"/sys/fs/cgroup/systemd","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/pstore","{#FSTYPE}":"pstore"},{"{#FSNAME}":"/sys/fs/cgroup/cpu,cpuacct","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/net_cls,net_prio","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/cpuset","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/memory","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/devices","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/perf_event","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/freezer","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/pids","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/blkio","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/fs/cgroup/hugetlb","{#FSTYPE}":"cgroup"},{"{#FSNAME}":"/sys/kernel/config","{#FSTYPE}":"configfs"},{"{#FSNAME}":"/","{#FSTYPE}":"xfs"},{"{#FSNAME}":"/proc/sys/fs/binfmt_misc","{#FSTYPE}":"autofs"},{"{#FSNAME}":"/dev/mqueue","{#FSTYPE}":"mqueue"},{"{#FSNAME}":"/dev/hugepages","{#FSTYPE}":"hugetlbfs"},{"{#FSNAME}":"/sys/kernel/debug","{#FSTYPE}":"debugfs"},{"{#FSNAME}":"/boot","{#FSTYPE}":"xfs"},{"{#FSNAME}":"/proc/sys/fs/binfmt_misc","{#FSTYPE}":"binfmt_misc"},{"{#FSNAME}":"/run/user/0","{#FSTYPE}":"tmpfs"}]}]

2)

3)

4)自动创建的item配置:

5)自动发现正则表达式填写的位置:

(2)创建mysql多实例低级自动发现规则:

1)创建mysql多实例:

在salve-node1上已经使用yum安装了mariadb数据库:

#配置mysql多实例文件:

[root@slave-node1 ~]# mkdir -p /data/3307/

[root@slave-node1 ~]# mkdir -p /data/3308/

[root@slave-node1 ~]# chown -R mysql.mysql /data/3307/

[root@slave-node1 ~]# chown -R mysql.mysql /data/3308/

[root@slave-node1 ~]# cp -a /etc/my.cnf /etc/my3307.cnf

[root@slave-node1 ~]# cp -a /etc/my.cnf /etc/my3308.cnf

[root@slave-node1 ~]# egrep -v "^#|^$" /etc/my3307.cnf

[mysqld]

datadir=/data/3307

socket=/data/3307/mysql.sock

port=3307

symbolic-links=0

[mysqld_safe]

log-error=/data/3307/mariadb.log

pid-file=/data/3307/mariadb.pid

!includedir /etc/my.cnf.d

[root@slave-node1 ~]# egrep -v "^#|^$" /etc/my3308.cnf

[mysqld]

datadir=/data/3308/mysql

socket=/data/3308/mysql.sock

symbolic-links=0

port=3308

[mysqld_safe]

log-error=/data/3308/mariadb.log

pid-file=/data/3308/mariadb.pid

!includedir /etc/my.cnf.d

#初始化mysql多实例:

[root@slave-node1 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3307.cnf

[root@slave-node1 ~]# mysql_install_db --user=mysql --defaults-file=/etc/my3308.cnf

#启动mysql多实例:

[root@slave-node1 ~]# mysqld_safe --defaults-file=/etc/my3307.cnf &

[root@slave-node1 ~]# mysqld_safe --defaults-file=/etc/my3308.cnf &

[root@slave-node1 ~]# netstat -tunlp -4 | grep 330

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1273/mysqld

tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 6794/mysqld

tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 6608/mysqld

#为mysql多实例创建新的密码:

[root@slave-node1 ~]# mysqladmin -S /data/3307/mysql.sock -uroot password '123456'

[root@slave-node1 ~]# mysqladmin -h127.0.0.1 -P3308 -uroot password '123456'

#创建mysql多实例监控的用户:

[root@slave-node1 ~]# mysql -h127.0.0.1 -P3307 -uroot -p123456

MariaDB [(none)]> show global variables like 'port';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3307 |

+---------------+-------+

1 row in set (0.00 sec)

MariaDB [(none)]> grant select on *.* to zabbix@"localhost" identified by "123456";

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> exit;

[root@slave-node1 ~]# mysql -h127.0.0.1 -P3308 -uroot -p123456

MariaDB [(none)]> show global variables like 'port';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port | 3308 |

+---------------+-------+

1 row in set (0.00 sec)

MariaDB [(none)]> grant select on *.* to zabbix@"localhost" identified by "123456";

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> exit;

2)编写mysql多实例低级自动发现脚本:

[root@slave-node1 ~]# netstat -tunlp -4 | grep mysqld | awk -F "[ :]+" '{print $5}'

3306

3307

3308

[root@slave-node1 ~]# cat /etc/zabbix/zabbix_agentd.d/discovery-mysql.sh

#!/bin/bash

# ###################################################

# Script-Name: discovery-mysql.sh

# Revision: 1.0

# Date:

# Author:

# Email:

# Website:

#####################################################

res=`netstat -tunlp -4 | grep mysqld | awk -F "[ :]+" '{print $5}'`

port=($res)

printf '{\n'

printf '\t"data":[\n'

for key in ${!port[@]}

do

if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then

printf '\t {\n'

printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n"

else [[ "${key}" -eq "((${#port[@]}-1))" ]]

printf '\t {\n'

printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n"

fi

done

printf '\t ]\n'

printf '}\n'

[root@slave-node1 ~]# sh /etc/zabbix/zabbix_agentd.d/discovery-mysql.sh | python -m json.tool

{

"data": [

{

"{#MYSQLPORT}": "3306"

},

{

"{#MYSQLPORT}": "3307"

},

{

"{#MYSQLPORT}": "3308"

}

]

}

[root@slave-node1 ~]# cat /etc/zabbix/zabbix_agentd.d/discovery-mysql.conf

UserParameter=discovery-mysql,sh /etc/zabbix/zabbix_agentd.d/discovery-mysql.sh

#由于netstat命令zabbix用户无法使用,需要赋予该命令s权限位:

[root@slave-node1 ~]# chmod u+s /usr/bin/netstat

3)重启zabbix-agent:

[root@slave-node1 ~]# systemctl restart zabbix-agent.service

4)使用"zabbix_get"命令测试:

zabbix_get命令是被动模式,现在zabbix-agent使用的是主动模式所以需要在zabbix-agent配置文件中打开被动模式;

[root@slave-node1 ~]# zabbix_get -s 172.16.1.91 -k discovery-mysql

{

"data":[

{

"{#MYSQLPORT}":"3306"},

{

"{#MYSQLPORT}":"3307"},

{

"{#MYSQLPORT}":"3308"}

]

}

5)创建mysql多实例低级自动现item原型脚本:

#修改zabbix-agent自带的mysql监控配置文件:

[root@slave-node1 ~]# egrep -v "^#|^$" /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

UserParameter=mysql.status[*],echo "show global status where Variable_name='$4';" | HOME=/var/lib/zabbix mysql -u$1 -p$2 -h127.0.0.1 -P$3 -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 -h127.0.0.1 -P$3 ping | grep -c alive

UserParameter=mysql.version[*],mysql -u$1 -p$2 -h127.0.0.1 -P$3 -V

6)重启zabbix-agent:

[root@slave-node1 ~]# systemctl restart zabbix-agent.service

7)测试:

[root@slave-node1 ~]# zabbix_get -s 172.16.1.91 -k mysql.status[zabbix,123456,3308,Uptime]

16922

[root@slave-node1 ~]# zabbix_get -s 172.16.1.91 -k mysql.status[zabbix,123456,3307,Uptime]

16918

8)在zabbix-web界面中创建mysql多实例低级自动发现规则:

A、

B、

9)创建mysql多实例低级自动现item、触发器、图像原型(模仿系统中自带的mysql模板):

A、item原型:

a、

b、

B、触发器原型:

C、图像原型:

10)测试:

A、查看最新数据:

B、触发器:

C、图像: