diff --git a/package/gluon-state-check/Makefile b/package/gluon-state-check/Makefile new file mode 100644 index 00000000..28fd2418 --- /dev/null +++ b/package/gluon-state-check/Makefile @@ -0,0 +1,28 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=gluon-state-check +PKG_VERSION:=1 + +include ../gluon.mk + +define Package/gluon-state-check + TITLE:=Provides info about the routers state + DEPENDS:=+gluon-core +micrond +endef + +define Package/gluon-state-check/description + gluon-state-check executes checks in `/lib/gluon/state/check.d/` and provides + a flag file for each check in `/var/gluon/state` depending on the return code + of the check. A flag file is created (or "touched") if the corresponding check + exits cleanly and gets removed otherwise. If the flags are "touched", they + are only accessed, but not modified. In this way, the atime of a flag file + reflects when the last check was performed and the mtime reflects when + when the state was last changed. + + This package provides the following checks: + - `has_default_gw6` - check whether the router has a default IPv6-route on br-client. + + The checks are executed once every minute (by micron.d). +endef + +$(eval $(call BuildPackageGluon,gluon-state-check)) diff --git a/package/gluon-state-check/files/lib/gluon/state/check.d/has_default_gw6 b/package/gluon-state-check/files/lib/gluon/state/check.d/has_default_gw6 new file mode 100755 index 00000000..d099a60d --- /dev/null +++ b/package/gluon-state-check/files/lib/gluon/state/check.d/has_default_gw6 @@ -0,0 +1,2 @@ +#!/bin/sh +out=$(ip -6 route show default dev br-client 2>/dev/null) && [ -n "$out" ] diff --git a/package/gluon-state-check/files/usr/lib/micron.d/gluon-state-check b/package/gluon-state-check/files/usr/lib/micron.d/gluon-state-check new file mode 100644 index 00000000..312dadfb --- /dev/null +++ b/package/gluon-state-check/files/usr/lib/micron.d/gluon-state-check @@ -0,0 +1 @@ +* * * * * /usr/sbin/gluon-state-check diff --git a/package/gluon-state-check/luasrc/usr/sbin/gluon-state-check b/package/gluon-state-check/luasrc/usr/sbin/gluon-state-check new file mode 100755 index 00000000..04348f9f --- /dev/null +++ b/package/gluon-state-check/luasrc/usr/sbin/gluon-state-check @@ -0,0 +1,41 @@ +#!/usr/bin/lua + +local util = require 'gluon.util' +local unistd = require 'posix.unistd' + +local state_dir = "/var/gluon/state/" +local check_dir = "/lib/gluon/state/check.d/" + + +local function set_flag(stateflag, state) + if state then + -- this does not modify atime + local flaghandle = io.open(stateflag, "w") + flaghandle:close() + else + os.remove(stateflag) + end +end + +local function exec_check(checkpath) + local checkname = string.sub(checkpath, #check_dir+1) + local ret = os.execute(checkpath) + local flagfile = state_dir..checkname + set_flag(flagfile, 0==ret) +end + +local function run_executable_checks() + for _, v in ipairs(util.glob(check_dir..'*')) do + if unistd.access(v, 'x') then + exec_check(v) + end + end +end + + +-- ensure state path exists +if not unistd.access(state_dir) then + os.execute("mkdir -p "..state_dir) +end + +run_executable_checks()