aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/websites/commons/stats.nix
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2019-05-11 10:23:33 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2019-05-11 10:23:33 +0200
commit9eae2b47b7b315b05a0e010f3003bd875685e260 (patch)
tree43c9cfeb2db393f64743daa4ec87e0fe78ab772e /nixops/modules/websites/commons/stats.nix
parentb7ee93fcdee2509cd4c0caec2c5c59ccff5bab2c (diff)
downloadNix-9eae2b47b7b315b05a0e010f3003bd875685e260.tar.gz
Nix-9eae2b47b7b315b05a0e010f3003bd875685e260.tar.zst
Nix-9eae2b47b7b315b05a0e010f3003bd875685e260.zip
Move webstats outside of nixops
Diffstat (limited to 'nixops/modules/websites/commons/stats.nix')
-rw-r--r--nixops/modules/websites/commons/stats.nix69
1 files changed, 0 insertions, 69 deletions
diff --git a/nixops/modules/websites/commons/stats.nix b/nixops/modules/websites/commons/stats.nix
deleted file mode 100644
index 73595f1..0000000
--- a/nixops/modules/websites/commons/stats.nix
+++ /dev/null
@@ -1,69 +0,0 @@
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 {
9 type = lib.types.listOf (lib.types.submodule {
10 options = {
11 conf = lib.mkOption {
12 type = lib.types.nullOr lib.types.path;
13 default = null;
14 };
15 name = lib.mkOption { type = lib.types.string; };
16 };
17 });
18 default = [];
19 description = "Sites to generate stats";
20 };
21 };
22 };
23
24 config = lib.mkIf cfg.enable {
25 users.users.root.packages = [
26 pkgs.goaccess
27 ];
28
29 services.cron = {
30 enable = true;
31 systemCronJobs = let
32 stats = domain: conf: let
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;
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
50 ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${config}
51 '';
52 in "${d}/bin/stats-${domain}";
53 allStats = sites: pkgs.writeScript "stats" ''
54 #!${pkgs.stdenv.shell}
55
56 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
57 '';
58 in
59 [
60 "5 0 * * * root ${allStats cfg.sites}"
61 ];
62 };
63
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}