diff --git a/modules/all-modules.nix b/modules/all-modules.nix index 9afff7d..14aefa0 100644 --- a/modules/all-modules.nix +++ b/modules/all-modules.nix @@ -20,6 +20,7 @@ ./mount ./network ./ntp + ./openwrt-prometheus-exporter ./outputs.nix ./outputs/ext4fs.nix ./outputs/initramfs.nix diff --git a/modules/openwrt-prometheus-exporter/default.nix b/modules/openwrt-prometheus-exporter/default.nix new file mode 100644 index 0000000..e109b1c --- /dev/null +++ b/modules/openwrt-prometheus-exporter/default.nix @@ -0,0 +1,25 @@ +{ lib, pkgs, config, ... }: +let + inherit (lib) mkOption types; + inherit (pkgs) liminix; +in { + options = { + system.service.openwrt-prometheus-exporter = mkOption { + type = liminix.lib.types.serviceDefn; + }; + }; + + config = { + system.service.openwrt-prometheus-exporter = liminix.callService ./service.nix { + httpPorts = mkOption { + type = types.listOf types.port; + default = [ ]; + }; + + httpsPorts = mkOption { + type = types.listOf types.port; + default = [ ]; + }; + }; + }; +} diff --git a/modules/openwrt-prometheus-exporter/service.nix b/modules/openwrt-prometheus-exporter/service.nix new file mode 100644 index 0000000..d4ee382 --- /dev/null +++ b/modules/openwrt-prometheus-exporter/service.nix @@ -0,0 +1,38 @@ +{ + liminix, + lib, + luasocket, + lua, + uhttpd, + ubus, + openwrt-prometheus-exporter +}: +{ httpPorts, httpsPorts }: +let + inherit (liminix.services) longrun; + + package = openwrt-prometheus-exporter; + + luaPath = lib.concatStringsSep ";" [ + "$LUA_PATH" + "${luasocket}/share/lua/${lua.luaversion}/?.lua" + "${package}/lib/lua/?.lua" + ]; + + luaCPath = lib.concatStringsSep ";" [ + "$LUA_CPATH" + "${luasocket}/lib/lua/${lua.luaversion}/?.so" + "${ubus}/lib/lua/${lua.luaversion}/?.so" + ]; +in longrun { + name = "openwrt-prometheus-exporter"; + run = '' + export LD_LIBRARY_PATH=${uhttpd}/lib/:$LD_LIBRARY_PATH + export LUA_CPATH="${luaCPath}" + export LUA_PATH="${luaPath}" + + ${uhttpd}/bin/uhttpd -f -c /dev/null -l / -L ${package}/bin/openwrt-prometheus-exporter \ + ${lib.concatStringsSep " " (lib.map (port: "-p ${toString port}") httpPorts)} \ + ${lib.concatStringsSep " " (lib.map (port: "-s ${toString port}") httpsPorts)} + ''; +}