]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/commons/stats.nix
c70730376b4f06b59c85eb19057a77c63e8fb7fc
[perso/Immae/Config/Nix.git] / nixops / modules / websites / commons / stats.nix
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 {
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 });
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
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
42 goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf}
43 '';
44 in "${d}/bin/stats-${domain}";
45 allStats = sites: pkgs.writeScript "stats" ''
46 #!${pkgs.stdenv.shell}
47
48 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
49 '';
50 in
51 [
52 "5 0 * * * root ${allStats cfg.sites}"
53 ];
54 };
55
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 }