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

210 lines
9.0 KiB
Bash
Raw Permalink Normal View History

2020-11-12 04:50:54 +01:00
#!/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/>.
2020-11-20 09:10:14 +01:00
# 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
2020-11-12 04:50:54 +01:00
# ============================================
2020-11-20 09:10:14 +01:00
LOG_PREFIX="/var/log/nginx/mobilizon.chapril.org/mobilizon.chapril.org"
2020-11-12 04:50:54 +01:00
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}"
2020-11-26 17:42:02 +01:00
# https://docs.joinmobilizon.org/contribute/graphql_api/
API_URL="https://mobilizon.chapril.org/api/"
# get statistics from the graphQL api of the website
2020-11-27 12:15:04 +01:00
json=$(curl -s -X POST \
2020-11-26 17:42:02 +01:00
-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)
2020-11-12 04:50:54 +01:00
#
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"
2020-11-12 04:50:54 +01:00
echo "=§= Rapport d'activité HTTP"
echo
2020-11-26 14:52:42 +01:00
local httpRequestCount=$(zgrep "$accessLogFormatDate" $LOG_PREFIX-access.log*|grep $year|wc -l)
2020-11-12 04:50:54 +01:00
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)
2020-11-12 04:50:54 +01:00
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)
2020-11-12 04:50:54 +01:00
echo "Nombre de requêtes bot = $botRequestCount"
local errorRequestCount=$(zgrep "$errorLogFormatDate" $LOG_PREFIX-error.log*|grep -c "$year")
2020-11-12 04:50:54 +01:00
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 )
2020-11-12 04:50:54 +01:00
echo "Nombre d'ipv4 ayant visité le service = $ipv4VisitorCount"
2020-11-26 14:52:42 +01:00
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 )
2020-11-12 04:50:54 +01:00
echo "Nombre d'ipv6 ayant visité le service = $ipv6VisitorCount"
echo "Nombre total d'ip ayant visité le service = $((ipv4VisitorCount+ipv6VisitorCount))"
echo
}
#
generateSpecificReport()
{
2020-11-26 17:42:02 +01:00
2020-11-12 04:50:54 +01:00
local monthEnglish="$1"
local year="$2"
local yearMonth="$3"
2020-11-26 18:15:30 +01:00
local numMonth="$4"
local errorLogFormatDate="$2/$4"
local accessLogFormatDate="$1/$2"
2020-11-12 04:50:54 +01:00
echo "=§= Rapport d'activité spécifique"
echo
errorsCount=$(zgrep -h "$errorLogFormatDate" $LOG_PREFIX-error.log*|wc -l )
2020-11-26 18:15:30 +01:00
2021-06-02 10:57:38 +02:00
# trier par passage à la création
# schéma recherché dans les logs d'accès : mobilizon.chapril.org/events/create
# trier par passage à la suppression
# schéma recherché dans les logs d'accès :
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 )
2020-11-12 04:50:54 +01:00
echo "Nombre d'ipv4 utilisatrices du service = $ipv4UserCount"
2020-11-26 14:52:42 +01:00
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 )
2020-11-12 04:50:54 +01:00
echo "Nombre d'ipv6 utilisatrices du service = $ipv6UserCount"
echo "Nombre total d'ip utilisatrices du service = $((ipv4UserCount+ipv6UserCount))"
2020-11-29 13:59:03 +01:00
echo "Nombre total d'erreurs = $errorsCount"
echo "Nombre d'évènements = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfEvents" -u)"
echo "Nombre d'évènements locaux = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfLocalEvents" -u)"
echo "Nombre de groupes = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfGroups" -u)"
echo "Nombre de groupes locaux = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfLocalGroups" -u)"
echo "Nombre de commentaires = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfComments" -u)"
echo "Nombre de commentaires locaux = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfLocalComments" -u)"
echo "Nombre d'instances abonnées = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfInstanceFollowers" -u)"
echo "Nombre d'instances suivies = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfInstanceFollowings" -u)"
2021-06-02 10:57:38 +02:00
# vérifier si les comptes incluent les identités différentes
echo "Nombre de comptes selon l'api = $(echo "$json" | jshon -e "data" -e "statistics" -e "numberOfUsers" -u)"
# décompte des profils
2021-08-19 16:13:59 +02:00
DAY_INTERVAL=30000;
2021-06-02 10:57:38 +02:00
profiles_all_time=$(sudo -u postgres -H -- psql -d mobilizon_prod -c "SELECT count(user_id) FROM actors WHERE type='Person' AND user_id is not null AND inserted_at >= ('now'::date - '$DAY_INTERVAL day'::interval )" -t | grep -v '^$' | awk '{print $1}' )
DAY_INTERVAL=30;
profiles_created_last_month=$(sudo -u postgres -H -- psql -d mobilizon_prod -c "SELECT count(user_id) FROM actors WHERE type='Person' AND user_id is not null AND inserted_at >= ('now'::date - '$DAY_INTERVAL day'::interval )" -t | grep -v '^$' | awk '{print $1}' )
echo "Nombre de profils en tout = $profiles_all_time"
echo "Nombre de profils créés ces 30 derniers jours = $profiles_created_last_month"
2020-11-29 13:59:03 +01:00
2020-11-12 04:50:54 +01:00
}
#
generateReport()
{
echo "Rapport d'activité du service $SERVICE_NAME de $monthEnglish $year"
2020-11-26 18:43:53 +01:00
echo ""
2020-11-12 04:50:54 +01:00
echo "================================================================="
echo
2020-11-26 18:15:30 +01:00
generateHTTPReport "$1" "$2" "$3" "$4"
generateSpecificReport "$1" "$2" "$3" "$4"
2020-11-12 04:50:54 +01:00
}
# ============================================
echo
if [ "$#" = 1 ] && [ "$1" = "-h" ]; then
help
elif [ "$#" = 1 ] && [ "$1" = "-p" ]; then
if [ "$(date +%m)" -gt 1 ]; then
precedingMonth="$((10#$(date +%m)-1))"
2020-11-26 18:43:53 +01:00
2020-11-12 04:50:54 +01:00
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"
2020-11-26 18:43:53 +01:00
2020-11-12 04:50:54 +01:00
fi
monthEnglish=$(LANG=en_EN.UTF-8;date -d $year-$precedingMonth-01 +%b)
2020-11-26 18:15:30 +01:00
generateReport "$monthEnglish" "$year" "$yearMonth" "$precedingMonth"
2020-11-12 04:50:54 +01:00
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
2020-11-26 18:43:53 +01:00
elif [ "$2" -lt 2020 ] || [ "$2" -gt "$(date +%Y)" ]; then
echo -e "Année non valable $2. Le service a été mis en ligne en Novembre 2020"
2020-11-12 04:50:54 +01:00
help
2020-11-26 18:43:53 +01:00
elif [ "$2" -le 2020 ] || [ "$2" -ge $(($(date +%Y)+1)) ] ; then
2020-11-12 04:50:54 +01:00
echo -e "Pas de données pour ce mois."
help
else
if [ "$1" -lt 10 ]; then
yearMonth="$2-0$1"
else
yearMonth="$2-$1"
2020-11-26 14:52:42 +01:00
fi
2020-11-12 04:50:54 +01:00
monthEnglish=$(LANG=en_EN.UTF-8;date -d "$2-$1-01" +%b)
year="$2"
2020-11-26 18:43:53 +01:00
generateReport "$monthEnglish" "$year" "$yearMonth" "$1"
2020-11-12 04:50:54 +01:00
fi
elif [ "$#" -eq 0 ]; then
yearMonth="$(date +%Y-%m)"
2020-11-26 18:43:53 +01:00
monthNumeric=$(date +%m)
2020-11-12 04:50:54 +01:00
monthEnglish=$(LANG=en_EN.UTF-8;date +%b)
year="$(date +%Y)"
2020-11-26 18:43:53 +01:00
generateReport "$monthEnglish" "$year" "$yearMonth" $(date +%m)
2020-11-12 04:50:54 +01:00
else
echo -e "Bad parameter."
help
fi