From fb5ddac9f148ca89dc4c0c75cfbdd5942767a21a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Josef=20N=C4=9Bmec?= <josef.nemec@cesnet.cz>
Date: Mon, 25 Mar 2024 12:50:34 +0100
Subject: [PATCH] Finalize cephpingmon

---
 CHANGELOG.md                         |  4 +--
 dependencies.yum.txt                 |  1 +
 src/metric-generators/cephpingmon.sh | 46 ++++++++++++++++++----------
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 121ce0f..96f90f4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,9 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ## [Unreleased]
 
-## [1.3.4] - 2024-03-19
+## [1.3.4] - 2024-03-25
 ### Added
-- cephpingmon added as optional module (default behavior unchanged)
+- cephpingmon added as optional module (default behavior unchanged), add fping dependency
 
 ## [1.3.3] - 2023-10-25
 ### Fixed
diff --git a/dependencies.yum.txt b/dependencies.yum.txt
index 1db6d4d..dab0d12 100644
--- a/dependencies.yum.txt
+++ b/dependencies.yum.txt
@@ -6,3 +6,4 @@ curl
 cronie
 smartmontools
 pciutils
+fping
diff --git a/src/metric-generators/cephpingmon.sh b/src/metric-generators/cephpingmon.sh
index dfc43d1..afafc5b 100755
--- a/src/metric-generators/cephpingmon.sh
+++ b/src/metric-generators/cephpingmon.sh
@@ -1,5 +1,4 @@
 #!/bin/bash
-set -eo pipefail
 
 METRIC_NAME="cephpingmon_endpoint_accessible"
 if [[ -z "${CEPH_IPS}" ]]; then
@@ -9,24 +8,37 @@ if [[ -z "${MTU_SIZE}" ]]; then
   MTU_SIZE=8972
 fi
 
-printf '# HELP %s: Ceph endpoint ping success ratio. (1 ~ no loss, 0 ~ complete loss)\n' $METRIC_NAME
+function publish_metrics_nofrag {
+  local ping_result
+  local loss_percent
+  local success_ratio
+  local fragmentation="no"
 
-# $1 == ip address, $2 == yes/no fragmentation allowed
-function publish_metric {
-    if [[ $2 == "yes" ]]; then
-        local ping_result=$(ping -c4 -s $MTU_SIZE $ip || echo "0 received, 100% packet loss")
-    else
-        local ping_result=$(ping -c4 -s $MTU_SIZE -M do -q $ip || echo "0 received, 100% packet loss")
-    fi
+  for ip in $CEPH_IPS; do
+    ping_result=$(ping -q -c4 -W 1 -s $MTU_SIZE -M do $ip)
+    loss_percent=$(echo ${ping_result} | sed -n -e 's/^.*, \(.*\)% packet.*/\1/p')
+    success_ratio=$(echo "scale=2 ; 1 - ${loss_percent} / 100" | bc)
+    LC_NUMERIC=C printf '%s{endpoint_ip="%s", fragmentation="%s"} %.2f\n' \
+      "${METRIC_NAME}" $ip $fragmentation "${success_ratio}"
+  done
+}
 
-    local packet_loss_percent=$(echo ${ping_result} | sed -n -e 's/^.*, \(.*\)% packet.*/\1/p')
-    local ping_success_ratio=$(echo "scale=2 ; 1 - ${packet_loss_percent} / 100" | bc)
+function publish_metrics_frag {
+  local ip
+  local loss_percent
+  local success_ratio
+  local fragmentation="yes"
 
-    LC_NUMERIC=C printf '%s{ip="%s", fragmentation="%s"} %.2f\n' \
-        "${METRIC_NAME}" "$1" "$2" "${ping_success_ratio}"
+  while IFS= read -r line; do
+    ip=$(echo $line | cut -d' ' -f1)
+    loss_percent=$(echo $line | cut -d' ' -f5 | cut -d'/' -f3 | tr -d % | tr -d ,)
+    success_ratio=$(echo "scale=2 ; 1 - ${loss_percent} / 100" | bc)
+    LC_NUMERIC=C printf '%s{endpoint_ip="%s", fragmentation="%s"} %.2f\n' \
+      "${METRIC_NAME}" $ip $fragmentation "${success_ratio}"
+  done <<< "$1"
 }
 
-for ip in $CEPH_IPS; do
-    publish_metric $ip yes
-    publish_metric $ip no
-done
+printf '# HELP %s: Ceph endpoint ping success ratio. (1 ~ no loss, 0 ~ complete loss)\n' $METRIC_NAME
+ping_result=$(fping -q -c4 -b $MTU_SIZE $CEPH_IPS 2>&1)
+publish_metrics_frag "${ping_result}"
+publish_metrics_nofrag
-- 
GitLab