From c108c7cb694b4b5d7d89d8d4907fdcf23b788c30 Mon Sep 17 00:00:00 2001
From: omercier <32134301+omercier@users.noreply.github.com>
Date: Tue, 27 Feb 2024 17:16:07 +0100
Subject: [PATCH] Ctor 378 fix snmp collections random issue (#4918)

---
 .github/workflows/spellchecker.yml            |  2 +-
 src/apps/protocols/snmp/mode/collection.pm    |  2 +-
 .../collection/snmp/sputnik-environment.json  | 67 +++++++++++++++++++
 .../snmp/snmp-collection-sputnik-snmp.robot   | 46 +++++++++++++
 4 files changed, 115 insertions(+), 2 deletions(-)
 create mode 100644 src/contrib/collection/snmp/sputnik-environment.json
 create mode 100644 tests/functional/snmp/snmp-collection-sputnik-snmp.robot

diff --git a/.github/workflows/spellchecker.yml b/.github/workflows/spellchecker.yml
index 4330058fa..ae4d9d4f6 100644
--- a/.github/workflows/spellchecker.yml
+++ b/.github/workflows/spellchecker.yml
@@ -27,7 +27,7 @@ jobs:
           list-files: shell
           filters: |
             plugins:
-              - added|modified: src/**
+              - added|modified: 'src/**/*.pm'
 
       - name: Install CPAN Libraries
         uses: shogo82148/actions-setup-perl@ea0507898383e7dbce382138da0c21af1849eb9e # v1.27.0
diff --git a/src/apps/protocols/snmp/mode/collection.pm b/src/apps/protocols/snmp/mode/collection.pm
index 625bbc531..0df162493 100644
--- a/src/apps/protocols/snmp/mode/collection.pm
+++ b/src/apps/protocols/snmp/mode/collection.pm
@@ -242,7 +242,7 @@ sub collect_snmp_tables {
         my $used_instance = defined($table->{used_instance}) && $table->{used_instance} ne '' ? $table->{used_instance} : '\.(\d+)$';
         my $snmp_result = $options{snmp}->get_table(oid => $table->{oid});
         foreach (keys %$snmp_result) {
-            /$used_instance/;
+            /$used_instance/ or next;
             next if (defined($self->{snmp_collected}->{tables}->{ $table->{name} }->{$1}));
             my $instance = $1;
     
diff --git a/src/contrib/collection/snmp/sputnik-environment.json b/src/contrib/collection/snmp/sputnik-environment.json
new file mode 100644
index 000000000..cdaefa7b5
--- /dev/null
+++ b/src/contrib/collection/snmp/sputnik-environment.json
@@ -0,0 +1,67 @@
+{
+    "constants": {},
+    "mapping": {},
+    "snmp": {
+        "tables": [
+            {
+                "name": "upsEnvSensors",
+                "oid": ".1.3.6.1.4.1.54661.1.1.1.2.2.1",
+                "used_instance": "\\.2\\.(\\d+)$",
+                "entries": [
+                    {
+                        "name": "temperature",
+                        "oid": ".1.3.6.1.4.1.54661.1.1.1.2.2.1.2"
+                    },
+                    {
+                        "name": "humidity",
+                        "oid": ".1.3.6.1.4.1.54661.1.1.1.2.2.1.3"
+                    }
+                ]
+            }
+        ]
+    },
+    "selection_loop": [
+        {
+            "name": "Sensors",
+            "source": "%(snmp.tables.upsEnvSensors)",
+            "expand_table": {
+                "upsEnvSensors": "%(snmp.tables.upsEnvSensors.[%(upsEnvSensors.instance)])"
+            },
+            "functions": [
+                {
+                   "type": "replace",
+                   "src": "%(upsEnvSensors.temperature)",
+                   "expression": "s/(\\d?\\d)(\\d\\d)/$1.$2/"
+                }
+            ],
+
+            "perfdatas": [
+                {
+                    "nlabel": "environment.temperature.celsius",
+                    "instances": ["%(upsEnvSensors.instance)"],
+                    "value": "%(upsEnvSensors.temperature)",
+                    "critical": "",
+                    "unit": "C"
+                },
+                {
+                    "nlabel": "environment.humidity.percent",
+                    "instances": ["%(upsEnvSensors.instance)"],
+                    "value": "%(upsEnvSensors.humidity)",
+                    "critical": "",
+                    "unit": "%",
+                    "min": 0,
+                    "max": 100
+                }
+            ],
+            "formatting": {
+                "printf_msg": "Sensor '%s' temperature is '%s'°C and humidity is '%s'%%",
+                "printf_var": [
+                    "%(upsEnvSensors.instance)",
+                    "%(upsEnvSensors.temperature)",
+                    "%(upsEnvSensors.humidity)"
+                ],
+                "display_ok": true
+            }
+        }
+    ]
+}
\ No newline at end of file
diff --git a/tests/functional/snmp/snmp-collection-sputnik-snmp.robot b/tests/functional/snmp/snmp-collection-sputnik-snmp.robot
new file mode 100644
index 000000000..2e380c0aa
--- /dev/null
+++ b/tests/functional/snmp/snmp-collection-sputnik-snmp.robot
@@ -0,0 +1,46 @@
+*** Settings ***
+Documentation       Hardware UPS Sputnik SNMP plugin
+
+Library             OperatingSystem
+Library             String
+Library             Examples
+
+Test Timeout        120s
+
+
+*** Variables ***
+${CENTREON_PLUGINS}         ${CURDIR}${/}..${/}..${/}..${/}src${/}centreon_plugins.pl
+
+${CMD}                      perl ${CENTREON_PLUGINS} --plugin=apps::protocols::snmp::plugin
+
+*** Test Cases ***
+SNMP Collection - Sputnik Environment ${tc}/3
+    [Tags]    SNMP Collection
+    ${command}    Catenate
+    ...    ${CMD}
+    ...    --mode=collection
+    ...    --hostname=127.0.0.1
+    ...    --snmp-version=2c
+    ...    --snmp-port=2024
+    ...    --snmp-community=hardware-ups/hardware-ups-sputnik
+    ...    --config=${CURDIR}${/}..${/}..${/}..${/}src/contrib/collection/snmp/sputnik-environment.json
+
+    ${output}    Run    ${command}
+    ${output}    Strip String    ${output}
+    Should Be Equal As Strings
+    ...    ${output}
+    ...    ${expected_result}
+    ...    Wrong output result for compliance of ${expected_result}{\n}Command output:{\n}${output}{\n}{\n}{\n}
+
+    Examples:        tc    expected_result    --
+            ...      1     OK: Sensor '1' temperature is '20.06'°C and humidity is '33'% | '1#environment.temperature.celsius'=20.06C;;;; '1#environment.humidity.percent'=33%;;;0;100
+            ...      2     OK: Sensor '1' temperature is '20.06'°C and humidity is '33'% | '1#environment.temperature.celsius'=20.06C;;;; '1#environment.humidity.percent'=33%;;;0;100
+            ...      3     OK: Sensor '1' temperature is '20.06'°C and humidity is '33'% | '1#environment.temperature.celsius'=20.06C;;;; '1#environment.humidity.percent'=33%;;;0;100
+
+*** Keywords ***
+Append Option
+    [Documentation]    Concatenates the first argument (option) with the second (value) after having replaced the value with "" if its content is '_empty_'
+    [Arguments]    ${option}    ${value}
+    ${value}    Set Variable If    '${value}' == '_empty_'    ''    ${value}
+    [return]    ${option}=${value}
+