From ef65b728d30335c1fca72e93504c205b547f0001 Mon Sep 17 00:00:00 2001 From: Evils Date: Tue, 24 Mar 2020 23:21:03 +0100 Subject: [PATCH 1/3] tuptime: init at 4.1.0 --- pkgs/tools/system/tuptime/default.nix | 35 +++++++++++++++++++++++++++ pkgs/top-level/all-packages.nix | 2 ++ 2 files changed, 37 insertions(+) create mode 100644 pkgs/tools/system/tuptime/default.nix diff --git a/pkgs/tools/system/tuptime/default.nix b/pkgs/tools/system/tuptime/default.nix new file mode 100644 index 000000000000..acc96998952d --- /dev/null +++ b/pkgs/tools/system/tuptime/default.nix @@ -0,0 +1,35 @@ +{ stdenv, fetchFromGitHub, python3 }: + +stdenv.mkDerivation rec { + pname = "tuptime"; + version = "4.1.0"; + + src = fetchFromGitHub { + owner = "rfrail3"; + repo = "tuptime"; + rev = version; + sha256 = "0p5v1jp6bl0hjv04q3gh11q6dx9z0x61h6svcbvwp5ni0h1bkz1a"; + }; + + buildInputs = [ python3 ]; + + installPhase = '' + mkdir -p $out/bin + install -m 755 src/tuptime $out/bin/ + + mkdir -p $out/share/man/man1 + cp src/man/tuptime.1 $out/share/man/man1/ + + # upstream only ships this, there are more scripts there... + mkdir -p $out/usr/share/doc/tuptime/contrib + cp misc/scripts/uptimed-to-tuptime.py $out/usr/share/doc/tuptime/contrib/ + ''; + + meta = with stdenv.lib; { + description = "Total uptime & downtime statistics utility"; + homepage = "https://github.com/rfrail3/tuptime"; + license = licenses.gpl2; + platforms = platforms.all; + maintainers = [ maintainers.evils ]; + }; +} diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index eba10401d5cb..8527751e9bb5 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -7082,6 +7082,8 @@ in tuir = callPackage ../applications/misc/tuir { }; + tuptime = callPackage ../tools/system/tuptime { }; + turses = callPackage ../applications/networking/instant-messengers/turses { }; oysttyer = callPackage ../applications/networking/instant-messengers/oysttyer { }; From b29d48acfb9d1b489996726694030c5e3fc540c1 Mon Sep 17 00:00:00 2001 From: Evils Date: Tue, 24 Mar 2020 23:45:20 +0100 Subject: [PATCH 2/3] nixos/tuptime: init module --- nixos/modules/module-list.nix | 1 + nixos/modules/services/monitoring/tuptime.nix | 84 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 nixos/modules/services/monitoring/tuptime.nix diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index c3d2bb85809e..8cadc9d16290 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -545,6 +545,7 @@ ./services/monitoring/teamviewer.nix ./services/monitoring/telegraf.nix ./services/monitoring/thanos.nix + ./services/monitoring/tuptime.nix ./services/monitoring/ups.nix ./services/monitoring/uptime.nix ./services/monitoring/vnstat.nix diff --git a/nixos/modules/services/monitoring/tuptime.nix b/nixos/modules/services/monitoring/tuptime.nix new file mode 100644 index 000000000000..731260a5c20a --- /dev/null +++ b/nixos/modules/services/monitoring/tuptime.nix @@ -0,0 +1,84 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + + cfg = config.services.tuptime; + +in { + + options.services.tuptime = { + + enable = mkEnableOption "the total uptime service"; + + timer = { + enable = mkOption { + type = types.bool; + default = true; + description = "Whether to regularly log uptime to detect bad shutdowns."; + }; + + period = mkOption { + type = types.str; + default = "*:0/5"; + description = "systemd calendar event"; + }; + }; + }; + + + config = mkIf cfg.enable { + + environment.systemPackages = [ pkgs.tuptime ]; + + users.users.tuptime.description = "tuptime database owner"; + + systemd = { + services = { + + tuptime = { + description = "the total uptime service"; + documentation = [ "man:tuptime(1)" ]; + after = [ "time-sync.target" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + StateDirectory = "tuptime"; + Type = "oneshot"; + User = "tuptime"; + RemainAfterExit = true; + ExecStart = "${pkgs.tuptime}/bin/tuptime -x"; + ExecStop = "${pkgs.tuptime}/bin/tuptime -xg"; + }; + }; + + tuptime-oneshot = mkIf cfg.timer.enable { + description = "the tuptime scheduled execution unit"; + serviceConfig = { + StateDirectory = "tuptime"; + Type = "oneshot"; + User = "tuptime"; + ExecStart = "${pkgs.tuptime}/bin/tuptime -x"; + }; + }; + }; + + timers.tuptime = mkIf cfg.timer.enable { + description = "the tuptime scheduled execution timer"; + # this timer should be started if the service is started + # even if the timer was previously stopped + wantedBy = [ "tuptime.service" "timers.target" ]; + # this timer should be stopped if the service is stopped + partOf = [ "tuptime.service" ]; + timerConfig = { + OnBootSec = "1min"; + OnCalendar = cfg.timer.period; + Unit = "tuptime-oneshot.service"; + }; + }; + }; + }; + + meta.maintainers = [ maintainers.evils ]; + +} From b7b99c93f5a56aca8c726045c36f32bc3b3ed544 Mon Sep 17 00:00:00 2001 From: Evils Date: Tue, 24 Mar 2020 23:45:49 +0100 Subject: [PATCH 3/3] nixosTests.tuptime: init test --- nixos/tests/all-tests.nix | 1 + nixos/tests/tuptime.nix | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 nixos/tests/tuptime.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 2bdced6a3cc1..d059f55adb5d 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -308,6 +308,7 @@ in trilium-server = handleTestOn ["x86_64-linux"] ./trilium-server.nix {}; trezord = handleTest ./trezord.nix {}; trickster = handleTest ./trickster.nix {}; + tuptime = handleTest ./tuptime.nix {}; udisks2 = handleTest ./udisks2.nix {}; upnp = handleTest ./upnp.nix {}; uwsgi = handleTest ./uwsgi.nix {}; diff --git a/nixos/tests/tuptime.nix b/nixos/tests/tuptime.nix new file mode 100644 index 000000000000..36ce2b1ae192 --- /dev/null +++ b/nixos/tests/tuptime.nix @@ -0,0 +1,29 @@ +import ./make-test-python.nix ({ pkgs, ...} : { + name = "tuptime"; + meta = with pkgs.stdenv.lib.maintainers; { + maintainers = [ evils ]; + }; + + machine = { pkgs, ... }: { + imports = [ ../modules/profiles/minimal.nix ]; + services.tuptime.enable = true; + }; + + testScript = + '' + # see if it starts + start_all() + machine.wait_for_unit("multi-user.target") + machine.succeed("tuptime | grep 'System startups:[[:blank:]]*1'") + machine.succeed("tuptime | grep 'System uptime:[[:blank:]]*100.0%'") + machine.shutdown() + + # restart machine and see if it correctly reports the reboot + machine.start() + machine.wait_for_unit("multi-user.target") + machine.succeed("tuptime | grep 'System startups:[[:blank:]]*2'") + machine.succeed("tuptime | grep 'System shutdowns:[[:blank:]]*1 ok'") + machine.shutdown() + ''; +}) +