2020-11-12 00:53:52 +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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
# ============================================
|
|
|
|
|
|
|
|
LOG_PREFIX="/var/log/apache2/forge.chapril.org/forge.chapril.org"
|
|
|
|
SERVICE_NAME="forge.chapril.org"
|
|
|
|
GITEA_REPO_HOME="/srv/gitea/repositories/"
|
|
|
|
|
2020-11-12 01:25:37 +01:00
|
|
|
source "/etc/forge.chapril.org/forgechaprilorg.conf"
|
2020-11-12 00:53:52 +01:00
|
|
|
|
|
|
|
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}"
|
|
|
|
#
|
|
|
|
help()
|
|
|
|
{
|
2024-06-17 02:20:47 +02:00
|
|
|
local prog
|
|
|
|
prog=$(basename "$0")
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Usage:"
|
2024-06-17 02:20:47 +02:00
|
|
|
echo " $prog -h display help"
|
|
|
|
echo " $prog analyse current month"
|
|
|
|
echo " $prog -p analyse preceding month"
|
|
|
|
echo " $prog month_number year analyse this month"
|
2020-11-12 00:53:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
generateHTTPReport()
|
|
|
|
{
|
|
|
|
local monthEnglish="$1"
|
|
|
|
local year="$2"
|
|
|
|
local yearMonth="$3"
|
|
|
|
|
|
|
|
echo "=§= Rapport d'activité HTTP"
|
|
|
|
echo
|
2024-06-17 01:51:44 +02:00
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local httpRequestCount
|
|
|
|
httpRequestCount=$(zgrep "$monthEnglish" $LOG_PREFIX-access.log*|grep -c "$year")
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre total de requêtes http = $httpRequestCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local trueHttpRequestCount
|
|
|
|
trueHttpRequestCount=$(zgrep "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -c -v check_http)
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de vraies requêtes http = $trueHttpRequestCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local botRequestCount
|
|
|
|
botRequestCount=$(zgrep "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -i bot|grep -c -v check_http)
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de requêtes bot = $botRequestCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local errorRequestCount
|
|
|
|
errorRequestCount=$(zgrep "$monthEnglish" "$LOG_PREFIX-error.log*"|grep -c "$year")
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de requêtes http en erreur = $errorRequestCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local ipv4VisitorCount
|
|
|
|
ipv4VisitorCount=$(zgrep -h "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep -E "^$IPV4_PATTERN "|cut -d' ' -f1|sort|uniq|wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre d'ipv4 ayant visité le service = $ipv4VisitorCount"
|
2024-06-17 01:51:44 +02:00
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local ipv6VisitorCount
|
|
|
|
ipv6VisitorCount=$(zgrep -h "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep -E "^$IPV6_PATTERN "|cut -d' ' -f1|sort|uniq|wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre d'ipv6 ayant visité le service = $ipv6VisitorCount"
|
|
|
|
|
|
|
|
echo "Nombre total d'ip ayant visité le service = $((ipv4VisitorCount+ipv6VisitorCount))"
|
|
|
|
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
generateSpecificReport()
|
|
|
|
{
|
|
|
|
local monthEnglish="$1"
|
|
|
|
local year="$2"
|
|
|
|
local yearMonth="$3"
|
|
|
|
|
|
|
|
echo "=§= Rapport d'activité spécifique"
|
|
|
|
echo
|
|
|
|
|
2024-06-17 02:04:16 +02:00
|
|
|
ipv4UserCount=$(zgrep -h "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep -E "^$IPV4_PATTERN " | cut -d' ' -f 1|sort|uniq|wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre d'ipv4 utilisatrices du service = $ipv4UserCount"
|
2024-06-17 01:51:44 +02:00
|
|
|
|
2024-06-17 02:04:16 +02:00
|
|
|
ipv6UserCount=$(zgrep -h "$monthEnglish" $LOG_PREFIX-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep -E "^$IPV6_PATTERN " | cut -d' ' -f 1|sort|uniq|wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre d'ipv6 utilisatrices du service = $ipv6UserCount"
|
|
|
|
|
|
|
|
echo "Nombre total d'ip utilisatrices du service = $((ipv4UserCount+ipv6UserCount))"
|
|
|
|
echo
|
|
|
|
|
|
|
|
# https://forge.chapril.org/api/swagger
|
|
|
|
#$(curl -s --dump-header - "https://forge.chapril.org/api/v1/admin/orgs?limit=1&page1&token=$GITEA_API_KEY" > t)
|
|
|
|
#$(curl -s "https://forge.chapril.org/api/v1/users/search?limit=1&token=$GITEA_API_KEY" | jq '. | length' > t)
|
2024-06-17 02:18:17 +02:00
|
|
|
local accountCount
|
|
|
|
accountCount=$(curl -s "https://forge.chapril.org/api/v1/users/search?limit=100000&token=$GITEA_API_KEY" | jq '.data | length')
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de comptes = $accountCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local connectedAccountCount
|
|
|
|
connectedAccountCount=$(curl -s "https://forge.chapril.org/api/v1/users/search?limit=100000&token=$GITEA_API_KEY" | jq '.data[].last_login' | grep -cv "1970")
|
2021-05-01 03:48:27 +02:00
|
|
|
echo "Nombre de comptes connectés au moins une fois = $connectedAccountCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local unconnectedAccountCount
|
|
|
|
unconnectedAccountCount=$(curl -s "https://forge.chapril.org/api/v1/users/search?limit=100000&token=$GITEA_API_KEY" | jq '.data[].last_login' | grep -c "1970")
|
2021-05-01 03:48:27 +02:00
|
|
|
echo "Nombre de comptes jamais connectés = $unconnectedAccountCount"
|
|
|
|
|
2020-11-12 00:53:52 +01:00
|
|
|
#$(curl -s "https://forge.chapril.org/api/v1/admin/orgs?limit=100000&token=$GITEA_API_KEY" | jq '. | length' > t)
|
2024-06-17 02:18:17 +02:00
|
|
|
local organizationCount
|
|
|
|
organizationCount=$(curl -s "https://forge.chapril.org/api/v1/admin/orgs?limit=100000&token=$GITEA_API_KEY" | jq '. | length')
|
2020-11-12 00:53:52 +01:00
|
|
|
if [[ -z "$organizationCount" ]]; then
|
2024-06-17 01:55:41 +02:00
|
|
|
organizationCount=0
|
2020-11-12 00:53:52 +01:00
|
|
|
fi
|
|
|
|
echo "Nombre d'organisations = $organizationCount"
|
|
|
|
|
|
|
|
#$(curl -s "https://forge.chapril.org/api/v1/orgs?limit=100000" > t)
|
2024-06-17 02:18:17 +02:00
|
|
|
local publicOrganizationCount
|
|
|
|
publicOrganizationCount=$(curl -s "https://forge.chapril.org/api/v1/orgs?limit=100000" | jq '. | length')
|
2020-11-12 00:53:52 +01:00
|
|
|
if [[ -z "$publicOrganizationCount" ]]; then
|
2024-06-17 01:55:41 +02:00
|
|
|
publicOrganizationCount=0
|
2020-11-12 00:53:52 +01:00
|
|
|
fi
|
|
|
|
echo "Nombre d'organisations publiques = $publicOrganizationCount"
|
2024-06-17 01:51:44 +02:00
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local repositoryCount
|
|
|
|
repositoryCount=$(curl -s --dump-header - "https://forge.chapril.org/api/v1/repos/search?limit=1&token=$GITEA_API_KEY" | grep "X-Total-Count\:" | awk '{ print $2 }' | jq '.')
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de dépôts = $repositoryCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local publicRepositoryCount
|
|
|
|
publicRepositoryCount=$(curl -s --dump-header - "https://forge.chapril.org/api/v1/repos/search?limit=1" | grep "X-Total-Count\:" | awk '{ print $2 }' | jq '.')
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de dépôts publics = $publicRepositoryCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local repositorySpaceDisk
|
|
|
|
repositorySpaceDisk=$(du -sh "$GITEA_REPO_HOME" | head -1 | awk '{ print $1 } ')
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Espace disque occupé par les dépôts = $repositorySpaceDisk"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local directoryCount
|
|
|
|
directoryCount=$(find "$GITEA_REPO_HOME" -type d | wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de dossiers de l'espace disque = $directoryCount"
|
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local fileCount
|
|
|
|
fileCount=$(find "$GITEA_REPO_HOME" -type f | wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre de fichiers de l'espace disque = $fileCount"
|
2024-06-17 01:51:44 +02:00
|
|
|
|
2024-06-17 02:18:17 +02:00
|
|
|
local totalFileCount
|
|
|
|
totalFileCount=$(find "$GITEA_REPO_HOME" | wc -l )
|
2020-11-12 00:53:52 +01:00
|
|
|
echo "Nombre total de fichiers de l'espace disque = $totalFileCount"
|
|
|
|
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
generateReport()
|
|
|
|
{
|
|
|
|
echo "Rapport d'activité du service $SERVICE_NAME de $monthEnglish $year"
|
|
|
|
echo
|
|
|
|
echo "================================================================="
|
|
|
|
echo
|
|
|
|
generateHTTPReport "$1" "$2" "$3"
|
|
|
|
generateSpecificReport "$1" "$2" "$3"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ============================================
|
|
|
|
echo
|
|
|
|
if [ "$#" = 1 ] && [ "$1" = "-h" ]; then
|
|
|
|
help
|
|
|
|
elif [ "$#" = 1 ] && [ "$1" = "-p" ]; then
|
|
|
|
if [ "$(date +%m)" -gt 1 ]; then
|
2024-06-17 01:55:41 +02:00
|
|
|
precedingMonth="$((10#$(date +%m)-1))"
|
|
|
|
year="$(date +%Y)"
|
|
|
|
if [ $precedingMonth -lt 10 ]; then
|
|
|
|
yearMonth="$year-0$precedingMonth"
|
|
|
|
else
|
|
|
|
yearMonth="$year-$precedingMonth"
|
|
|
|
fi
|
2020-11-12 00:53:52 +01:00
|
|
|
else [ "$(date +%m)" -eq 1 ]
|
2024-06-17 01:55:41 +02:00
|
|
|
precedingMonth=12
|
|
|
|
year="$(($(date +%Y)-1))"
|
|
|
|
yearMonth="$year-$precedingMonth"
|
2020-11-12 00:53:52 +01:00
|
|
|
fi
|
2024-06-17 02:18:17 +02:00
|
|
|
monthEnglish=$(LANG=en_EN.UTF-8;date -d "$year-$precedingMonth-01" +%b)
|
2020-11-12 00:53:52 +01:00
|
|
|
generateReport "$monthEnglish" "$year" "$yearMonth"
|
|
|
|
elif [ "$#" -eq 2 ]; then
|
|
|
|
if ! (let "$1") 2>/dev/null; then
|
2024-06-17 01:55:41 +02:00
|
|
|
echo -e "Le mois doit être numérique."
|
|
|
|
help
|
2024-06-17 02:18:17 +02:00
|
|
|
elif [ "$1" -lt 1 ] || [ "$1" -gt 12 ]; then
|
2024-06-17 01:55:41 +02:00
|
|
|
echo -e "Numéro du mois non valable $1."
|
|
|
|
help
|
2020-11-12 00:53:52 +01:00
|
|
|
elif ! (let "$2") 2>/dev/null; then
|
2024-06-17 01:55:41 +02:00
|
|
|
echo -e "L'année doit être numérique."
|
|
|
|
help
|
2020-11-12 00:53:52 +01:00
|
|
|
elif [ "$2" -lt 2018 ] || [ "$2" -gt "$(date +%Y)" ]; then
|
2024-06-17 01:55:41 +02:00
|
|
|
echo -e "Année non valable $2."
|
|
|
|
help
|
2020-11-12 00:53:52 +01:00
|
|
|
elif [ "$2" -le 2017 ] || [ "$2" -ge $(($(date +%Y)+1)) ] ; then
|
2024-06-17 01:55:41 +02:00
|
|
|
echo -e "Pas de données pour ce mois."
|
|
|
|
help
|
2020-11-12 00:53:52 +01:00
|
|
|
else
|
2024-06-17 01:55:41 +02:00
|
|
|
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"
|
2020-11-12 00:53:52 +01:00
|
|
|
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
|