Go to content Go to menu Go to search

Мониторинг SMART жестких дисков в Linux при помощи Zabbix

Скрипты и шаблон для мониторинга параметров SMART при помощи zabbix.

Создаем скрипт автобнаружения дисков /etc/zabbix/scripts/drives.discovery:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#! /usr/bin/perl
use strict;

my @drives;
my @lines = `lsblk --output KNAME,TYPE,STATE  --list`;

foreach my $line (@lines){
    chomp $line;
    if ($line =~ /^(\S+)\s+disk\s+running/) {push (@drives,$1);}
}

print '{"data":[';
my $i=0;
foreach my $drive (@drives){
    print "," if $i > 0;
    print '{"{#DRIVENAME}":"',$drive,'"}';
    $i++;
}
print ']}';
print "\n";

Добавляем пользовательский параметр drives.discovery в zabbix-agent

echo "UserParameter=drives.discovery, /etc/zabbix/scripts/drives.discovery" > /etc/zabbix/zabbix_agentd.d/userparameter_drives.discovery.conf
systemctl restart zabbix-agent

Создадим скрипты для отправки параметров smart на zabbix-server

Скрипт /etc/zabbix/scripts/drives.drives.smart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/perl
use strict;

my $ZABBIX_SENDER='/usr/bin/zabbix_sender';
my $ZABBIX_CONF='/etc/zabbix/zabbix_agentd.conf';
my $debug = 0;

# Find drives

my @drives;
my @lines = `/bin/lsblk --output KNAME,TYPE,STATE  --list`;

foreach my $line (@lines){
    chomp $line;
    if ($line =~ /^(\S+)\s+disk\s+running/) {push (@drives,$1);}
}

# Send smart to zabbix
foreach my $drive (@drives) {
    my $health='ERROR WHILE GETTING SMART';
    my @params=('Start_Stop_Count','Reallocated_Sector_Ct','Power_On_Hours','Power_Cycle_Count','Current_Pending_Sector');

    my @smarts = `/usr/sbin/smartctl -a /dev/$drive`;
    # parse lines and find data
    foreach my $smart (@smarts) {
        chomp $smart;
        # health
        if ($smart =~ /.+overall-health.+result:\s+(\S+)$/) { $health=$1; next;}
        # Other params
        foreach my $param (@params) {
        my $value = &parse_smart ($smart,$param);
        &zabbix_send ($drive,$param,$value) if defined $value;
    } 
    }
    # send health parameter to zabbix
    &zabbix_send ($drive,'health',$health);
}

sub parse_smart {
    my $smart = shift;
    my $param = shift;
    return $1 if $smart =~ /.+$param.+\s+(\d+)$/;
    return undef;
}

sub zabbix_send {
    my $drive = shift;
    my $key = shift;
    my $value = shift;
    `$ZABBIX_SENDER -c $ZABBIX_CONF -k smart.$key\[$drive\] -o "$value"`;
    print "$drive\t$key\t$value\n" if $debug;
}

Скрипт /etc/zabbix/scripts/drives.drives.smart.temperature:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /usr/bin/perl
use strict;

my $ZABBIX_SENDER='/usr/bin/zabbix_sender';
my $ZABBIX_CONF='/etc/zabbix/zabbix_agentd.conf';
my $debug = 0;

# Find drives

my @drives;
my @lines = `/bin/lsblk --output KNAME,TYPE,STATE  --list`;

foreach my $line (@lines){
    chomp $line;
    if ($line =~ /^(\S+)\s+disk\s+running/) {push (@drives,$1);}
}

# Send smart to zabbix
foreach my $drive (@drives) {
    # send drive temperature to zabbix
    my $temp = `/usr/sbin/smartctl -a /dev/$drive | grep '194 Temperature' | awk '{print \$10}'`;
    chomp $temp;
    `$ZABBIX_SENDER -c $ZABBIX_CONF -k smart.Temperature\[$drive\] -o "$temp"` if $temp;
    print "$drive\tsmart.Temperature\t$temp\n" if $debug;
}

Добавим скрипты в запуск по cron (drives.smart каждый час, drives.smart.temperature каждую минуту):

echo "PATH=/sbin:/usr/sbin:/bin:/usr/bin
1 */1 * * * root /etc/zabbix/scripts/drives.smart
*/1 * * * * root /etc/zabbix/scripts/drives.smart.temperature" > /etc/cron.d/zabbix

Шаблон для zabbix: Template_HDD-SMART-LINUX.xml

Enjoy!


при публикации материалов с данного сайта обратная ссылка на сайт обязательна.
valynkin.ru © no rights reserved