From 7e38c70b734424a5ac6c9fb32a77a84bbc0de062 Mon Sep 17 00:00:00 2001 From: lucasperovani Date: Thu, 20 Oct 2022 12:27:45 -0300 Subject: [PATCH 1/2] Added index to traceroute report. --- files/usr/share/functions/api_functions.sh | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/files/usr/share/functions/api_functions.sh b/files/usr/share/functions/api_functions.sh index 60ae3e11..68d71426 100644 --- a/files/usr/share/functions/api_functions.sh +++ b/files/usr/share/functions/api_functions.sh @@ -392,12 +392,15 @@ check_and_set_default_traceroute() { } create_time_object_traceroute() { - # $1: ip - # $2: hops - local _ip="$1" - local _hops="$2" + # $1: index of the hop + # $2: ip + # $3: hops + local _index="$1" + local _ip="$2" + local _hops="$3" json_add_object + json_add_int index "$_index" json_add_string ip "$_ip" # Create an array for storing the times @@ -537,6 +540,9 @@ get_traceroute() { for _hop in $_hops do + # Get the index of the hop + local _index="$(echo "$_hop" | awk '{print $0}')" + # Get the ip of the hop local _ip="$(echo "$_hop" | awk '{print $1}')" @@ -545,7 +551,7 @@ get_traceroute() { if [ -n "$_hops_to_add" ] && [ -z "$(echo "$_blacklist_hops" | grep "$_ip")" ] then - create_time_object_traceroute "$_ip" "$_hops_to_add" + create_time_object_traceroute "$_index" "$_ip" "$_hops_to_add" # Add the ip to blacklist _blacklist_hops="${_blacklist_hops}"$'\n'"${_ip}" @@ -554,7 +560,7 @@ get_traceroute() { # Check if the ip is in _blacklist_hops, otherwise append to array if [ -z "$(echo "$_blacklist_hops" | grep "$_ip")" ] then - create_time_object_traceroute "$_ip" "$_hop" + create_time_object_traceroute "$_index" "$_ip" "$_hop" fi done From 69a9741a5bacd79891c12c5032b10f0e0b32e89a Mon Sep 17 00:00:00 2001 From: lucasperovani Date: Mon, 31 Oct 2022 14:23:15 -0300 Subject: [PATCH 2/2] Fixed indexes extraction. --- files/usr/share/functions/api_functions.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/files/usr/share/functions/api_functions.sh b/files/usr/share/functions/api_functions.sh index 68d71426..cda1bef9 100644 --- a/files/usr/share/functions/api_functions.sh +++ b/files/usr/share/functions/api_functions.sh @@ -400,7 +400,7 @@ create_time_object_traceroute() { local _hops="$3" json_add_object - json_add_int index "$_index" + json_add_int hop_index "$_index" json_add_string ip "$_ip" # Create an array for storing the times @@ -482,6 +482,9 @@ get_traceroute() { # Only process the traceroute if it is not empty if [ -n "$_traceroute" ] then + # Get all hops with index + local _hops_index="$(echo "$_traceroute" | grep -E -o '^ *[0-9]+.+[0-9]*(\.[0-9]*){3}')" + # Get all hops, might find more than one per line local _hops="$(echo "$_traceroute" | grep -E -o '[0-9]*(\.[0-9]*){3}(( \*)* [0-9]*\.[0-9]* ms)+')" @@ -540,12 +543,12 @@ get_traceroute() { for _hop in $_hops do - # Get the index of the hop - local _index="$(echo "$_hop" | awk '{print $0}')" - # Get the ip of the hop local _ip="$(echo "$_hop" | awk '{print $1}')" + # Get the index of the hop + local _index="$(echo "$_hops_index" | grep "$_ip" | awk '{print $1}' | head -1)" + # Check if ip is in _repeated_hops, add them, joining the time values local _hops_to_add="$(echo "$_repeated_hops" | grep "$_ip")" @@ -555,12 +558,18 @@ get_traceroute() { # Add the ip to blacklist _blacklist_hops="${_blacklist_hops}"$'\n'"${_ip}" + + # Update _hops_index to remove the line + _hops_index="$(echo "$_hops_index" | grep -vE "^ *$_index ")" fi # Check if the ip is in _blacklist_hops, otherwise append to array if [ -z "$(echo "$_blacklist_hops" | grep "$_ip")" ] then create_time_object_traceroute "$_index" "$_ip" "$_hop" + + # Update _hops_index to remove the line + _hops_index="$(echo "$_hops_index" | grep -vE "^ *$_index ")" fi done