自动发现磁盘不同的设备,如sdb,xda,vsda等通过脚本获取系统块设备的名称,然后保存到变量中,在通过zabbix客户端的自定义监控选项将监控的键值传给服务端
创建脚本目录:mkdir /etc/zabbix/scripts/
创建搜索脚本:
[root@localhost scripts]# cat disk.pl
#!/usr/bin/perl
#give disk dmname, returns Proxmox VM name
sub get_vmname_by_id
{
$vmname=`cat /etc/qemu-server/$_[0].conf | grep name | cut -d : -f 2`;
$vmname =~ s/^s+//; #remove leading spaces
$vmname =~ s/s+$//; #remove trailing spaces
return $vmname
}
$first = 1;
print "{n";
print "t"data":[nn";
for (`cat /proc/diskstats`)
{
($major,$minor,$disk) = m/^s*([0-9]+)s+([0-9]+)s+(S+)s.*$/;
$dmnamefile = "/sys/dev/block/$major:$minor/dm/name";
$vmid= "";
$vmname = "";
$dmname = $disk;
$diskdev = "/dev/$disk";
#DM name
if (-e $dmnamefile) {
$dmname = `cat $dmnamefile`;
$dmname =~ s/n$//; #remove trailing n
$diskdev = "/dev/mapper/$dmname";
# VM name and ID
if ($dmname =~ m/^.*--([0-9]+)--.*$/) {
$vmid = $1;
#$vmname = get_vmname_by_id($vmid);
}
}
#print("$major $minor $disk $diskdev $dmname $vmid $vmname n");
print "t,n" if not $first;
$first = 0;
print "t{n";
print "tt"{#DISK}":"$disk",n";
print "tt"{#DMNAME}":"$dmname",n";
print "tt"{#VMNAME}":"$vmname",n";
print "tt"{#VMID}":"$vmid"n";
print "t}n";
}
print "nt]n";
print "}n";
[root@localhost scripts]# cat /etc/zabbix/scripts/
disk.pl fdisk.sh ls/
[root@localhost scripts]# cat /etc/zabbix/scripts/disk.pl
#!/usr/bin/perl
#give disk dmname, returns Proxmox VM name
sub get_vmname_by_id
{
$vmname=`cat /etc/qemu-server/$_[0].conf | grep name | cut -d : -f 2`;
$vmname =~ s/^s+//; #remove leading spaces
$vmname =~ s/s+$//; #remove trailing spaces
return $vmname
}
$first = 1;
print "{n";
print "t"data":[nn";
for (`cat /proc/diskstats`)
{
($major,$minor,$disk) = m/^s*([0-9]+)s+([0-9]+)s+(S+)s.*$/;
$dmnamefile = "/sys/dev/block/$major:$minor/dm/name";
$vmid= "";
$vmname = "";
$dmname = $disk;
$diskdev = "/dev/$disk";
#DM name
if (-e $dmnamefile) {
$dmname = `cat $dmnamefile`;
$dmname =~ s/n$//; #remove trailing n
$diskdev = "/dev/mapper/$dmname";
# VM name and ID
if ($dmname =~ m/^.*--([0-9]+)--.*$/) {
$vmid = $1;
#$vmname = get_vmname_by_id($vmid);
}
}
#print("$major $minor $disk $diskdev $dmname $vmid $vmname n");
print "t,n" if not $first;
$first = 0;
print "t{n";
print "tt"{#DISK}":"$disk",n";
print "tt"{#DMNAME}":"$dmname",n";
print "tt"{#VMNAME}":"$vmname",n";
print "tt"{#VMID}":"$vmid"n";
print "t}n";
}
print "nt]n";
print "}n";
[root@localhost scripts]#
[root@localhost scripts]# chown -R zabbix.zabbix /etc/zabbix/scripts/
[root@localhost scripts]# chmod 755 /etc/zabbix/scripts/disk.pl
添加key
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
# diskio discovery
UserParameter=discovery.disks.iostats,/etc/zabbix/scripts/disk.pl
#读扇区的次数
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'
#写扇区次数
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
#合并读完成次数
UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 |awk '{print $$4}'
#合并写完成次数
UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}'
#读花费的毫秒数
UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}'
#写操作花费的毫秒数
UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}'
zabbix-server test
[root@ds fonts]# zabbix_get -s 192.168.158.130 -k custom.vfs.dev.write.ms[sda1]
35110
[root@ds fonts]# zabbix_get -s 192.168.158.130 -k custom.vfs.dev.write.ms[sda2]
1070115
[root@ds fonts]# zabbix_get -s 192.168.158.130 -k custom.vfs.dev.write.ms[sda3]
0
导入模板:链接: 密码:isij修改如图:最终效果图