Möchten Sie lernen, wie eine Zabbix-Datenbankpartitionierung durchgeführt wird? In diesem Tutorial zeigen wir Ihnen, wie Sie die Zabbix MySQL-Datenbank auf einem Computer mit Ubuntu Linux partitionieren.

• Zabbix-Version: 4.0.5
• Linux: Ubuntu 18.04
• MySQL: Ver 14.14 Distrib 5.7.25

In diesem Tutorial wird eine brandneue Zabbix-Installation behandelt.

Hardware-Liste:

Der folgende Abschnitt enthält eine Liste der zum Erstellen dieses Zabbix-Lernprogramms verwendeten Geräte.

Alle oben aufgeführten Hardwarekomponenten finden Sie auf der Amazon-Website.

Zabbix Playlist:

Auf dieser Seite bieten wir schnellen Zugriff auf eine Liste von Videos, die sich auf die Zabbix-Installation beziehen.

Vergessen Sie nicht, unseren Youtube-Kanal mit dem Namen zu abonnieren FKIT.

Tutorial - Zabbix-Datenbankpartitionierung

Verwenden Sie den folgenden Befehl, um die erforderliche Software zu installieren.

# apt-get update
# apt-get install perl libdbi-perl libdatetime-perl libdbd-mysql-perl

Wir müssen die folgenden Tabellen aus der Zabbix-Datenbank partitionieren:

• history
• history_log
• history_str
• history_text
• history_uint
• trends
• trends_uint

Ihr Zabbix-Server verfügt wahrscheinlich bereits über Informationen aus der Überwachung in seiner Datenbank.

Greifen Sie auf die MySQL-Konsole zu und überprüfen Sie das älteste Eintragungsdatum von Zabbix für die 7 oben aufgeführten Tabellen:

# mysql -u root -p

use zabbix;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `history`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `history_log`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `history_str`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `history_text`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `history_uint`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `trends`;
SELECT FROM_UNIXTIME(MIN(clock)) FROM `trends_uint`;

Für jeden dieser MySQL-Befehle sollte die Ausgabe etwa folgendermaßen aussehen:

+---------------------------------------+
| FROM_UNIXTIME(MIN(clock)) |
+---------------------------------------+
| 2019-03-22 17:48:07                       |
+---------------------------------------+
1 row in set (0.00 sec)

Wichtig! Notieren Sie sich das angezeigte Datum für jede der Tabellen.

Lassen Sie uns nun jede der Verlaufstabellen nach Datum partitionieren:

Zuerst partitionieren wir die History-Tabelle.

# mysql -u root -p
# use zabbix;

ALTER TABLE `history` PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (UNIX_TIMESTAMP("2019-03-23 00:00:00")) ENGINE = InnoDB);

In unserem Beispiel enthält eine Datei mit dem Namen p2019_03_22 vor dem 23.03.2017 alle MySQL-Informationen.

Wenn Ihr Zieldatum 2019-03-22 ist, müssen Sie alles vor dem 2019-03-23 eingeben.

Zweitens partitionieren wir die history_log-Tabelle.

ALTER TABLE `history_log` PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (UNIX_TIMESTAMP("2019-03-23 00:00:00")) ENGINE = InnoDB);

Drittens partitionieren wir die history_str-Tabelle.

ALTER TABLE `history_str` PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (UNIX_TIMESTAMP("2019-03-23 00:00:00")) ENGINE = InnoDB);

Viertens partitionieren wir die history_text-Tabelle.

ALTER TABLE `history_text` PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (UNIX_TIMESTAMP("2019-03-23 00:00:00")) ENGINE = InnoDB);

Fünftens wollen wir die history_uint-Tabelle partitionieren.

ALTER TABLE `history_uint` PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (UNIX_TIMESTAMP("2019-03-23 00:00:00")) ENGINE = InnoDB);

Nun lassen Sie uns jede der Trends Table nach Monat aufteilen:

Lassen Sie uns zuerst die Trends-Tabelle partitionieren.

ALTER TABLE `trends` PARTITION BY RANGE ( clock)
(PARTITION p2019_03 VALUES LESS THAN (UNIX_TIMESTAMP("2019-04-01 00:00:00")) ENGINE = InnoDB);

In unserem Beispiel enthält eine Datei mit dem Namen p2019_03 vor dem 01.09.2009 alle MySQL-Informationen.

Wenn Ihr Zieldatum 2019-03 ist, müssen Sie alles vor 2019-04-01 eingeben.

Zweitens partitionieren wir die Tabelle trends_uint.

ALTER TABLE `trends_uint` PARTITION BY RANGE ( clock)
(PARTITION p2019_03 VALUES LESS THAN (UNIX_TIMESTAMP("2019-04-01 00:00:00")) ENGINE = InnoDB);

Zabbix-Datenbankpartitionen wurden erfolgreich erstellt.

Tutorial - Zabbix-Datenbankpartitionsskript

Wir werden ein Perl-Skript verwenden, um die folgenden Aufgaben auszuführen:

Erstellen Sie 10 Tage lang eine MySQL-Datenbankpartition, damit der Dienst nicht angehalten wird.

Löschen Sie ältere MySQL-Partitionen, die nicht mehr benötigt werden.

# mkdir /downloads/zabbix_script
# cd /downloads/zabbix_script
# vi zabbix_partition_creator.pl

Hier ist der Inhalt des Skripts zabbix_partition_creator.pl:

use strict;
use Data::Dumper;
use DBI;
use Sys::Syslog qw(:standard :macros);
use DateTime;
use POSIX qw(strftime);
openlog("mysql_zbx_part", "ndelay,pid", LOG_LOCAL0);
my $db_schema = 'zabbix';
my $dsn = 'DBI:mysql:'.$db_schema.':mysql_socket=/var/run/mysqld/mysqld.sock';
my $db_user_name = 'zabbix';
my $db_password = 'kamisama123';
my $tables = { 'history' => { 'period' => 'day', 'keep_history' => '30'},
'history_log' => { 'period' => 'day', 'keep_history' => '30'},
'history_str' => { 'period' => 'day', 'keep_history' => '30'},
'history_text' => { 'period' => 'day', 'keep_history' => '30'},
'history_uint' => { 'period' => 'day', 'keep_history' => '30'},
'trends' => { 'period' => 'month', 'keep_history' => '2'},
'trends_uint' => { 'period' => 'month', 'keep_history' => '2'},
};
my $amount_partitions = 10;
my $curr_tz = 'America/Sao_Paulo';
my $part_tables;
my $dbh = DBI->connect($dsn, $db_user_name, $db_password, {'ShowErrorStatement' => 1});
my $sth = $dbh->prepare(qq{SELECT table_name, partition_name, lower(partition_method) as partition_method,
rtrim(ltrim(partition_expression)) as partition_expression,
partition_description, table_rows
FROM information_schema.partitions
WHERE partition_name IS NOT NULL AND table_schema = ?});
$sth->execute($db_schema);
while (my $row = $sth->fetchrow_hashref()) {
$part_tables->{$row->{'table_name'}}->{$row->{'partition_name'}} = $row;
}
$sth->finish();
foreach my $key (sort keys %{$tables}) {
unless (defined($part_tables->{$key})) {
syslog(LOG_ERR, 'Partitioning for "'.$key.'" is not found! The table might be not partitioned.');
next;
}
create_next_partition($key, $part_tables->{$key}, $tables->{$key}->{'period'});
remove_old_partitions($key, $part_tables->{$key}, $tables->{$key}->{'period'}, $tables->{$key}->{'keep_history'})
}
delete_old_data();
$dbh->disconnect();
sub create_next_partition {
my $table_name = shift;
my $table_part = shift;
my $period = shift;
for (my $curr_part = 0; $curr_part < $amount_partitions; $curr_part++) {
my $next_name = name_next_part($tables->{$table_name}->{'period'}, $curr_part);
my $found = 0;
foreach my $partition (sort keys %{$table_part}) {
if ($next_name eq $partition) {
syslog(LOG_INFO, "Next partition for $table_name table has already been created. It is $next_name");
$found = 1;
}
}
if ( $found == 0 ) {
syslog(LOG_INFO, "Creating a partition for $table_name table ($next_name)");
my $query = 'ALTER TABLE '."$db_schema.$table_name".' ADD PARTITION (PARTITION '.$next_name.
' VALUES less than (UNIX_TIMESTAMP("'.date_next_part($tables->{$table_name}->{'period'}, $curr_part).'") div 1))';
syslog(LOG_DEBUG, $query);
$dbh->do($query);
}
}
}
sub remove_old_partitions {
my $table_name = shift;
my $table_part = shift;
my $period = shift;
my $keep_history = shift;
my $curr_date = DateTime->now;
$curr_date->set_time_zone( $curr_tz );
if ( $period eq 'day' ) {
$curr_date->add(days => -$keep_history);
$curr_date->add(hours => -$curr_date->strftime('%H'));
$curr_date->add(minutes => -$curr_date->strftime('%M'));
$curr_date->add(seconds => -$curr_date->strftime('%S'));
}
elsif ( $period eq 'week' ) {
}
elsif ( $period eq 'month' ) {
$curr_date->add(months => -$keep_history);
$curr_date->add(days => -$curr_date->strftime('%d')+1);
$curr_date->add(hours => -$curr_date->strftime('%H'));
$curr_date->add(minutes => -$curr_date->strftime('%M'));
$curr_date->add(seconds => -$curr_date->strftime('%S'));
}
foreach my $partition (sort keys %{$table_part}) {
if ($table_part->{$partition}->{'partition_description'} <= $curr_date->epoch) {
syslog(LOG_INFO, "Removing old $partition partition from $table_name table");
my $query = "ALTER TABLE $db_schema.$table_name DROP PARTITION $partition";
syslog(LOG_DEBUG, $query);
$dbh->do($query);
}
}
}
sub name_next_part {
my $period = shift;
my $curr_part = shift;
my $name_template;
my $curr_date = DateTime->now;
$curr_date->set_time_zone( $curr_tz );
if ( $period eq 'day' ) {
my $curr_date = $curr_date->truncate( to => 'day' );
$curr_date->add(days => 1 + $curr_part);
$name_template = $curr_date->strftime('p%Y_%m_%d');
}
elsif ($period eq 'week') {
my $curr_date = $curr_date->truncate( to => 'week' );
$curr_date->add(days => 7 * $curr_part);
$name_template = $curr_date->strftime('p%Y_%m_w%W');
}
elsif ($period eq 'month') {
my $curr_date = $curr_date->truncate( to => 'month' );
$curr_date->add(months => 1 + $curr_part);
$name_template = $curr_date->strftime('p%Y_%m');
}
return $name_template;
}
sub date_next_part {
my $period = shift;
my $curr_part = shift;
my $period_date;
my $curr_date = DateTime->now;
$curr_date->set_time_zone( $curr_tz );
if ( $period eq 'day' ) {
my $curr_date = $curr_date->truncate( to => 'day' );
$curr_date->add(days => 2 + $curr_part);
$period_date = $curr_date->strftime('%Y-%m-%d');
}
elsif ($period eq 'week') {
my $curr_date = $curr_date->truncate( to => 'week' );
$curr_date->add(days => 7 * $curr_part + 1);
$period_date = $curr_date->strftime('%Y-%m-%d');
}
elsif ($period eq 'month') {
my $curr_date = $curr_date->truncate( to => 'month' );
$curr_date->add(months => 2 + $curr_part);
$period_date = $curr_date->strftime('%Y-%m-%d');
}
return $period_date;
}
sub delete_old_data {
$dbh->do("DELETE FROM sessions WHERE lastaccess < UNIX_TIMESTAMP(NOW() - INTERVAL 1 MONTH)");
$dbh->do("TRUNCATE housekeeper");
$dbh->do("DELETE FROM auditlog_details WHERE NOT EXISTS (SELECT NULL FROM auditlog WHERE auditlog.auditid = auditlog_details.auditid)");
}

Sie müssen die folgenden Teile des Skripts an Ihre MySQL-Umgebung anpassen.

my $db_schema = 'zabbix';
my $dsn = 'DBI:mysql:'.$db_schema.':mysql_socket=/var/run/mysqld/mysqld.sock';
my $db_user_name = 'zabbix';
my $db_password = 'kamisama123';
my $tables = { 'history' => { 'period' => 'day', 'keep_history' => '30'},
'history_log' => { 'period' => 'day', 'keep_history' => '30'},
'history_str' => { 'period' => 'day', 'keep_history' => '30'},
'history_text' => { 'period' => 'day', 'keep_history' => '30'},
'history_uint' => { 'period' => 'day', 'keep_history' => '30'},
'trends' => { 'period' => 'month', 'keep_history' => '2'},
'trends_uint' => { 'period' => 'month', 'keep_history' => '2'},
};
my $amount_partitions = 10;
my $curr_tz = 'America/Sao_Paulo';

In unserem Beispiel verwenden wir den zabbix MySQL-Benutzernamen und das kamisama123 MySQL-Passwort.

In unserem Beispiel behalten wir 30 Tage Inhalt der Verlaufstabellen.

In unserem Beispiel behalten wir den Inhalt von Trendtabellen für 2 Monate.

In unserem Beispiel legen wir die Zeitzone Amerika / Sao_Paulo-Datum fest.

Jetzt müssen Sie das Skript manuell ohne Fehler ausführen.

# cd /downloads/zabbix_script
# chmod 700 zabbix_partition_creator.pl
# ./zabbix_partition_creator.pl

Greifen Sie auf die MySQL-Konsole zu und vergewissern Sie sich, dass das System die Protokollpartition 10 Tage zuvor erstellt hat.

# mysql -u root -p

use zabbix;
show create table history;

/*!50100 PARTITION BY RANGE ( clock)
(PARTITION p2019_03_22 VALUES LESS THAN (1553310000) ENGINE = InnoDB,
PARTITION p2019_03_23 VALUES LESS THAN (1553396400) ENGINE = InnoDB,
PARTITION p2019_03_24 VALUES LESS THAN (1553482800) ENGINE = InnoDB,
PARTITION p2019_03_25 VALUES LESS THAN (1553569200) ENGINE = InnoDB,
PARTITION p2019_03_26 VALUES LESS THAN (1553655600) ENGINE = InnoDB,
PARTITION p2019_03_27 VALUES LESS THAN (1553742000) ENGINE = InnoDB,
PARTITION p2019_03_28 VALUES LESS THAN (1553828400) ENGINE = InnoDB,
PARTITION p2019_03_29 VALUES LESS THAN (1553914800) ENGINE = InnoDB,
PARTITION p2019_03_30 VALUES LESS THAN (1554001200) ENGINE = InnoDB,
PARTITION p2019_03_31 VALUES LESS THAN (1554087600) ENGINE = InnoDB,
PARTITION p2019_04_01 VALUES LESS THAN (1554174000) ENGINE = InnoDB) */

Greifen Sie auf die MySQL-Konsole zu und überprüfen Sie, ob das System die Trendpartition 10 Monate im Voraus erstellt hat.

# mysql -u root -p

use zabbix;
show create table trends;

/*!50100 PARTITION BY RANGE ( clock)
(PARTITION p2019_03 VALUES LESS THAN (1554087600) ENGINE = InnoDB,
PARTITION p2019_04 VALUES LESS THAN (1556679600) ENGINE = InnoDB,
PARTITION p2019_05 VALUES LESS THAN (1559358000) ENGINE = InnoDB,
PARTITION p2019_06 VALUES LESS THAN (1561950000) ENGINE = InnoDB,
PARTITION p2019_07 VALUES LESS THAN (1564628400) ENGINE = InnoDB,
PARTITION p2019_08 VALUES LESS THAN (1567306800) ENGINE = InnoDB,
PARTITION p2019_09 VALUES LESS THAN (1569898800) ENGINE = InnoDB,
PARTITION p2019_10 VALUES LESS THAN (1572577200) ENGINE = InnoDB,
PARTITION p2019_11 VALUES LESS THAN (1575165600) ENGINE = InnoDB,
PARTITION p2019_12 VALUES LESS THAN (1577844000) ENGINE = InnoDB,
PARTITION p2020_01 VALUES LESS THAN (1580522400) ENGINE = InnoDB) */ |

Lassen Sie uns nun das Perl-Skript so planen, dass es täglich ausgeführt wird.

# crontab -e

1  0  *  *  * /downloads/scripts/zabbix_partition_creator.pl

Herzliche Glückwünsche! Sie haben die Zabbix MySQL Database-Tabellen partitioniert.