]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/modules/websites/commons/stats.nix
Make goaccess a template
[perso/Immae/Config/Nix.git] / nixops / modules / websites / commons / stats.nix
CommitLineData
e4a945cd
IB
1{ lib, pkgs, config, mylibs, ... }:
2let
3 cfg = config.services.myWebsites.commons.stats;
4in {
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 = {
b7ee93fc
IB
11 conf = lib.mkOption {
12 type = lib.types.nullOr lib.types.path;
13 default = null;
14 };
a5365ec3
IB
15 name = lib.mkOption { type = lib.types.string; };
16 };
17 });
e4a945cd
IB
18 default = [];
19 description = "Sites to generate stats";
20 };
21 };
22 };
23
24 config = lib.mkIf cfg.enable {
ee5c6141 25 users.users.root.packages = [
e4a945cd
IB
26 pkgs.goaccess
27 ];
28
e4a945cd
IB
29 services.cron = {
30 enable = true;
31 systemCronJobs = let
32 stats = domain: conf: let
b7ee93fc
IB
33 config = if builtins.isNull conf
34 then pkgs.runCommand "goaccess.conf" {
35 dbPath = "/var/lib/goaccess/${domain}";
36 } "substituteAll ${./goaccess.conf} $out"
37 else conf;
e4a945cd
IB
38 d = pkgs.writeScriptBin "stats-${domain}" ''
39 #!${pkgs.stdenv.shell}
40 set -e
41 shopt -s nullglob
42 date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y')
43 TMPFILE=$(mktemp)
44 trap "rm -f $TMPFILE" EXIT
45
46 cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE
47 for i in /var/log/httpd/access_log-${domain}*.gz; do
48 zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE
49 done
b7ee93fc 50 ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${config}
e4a945cd
IB
51 '';
52 in "${d}/bin/stats-${domain}";
7c059af8
IB
53 allStats = sites: pkgs.writeScript "stats" ''
54 #!${pkgs.stdenv.shell}
55
56 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
57 '';
e4a945cd 58 in
7c059af8
IB
59 [
60 "5 0 * * * root ${allStats cfg.sites}"
61 ];
e4a945cd
IB
62 };
63
e4a945cd
IB
64 system.activationScripts.goaccess = ''
65 mkdir -p /var/lib/goaccess
66 '' +
67 builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);
68 };
69}