iopsys-feed/mcastmngr/files/linux/usr/libexec/rpcd/mcast
Markus Gothe 4e2adf77cb mcastmngr: Implement L2 stats over UBUS.
Implement support for L2 mcast stats over UBUS.
2024-10-22 05:06:52 +00:00

214 lines
5.5 KiB
Bash
Executable file

#!/bin/sh
. /usr/share/libubox/jshn.sh
. /lib/functions.sh
readonly TEMPFILE="/tmp/snooping_stats_"$$
mcast_snooping_interface() {
local interface intf gip vid port
config_get interface "$1" interface
for intf in $interface; do
[ -z "$(bridge mdb show dev "$intf" 2> /dev/null | grep -v -F "port ${intf} " | xargs)" ] && continue
json_add_object ""
json_add_string "interface" "$intf"
json_add_array "groups"
for gip in $(bridge mdb show dev "$intf" | grep -v -F "port ${intf} " | cut -f6 -d' ' | sort -u | xargs); do
json_add_object ""
json_add_string "groupaddr" "$gip"
json_add_array "clients"
for port in $(bridge mdb show dev "$intf" | grep -v -F "port ${intf} " | grep -F "grp ${gip} " | cut -f4 -d' ' | sort -u | xargs); do
json_add_object ""
json_add_string "device" "$port"
for vid in $(bridge mdb show dev "$intf" | grep -F "port ${port} grp ${gip} " | grep -F ' vid ' | cut -f9 -d' ' | sort -u | xargs); do
json_add_object ""
json_add_string "vid" "$vid"
json_close_object #close the associated vid object
done
json_close_object #close the associated device object
done
json_close_array #close the associated devices array
json_close_object # close the groups object
done # close the loop for group addresses
json_close_array #close the groups array
json_close_object # close the snooping object
done # close the loop for interfaces
}
meld_files() {
local type="$1"
local snooping_stats="/tmp/${type}_snooping_stats"
local old_mod_time=$(date +%s 2>/dev/null -r "$snooping_stats")
# Sending signal to mcproxy to dump multicast data in /tmp/${type}_snooping_stats
local mcast_pids=$(pidof mcproxy)
for pid in $mcast_pids
do
$(kill -10 $pid)
done
# Wait for signal is being processed by mcproxy
if [ -n "$mcast_pids" ]; then
for i in 1 2 3; do
local new_mod_time=$(date +%s 2>/dev/null -r "$snooping_stats")
[ -n "$new_mod_time" ] && [ "$new_mod_time" != "$old_mod_time" ] && break
sleep 0.1
done
fi
[ ! -e "$snooping_stats" ] && return 0
cat "$snooping_stats" >> "$TEMPFILE"
}
read_snooping_file() {
local mcast_addrs=""
local ifaces=""
while read line; do
# reading each line
case $line in
br-*)
found_iface=0
snoop_iface="$(echo $line | awk -F ' ' '{ print $1 }')"
if [ -z "$ifaces" ]; then
ifaces="$snoop_iface"
continue
fi
IFS=" "
for ifx in $ifaces; do
if [ $ifx == $snoop_iface ]; then
found_iface=1
break
fi
done
if [ $found_iface -eq 0 ]; then
ifaces="$ifaces $snoop_iface"
continue
fi
;;
esac
done < "$TEMPFILE"
while read line; do
# reading each line
case $line in
br-*)
found_ip=0
grp_ip="$(echo $line | awk -F ' ' '{ print $2 }')"
if [ -z "$mcast_addrs" ]; then
mcast_addrs="$grp_ip"
continue
fi
IFS=" "
for ip_addr in $mcast_addrs; do
if [ $ip_addr == $grp_ip ]; then
found_ip=1
break
fi
done
if [ $found_ip -eq 0 ]; then
mcast_addrs="$mcast_addrs $grp_ip"
continue
fi
;;
esac
done < "$TEMPFILE"
IFS=" "
for intf in $ifaces; do
json_add_object ""
json_add_string "interface" "$intf"
json_add_array "groups"
IFS=" "
for gip_addr in $mcast_addrs; do
grp_obj_added=0
while read line; do
# reading each line
case $line in
br-*)
snoop_iface="$(echo $line | awk -F ' ' '{ print $1 }')"
if [ "$snoop_iface" != "$intf" ]; then
continue
fi
grp_ip="$(echo $line | awk -F ' ' '{ print $2 }')"
if [ "$grp_ip" != "$gip_addr" ]; then
continue
fi
if [ $grp_obj_added -eq 0 ]; then
json_add_object ""
if [ -n "$(echo $gip_addr | grep -oE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$")" ]; then
gip="$(ipcalc.sh $gip_addr | grep IP | awk '{print substr($0,4)}')"
else
gip="$(echo $gip_addr | grep -oE '^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$')"
fi
json_add_string "groupaddr" "$gip"
json_add_array "clients"
grp_obj_added=1
fi
json_add_object ""
host_ip="$(echo $line | awk -F ' ' '{ print $3 }')"
if [ -n "$(echo $host_ip | grep -oE "^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$")" ]; then
h_ip="$(ipcalc.sh $host_ip | grep IP | awk '{print substr($0,4)}')"
else
h_ip="$(echo $host_ip | grep -oE '^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$')"
fi
json_add_string "ipaddr" "$h_ip"
src_port="$(echo $line | awk -F ' ' '{ print $4 }')"
json_add_string "device" "$src_port"
timeout="$(echo $line | awk -F ' ' '{ print $5 }')"
json_add_int "timeout" "$timeout"
json_close_object #close the associated device object
;;
esac
done < "$TEMPFILE"
if [ $grp_obj_added -eq 1 ]; then
json_close_array #close the associated devices array
json_close_object # close the groups object
fi
done # close the loop for group addresses
json_close_array #close the groups array
json_close_object # close the snooping object
done # close the loop for interfaces
}
read_mcast_stats() {
echo -n > "$TEMPFILE"
json_init
json_add_array "snooping"
meld_files "igmp"
meld_files "mld"
read_snooping_file
# L2 Snooping goes here
config_load mcast
config_foreach mcast_snooping_interface "snooping"
json_close_array # close the snooping array
json_dump
rm -f "$TEMPFILE"
}
case "$1" in
list)
echo '{ "stats":{} }'
;;
call)
case "$2" in
stats)
out="$(read_mcast_stats)"
if [ -z "${out}" ]; then
echo '{}'
else
echo "${out}"
fi
;;
esac
;;
esac