]>
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 { | |
22 | environment.systemPackages = [ | |
23 | pkgs.goaccess | |
24 | ]; | |
25 | ||
2368a4b7 IB |
26 | nixpkgs.overlays = [ (self: super: rec { |
27 | goaccess = super.goaccess.overrideAttrs(old: rec { | |
e4a945cd | 28 | configureFlags = old.configureFlags ++ [ "--enable-tcb=btree" ]; |
2368a4b7 | 29 | buildInputs = old.buildInputs ++ [ self.tokyocabinet self.bzip2 ]; |
e4a945cd | 30 | }); |
2368a4b7 | 31 | }) ]; |
e4a945cd IB |
32 | |
33 | services.cron = { | |
34 | enable = true; | |
35 | systemCronJobs = let | |
36 | stats = domain: conf: let | |
37 | d = pkgs.writeScriptBin "stats-${domain}" '' | |
38 | #!${pkgs.stdenv.shell} | |
39 | set -e | |
40 | shopt -s nullglob | |
41 | date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y') | |
42 | TMPFILE=$(mktemp) | |
43 | trap "rm -f $TMPFILE" EXIT | |
44 | ||
45 | cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE | |
46 | for i in /var/log/httpd/access_log-${domain}*.gz; do | |
47 | zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE | |
48 | done | |
49 | goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf} | |
50 | ''; | |
51 | in "${d}/bin/stats-${domain}"; | |
7c059af8 IB |
52 | allStats = sites: pkgs.writeScript "stats" '' |
53 | #!${pkgs.stdenv.shell} | |
54 | ||
55 | ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)} | |
56 | ''; | |
e4a945cd | 57 | in |
7c059af8 IB |
58 | [ |
59 | "5 0 * * * root ${allStats cfg.sites}" | |
60 | ]; | |
e4a945cd IB |
61 | }; |
62 | ||
e4a945cd IB |
63 | system.activationScripts.goaccess = '' |
64 | mkdir -p /var/lib/goaccess | |
65 | '' + | |
66 | builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites); | |
67 | }; | |
68 | } |