]>
Commit | Line | Data |
---|---|---|
e4a945cd IB |
1 | { lib, pkgs, config, mylibs, ... }: |
2 | let | |
3 | cfg = config.services.myWebsites.commons.stats; | |
4 | in { | |
5 | options = { | |
6 | services.myWebsites.commons.stats = { | |
7 | enable = lib.mkEnableOption "enable statistics"; | |
8 | sites = lib.mkOption { | |
a5365ec3 IB |
9 | type = lib.types.listOf (lib.types.submodule { |
10 | options = { | |
11 | conf = lib.mkOption { type = lib.types.path; }; | |
12 | name = lib.mkOption { type = lib.types.string; }; | |
13 | }; | |
14 | }); | |
e4a945cd IB |
15 | default = []; |
16 | description = "Sites to generate stats"; | |
17 | }; | |
18 | }; | |
19 | }; | |
20 | ||
21 | config = lib.mkIf cfg.enable { | |
ee5c6141 | 22 | users.users.root.packages = [ |
e4a945cd IB |
23 | pkgs.goaccess |
24 | ]; | |
25 | ||
e4a945cd IB |
26 | services.cron = { |
27 | enable = true; | |
28 | systemCronJobs = let | |
29 | stats = domain: conf: let | |
30 | d = pkgs.writeScriptBin "stats-${domain}" '' | |
31 | #!${pkgs.stdenv.shell} | |
32 | set -e | |
33 | shopt -s nullglob | |
34 | date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y') | |
35 | TMPFILE=$(mktemp) | |
36 | trap "rm -f $TMPFILE" EXIT | |
37 | ||
38 | cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE | |
39 | for i in /var/log/httpd/access_log-${domain}*.gz; do | |
40 | zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE | |
41 | done | |
ee5c6141 | 42 | ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf} |
e4a945cd IB |
43 | ''; |
44 | in "${d}/bin/stats-${domain}"; | |
7c059af8 IB |
45 | allStats = sites: pkgs.writeScript "stats" '' |
46 | #!${pkgs.stdenv.shell} | |
47 | ||
48 | ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)} | |
49 | ''; | |
e4a945cd | 50 | in |
7c059af8 IB |
51 | [ |
52 | "5 0 * * * root ${allStats cfg.sites}" | |
53 | ]; | |
e4a945cd IB |
54 | }; |
55 | ||
e4a945cd IB |
56 | system.activationScripts.goaccess = '' |
57 | mkdir -p /var/lib/goaccess | |
58 | '' + | |
59 | builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites); | |
60 | }; | |
61 | } |