]>
Commit | Line | Data |
---|---|---|
1 | { lib, pkgs, config, ... }: | |
2 | let | |
3 | name = "goaccess"; | |
4 | cfg = config.services.webstats; | |
5 | in { | |
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 { | |
26 | type = lib.types.str; | |
27 | description = '' | |
28 | Domain name. Corresponds to the Apache file name and the | |
29 | folder name in which the state will be saved. | |
30 | ''; | |
31 | }; | |
32 | }; | |
33 | }); | |
34 | default = []; | |
35 | description = "Sites to generate stats"; | |
36 | }; | |
37 | }; | |
38 | ||
39 | config = lib.mkIf (builtins.length cfg.sites > 0) { | |
40 | services.duplyBackup.profiles.goaccess = { | |
41 | rootDir = cfg.dataDir; | |
42 | }; | |
43 | users.users.root.packages = [ | |
44 | pkgs.goaccess | |
45 | ]; | |
46 | ||
47 | services.cron = { | |
48 | enable = true; | |
49 | systemCronJobs = let | |
50 | stats = domain: conf: let | |
51 | config = if builtins.isNull conf | |
52 | then pkgs.runCommand "goaccess.conf" { | |
53 | dbPath = "${cfg.dataDir}/${domain}"; | |
54 | } "substituteAll ${./goaccess.conf} $out" | |
55 | else conf; | |
56 | d = pkgs.writeScriptBin "stats-${domain}" '' | |
57 | #!${pkgs.stdenv.shell} | |
58 | set -e | |
59 | shopt -s nullglob | |
60 | date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y') | |
61 | TMPFILE=$(mktemp) | |
62 | trap "rm -f $TMPFILE" EXIT | |
63 | ||
64 | mkdir -p ${cfg.dataDir}/${domain} | |
65 | cat /var/log/httpd/access-${domain}.log | sed -n "/\\[$date_regex/ p" > $TMPFILE | |
66 | for i in /var/log/httpd/access-${domain}*.gz; do | |
67 | zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE | |
68 | done | |
69 | ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o ${cfg.dataDir}/${domain}/index.html -p ${config} | |
70 | ''; | |
71 | in "${d}/bin/stats-${domain}"; | |
72 | allStats = sites: pkgs.writeScript "stats" '' | |
73 | #!${pkgs.stdenv.shell} | |
74 | ||
75 | mkdir -p ${cfg.dataDir} | |
76 | ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)} | |
77 | ''; | |
78 | in | |
79 | [ | |
80 | "5 0 * * * root ${allStats cfg.sites}" | |
81 | ]; | |
82 | }; | |
83 | }; | |
84 | } |