Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keepalived: add ubus status #25462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions net/keepalived/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=keepalived
PKG_VERSION:=2.3.1
PKG_RELEASE:=1
PKG_RELEASE:=2

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://www.keepalived.org/software
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions net/keepalived/files/etc/hotplug.d/keepalived/01-ubus
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions net/keepalived/files/usr/libexec/keepalived/rpc/status.sh
Original file line number Diff line number Diff line change
@@ -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
}