commit f1190a36f527d0537296e76608e52c499066a185 Author: Romain H Date: Sun Jan 26 23:15:42 2020 +0100 initial import of activity report diff --git a/rapports_activites/dropchaprilorg.cron b/rapports_activites/dropchaprilorg.cron new file mode 100644 index 0000000..919d621 --- /dev/null +++ b/rapports_activites/dropchaprilorg.cron @@ -0,0 +1,4 @@ +# Génération des statistiques du service drop.chapril.org (tous les 1er du mois). +5 0 1 * * root /srv/drop.chapril.org/tools/rapports_activites/update_redmine_ticket.sh >> /var/log/dropchaprilorg_stats.log + + diff --git a/rapports_activites/dropchaprilorg.example.conf b/rapports_activites/dropchaprilorg.example.conf new file mode 100644 index 0000000..0b72a62 --- /dev/null +++ b/rapports_activites/dropchaprilorg.example.conf @@ -0,0 +1,6 @@ +# Clef API redmine agir pour la mise à jour des tickets +REDMINE_API_KEY="XXXXX" + +# Localisation du ticket récurrent destinataire des statistiques du service. +REDMINE_BASE_URL="https://agir.april.org" +REDMINE_TICKET_ID=4144 diff --git a/rapports_activites/rapport_activites.sh b/rapports_activites/rapport_activites.sh new file mode 100755 index 0000000..dec33a6 --- /dev/null +++ b/rapports_activites/rapport_activites.sh @@ -0,0 +1,50 @@ +#! /bin/bash +# +# Copyright (C) 2020 Christian MOMON +# +# This file is part of drop.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 . +# + +# ============================================ +# This scripts extracts and writes plain text report on stdout +# ============================================ + +# Configuration files. +DATA_DIRECTORY="/var/drop/" + + + + +# Generate report +echo "Rapport d'activité du service drop.chapril.org au "`date "+%-d %B %Y"` +echo +echo "=================================================================" +echo + +echo -e "=================================================================" +echo +fileCount=$(find "$DATA_DIRECTORY" | wc -l) +echo "Nombre fichiers = $fileCount" + +usedDiskSpace=$(du -sh "$DATA_DIRECTORY" | awk '{ print $1 }') +echo "Espace disque utilisé = $usedDiskSpace" + +freeDiskSpace=$(df -h "$DATA_DIRECTORY" | tail -1 | awk '{ print $4 }') +echo "Espace disque libre = $freeDiskSpace" + + +# Addition des infos HTTP. +/srv/drop.chapril.org/tools/rapports_activites/rapport_activites_http.sh -p diff --git a/rapports_activites/rapport_activites_http.sh b/rapports_activites/rapport_activites_http.sh new file mode 100755 index 0000000..d2c8cd9 --- /dev/null +++ b/rapports_activites/rapport_activites_http.sh @@ -0,0 +1,125 @@ +#!/bin/bash +# +# Copyright (C) 2018,2019 Didier Clermonté +# Copyright (C) 2018,2019 Christian Pierre Momon +# Copyright (C) 2019 Laurent Poujoulat [pour la drop] +# +# This file is part of drop.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 . +# +# ============================================ +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" +} + +generateReport() +{ + local monthEnglish="$1" + local month="$2" + local year="$3" + echo -e "=================================================================" + echo + httpRequestCount=$(zgrep "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep -c "$year") + echo "Nombre total de requêtes http = $httpRequestCount" + + trueHttpRequestCount=$(zgrep "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -vi bot|grep -c -v check_http) + echo "Nombre de vraies requêtes http = $trueHttpRequestCount" + + botRequestCount=$(zgrep "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -i bot|grep -c -v check_http) + echo "Nombre de requêtes bot = $botRequestCount" + + errorRequestCount=$(zgrep "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-error.log*|grep -c "$year") + echo "Nombre de requêtes http en erreur = $errorRequestCount" + + ipv4Count=$(zgrep -h "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep '^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\} '|cut -d' ' -f1|sort|uniq |wc -l) + echo "Nombre d'ipv4 ayant visité le service = $ipv4Count" + + ipv6Count=$(zgrep -h "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep '^\([0-9a-f]\{0,4\}:\)\{2,7\}\([0-9a-f]\{0,4\}\)\{1\} '|cut -d' ' -f1|sort|uniq |wc -l) + echo "Nombre d'ipv6 ayant visité le service = $ipv6Count" + + echo "Nombre total d'ip ayant visité le service = $((ipv4Count+ipv6Count))" + + uploadCount=$(zgrep -h "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep '^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\} '|grep "\(POST /api/info/\)"|grep -v "HTTP/1.0\" 404"|grep -v "HEAD"|cut -d' ' -f1|sort|uniq|wc -l ) + echo "Nombre de téléversements = $uploadCount" + + downloadCount=$(zgrep -h "$monthEnglish" /var/log/apache2/drop.chapril.org/drop.chapril.org-access.log*|grep "$year"|grep -vi bot|grep -v check_http|grep '^\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\} '|grep "\(GET /api/download/\)"|grep -v "HTTP/1.0\" 404"|grep -v "HEAD"|cut -d' ' -f1|sort|uniq|wc -l ) + echo "Nombre de téléchargements = $downloadCount" + + echo -e "\n" + +} + +# ============================================ +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 + month="${year}0$precedingMonth" + else + month="$year$precedingMonth" + fi + else + precedingMonth=12 + year="$(($(date +%Y)-1))" + fi + monthEnglish=$(LANG=en_EN.UTF-8;date -d $year-$precedingMonth-01 +%b) + echo + generateReport "$monthEnglish" "$month" "$year" +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 + month="$2"0"$1" + else + month="$2""$1" + fi + monthEnglish=$(LANG=en_EN.UTF-8;date -d "$2-$1-01" +%b) + year="$2" + echo -e "Rapport d'activité du service drop.chapril.org du mois $monthEnglish $2" + generateReport "$monthEnglish" "$month" "$year" + fi +elif [ "$#" -eq 0 ]; then + month="$(date +%Y%m)" + monthEnglish=$(LANG=en_EN.UTF-8;date +%b) + year="$(date +%Y)" + echo -e "Rapport d'activité du service drop.chapril.org du mois $(date +%b) 20$(date +%y)" + generateReport "$monthEnglish" "$month" "$year" +else + echo -e "Bad parameter." + help +fi diff --git a/rapports_activites/update_redmine_ticket.sh b/rapports_activites/update_redmine_ticket.sh new file mode 100755 index 0000000..48673dd --- /dev/null +++ b/rapports_activites/update_redmine_ticket.sh @@ -0,0 +1,40 @@ +#! /bin/bash + +# Lecture configuration +CONFIG="/srv/drop.chapril.org/tools/rapports_activites/dropchaprilorg.conf" +source ${CONFIG} + + +main() { + post_report +} + +post_report() { + datafile=$(mktemp /tmp/report_stats_$(date +%Y-%m-%d_%H%M)_XXXX.json) + cat < "${datafile}" +{ + "issue": { + "notes": "$(/srv/drop.chapril.org/tools/rapports_activites/rapport_activites.sh | sed -z 's/\n/\\n/g')" + } +} +EOF + + curl -s \ + -H "Content-Type: application/json" \ + -H "X-Redmine-API-Key: ${REDMINE_API_KEY}" \ + -X PUT --data-binary "@${datafile}" \ + "${REDMINE_BASE_URL}/issues/${REDMINE_TICKET_ID}.json" + rm "${datafile}" +} + +last_comment_date() { + curl -H "X-Redmine-API-Key: ${REDMINE_API_KEY}" -s "${REDMINE_BASE_URL}/issues/${REDMINE_TICKET_ID}.json?include=journals" \ + | jq '.issue.journals | last | .created_on' +} + +list() { + curl -H "X-Redmine-API-Key: ${REDMINE_API_KEY}" -s "${REDMINE_BASE_URL}/issues/${REDMINE_TICKET_ID}.json?include=journals" \ + | jq '.issue.journals[] | [.user.name, .notes]' +} + +main