nixpkgs/nixos/modules/services/networking/smokeping.nix

363 lines
12 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.smokeping;
smokepingHome = "/var/lib/smokeping";
smokepingPidDir = "/run";
configFile =
if cfg.config == null
then
''
*** General ***
2017-01-03 22:08:26 +00:00
cgiurl = ${cfg.cgiUrl}
contact = ${cfg.ownerEmail}
2017-01-03 22:08:26 +00:00
datadir = ${smokepingHome}/data
imgcache = ${smokepingHome}/cache
imgurl = ${cfg.imgUrl}
2017-01-03 22:08:26 +00:00
linkstyle = ${cfg.linkStyle}
${lib.optionalString (cfg.mailHost != "") "mailhost = ${cfg.mailHost}"}
owner = ${cfg.owner}
pagedir = ${smokepingHome}/cache
piddir = ${smokepingPidDir}
2017-01-03 22:08:26 +00:00
${lib.optionalString (cfg.sendmail != null) "sendmail = ${cfg.sendmail}"}
smokemail = ${cfg.smokeMailTemplate}
*** Presentation ***
template = ${cfg.presentationTemplate}
${cfg.presentationConfig}
*** Alerts ***
${cfg.alertConfig}
*** Database ***
${cfg.databaseConfig}
*** Probes ***
${cfg.probeConfig}
*** Targets ***
${cfg.targetConfig}
${cfg.extraConfig}
''
else
cfg.config;
configPath = pkgs.writeText "smokeping.conf" configFile;
cgiHome = pkgs.writeScript "smokeping.fcgi" ''
#!${pkgs.bash}/bin/bash
${cfg.package}/bin/smokeping_cgi ${configPath}
'';
in
{
options = {
services.smokeping = {
enable = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Enable the smokeping service";
};
2017-01-03 22:08:26 +00:00
alertConfig = mkOption {
type = types.lines;
2017-01-03 22:08:26 +00:00
default = ''
to = root@localhost
from = smokeping@localhost
'';
example = ''
2017-01-03 22:08:26 +00:00
to = alertee@address.somewhere
from = smokealert@company.xy
2017-01-03 22:08:26 +00:00
+someloss
type = loss
# in percent
pattern = >0%,*12*,>0%,*12*,>0%
comment = loss 3 times in a row;
'';
description = lib.mdDoc "Configuration for alerts.";
};
cgiUrl = mkOption {
type = types.str;
default = "http://${cfg.hostName}:${toString cfg.port}/smokeping.cgi";
defaultText = literalExpression ''"http://''${hostName}:''${toString port}/smokeping.cgi"'';
example = "https://somewhere.example.com/smokeping.cgi";
description = lib.mdDoc "URL to the smokeping cgi.";
};
2017-01-03 22:08:26 +00:00
config = mkOption {
type = types.nullOr types.lines;
2017-01-03 22:08:26 +00:00
default = null;
description = "Full smokeping config supplied by the user. Overrides " +
"and replaces any other configuration supplied.";
};
databaseConfig = mkOption {
type = types.lines;
default = ''
step = 300
pings = 20
# consfn mrhb steps total
AVERAGE 0.5 1 1008
AVERAGE 0.5 12 4320
MIN 0.5 12 4320
MAX 0.5 12 4320
AVERAGE 0.5 144 720
MAX 0.5 144 720
MIN 0.5 144 720
'';
example = ''
# near constant pings.
2019-06-17 10:01:51 +00:00
step = 30
pings = 20
# consfn mrhb steps total
AVERAGE 0.5 1 10080
AVERAGE 0.5 12 43200
MIN 0.5 12 43200
MAX 0.5 12 43200
AVERAGE 0.5 144 7200
MAX 0.5 144 7200
MIN 0.5 144 7200
'';
description = lib.mdDoc ''Configure the ping frequency and retention of the rrd files.
Once set, changing the interval will require deletion or migration of all
the collected data.'';
};
2017-01-03 22:08:26 +00:00
extraConfig = mkOption {
type = types.lines;
default = "";
description = lib.mdDoc "Any additional customization not already included.";
2017-01-03 22:08:26 +00:00
};
hostName = mkOption {
type = types.str;
default = config.networking.fqdn;
defaultText = literalExpression "config.networking.fqdn";
2017-01-03 22:08:26 +00:00
example = "somewhere.example.com";
description = lib.mdDoc "DNS name for the urls generated in the cgi.";
};
2017-01-03 22:08:26 +00:00
imgUrl = mkOption {
type = types.str;
default = "cache";
defaultText = literalExpression ''"cache"'';
2017-01-03 22:08:26 +00:00
example = "https://somewhere.example.com/cache";
description = lib.mdDoc ''
Base url for images generated in the cgi.
The default is a relative URL to ensure it works also when e.g. forwarding
the GUI port via SSH.
'';
2017-01-03 22:08:26 +00:00
};
linkStyle = mkOption {
type = types.enum ["original" "absolute" "relative"];
default = "relative";
example = "absolute";
description = lib.mdDoc "DNS name for the urls generated in the cgi.";
2017-01-03 22:08:26 +00:00
};
mailHost = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "";
example = "localhost";
description = lib.mdDoc "Use this SMTP server to send alerts";
2017-01-03 22:08:26 +00:00
};
owner = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "nobody";
example = "Bob Foobawr";
description = lib.mdDoc "Real name of the owner of the instance";
2017-01-03 22:08:26 +00:00
};
ownerEmail = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "no-reply@${cfg.hostName}";
defaultText = literalExpression ''"no-reply@''${hostName}"'';
2017-01-03 22:08:26 +00:00
example = "no-reply@yourdomain.com";
description = lib.mdDoc "Email contact for owner";
2017-01-03 22:08:26 +00:00
};
package = mkOption {
type = types.package;
default = pkgs.smokeping;
defaultText = literalExpression "pkgs.smokeping";
description = lib.mdDoc "Specify a custom smokeping package";
2017-01-03 22:08:26 +00:00
};
host = mkOption {
type = types.nullOr types.str;
default = "localhost";
example = "192.0.2.1"; # rfc5737 example IP for documentation
description = lib.mdDoc ''
Host/IP to bind to for the web server.
Setting it to `null` skips passing the -h option to thttpd,
which makes it bind to all interfaces.
'';
};
2017-01-03 22:08:26 +00:00
port = mkOption {
type = types.int;
default = 8081;
description = lib.mdDoc "TCP port to use for the web server.";
};
presentationConfig = mkOption {
type = types.lines;
default = ''
+ charts
menu = Charts
title = The most interesting destinations
++ stddev
sorter = StdDev(entries=>4)
title = Top Standard Deviation
menu = Std Deviation
format = Standard Deviation %f
++ max
sorter = Max(entries=>5)
title = Top Max Roundtrip Time
menu = by Max
format = Max Roundtrip Time %f seconds
++ loss
sorter = Loss(entries=>5)
title = Top Packet Loss
menu = Loss
format = Packets Lost %f
++ median
sorter = Median(entries=>5)
title = Top Median Roundtrip Time
menu = by Median
format = Median RTT %f seconds
+ overview
width = 600
height = 50
range = 10h
+ detail
width = 600
height = 200
unison_tolerance = 2
"Last 3 Hours" 3h
"Last 30 Hours" 30h
"Last 10 Days" 10d
"Last 360 Days" 360d
'';
description = lib.mdDoc "presentation graph style";
};
2017-01-03 22:08:26 +00:00
presentationTemplate = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "${pkgs.smokeping}/etc/basepage.html.dist";
defaultText = literalExpression ''"''${pkgs.smokeping}/etc/basepage.html.dist"'';
description = lib.mdDoc "Default page layout for the web UI.";
2017-01-03 22:08:26 +00:00
};
probeConfig = mkOption {
type = types.lines;
default = ''
+ FPing
2017-01-29 04:48:03 +00:00
binary = ${config.security.wrapperDir}/fping
'';
defaultText = literalExpression ''
'''
+ FPing
binary = ''${config.security.wrapperDir}/fping
'''
'';
description = lib.mdDoc "Probe configuration";
};
2017-01-03 22:08:26 +00:00
sendmail = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/wrappers/bin/sendmail";
description = lib.mdDoc "Use this sendmail compatible script to deliver alerts";
2017-01-03 22:08:26 +00:00
};
smokeMailTemplate = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "${cfg.package}/etc/smokemail.dist";
defaultText = literalExpression ''"''${package}/etc/smokemail.dist"'';
description = lib.mdDoc "Specify the smokemail template for alerts.";
2017-01-03 22:08:26 +00:00
};
targetConfig = mkOption {
type = types.lines;
default = ''
probe = FPing
menu = Top
title = Network Latency Grapher
remark = Welcome to the SmokePing website of xxx Company. \
Here you will learn all about the latency of our network.
+ Local
menu = Local
title = Local Network
++ LocalMachine
menu = Local Machine
title = This host
host = localhost
'';
description = lib.mdDoc "Target configuration";
};
2017-01-03 22:08:26 +00:00
user = mkOption {
type = types.str;
2017-01-03 22:08:26 +00:00
default = "smokeping";
description = lib.mdDoc "User that runs smokeping and (optionally) thttpd. A group of the same name will be created as well.";
};
2017-01-03 22:08:26 +00:00
webService = mkOption {
type = types.bool;
default = true;
description = lib.mdDoc "Enable a smokeping web interface";
};
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion = !(cfg.sendmail != null && cfg.mailHost != "");
message = "services.smokeping: sendmail and Mailhost cannot both be enabled.";
}
];
2017-01-29 11:33:56 +00:00
security.wrappers = {
fping =
{ setuid = true;
owner = "root";
group = "root";
source = "${pkgs.fping}/bin/fping";
};
2017-01-29 11:33:56 +00:00
};
environment.systemPackages = [ pkgs.fping ];
users.users.${cfg.user} = {
isNormalUser = false;
isSystemUser = true;
group = cfg.user;
description = "smokeping daemon user";
home = smokepingHome;
createHome = true;
};
users.groups.${cfg.user} = {};
systemd.services.smokeping = {
requiredBy = [ "multi-user.target"];
2017-04-12 13:12:53 +00:00
serviceConfig = {
User = cfg.user;
Restart = "on-failure";
ExecStart = "${cfg.package}/bin/smokeping --config=${configPath} --nodaemon";
2017-04-12 13:12:53 +00:00
};
preStart = ''
mkdir -m 0755 -p ${smokepingHome}/cache ${smokepingHome}/data
rm -f ${smokepingHome}/cropper
ln -s ${cfg.package}/htdocs/cropper ${smokepingHome}/cropper
rm -f ${smokepingHome}/smokeping.fcgi
ln -s ${cgiHome} ${smokepingHome}/smokeping.fcgi
${cfg.package}/bin/smokeping --check --config=${configPath}
${cfg.package}/bin/smokeping --static --config=${configPath}
'';
};
systemd.services.thttpd = mkIf cfg.webService {
requiredBy = [ "multi-user.target"];
requires = [ "smokeping.service"];
path = with pkgs; [ bash rrdtool smokeping thttpd ];
serviceConfig = {
Restart = "always";
ExecStart = lib.concatStringsSep " " (lib.concatLists [
[ "${pkgs.thttpd}/bin/thttpd" ]
[ "-u ${cfg.user}" ]
[ ''-c "**.fcgi"'' ]
[ "-d ${smokepingHome}" ]
(lib.optional (cfg.host != null) "-h ${cfg.host}")
[ "-p ${builtins.toString cfg.port}" ]
[ "-D -nos" ]
]);
};
};
};
2019-10-14 19:23:11 +00:00
meta.maintainers = with lib.maintainers; [
erictapen
nh2
];
}