]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/webapps/webstats/default.nix
Upgrade goaccess and adjust parsing
[perso/Immae/Config/Nix.git] / modules / webapps / webstats / default.nix
CommitLineData
a1a8649a 1{ lib, pkgs, config, ... }:
e4a945cd 2let
9eae2b47
IB
3 name = "goaccess";
4 cfg = config.services.webstats;
e4a945cd 5in {
9eae2b47
IB
6 options.services.webstats = {
7 dataDir = lib.mkOption {
8 type = lib.types.path;
9 default = "/var/lib/${name}";
10 description = ''
11 The directory where Goaccess stores its data.
12 '';
13 };
14 sites = lib.mkOption {
15 type = lib.types.listOf (lib.types.submodule {
16 options = {
17 conf = lib.mkOption {
18 type = lib.types.nullOr lib.types.path;
19 default = null;
20 description = ''
21 use custom goaccess configuration file instead of the
22 default one.
23 '';
24 };
25 name = lib.mkOption {
5400b9b6 26 type = lib.types.str;
9eae2b47
IB
27 description = ''
28 Domain name. Corresponds to the Apache file name and the
29 folder name in which the state will be saved.
30 '';
a5365ec3 31 };
9eae2b47
IB
32 };
33 });
34 default = [];
35 description = "Sites to generate stats";
e4a945cd
IB
36 };
37 };
38
9eae2b47 39 config = lib.mkIf (builtins.length cfg.sites > 0) {
d2e703c5 40 services.duplyBackup.profiles.goaccess = {
6a8252b1
IB
41 rootDir = cfg.dataDir;
42 };
ee5c6141 43 users.users.root.packages = [
e4a945cd
IB
44 pkgs.goaccess
45 ];
46
e4a945cd
IB
47 services.cron = {
48 enable = true;
49 systemCronJobs = let
50 stats = domain: conf: let
b7ee93fc
IB
51 config = if builtins.isNull conf
52 then pkgs.runCommand "goaccess.conf" {
9eae2b47 53 dbPath = "${cfg.dataDir}/${domain}";
b7ee93fc
IB
54 } "substituteAll ${./goaccess.conf} $out"
55 else conf;
e4a945cd
IB
56 d = pkgs.writeScriptBin "stats-${domain}" ''
57 #!${pkgs.stdenv.shell}
58 set -e
59 shopt -s nullglob
e4a945cd
IB
60 TMPFILE=$(mktemp)
61 trap "rm -f $TMPFILE" EXIT
62
2a5cde8d 63 mkdir -p ${cfg.dataDir}/${domain}
9129f327 64 for i in /var/log/httpd/access-${domain}*.gz; do
b1b8a562 65 zcat "$i" >> $TMPFILE
e4a945cd 66 done
b1b8a562 67 cat /var/log/httpd/access-${domain}.log > $TMPFILE
9eae2b47 68 ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o ${cfg.dataDir}/${domain}/index.html -p ${config}
e4a945cd
IB
69 '';
70 in "${d}/bin/stats-${domain}";
7c059af8
IB
71 allStats = sites: pkgs.writeScript "stats" ''
72 #!${pkgs.stdenv.shell}
73
2a5cde8d 74 mkdir -p ${cfg.dataDir}
7c059af8
IB
75 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
76 '';
e4a945cd 77 in
7c059af8
IB
78 [
79 "5 0 * * * root ${allStats cfg.sites}"
80 ];
e4a945cd 81 };
e4a945cd
IB
82 };
83}