keepalived: add ubus status
authorFlorian Eckert <fe@dev.tdt.de>
Tue, 30 Jul 2024 05:53:22 +0000 (07:53 +0200)
committerFlorian Eckert <Eckert.Florian@googlemail.com>
Tue, 4 Feb 2025 12:48:09 +0000 (13:48 +0100)
The hotplug scripts are called with every state change. When called, the
scripts are processed under '/etc/hotplug.d/keepalived'. This change adds
the functionality that the last state change of the keepalived can be
queried via the ubus.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
net/keepalived/Makefile
net/keepalived/files/etc/hotplug.d/keepalived/01-ubus [new file with mode: 0644]
net/keepalived/files/usr/libexec/keepalived/rpc/status.sh [new file with mode: 0644]

index dc77262c7a9c41cedf7ef6e8a0bac3438940bbfb..db660152edd36ddea9a8a12138ef0ba607d37917 100644 (file)
@@ -236,11 +236,17 @@ endif
        $(INSTALL_DIR) $(1)/etc/hotplug.d/keepalived
        $(INSTALL_DATA) ./files/hotplug-user \
                $(1)/etc/hotplug.d/keepalived/01-user
+       $(INSTALL_DATA) ./files/etc/hotplug.d/keepalived/01-ubus \
+               $(1)/etc/hotplug.d/keepalived/01-ubus
 
        $(INSTALL_DIR) $(1)/usr/libexec/rpcd
        $(INSTALL_BIN) ./files/usr/libexec/rpcd/keepalived \
                $(1)/usr/libexec/rpcd/keepalived
 
+       $(INSTALL_DIR) $(1)/usr/libexec/keepalived/rpc
+       $(INSTALL_DATA) ./files/usr/libexec/keepalived/rpc/status.sh \
+               $(1)/usr/libexec/keepalived/rpc/status.sh
+
 ifneq ($(CONFIG_KEEPALIVED_SNMP_VRRP)$(CONFIG_KEEPALIVED_SNMP_CHECKER)$(CONFIG_KEEPALIVED_SNMP_RFC2)$(CONFIG_KEEPALIVED_SNMP_RFC3),)
        $(INSTALL_DIR) $(1)/usr/share/snmp/mibs
 endif
@@ -318,7 +324,21 @@ define Package/keepalived-sync/install
                $(1)/usr/libexec/keepalived/rpc/sync.sh
 
        $(INSTALL_DIR) $(1)/etc/hotplug.d/keepalived
-       $(CP) ./files/etc/hotplug.d/keepalived/* \
+       $(CP) ./files/etc/hotplug.d/keepalived/501-rpcd \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/505-system \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/511-firewall \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/551-dnsmasq \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/555-dropbear \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/600-uhttpd \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/700-luci \
+               $(1)/etc/hotplug.d/keepalived
+       $(CP) ./files/etc/hotplug.d/keepalived/810-files \
                $(1)/etc/hotplug.d/keepalived
 endef
 
diff --git a/net/keepalived/files/etc/hotplug.d/keepalived/01-ubus b/net/keepalived/files/etc/hotplug.d/keepalived/01-ubus
new file mode 100644 (file)
index 0000000..ab5907f
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+. /lib/functions.sh
+
+KEEPALIVED_STATUS_DIR="/var/run/keepalived"
+
+# Add a TAG for configured INSTANCE/GROUP
+tag_config() {
+       local cfg="$1"
+       local type="$2"
+       local name
+
+       config_get name "$cfg" name
+       mkdir -p "${KEEPALIVED_STATUS_DIR}/${name}_${type}"
+       touch "${KEEPALIVED_STATUS_DIR}/${name}_${type}/TAG"
+}
+
+main() {
+       # Remove TAG flag
+       for dir in ${KEEPALIVED_STATUS_DIR}/*; do
+               rm -rf "$dir/TAG"
+       done
+
+       config_load keepalived
+       config_foreach tag_config vrrp_instance INSTANCE
+       config_foreach tag_config vrrp_sync_group GROUP
+
+       # Delete run time directories which are not configured anymore
+       for dir in ${KEEPALIVED_STATUS_DIR}/*; do
+               if [ ! -e "$dir/TAG" ]; then
+                       rm -rf "$dir"
+               fi
+       done
+
+       # Do not update 'GROUP' status if action is 'NOTIFY'
+       [ "$ACTION" = "NOTIFY" ] && [ "$TYPE" = "GROUP" ] && return
+
+       if [ -n "${NAME}" ] && [ -n "${TYPE}" ]; then
+               mkdir -p "${KEEPALIVED_STATUS_DIR}/${NAME}_${TYPE}"
+               [ -n "$ACTION" ] || ACTION="NOTIFY_UNKNOWN"
+               echo "$ACTION" > "${KEEPALIVED_STATUS_DIR}/${NAME}_${TYPE}/STATUS"
+               date +'%s' > "${KEEPALIVED_STATUS_DIR}/${NAME}_${TYPE}/TIME"
+       fi
+}
+
+main
diff --git a/net/keepalived/files/usr/libexec/keepalived/rpc/status.sh b/net/keepalived/files/usr/libexec/keepalived/rpc/status.sh
new file mode 100644 (file)
index 0000000..25fd76f
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+
+KEEPALIVED_STATUS_DIR="/var/run/keepalived"
+
+dump() {
+       local type="${1}"
+       local value name time status
+
+       json_add_array "$(echo "$type" | tr A-Z a-z)"
+       for dir in ${KEEPALIVED_STATUS_DIR}/*; do
+               value="${dir##*_}"
+               name="${dir%_*}"
+               name="${name##*/}"
+               [ -f "${dir}/TIME" ] && time=$(cat "${dir}/TIME")
+               [ -f "${dir}/STATUS" ] && status=$(cat "${dir}/STATUS")
+               if [ "${value}" = "${type}" ]; then
+                       json_add_object
+                       json_add_string "name" "${name}"
+                       json_add_int "event" "${time}"
+                       json_add_string "status" "${status}"
+                       json_close_object
+               fi
+       done
+       json_close_array
+}
+
+status() {
+       json_init
+       dump "INSTANCE"
+       dump "GROUP"
+       json_dump
+}