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
nixos/unifi-poller: add unifi-poller service #95994
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
{ config, pkgs, lib, ... }: | ||
|
||
with lib; | ||
|
||
let | ||
cfg = config.services.unifi-poller; | ||
|
||
controllerConfig = types.submodule { | ||
options = { | ||
role = mkOption { | ||
type = types.nullOr types.str; | ||
description = "Role allows grouping of controllers. By default grouping is done on the URL."; | ||
default = null; | ||
}; | ||
url = mkOption { | ||
type = types.str; | ||
description = "URL of the Unifi controller."; | ||
example = "https://127.0.0.1:8443"; | ||
}; | ||
username = mkOption { | ||
type = types.str; | ||
description = "Username used to access the unifi controller. The user needs read access."; | ||
example = "unifipoller"; | ||
}; | ||
passwordFile = mkOption { | ||
description = "File that containts the password to access the unifi controller."; | ||
type = types.path; | ||
}; | ||
verifySSL = mkOption { | ||
description = "Wheter to verify the TLS certificate of the unifi controller."; | ||
type = types.bool; | ||
default = false; | ||
}; | ||
}; | ||
}; | ||
controllerToEnv = i: controller: ('' | ||
# Controller ${toString i} | ||
export UP_UNIFI_CONTROLLER_${toString i}_URL="${controller.url}" | ||
export UP_UNIFI_CONTROLLER_${toString i}_USER="${controller.username}" | ||
export UP_UNIFI_CONTROLLER_${toString i}_PASS="$(cat ${escapeShellArg controller.passwordFile})" | ||
export UP_UNIFI_CONTROLLER_${toString i}_VERIFY_SSL="${if controller.verifySSL then "true" else "false"}" | ||
'' | ||
+ lib.optionalString (controller.role != null) '' | ||
export UP_UNIFI_CONTROLLER_${toString i}_ROLE="${controller.role}" | ||
''); | ||
controllerEnvOptions = foldr (l: r: l + r) "" (imap0 controllerToEnv cfg.controllers); | ||
|
||
influxdbEnvOptions = optionalString (cfg.influxdb.url != null) '' | ||
# InfluxDB | ||
export UP_INFLUXDB_URL="${cfg.influxdb.url}" | ||
export UP_INFLUXDB_DB="${cfg.influxdb.db}" | ||
export UP_INFLUXDB_USER="${cfg.influxdb.username}" | ||
export UP_INFLUXDB_PASS="$(cat ${escapeShellArg cfg.influxdb.passwordFile})" | ||
export UP_INFLUXDB_INTERVAL="${cfg.influxdb.interval}" | ||
''; | ||
|
||
prometheusEnvOptions = '' | ||
# Prometheus | ||
export UP_PROMETHEUS_DISABLE="${if cfg.prometheus.enable then "false" else "true"}" | ||
export UP_PROMETHEUS_HTTP_LISTEN="${cfg.prometheus.httpListen}" | ||
''; | ||
|
||
in { | ||
options = { | ||
services.unifi-poller = { | ||
enable = mkEnableOption "unifi-poller service"; | ||
|
||
controllers = mkOption { | ||
description = "Unifi controller configuration"; | ||
type = types.listOf controllerConfig; | ||
}; | ||
|
||
prometheus = { | ||
enable = mkOption { | ||
description = "Enable the prometheus exporter."; | ||
type = types.bool; | ||
default = true; | ||
}; | ||
|
||
httpListen = mkOption { | ||
description = "Address and port to listen for incomming prometheus requests."; | ||
type = types.str; | ||
default = "0.0.0.0:9130"; | ||
}; | ||
}; | ||
|
||
influxdb = { | ||
url = mkOption { | ||
type = types.nullOr types.str; | ||
description = "URL of the influx database server."; | ||
example = "http://127.0.0.1:8086"; | ||
default = null; | ||
}; | ||
db = mkOption { | ||
type = types.str; | ||
description = "Name of influx database. Must already exist and be writable."; | ||
default = "unifi"; | ||
}; | ||
username = mkOption { | ||
type = types.str; | ||
description = "Username to access the influx database."; | ||
}; | ||
passwordFile = mkOption { | ||
description = "File that containts the password to access the influx database."; | ||
type = types.path; | ||
}; | ||
interval = mkOption { | ||
type = types.str; | ||
description = "How often to poll and collect metrics."; | ||
example = "1m"; | ||
default = "30s"; | ||
}; | ||
}; | ||
}; | ||
}; | ||
config = mkIf cfg.enable { | ||
systemd.services.unifi-poller = { | ||
description = "Unifi poller service"; | ||
wantedBy = ["multi-user.target"]; | ||
after = ["networking.target"]; | ||
script = '' | ||
${prometheusEnvOptions} | ||
${controllerEnvOptions} | ||
${influxdbEnvOptions} | ||
|
||
# We configure unifi-poller via env variable, but it still tries to load a config file from /etc | ||
# so we pass /dev/null as config file which is sufficient as the content of the file is not used. | ||
exec ${pkgs.unifi-poller}/bin/unifi-poller -c /dev/null | ||
''; | ||
serviceConfig = { | ||
DynamicUser = true; | ||
}; | ||
}; | ||
}; | ||
meta.maintainers = with lib.maintainers; [ bachp ]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ stdenv, buildGoModule, fetchFromGitHub }: | ||
|
||
buildGoModule rec { | ||
pname = "unifi-poller"; | ||
version = "2.0.1"; | ||
|
||
src = fetchFromGitHub { | ||
owner = "unifi-poller"; | ||
repo = "unifi-poller"; | ||
rev = "v${version}"; | ||
sha256 = "16q9hrbl9qgilj3vb7865l1yx0xhm7m4sx5j1ys5vi63drq59g93"; | ||
}; | ||
|
||
vendorSha256 = "1fgcbg34g0a0f85qv7bjanv2lpnnszcrspfppp2lnj9kv52j4c1w"; | ||
|
||
subPackages = [ "." ]; | ||
|
||
buildFlagsArray = [ "-ldflags=-s -w -X github.com/prometheus/common/version.Version=${version}" ]; | ||
|
||
meta = with stdenv.lib; { | ||
description = "Unifi-polles collects UniFi Controller, Site, Device & Client Data - Export to InfluxDB or Prometheus"; | ||
homepage = "https://github.com/unifi-poller/unifi-poller"; | ||
license = licenses.mit; | ||
maintainers = with maintainers; [ bachp ]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this is almost no safer than storing a password in the nix store because of
DynamicUser
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the alternatives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally using LoadCredential. Until that lands in NixOS the better option would be a script in
ExecStart
which is executed asroot
and copiespasswordFile
into the privateWorkingDirectory
StateDirectory
(or such) folder so theDynamicUser
service could read it (but no one else). As a last resort you could avoidDynamicUser
... but then you are foregoing certain security features in favour of a securepasswordFile
option which is not ideal.