commit 9b0c1662ce6f5367639cd9ccc660370c9d558cca
parent e70613445831a2b2fcfd26f7ae00db4553d4fc75
Author: oscarbenedito <oscar@obenedito.org>
Date:   Sun, 16 Feb 2020 21:44:04 +0100

Added some scripts from server

Diffstat:
Ratenea-updates-notifications/aun.py -> atenea-updates-notifications/atenea-updates-notifications.py | 0
Agoaccess-stats/README.md | 11+++++++++++
Agoaccess-stats/goaccess-run-daily.sh | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agoaccess-stats/stats-index-generator.py | 170+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ascripts/deploy_website.sh | 37+++++++++++++++++++++++++++++++++++++
Ascripts/login-notify.sh | 34++++++++++++++++++++++++++++++++++
6 files changed, 377 insertions(+), 0 deletions(-)

diff --git a/atenea-updates-notifications/aun.py b/atenea-updates-notifications/atenea-updates-notifications.py diff --git a/goaccess-stats/README.md b/goaccess-stats/README.md @@ -0,0 +1,11 @@ +# GoAccess stats + +This scripts create different outputs for GoAccess to check statistics on a given website. + +## License + +The program is licensed under the GPL v3. License is available [here](https://www.gnu.org/licenses/gpl-3.0.html). + +## Author + + - **Oscar Benedito** - oscar@obenedito.org diff --git a/goaccess-stats/goaccess-run-daily.sh b/goaccess-stats/goaccess-run-daily.sh @@ -0,0 +1,125 @@ +#!/usr/bin/env sh +# Copyright (C) 2020 Oscar Benedito +# +# This file is part of Utilities. +# +# Utilities is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Utilities is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Utilities. If not, see <https://www.gnu.org/licenses/>. + +OUT_DIR="/srv/stats" +DB_DIR="/srv/goaccess-db" +LOGS_PREFIX="/var/log/apache2/access-obenedito.log" + +# Shortcut to run GoAccess. +# Arguments: +# 1. Log file +# 2. Output file +# 3. (Optional) Database location +# 4. (Optional) Load from disk? Options: 0 (default), 1 +# 5. (Optional) Keep DB files? Options: 0 (default), 1 +run_goaccess() { + if [ "$#" -ge 2 ]; then + cmd="goaccess $1 -o $2" + cmd="${cmd} --log-format=COMBINED --static-file={.js,.css,.ico,.svg,.png}" + if [ "$#" -ge 3 ]; then + cmd="${cmd} --db-path=$3" + fi + if [ "$#" -ge 4 ]; then + if [ "$4" -eq 1 ]; then + cmd="${cmd} --load-from-disk" + fi + fi + if [ "$#" -ge 5 ]; then + if [ "$5" -eq 1 ]; then + cmd="${cmd} --keep-db-files" + fi + fi + $cmd + else + exit 1 + fi +} + +# Runs GoAccess for on a time interval (year, month, etc.) respects the fact +# that the last day of the week will also be in the following week (because logs +# are created at an hour in the middle of the day, the boundary days are not +# complete). +# Runs GoAccess with a filtered accesses file and without a filter. +run_goaccess_time_interval() { + if [ "$#" -eq 2 ]; then + DB="$DB_DIR/$1" + FILTERED_FILE=$(mktemp) + grep -v ".well-known/acme-challenge" "$LOGS_PREFIX.1" > "$FILTERED_FILE" + if [ -d "$DB" ]; then + mkdir -p "$OUT_DIR/$1/nf" + run_goaccess "$FILTERED_FILE" "$OUT_DIR/$1/index.html" "$DB" 1 1 + run_goaccess "$LOGS_PREFIX.1" "$OUT_DIR/$1/nf/index.html" "$DB/nf" 1 1 + else + mkdir -p "$DB/f" + mkdir -p "$DB/nf" + mkdir -p "$OUT_DIR/$1/nf" + run_goaccess "$FILTERED_FILE" "$OUT_DIR/$1/index.html" "$DB" 0 1 + run_goaccess "$LOGS_PREFIX.1" "$OUT_DIR/$1/nf/index.html" "$DB/nf" 0 1 + fi + + if [ "$1" != "$2" ]; then + DB="$DB_DIR/$2" + if [ -d "$DB" ]; then + mkdir -p "$OUT_DIR/$2" + run_goaccess "$FILTERED_FILE" "$OUT_DIR/$2/index.html" "$DB" 1 0 + run_goaccess "$LOGS_PREFIX.1" "$OUT_DIR/$2/nf/index.html" "$DB/nf" 1 0 + rm -rf "$DB" + fi + fi + rm "$FILTERED_FILE" + else + exit 1 + fi +} + +# Day +TMP_FILE=$(mktemp) +LOGS_2=$(mktemp) +OUT_DIR_TODAY="$OUT_DIR/$(date --date="yesterday" +"d/%Y/%m/%d")" + +cp "$LOGS_PREFIX.2.gz" "$LOGS_2.gz" +gunzip -f "$LOGS_2.gz" + +mkdir -p "$OUT_DIR_TODAY" +cat "$LOGS_2" "$LOGS_PREFIX.1" | grep -v ".well-known/acme-challenge" > "$TMP_FILE" +run_goaccess "$TMP_FILE" "$OUT_DIR_TODAY/index.html" + +mkdir -p "$OUT_DIR_TODAY/nf" +cat "$LOGS_2" "$LOGS_PREFIX.1" > "$TMP_FILE" +run_goaccess "$TMP_FILE" "$OUT_DIR_TODAY/nf/index.html" +rm "$TMP_FILE" + +# Week +TODAY="$(date +"w/%G/%V")" +YESTERDAY="$(date --date="yesterday" +"%w/G/%V")" +run_goaccess_time_interval "$TODAY" "$YESTERDAY" + +# Month +TODAY="$(date +"m/%Y/%m")" +YESTERDAY="$(date --date="yesterday" +"m/%Y/%m")" +run_goaccess_time_interval "$TODAY" "$YESTERDAY" + +# Year +TODAY="$(date +"y/%Y")" +YESTERDAY="$(date --date="yesterday" +"y/%Y")" +run_goaccess_time_interval "$TODAY" "$YESTERDAY" + +# All time +TODAY="all" +YESTERDAY="all" +run_goaccess_time_interval "$TODAY" "$YESTERDAY" diff --git a/goaccess-stats/stats-index-generator.py b/goaccess-stats/stats-index-generator.py @@ -0,0 +1,170 @@ +#!/usr/bin/env python3 +# Copyright (C) 2020 Oscar Benedito +# +# This file is part of Utilities. +# +# Utilities is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Utilities is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Utilities. If not, see <https://www.gnu.org/licenses/>. + +import datetime +import os.path + +OUTPUT_DIR = '/srv/stats' + +def has_info(link): + return os.path.isfile(OUTPUT_DIR + link + '/index.html') + +output = """<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"> + <meta name="description" content="Oscar Benedito's website stats"> + <meta name="author" content="Oscar Benedito"> + <title>Website stats</title> + + <link rel="icon" href="/favicon.min.svg"> + <link rel="shortcut icon" href="/favicon.ico"> + <style> +body { + font-family: 'sans-serif'; + margin: 3em; +} + +a.no-format { + color: inherit; + text-decoration: none; +} + +a.hover:hover { + text-decoration: underline; +} + +h1, +h2 { + text-align: center; +} + +div.year { + margin: 5em auto; +} + +div.year-cal { + display: flex; + flex-flow: row wrap; + justify-content: flex-start; + max-width: 1024px; + margin: 0 auto; +} + +div.month { + width: 17em; + margin: 0; + padding: 1em 0; + flex: 1; +} + +div.month ul { + width: 17em; + margin: 0 auto; + padding: 0em 1em 1em 1em; +} + +div.month li { + float: left; + display: block; + width: 12.5%; + text-align: center; + list-style-type: none; +} + +div.month a li:hover { + border-radius: 5px; + background-color: #1abc9c; + color: #ecf0f1 !important; +} + +div.month li.today { + border-radius: 5px; + background-color: #5759D4; + color: #ecf0f1; +} + +div.month li.week { + font-weight: 900; + color: #e67e22; +} + </style> + </head> + + <body> +""" + +output += '<h1><a class="no-format hover" href="/all">See stats of all time</a></h1>' + +for year in [2020]: + link = '/y/' + str(year) + if has_info(link): + output += '<div class="year"><h1><a class="no-format hover" href="' + link + '">' + str(year) + '</a></h1><div class="year-cal">' + else: + output += '<div class="year"><h1>' + str(year) + '</h1><div class="year-cal">' + + for month in range(1, 13): + date = datetime.datetime(year, month, 1) + link = date.strftime("/m/%Y/%m") + if has_info(link): + output += '<div class="month"><h2><a class="no-format hover" href="' + link + '">' + datetime.datetime(year, month, 1).strftime("%B") + '</a></h2><ul>' + else: + output += '<div class="month"><h2>' + datetime.datetime(year, month, 1).strftime("%B") + '</h2><ul>' + + if date.isocalendar()[2] != 1: + date = datetime.datetime(year, month, 1) + isoweek = date.strftime("%V") + link = date.strftime("/w/%G/%V") + if has_info(link): + output += '<a class="no-format" href="' + link + '"><li class="week">' + isoweek + '</li></a>' + else: + output += '<li class="week">' + isoweek + '</li>' + + for i in range(date.isocalendar()[2] - 1): + output += '<li style="opacity: 0;">-</li>' + + for day in range(1, 32): + try: + date = datetime.datetime(year, month, day) + except: + break + + if date.isocalendar()[2] == 1: + isoweek = date.strftime("%V") + link = date.strftime("/w/%G/%V") + if has_info(link): + output += '<a class="no-format" href="' + link + '"><li class="week">' + isoweek + '</li></a>' + else: + output += '<li class="week">' + isoweek + '</li>' + + today = ' class="today"' if datetime.datetime.today().date() == date.date() else '' + link = date.strftime("/d/%Y/%m/%d") + if has_info(link): + output += '<a class="no-format" href="' + link + '"><li' + today + '>' + str(day) + '</li></a>' + else: + output += '<li' + today + '>' + str(day) + '</li>' + + output += '</ul></div>' + + output += '</div></div>' + +output += '</body></html>\n' + +with open(OUTPUT_DIR + '/index.html', 'wt') as f: + f.write(output) diff --git a/scripts/deploy_website.sh b/scripts/deploy_website.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env sh +# Copyright (C) 2020 Oscar Benedito +# +# This file is part of Utilities. +# +# Utilities is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Utilities is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Utilities. If not, see <https://www.gnu.org/licenses/>. + +# Script to deploy a website built with Hugo. + +HUGO_PATH="/srv/obenedito.org" + +git -C /srv/obenedito.org pull +rm -rf $HUGO_PATH/public +rm -rf $HUGO_PATH/resources +hugo -s $HUGO_PATH --minify + +DOMAIN=gotify.obenedito.org +API_TOKEN=<redacted> +TITLE="Web update triggered" +MESSAGE="Git hooks triggered an update of the website." + +curl -X POST "https://$DOMAIN/message?token=$API_TOKEN" \ + -F "title=$TITLE" \ + -F "message=$MESSAGE" \ + -F "priority=5" \ + >/dev/null 2>&1 diff --git a/scripts/login-notify.sh b/scripts/login-notify.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env sh +# Copyright (C) 2020 Oscar Benedito +# +# This file is part of Utilities. +# +# Utilities is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Utilities is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Utilities. If not, see <https://www.gnu.org/licenses/>. + +# Script that notifies Gotify when someone logs in through SSH to a computer/server. + +DOMAIN=gotify.obenedito.org +API_TOKEN=<redacted> + +if [ "$PAM_TYPE" != "close_session" ]; then + TITLE="SSH login: ${PAM_USER}@$(hostname)" + MESSAGE="IP: ${PAM_RHOST} +Date: $(TZ='Europe/Madrid' date)" + + curl -X POST "https://$DOMAIN/message?token=$API_TOKEN" \ + -F "title=$TITLE" \ + -F "message=$MESSAGE" \ + -F "priority=5" \ + >/dev/null 2>&1 +fi