24
0
Fork 0
mobilizon.chapril.org-tools/rapport_activite/rapport_activite.sh

211 lines
8.0 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright (C) 2020 Christian Pierre Momon <christian.momon@devinsy.fr>
# Copyright (C) 2018, 2019 Didier Clermonté <dclermonte@april.org>
# Copyright (C) 2018, 2019 Christian Pierre Momon <christian.momon@devinsy.fr>
#
# This file is part of paste.chapril.org.
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# access_log /var/log/nginx/mobilizon.chapril.org/mobilizon.chapril.org-access.log
# error_log /var/log/nginx/mobilizon.chapril.org/mobilizon.chapril.org-error.log
# ============================================
LOG_PREFIX="/var/log/nginx/mobilizon.chapril.org/mobilizon.chapril.org"
SERVICE_NAME="mobilizon.chapril.org"
source "/etc/mobilizon.chapril.org/mobilizonchaprilorg.conf"
IPV4_PATTERN="[0-9]{1,3}(\.[0-9]{1,3}){3}"
IPV6_PATTERN="[0-9a-f]{0,4}(:[0-9a-f]{0,4}){2,7}"
# TODO métriques à récupérer
# https://docs.joinmobilizon.org/contribute/graphql_api/
API_URL="https://mobilizon.chapril.org/api/"
# get statistics from the graphQL api of the website
json=$(curl -X POST \
-H "Content-Type: application/json" \
--data '{"query":"{\n statistics {\n numberOfUsers\n numberOfEvents\n numberOfLocalEvents\n numberOfComments\n numberOfLocalComments\n numberOfGroups\n numberOfLocalGroups\n numberOfInstanceFollowers\n numberOfInstanceFollowings\n }\n}\n"}' $API_URL)
#
help()
{
echo "Usage:"
echo " $(basename "$0") -h display help"
echo " $(basename "$0") analyse current month"
echo " $(basename "$0") -p analyse preceding month"
echo " $(basename "$0") month_number year analyse this month"
}
#
generateHTTPReport()
{
local monthEnglish="$1"
local year="$2"
local yearMonth="$3"
local errorLogFormatDate="$2/$4"
local accessLogFormatDate="$1/$2"
echo "=§= Rapport d'activité HTTP"
echo
local httpRequestCount=$(zgrep "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep $year|wc -l)
echo "Nombre total de requêtes http = $httpRequestCount"
local trueHttpRequestCount=$(zgrep "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep $year|grep -vi bot|grep -v check_http|wc -l)
echo "Nombre de vraies requêtes http = $trueHttpRequestCount"
local botRequestCount=$(zgrep "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep "$year"|grep -i bot|grep -c -v check_http)
echo "Nombre de requêtes bot = $botRequestCount"
local errorRequestCount=$(zgrep "$errorLogFormatDate" $LOG_PREFIX-error.log*|grep -c "$year")
echo "Nombre de requêtes http en erreur = $errorRequestCount"
local ipv4VisitorCount=$(zgrep -h "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|egrep "^$IPV4_PATTERN "|cut -d' ' -f1|sort|uniq|wc -l )
echo "Nombre d'ipv4 ayant visité le service = $ipv4VisitorCount"
local ipv6VisitorCount=$(zgrep -h "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|egrep "^$IPV6_PATTERN "|cut -d' ' -f1|sort|uniq|wc -l )
echo "Nombre d'ipv6 ayant visité le service = $ipv6VisitorCount"
echo "Nombre total d'ip ayant visité le service = $((ipv4VisitorCount+ipv6VisitorCount))"
echo
}
# display the value of a key in the json fetched with curl
function show
{
# to navigate to sub keys like data.statistics.my_key , we must use multiple -e extract options
local value=$(echo "$json" | jshon -e "data" -e "statistics" -e $1 -u)
printf "$1=$value\n"
}
#
generateSpecificReport()
{
local monthEnglish="$1"
local year="$2"
local yearMonth="$3"
local numMonth="$4"
local errorLogFormatDate="$2/$4"
local accessLogFormatDate="$1/$2"
# nginx shows logs like this:
# error log format:
# 2020/11/19
# 2020/11/19 20:31:05 [error] 1720#1720: *40 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.93, server: mobilizon.chapril.org, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4000/", host: "mobilizon.chapril.org"
# access log, format :
# 26/Nov/2020
# 192.168.1.93 - - [26/Nov/2020:18:06:05 +0100] "GET / HTTP/1.1" 200 2731 "-" "check_http/v2.2 (monitoring-plugins 2.2)"
# 7d01:4f8:14b:c41::12 - - [26/Nov/2020:18:07:06 +0100] "POST /api/ HTTP/1.1" 200 238 "-" "curl/7.64.0"
echo "=§= Rapport d'activité spécifique"
echo
errorsCount=$(zgrep -h "$errorLogFormatDate" $LOG_PREFIX-error.log*|wc -l )
ipv4UserCount=$(zgrep -h "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|egrep "^$IPV4_PATTERN " | cut -d' ' -f 1|sort|uniq|wc -l )
echo "Nombre d'ipv4 utilisatrices du service = $ipv4UserCount"
ipv6UserCount=$(zgrep -h "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|gre\p -v check_http|egrep "^$IPV6_PATTERN " | cut -d' ' -f 1|sort|uniq|wc -l )
echo "Nombre d'ipv6 utilisatrices du service = $ipv6UserCount"
echo "Nombre total d'ip utilisatrices du service = $((ipv4UserCount+ipv6UserCount))"
echo "Nombre total d'erreurs = $errorsCount"
# echo "Nombre de comptes utilisateurs = "
show "numberOfEvents"
show "numberOfLocalEvents"
show "numberOfGroups"
show "numberOfLocalGroups"
show "numberOfComments"
show "numberOfLocalComments"
show "numberOfInstanceFollowers"
show "numberOfInstanceFollowings"
show "numberOfUsers"
# echo "Nombre d'évènements passés"
# echo "Nombre d'évènements à venir"
echo
}
#
generateReport()
{
echo "Rapport d'activité du service $SERVICE_NAME de $monthEnglish $year"
echo
echo "================================================================="
echo
generateHTTPReport "$1" "$2" "$3" "$4"
generateSpecificReport "$1" "$2" "$3" "$4"
}
# ============================================
echo
if [ "$#" = 1 ] && [ "$1" = "-h" ]; then
help
elif [ "$#" = 1 ] && [ "$1" = "-p" ]; then
if [ "$(date +%m)" -gt 1 ]; then
precedingMonth="$((10#$(date +%m)-1))"
year="$(date +%Y)"
if [ $precedingMonth -lt 10 ]; then
yearMonth="$year-0$precedingMonth"
else
yearMonth="$year-$precedingMonth"
fi
else [ "$(date +%m)" -eq 1 ]
precedingMonth=12
year="$(($(date +%Y)-1))"
yearMonth="$year-$precedingMonth"
fi
monthEnglish=$(LANG=en_EN.UTF-8;date -d $year-$precedingMonth-01 +%b)
generateReport "$monthEnglish" "$year" "$yearMonth" "$precedingMonth"
elif [ "$#" -eq 2 ]; then
if ! (let "$1") 2>/dev/null; then
echo -e "Le mois doit être numérique."
help
elif ([ "$1" -lt 1 ] || [ "$1" -gt 12 ]); then
echo -e "Numéro du mois non valable $1."
help
elif ! (let "$2") 2>/dev/null; then
echo -e "L'année doit être numérique."
help
elif [ "$2" -lt 2018 ] || [ "$2" -gt "$(date +%Y)" ]; then
echo -e "Année non valable $2."
help
elif [ "$2" -le 2017 ] || [ "$2" -ge $(($(date +%Y)+1)) ] ; then
echo -e "Pas de données pour ce mois."
help
else
if [ "$1" -lt 10 ]; then
yearMonth="$2-0$1"
else
yearMonth="$2-$1"
fi
monthEnglish=$(LANG=en_EN.UTF-8;date -d "$2-$1-01" +%b)
year="$2"
generateReport "$monthEnglish" "$year" "$yearMonth"
fi
elif [ "$#" -eq 0 ]; then
yearMonth="$(date +%Y-%m)"
monthEnglish=$(LANG=en_EN.UTF-8;date +%b)
year="$(date +%Y)"
generateReport "$monthEnglish" "$year" "$yearMonth"
else
echo -e "Bad parameter."
help
fi