summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--maintainers/maintainer-list.nix6
-rw-r--r--nixos/modules/hardware/rtl-sdr.nix20
-rw-r--r--nixos/modules/module-list.nix1
-rw-r--r--nixos/modules/services/monitoring/prometheus/exporters.nix3
-rw-r--r--nixos/modules/services/monitoring/prometheus/exporters/rtl_433.nix78
-rw-r--r--nixos/tests/prometheus-exporters.nix31
-rw-r--r--pkgs/applications/radio/rtl-sdr/default.nix8
-rw-r--r--pkgs/servers/monitoring/prometheus/rtl_433-exporter.nix26
-rw-r--r--pkgs/top-level/all-packages.nix1
9 files changed, 173 insertions, 1 deletions
diff --git a/maintainers/maintainer-list.nix b/maintainers/maintainer-list.nix
index 498fa7589042..0cebce87c817 100644
--- a/maintainers/maintainer-list.nix
+++ b/maintainers/maintainer-list.nix
@@ -9624,6 +9624,12 @@
githubId = 1069303;
name = "Kim Simmons";
};
+ zopieux = {
+ email = "zopieux@gmail.com";
+ github = "zopieux";
+ githubId = 81353;
+ name = "Alexandre Macabies";
+ };
zowoq = {
email = "59103226+zowoq@users.noreply.github.com";
github = "zowoq";
diff --git a/nixos/modules/hardware/rtl-sdr.nix b/nixos/modules/hardware/rtl-sdr.nix
new file mode 100644
index 000000000000..77c8cb59a3d5
--- /dev/null
+++ b/nixos/modules/hardware/rtl-sdr.nix
@@ -0,0 +1,20 @@
+{ config, lib, pkgs, ... }:
+
+let
+ cfg = config.hardware.rtl-sdr;
+
+in {
+ options.hardware.rtl-sdr = {
+ enable = lib.mkEnableOption ''
+ Enables rtl-sdr udev rules and ensures 'plugdev' group exists.
+ This is a prerequisite to using devices supported by rtl-sdr without
+ being root, since rtl-sdr USB descriptors will be owned by plugdev
+ through udev.
+ '';
+ };
+
+ config = lib.mkIf cfg.enable {
+ services.udev.packages = [ pkgs.rtl-sdr ];
+ users.groups.plugdev = {};
+ };
+}
diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix
index ed6201237b3d..cce4e8e74b4e 100644
--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -59,6 +59,7 @@
./hardware/pcmcia.nix
./hardware/printers.nix
./hardware/raid/hpsa.nix
+ ./hardware/rtl-sdr.nix
./hardware/steam-hardware.nix
./hardware/system-76.nix
./hardware/tuxedo-keyboard.nix
diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix
index 1233e5cdd1a9..a4aa470f5bc4 100644
--- a/nixos/modules/services/monitoring/prometheus/exporters.nix
+++ b/nixos/modules/services/monitoring/prometheus/exporters.nix
@@ -43,6 +43,7 @@ let
"postgres"
"redis"
"rspamd"
+ "rtl_433"
"snmp"
"surfboard"
"tor"
@@ -224,6 +225,8 @@ in
services.prometheus.exporters.minio.minioAccessSecret = mkDefault config.services.minio.secretKey;
})] ++ [(mkIf config.services.rspamd.enable {
services.prometheus.exporters.rspamd.url = mkDefault "http://localhost:11334/stat";
+ })] ++ [(mkIf config.services.prometheus.exporters.rtl_433.enable {
+ hardware.rtl-sdr.enable = mkDefault true;
})] ++ [(mkIf config.services.nginx.enable {
systemd.services.prometheus-nginx-exporter.after = [ "nginx.service" ];
systemd.services.prometheus-nginx-exporter.requires = [ "nginx.service" ];
diff --git a/nixos/modules/services/monitoring/prometheus/exporters/rtl_433.nix b/nixos/modules/services/monitoring/prometheus/exporters/rtl_433.nix
new file mode 100644
index 000000000000..01e420db3897
--- /dev/null
+++ b/nixos/modules/services/monitoring/prometheus/exporters/rtl_433.nix
@@ -0,0 +1,78 @@
+{ config, lib, pkgs, options }:
+
+let
+ cfg = config.services.prometheus.exporters.rtl_433;
+in
+{
+ port = 9550;
+
+ extraOpts = let
+ mkMatcherOptionType = field: description: with lib.types;
+ listOf (submodule {
+ options = {
+ name = lib.mkOption {
+ type = str;
+ description = "Name to match.";
+ };
+ "${field}" = lib.mkOption {
+ type = int;
+ inherit description;
+ };
+ location = lib.mkOption {
+ type = str;
+ description = "Location to match.";
+ };
+ };
+ });
+ in
+ {
+ rtl433Flags = lib.mkOption {
+ type = lib.types.str;
+ default = "-C si";
+ example = "-C si -R 19";
+ description = ''
+ Flags passed verbatim to rtl_433 binary.
+ Having <literal>-C si</literal> (the default) is recommended since only Celsius temperatures are parsed.
+ '';
+ };
+ channels = lib.mkOption {
+ type = mkMatcherOptionType "channel" "Channel to match.";
+ default = [];
+ example = [
+ { name = "Acurite"; channel = 6543; location = "Kitchen"; }
+ ];
+ description = ''
+ List of channel matchers to export.
+ '';
+ };
+ ids = lib.mkOption {
+ type = mkMatcherOptionType "id" "ID to match.";
+ default = [];
+ example = [
+ { name = "Nexus"; id = 1; location = "Bedroom"; }
+ ];
+ description = ''
+ List of ID matchers to export.
+ '';
+ };
+ };
+
+ serviceOpts = {
+ serviceConfig = {
+ # rtl-sdr udev rules make supported USB devices +rw by plugdev.
+ SupplementaryGroups = "plugdev";
+ ExecStart = let
+ matchers = (map (m:
+ "--channel_matcher '${m.name},${toString m.channel},${m.location}'"
+ ) cfg.channels) ++ (map (m:
+ "--id_matcher '${m.name},${toString m.id},${m.location}'"
+ ) cfg.ids); in ''
+ ${pkgs.prometheus-rtl_433-exporter}/bin/rtl_433_prometheus \
+ -listen ${cfg.listenAddress}:${toString cfg.port} \
+ -subprocess "${pkgs.rtl_433}/bin/rtl_433 -F json ${cfg.rtl433Flags}" \
+ ${lib.concatStringsSep " \\\n " matchers} \
+ ${lib.concatStringsSep " \\\n " cfg.extraFlags}
+ '';
+ };
+ };
+}
diff --git a/nixos/tests/prometheus-exporters.nix b/nixos/tests/prometheus-exporters.nix
index ad2fff2b01f6..2553f5dcf747 100644
--- a/nixos/tests/prometheus-exporters.nix
+++ b/nixos/tests/prometheus-exporters.nix
@@ -563,6 +563,37 @@ let
'';
};
+ rtl_433 = {
+ exporterConfig = {
+ enable = true;
+ };
+ metricProvider = {
+ # Mock rtl_433 binary to return a dummy metric stream.
+ nixpkgs.overlays = [ (self: super: {
+ rtl_433 = self.runCommand "rtl_433" {} ''
+ mkdir -p "$out/bin"
+ cat <<EOF > "$out/bin/rtl_433"
+ #!/bin/sh
+ while true; do
+ printf '{"time" : "2020-04-26 13:37:42", "model" : "zopieux", "id" : 55, "channel" : 3, "temperature_C" : 18.000}\n'
+ sleep 4
+ done
+ EOF
+ chmod +x "$out/bin/rtl_433"
+ '';
+ }) ];
+ };
+ exporterTest = ''
+ wait_for_unit("prometheus-rtl_433-exporter.service")
+ wait_for_open_port(9550)
+ wait_until_succeeds(
+ "curl -sSf localhost:9550/metrics | grep -q '{}'".format(
+ 'rtl_433_temperature_celsius{channel="3",id="55",location="",model="zopieux"} 18'
+ )
+ )
+ '';
+ };
+
snmp = {
exporterConfig = {
enable = true;
diff --git a/pkgs/applications/radio/rtl-sdr/default.nix b/pkgs/applications/radio/rtl-sdr/default.nix
index 8fb5154ff788..7e044296b02c 100644
--- a/pkgs/applications/radio/rtl-sdr/default.nix
+++ b/pkgs/applications/radio/rtl-sdr/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchgit, cmake, pkgconfig, libusb1 }:
+{ stdenv, fetchgit, fetchpatch, cmake, pkgconfig, libusb1 }:
stdenv.mkDerivation rec {
pname = "rtl-sdr";
@@ -10,6 +10,12 @@ stdenv.mkDerivation rec {
sha256 = "0lmvsnb4xw4hmz6zs0z5ilsah5hjz29g1s0050n59fllskqr3b8k";
};
+ patches = [ (fetchpatch {
+ name = "hardened-udev-rules.patch";
+ url = "https://osmocom.org/projects/rtl-sdr/repository/revisions/b2814731563be4d5a0a68554ece6454a2c63af12/diff?format=diff";
+ sha256 = "0ns740s2rys4glq4la4bh0sxfv1mn61yfjns2yllhx70rsb2fqrn";
+ }) ];
+
nativeBuildInputs = [ pkgconfig cmake ];
buildInputs = [ libusb1 ];
diff --git a/pkgs/servers/monitoring/prometheus/rtl_433-exporter.nix b/pkgs/servers/monitoring/prometheus/rtl_433-exporter.nix
new file mode 100644
index 000000000000..502ce8e0d43e
--- /dev/null
+++ b/pkgs/servers/monitoring/prometheus/rtl_433-exporter.nix
@@ -0,0 +1,26 @@
+{ lib, buildGoModule, fetchFromGitHub, bash, nixosTests }:
+
+buildGoModule rec {
+ pname = "rtl_433-exporter";
+ version = "0.1";
+
+ src = fetchFromGitHub {
+ owner = "mhansen";
+ repo = "rtl_433_prometheus";
+ rev = "v${version}";
+ sha256 = "1998gvfa5310bxhi6kfv8bn99369dxph3pwrpp335997b25lc2w2";
+ };
+
+ postPatch = "substituteInPlace rtl_433_prometheus.go --replace /bin/bash ${bash}/bin/bash";
+
+ vendorSha256 = "03mnmzq72844hzyw7iq5g4gm1ihpqkg4i9dgj2yln1ghwk843hq6";
+
+ passthru.tests = { inherit (nixosTests.prometheus-exporters) rtl_433; };
+
+ meta = with lib; {
+ description = "Prometheus time-series DB exporter for rtl_433 433MHz radio packet decoder";
+ homepage = "https://github.com/mhansen/rtl_433_prometheus";
+ license = licenses.mit;
+ maintainers = with maintainers; [ zopieux ];
+ };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index b2f08f229a37..fd731dd97561 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -17058,6 +17058,7 @@ in
prometheus-pushgateway = callPackage ../servers/monitoring/prometheus/pushgateway.nix { };
prometheus-redis-exporter = callPackage ../servers/monitoring/prometheus/redis-exporter.nix { };
prometheus-rabbitmq-exporter = callPackage ../servers/monitoring/prometheus/rabbitmq-exporter.nix { };
+ prometheus-rtl_433-exporter = callPackage ../servers/monitoring/prometheus/rtl_433-exporter.nix { };
prometheus-snmp-exporter = callPackage ../servers/monitoring/prometheus/snmp-exporter.nix { };
prometheus-tor-exporter = callPackage ../servers/monitoring/prometheus/tor-exporter.nix { };
prometheus-statsd-exporter = callPackage ../servers/monitoring/prometheus/statsd-exporter.nix { };