From 9eae2b47b7b315b05a0e010f3003bd875685e260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 11 May 2019 10:23:33 +0200 Subject: Move webstats outside of nixops --- modules/default.nix | 1 + modules/webapps/webstats/default.nix | 84 +++++++++++++++++++++++++++++ modules/webapps/webstats/goaccess.conf | 99 ++++++++++++++++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 modules/webapps/webstats/default.nix create mode 100644 modules/webapps/webstats/goaccess.conf (limited to 'modules') diff --git a/modules/default.nix b/modules/default.nix index c920a51..2c993c5 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -2,6 +2,7 @@ myids = ./myids.nix; secrets = ./secrets.nix; + webstats = ./webapps/webstats; diaspora = ./webapps/diaspora.nix; etherpad-lite = ./webapps/etherpad-lite.nix; mastodon = ./webapps/mastodon.nix; diff --git a/modules/webapps/webstats/default.nix b/modules/webapps/webstats/default.nix new file mode 100644 index 0000000..f4916bd --- /dev/null +++ b/modules/webapps/webstats/default.nix @@ -0,0 +1,84 @@ +{ lib, pkgs, config, mylibs, ... }: +let + name = "goaccess"; + cfg = config.services.webstats; +in { + options.services.webstats = { + dataDir = lib.mkOption { + type = lib.types.path; + default = "/var/lib/${name}"; + description = '' + The directory where Goaccess stores its data. + ''; + }; + sites = lib.mkOption { + type = lib.types.listOf (lib.types.submodule { + options = { + conf = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + description = '' + use custom goaccess configuration file instead of the + default one. + ''; + }; + name = lib.mkOption { + type = lib.types.string; + description = '' + Domain name. Corresponds to the Apache file name and the + folder name in which the state will be saved. + ''; + }; + }; + }); + default = []; + description = "Sites to generate stats"; + }; + }; + + config = lib.mkIf (builtins.length cfg.sites > 0) { + users.users.root.packages = [ + pkgs.goaccess + ]; + + services.cron = { + enable = true; + systemCronJobs = let + stats = domain: conf: let + config = if builtins.isNull conf + then pkgs.runCommand "goaccess.conf" { + dbPath = "${cfg.dataDir}/${domain}"; + } "substituteAll ${./goaccess.conf} $out" + else conf; + d = pkgs.writeScriptBin "stats-${domain}" '' + #!${pkgs.stdenv.shell} + set -e + shopt -s nullglob + date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y') + TMPFILE=$(mktemp) + trap "rm -f $TMPFILE" EXIT + + cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE + for i in /var/log/httpd/access_log-${domain}*.gz; do + zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE + done + ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o ${cfg.dataDir}/${domain}/index.html -p ${config} + ''; + in "${d}/bin/stats-${domain}"; + allStats = sites: pkgs.writeScript "stats" '' + #!${pkgs.stdenv.shell} + + ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)} + ''; + in + [ + "5 0 * * * root ${allStats cfg.sites}" + ]; + }; + + system.activationScripts.goaccess = '' + mkdir -p /var/lib/goaccess + '' + + builtins.concatStringsSep "\n" (map (v: "mkdir -p ${cfg.dataDir}/${v.name}") cfg.sites); + }; +} diff --git a/modules/webapps/webstats/goaccess.conf b/modules/webapps/webstats/goaccess.conf new file mode 100644 index 0000000..4918988 --- /dev/null +++ b/modules/webapps/webstats/goaccess.conf @@ -0,0 +1,99 @@ +time-format %H:%M:%S +date-format %d/%b/%Y + +#sur immae.eu +#log-format %v %h %^[%d:%t %^] "%r" %s %b "%R" "%u" $^ + +log-format VCOMBINED +#= %v:%^ %h %^[%d:%t %^] "%r" %s %b "%R" "%u" + +html-prefs {"theme":"bright","layout":"vertical"} + +exclude-ip 188.165.209.148 +exclude-ip 178.33.252.96 +exclude-ip 2001:41d0:2:9c94::1 +exclude-ip 2001:41d0:2:9c94:: +exclude-ip 176.9.151.89 +exclude-ip 2a01:4f8:160:3445:: +exclude-ip 82.255.56.72 + +no-query-string true + +keep-db-files true +load-from-disk true +db-path @dbPath@ + +ignore-panel REFERRERS +ignore-panel KEYPHRASES + +static-file .css +static-file .js +static-file .jpg +static-file .png +static-file .gif +static-file .ico +static-file .jpeg +static-file .pdf +static-file .csv +static-file .mpeg +static-file .mpg +static-file .swf +static-file .woff +static-file .woff2 +static-file .xls +static-file .xlsx +static-file .doc +static-file .docx +static-file .ppt +static-file .pptx +static-file .txt +static-file .zip +static-file .ogg +static-file .mp3 +static-file .mp4 +static-file .exe +static-file .iso +static-file .gz +static-file .rar +static-file .svg +static-file .bmp +static-file .tar +static-file .tgz +static-file .tiff +static-file .tif +static-file .ttf +static-file .flv +#static-file .less +#static-file .ac3 +#static-file .avi +#static-file .bz2 +#static-file .class +#static-file .cue +#static-file .dae +#static-file .dat +#static-file .dts +#static-file .ejs +#static-file .eot +#static-file .eps +#static-file .img +#static-file .jar +#static-file .map +#static-file .mid +#static-file .midi +#static-file .ogv +#static-file .webm +#static-file .mkv +#static-file .odp +#static-file .ods +#static-file .odt +#static-file .otf +#static-file .pict +#static-file .pls +#static-file .ps +#static-file .qt +#static-file .rm +#static-file .svgz +#static-file .wav +#static-file .webp + + -- cgit v1.2.3