Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Mon 3509 add trap regexp match 2 #7679

Merged
merged 8 commits into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bin/generateSqlLite
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ try {
$result_services_template = $stmt->fetchAll(PDO::FETCH_ASSOC);

// get trap info
$stmt = $db_centreon->prepare("SELECT traps_id, traps_oid, traps_status, severity_id, traps_submit_result_enable,
$stmt = $db_centreon->prepare("SELECT traps_id, traps_mode, traps_oid, traps_status, severity_id, traps_submit_result_enable,
traps_execution_command, traps_reschedule_svc_enable, traps_execution_command_enable, traps_args,
traps_routing_mode, traps_routing_value, traps_log, traps_name, traps_exec_method, traps_downtime, traps_routing_filter_services, traps_advanced_treatment, traps_advanced_treatment_default,
traps_timeout, traps_customcode, traps_exec_interval, traps_exec_interval_type, manufacturer_id, traps_output_transform
Expand Down Expand Up @@ -255,6 +255,7 @@ try {
CREATE TABLE IF NOT EXISTS `traps` (
`traps_id` int(11) UNIQUE NOT NULL,
`traps_name` varchar(255) DEFAULT NULL,
`traps_mode` int(11) DEFAULT 0,
`traps_oid` varchar(255) DEFAULT NULL,
`traps_args` varchar(255) DEFAULT NULL,
`traps_status` int(11) DEFAULT NULL,
Expand Down Expand Up @@ -460,12 +461,12 @@ try {
}

// Insert traps
$stmt = $dbh_sqlite->prepare("INSERT INTO traps (traps_id, traps_name, traps_oid, traps_args, traps_status, severity_id, manufacturer_id,
$stmt = $dbh_sqlite->prepare("INSERT INTO traps (traps_id, traps_name, traps_mode, traps_oid, traps_args, traps_status, severity_id, manufacturer_id,
traps_reschedule_svc_enable, traps_execution_command, traps_execution_command_enable, traps_submit_result_enable,
traps_advanced_treatment, traps_advanced_treatment_default, traps_timeout, traps_customcode,
traps_exec_interval, traps_exec_interval_type, traps_log,
traps_routing_mode, traps_routing_value, traps_exec_method, traps_downtime, traps_output_transform, traps_routing_filter_services) VALUES (
:traps_id, :traps_name, :traps_oid, :traps_args, :traps_status, :severity_id, :manufacturer_id,
:traps_id, :traps_name, :traps_mode, :traps_oid, :traps_args, :traps_status, :severity_id, :manufacturer_id,
:traps_reschedule_svc_enable, :traps_execution_command, :traps_execution_command_enable, :traps_submit_result_enable,
:traps_advanced_treatment, :traps_advanced_treatment_default, :traps_timeout, :traps_customcode,
:traps_exec_interval, :traps_exec_interval_type, :traps_log,
Expand All @@ -474,6 +475,7 @@ try {
foreach ($result_traps as $value) {
$stmt->bindParam(':traps_id', $value['traps_id'], PDO::PARAM_INT);
$stmt->bindParam(':traps_name', $value['traps_name'], PDO::PARAM_STR);
$stmt->bindParam(':traps_mode', $value['traps_mode'], PDO::PARAM_INT);
$stmt->bindParam(':traps_oid', $value['traps_oid'], PDO::PARAM_STR);
$stmt->bindParam(':traps_args', $value['traps_args'], PDO::PARAM_STR);
$stmt->bindParam(':traps_status', $value['traps_status'], PDO::PARAM_INT);
Expand Down
3 changes: 1 addition & 2 deletions doc/en/configuration_guide/advanced_configuration/traps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ Here is an example of possible configuration of the file **/etc/centreon/centreo
date_format => "",
time_format => "",
date_time_format => "",
# Internal OID cache from database
cache_unknown_traps_enable => 1,
# Time in seconds before cache reload
cache_unknown_traps_retention => 600,
# 0 = central, 1 = poller
Expand Down Expand Up @@ -256,6 +254,7 @@ It is also possible to create definitions of SNMP traps manually:
:align: center

* The field **Trap name** defines the name of the trap.
* The field **Mode** defines how to check the trap **OID** received.
* The field **OID** defines the Root OID to be received for this trap to be considered as received.
* The field **Vendor name** defines the name of the manufacturer to which the trap to be selected in the drop-down list belongs.
* The field **Output message** of contains the message to be displayed in the event of reception of a trap containing the OID configured above.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ To do this, perform the following steps:
+-----------------------------------+--------------------------------------------+
| Trap Name | Trap name |
+-----------------------------------+--------------------------------------------+
| Mode | Unique |
+-----------------------------------+--------------------------------------------+
| OID | OID of the trap |
+-----------------------------------+--------------------------------------------+
| Default Status | Trap default status |
Expand Down
3 changes: 1 addition & 2 deletions doc/fr/configuration_guide/advanced_configuration/traps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ Voici un exemple de configuration possible du fichier **/etc/centreon/centreontr
date_format => "",
time_format => "",
date_time_format => "",
# Utiliser le cache d'OID interne de la base de données
cache_unknown_traps_enable => 1,
# Temps en secondes avant de recharger le cache
cache_unknown_traps_retention => 600,
# 0 = central, 1 = poller
Expand Down Expand Up @@ -281,6 +279,7 @@ Il est également possible de créer manuellement des définitions de trap SNMP
:align: center

* Le champ **Nom du Trap** définit le nom du trap.
* Le champ **Mode** définit comment le champ **OID** est interpreté lors de la réception de ce trap.
* Le champ **OID** définit l'OID racine à recevoir pour que ce trap soit considéré comme reçu.
* Le champ **Nom du constructeur** définit le nom du constructeur auquel appartient le trap à sélectionner dans la liste déroulante.
* Le champ **Message de sortie** contient le message à afficher en cas de réception d'un trap contenant l'OID configuré au-dessus.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ Pour cela, exécutez la procédure suivante :
+-----------------------------------+--------------------------------------------+
| Nom | Nom du trap |
+-----------------------------------+--------------------------------------------+
| Mode | Unique |
+-----------------------------------+--------------------------------------------+
| OID | OID du trap |
+-----------------------------------+--------------------------------------------+
| Statut | Statut par défaut du trap |
Expand Down
6 changes: 6 additions & 0 deletions lang/fr_FR.UTF-8/LC_MESSAGES/help.po
Original file line number Diff line number Diff line change
Expand Up @@ -3826,6 +3826,12 @@ msgstr ""
"Entrer le nom de la trap comme spécifié dans le fichier MIB et envoyé par "
"l'agent SNMP."

#: centreon-web/www/include/configuration/configObject/traps/help.php
msgid ""
"Choose the matching mode."
msgstr ""
"Choisir le mode de correspondance."

#: centreon-web/www/include/configuration/configObject/traps/help.php:39
msgid ""
"Enter the full numeric object identifier (OID) starting with .1.3.6 (.iso.org."
Expand Down
4 changes: 4 additions & 0 deletions lang/fr_FR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -9347,6 +9347,10 @@ msgstr "Par OID"
msgid "By OID and Host"
msgstr "Par OID et hôte"

#: centreon-web/www/include/configuration/configObject/traps/formTraps.php
msgid "By OID, Host and Service"
msgstr "Par OID, hôte et service"

#: centreon-web/www/include/configuration/configObject/traps/formTraps.php:325
msgid "Execution type"
msgstr "Type d'exécution"
Expand Down
24 changes: 16 additions & 8 deletions lib/perl/centreon/script/centreontrapd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ sub new {
date_format => "",
time_format => "",
date_time_format => "",
cache_unknown_traps_enable => 1,
cache_unknown_traps_retention => 600,
# secure mode: 1 => cannot use customcode option for traps
secure_mode => 1,
Expand Down Expand Up @@ -625,6 +624,14 @@ sub manage_exec {
$self->{logger}->writeLogInfo("Skipping trap '" . $self->{current_trap_id} . "' for host ID '" . $self->{current_host_id} . "': time interval");
return 1;
}

# Host/Service type
if ($self->{trap_data}->{ref_oids}->{ $self->{current_trap_id} }->{traps_exec_interval_type} == 3 &&
defined($self->{last_time_exec}{host}->{$self->{current_host_id} . ";" . $self->{current_service_id} . ";" . $self->{current_oid}}) &&
$self->{trap_data}->{trap_date_time_epoch} < ($self->{last_time_exec}{host}->{$self->{current_host_id} . ";" . $self->{current_service_id} . ";" . $self->{current_oid}} + $self->{trap_data}->{ref_oids}->{ $self->{current_trap_id} }->{traps_exec_interval})) {
$self->{logger}->writeLogInfo("Skipping trap '" . $self->{current_trap_id} . "' for host ID '" . $self->{current_host_id} . "' and service ID '" . $self->{current_service_id} . "': time interval");
return 1;
}
}

### Check Sequential exec ###
Expand Down Expand Up @@ -664,6 +671,7 @@ sub manage_exec {
$self->{running_processes}->{$current_pid} = 1;
$self->{last_time_exec}{oid}->{$self->{current_oid}} = $self->{trap_data}->{trap_date_time_epoch};
$self->{last_time_exec}{host}->{$self->{current_host_id} . ";" . $self->{current_oid}} = $self->{trap_data}->{trap_date_time_epoch};
$self->{last_time_exec}{host}->{$self->{current_host_id} . ";" . $self->{current_service_id} . ";" . $self->{current_oid}} = $self->{trap_data}->{trap_date_time_epoch};
return 1;
}

Expand Down Expand Up @@ -1046,11 +1054,11 @@ sub executeCommand {
## GET HOSTNAME AND SERVICE DESCRIPTION
#
sub getTrapsInfos {
my $self = shift;
my ($fstatus);
my ($self, $ids) = @_;
my $fstatus;

### Get OIDS
($fstatus, $self->{trap_data}->{ref_oids}) = centreon::trapd::lib::get_oids($self->{cdb}, ${$self->{trap_data}->{var}}[3]);
($fstatus, $self->{trap_data}->{ref_oids}) = centreon::trapd::lib::get_oids($self->{cdb}, $ids);
return 0 if ($fstatus == -1);
foreach my $trap_id (keys %{$self->{trap_data}->{ref_oids}}) {
$self->{trap_data}->{current_trap_id} = $trap_id;
Expand Down Expand Up @@ -1237,17 +1245,17 @@ sub run {
entvarname => \@{$self->{trap_data}->{entvarname}});

if ($readtrap_result == 1) {
my ($status) = centreon::trapd::lib::check_known_trap(logger => $self->{logger},
my (@return) = centreon::trapd::lib::check_known_trap(logger => $self->{logger},
logger_unknown => $self->{logger_unknown},
config => $self->{centreontrapd_config},
trap_data => $self->{trap_data},
oid2verif => ${$self->{trap_data}->{var}}[3],
cdb => $self->{cdb},
last_cache_time => \$self->{last_cache_time},
oids_cache => \$self->{oids_cache});
if ($status == 1) {
$unlink_trap = $self->getTrapsInfos();
} elsif ($status == -1) {
if ($return[0] == 1) {
$unlink_trap = $self->getTrapsInfos($return[1]);
} elsif ($return[0] == -1) {
# DB deconnection. Need to keep the file. Not deleted it.
$unlink_trap = 0;
}
Expand Down
66 changes: 37 additions & 29 deletions lib/perl/centreon/trapd/lib.pm
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,19 @@ sub manage_params_conf {

# We get All datas for a TRAP
sub get_oids {
my ($cdb, $oid) = @_;
my ($cdb, $ids) = @_;
my $ref_result;

my ($dstatus, $sth) = $cdb->query("SELECT name, traps_log, traps_execution_command, traps_reschedule_svc_enable, traps_id, traps_args,
traps_oid, traps_name, traps_advanced_treatment, traps_advanced_treatment_default, traps_execution_command_enable, traps_submit_result_enable, traps_status,
traps_oid, traps_name, traps_mode, traps_advanced_treatment, traps_advanced_treatment_default, traps_execution_command_enable, traps_submit_result_enable, traps_status,
traps_timeout, traps_customcode, traps_exec_interval, traps_exec_interval_type,
traps_routing_mode, traps_routing_value, traps_routing_filter_services,
traps_exec_method, traps_downtime, traps_output_transform,
service_categories.level, service_categories.sc_name, service_categories.sc_id
FROM traps
LEFT JOIN traps_vendor ON (traps_vendor.id = traps.manufacturer_id)
LEFT JOIN service_categories ON (service_categories.sc_id = traps.severity_id)
WHERE traps_oid = " . $cdb->quote($oid));
WHERE traps_id IN (" . join(',', @$ids) . ")");
return -1 if ($dstatus == -1);
$ref_result = $sth->fetchall_hashref('traps_id');

Expand Down Expand Up @@ -492,9 +492,19 @@ sub get_cache_oids {
# oids_cache => ref
my %args = @_;

my ($status, $sth) = $args{cdb}->query("SELECT traps_oid FROM traps");
my ($status, $sth) = $args{cdb}->query("SELECT traps_oid, traps_id, traps_mode FROM traps");
return -1 if ($status == -1);
${$args{oids_cache}} = $sth->fetchall_hashref("traps_oid");

${$args{oids_cache}} = { 0 => {}, 1 => {} };
my $rows = [];
while (my $row = (
shift(@$rows) ||
shift(@{$rows = $sth->fetchall_arrayref(undef,5000)||[]})
)
) {
${$args{oids_cache}}->{$$row[2]}->{$$row[0]} = $$row[1];
}

${$args{last_cache_time}} = time();
return 0;
}
Expand Down Expand Up @@ -539,36 +549,34 @@ sub check_known_trap {
my %args = @_;
my $oid2verif = $args{oid2verif};

if ($args{config}->{cache_unknown_traps_enable} == 1) {
if (!defined(${$args{last_cache_time}}) || ((time() - ${$args{last_cache_time}}) > $args{config}->{cache_unknown_traps_retention})) {
if (get_cache_oids(cdb => $args{cdb}, oids_cache => $args{oids_cache}, last_cache_time => $args{last_cache_time}) == -1) {
$args{logger}->writeLogError("Cant load cache trap oids.");
return -1;
}
}
if (defined(${$args{oids_cache}}->{$oid2verif})) {
return 1;
} else {
display_unknown_traps(logger => $args{logger}, logger_unknown => $args{logger_unknown},
config => $args{config}, trap_data => $args{trap_data});
return 0;
if (!defined(${$args{last_cache_time}}) ||
((time() - ${$args{last_cache_time}}) > $args{config}->{cache_unknown_traps_retention})) {
if (get_cache_oids(cdb => $args{cdb}, oids_cache => $args{oids_cache}, last_cache_time => $args{last_cache_time}) == -1) {
$args{logger}->writeLogError("Cant load cache trap oids.");
return -1;
}
} else {
# Read db
my ($status, $sth) = $args{cdb}->query("SELECT traps_oid FROM traps WHERE traps_oid = " . $args{cdb}->quote($oid2verif));
return 0 if ($status == -1);
if (!$sth->fetchrow_hashref()) {
display_unknown_traps(logger => $args{logger}, logger_unknown => $args{logger_unknown},
config => $args{config}, trap_data => $args{trap_data});
return 0;
}

if (defined(${$args{oids_cache}}->{0}->{$oid2verif})) {
return (1, [${$args{oids_cache}}->{0}->{$oid2verif}]);
}

# Regexp
my @traps_ids = ();
foreach my $oid (keys %{${$args{oids_cache}}->{1}}) {
if ($oid2verif =~ m/$oid/) {
push @traps_ids, ${$args{oids_cache}}->{1}->{$oid};
}
}
if (scalar(@traps_ids) > 0) {
return (1, \@traps_ids);
}

return 1;
display_unknown_traps(logger => $args{logger}, logger_unknown => $args{logger_unknown},
config => $args{config}, trap_data => $args{trap_data});
return 0;
}



###
# Code from SNMPTT Modified
# Copyright 2002-2009 Alex Burger
Expand Down
Loading