]>
Commit | Line | Data |
---|---|---|
e4a945cd IB |
1 | { lib, pkgs, config, mylibs, ... }: |
2 | let | |
9eae2b47 IB |
3 | name = "goaccess"; |
4 | cfg = config.services.webstats; | |
e4a945cd | 5 | in { |
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 { | |
26 | type = lib.types.string; | |
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) { |
ee5c6141 | 40 | users.users.root.packages = [ |
e4a945cd IB |
41 | pkgs.goaccess |
42 | ]; | |
43 | ||
e4a945cd IB |
44 | services.cron = { |
45 | enable = true; | |
46 | systemCronJobs = let | |
47 | stats = domain: conf: let | |
b7ee93fc IB |
48 | config = if builtins.isNull conf |
49 | then pkgs.runCommand "goaccess.conf" { | |
9eae2b47 | 50 | dbPath = "${cfg.dataDir}/${domain}"; |
b7ee93fc IB |
51 | } "substituteAll ${./goaccess.conf} $out" |
52 | else conf; | |
e4a945cd IB |
53 | d = pkgs.writeScriptBin "stats-${domain}" '' |
54 | #!${pkgs.stdenv.shell} | |
55 | set -e | |
56 | shopt -s nullglob | |
57 | date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y') | |
58 | TMPFILE=$(mktemp) | |
59 | trap "rm -f $TMPFILE" EXIT | |
60 | ||
61 | cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE | |
62 | for i in /var/log/httpd/access_log-${domain}*.gz; do | |
63 | zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE | |
64 | done | |
9eae2b47 | 65 | ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o ${cfg.dataDir}/${domain}/index.html -p ${config} |
e4a945cd IB |
66 | ''; |
67 | in "${d}/bin/stats-${domain}"; | |
7c059af8 IB |
68 | allStats = sites: pkgs.writeScript "stats" '' |
69 | #!${pkgs.stdenv.shell} | |
70 | ||
71 | ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)} | |
72 | ''; | |
e4a945cd | 73 | in |
7c059af8 IB |
74 | [ |
75 | "5 0 * * * root ${allStats cfg.sites}" | |
76 | ]; | |
e4a945cd IB |
77 | }; |
78 | ||
e4a945cd IB |
79 | system.activationScripts.goaccess = '' |
80 | mkdir -p /var/lib/goaccess | |
81 | '' + | |
9eae2b47 | 82 | builtins.concatStringsSep "\n" (map (v: "mkdir -p ${cfg.dataDir}/${v.name}") cfg.sites); |
e4a945cd IB |
83 | }; |
84 | } |