From 1703471425eb659c850df5fce5b9a0d342ff289d Mon Sep 17 00:00:00 2001 From: Laurent Poujoulat Date: Fri, 27 Dec 2019 11:17:05 +0100 Subject: [PATCH] =?UTF-8?q?Commit=20initial=20apr=C3=A8s=20r=C3=A9architec?= =?UTF-8?q?ture=20du=20home=20de=20la=20valise?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/cron.d/valisechaprilorg | 5 + etc/valise.chapril.org/curl_credential | 1 + etc/valise.chapril.org/valisechaprilorg.conf | 8 ++ rapports_activites/rapport_activites.sh | 105 +++++++++++++++++++ rapports_activites/update_redmine_ticket.sh | 40 +++++++ 5 files changed, 159 insertions(+) create mode 100644 etc/cron.d/valisechaprilorg create mode 100644 etc/valise.chapril.org/curl_credential create mode 100644 etc/valise.chapril.org/valisechaprilorg.conf create mode 100755 rapports_activites/rapport_activites.sh create mode 100755 rapports_activites/update_redmine_ticket.sh diff --git a/etc/cron.d/valisechaprilorg b/etc/cron.d/valisechaprilorg new file mode 100644 index 0000000..0ba9875 --- /dev/null +++ b/etc/cron.d/valisechaprilorg @@ -0,0 +1,5 @@ +# Traitements periodique de Nextcloud (toutes les 15 min) +*/15 * * * * www-data php7.3 -f /var/www/valise.chapril.org/cron.php + +# Generation des statistiques du service valise (tous les 1er du mois) +5 0 1 * * root /srv/valise.chapril.org/update_redmine_ticket.sh >> /var/log/valise_stats.log diff --git a/etc/valise.chapril.org/curl_credential b/etc/valise.chapril.org/curl_credential new file mode 100644 index 0000000..08e12d1 --- /dev/null +++ b/etc/valise.chapril.org/curl_credential @@ -0,0 +1 @@ +machine localhost login xxxxxxxxx password zzzzzzzzzz diff --git a/etc/valise.chapril.org/valisechaprilorg.conf b/etc/valise.chapril.org/valisechaprilorg.conf new file mode 100644 index 0000000..32baebf --- /dev/null +++ b/etc/valise.chapril.org/valisechaprilorg.conf @@ -0,0 +1,8 @@ +# Clef API redmine agir pour la mise à jour des tickets +# Actuellement celle de lpoujoulat, mais devrait être celle d'un compte dédié +REDMINE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + +# Localisation du ticket récurrent destinataire des statistiques du service +REDMINE_BASE_URL="https://agir.april.org" +REDMINE_TICKET_ID=4095 + diff --git a/rapports_activites/rapport_activites.sh b/rapports_activites/rapport_activites.sh new file mode 100755 index 0000000..43af452 --- /dev/null +++ b/rapports_activites/rapport_activites.sh @@ -0,0 +1,105 @@ +#! /bin/bash +# +# Copyright (C) 2019 Laurent Poujoulat +# +# This file is part of valise.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 reads the nextcloud state, extract the main status variables +# and writes a plain text report on stdout +# ============================================ + +# Configuration file +CURL_CREDENTIAL="/etc/valise.chapril.org/curl_credential" +VALISE_STATS_DIR="/var/lib/valise.chapril.org" +VALISE_STATS_FILE="${VALISE_STATS_DIR}/stats" + +# Extract a json value by path (given as argument) +# The global JSON state is expected to be in NEXTCLOUD_STATE +# Only give the path part within the ocs.data section +getNcState() +{ + echo ${NEXTCLOUD_STATE} | jq ".ocs.data.$1" +} + +# Format statistic value for the report +writeStatValue() +{ + local LABEL=$1 + local UNIT=$2 + local CUR_VALUE=$3 + local PREV_VALUE=$4 + local VARIATION=$(( ${CUR_VALUE}-${PREV_VALUE} )) + + if [ ${VARIATION} -gt 0 ] + then + VARIATION="+"${VARIATION} + fi + + if [ "${UNIT}" == "" ] + then + echo ${LABEL}": "${CUR_VALUE}" ("${VARIATION}")" + else + echo ${LABEL}": "${CUR_VALUE}" "${UNIT}" ("${VARIATION}")" + fi +} + + +# Read and store the current service state +# If something fails, +NEXTCLOUD_STATE=`curl --silent --netrc-file ${CURL_CREDENTIAL} http://localhost/ocs/v2.php/apps/serverinfo/api/v1/info?format=json` + +# Extraction of important values +NB_OF_USERS=$(getNcState "nextcloud.storage.num_users") +NB_OF_FILES=$(getNcState "nextcloud.storage.num_files") +NB_OF_SHARES=$(getNcState "nextcloud.shares.num_shares") +FREE_DISK_GB=$(( $(getNcState "nextcloud.system.freespace") / (1024*1024*1024) )) +DATABASE_SIZE_MB=$(( $(getNcState "server.database.size") / (1024*1024) )) + +# Get previous values +NB_OF_USERS_P=0 +NB_OF_FILES_P=0 +NB_OF_SHARES_P=0 +FREE_DISK_GB_P=0 +DATABASE_SIZE_MB_P=0 + +if [ -e ${VALISE_STATS_FILE} ] +then + source ${VALISE_STATS_FILE} +fi + +# Save current values for the next run +mkdir -p ${VALISE_STATS_DIR} +echo "NB_OF_USERS_P="${NB_OF_USERS} >> ${VALISE_STATS_FILE} +echo "NB_OF_FILES_P="${NB_OF_FILES} >> ${VALISE_STATS_FILE} +echo "NB_OF_SHARES_P="${NB_OF_SHARES} >> ${VALISE_STATS_FILE} +echo "FREE_DISK_GB_P="${FREE_DISK_GB} >> ${VALISE_STATS_FILE} +echo "DATABASE_SIZE_MB_P="${DATABASE_SIZE_MB} >> ${VALISE_STATS_FILE} + +# Generate report +echo "Rapport d'activité du service valise.chapril.org au "`date "+%-d %B %Y"` +echo +echo "=======================================================================" +echo +writeStatValue "Nombre d'utilisateurs" "" ${NB_OF_USERS} ${NB_OF_USERS_P} +writeStatValue "Nombre de fichiers" "" ${NB_OF_FILES} ${NB_OF_FILES_P} +writeStatValue "Nombre de partages" "" ${NB_OF_SHARES} ${NB_OF_SHARES_P} +writeStatValue "Espace disque disponible" "Go" ${FREE_DISK_GB} ${FREE_DISK_GB_P} +writeStatValue "Espace occupé par la base de données" "Mo" ${DATABASE_SIZE_MB} ${DATABASE_SIZE_MB_P} + + + diff --git a/rapports_activites/update_redmine_ticket.sh b/rapports_activites/update_redmine_ticket.sh new file mode 100755 index 0000000..7483ab6 --- /dev/null +++ b/rapports_activites/update_redmine_ticket.sh @@ -0,0 +1,40 @@ +#! /bin/bash + +# Lecture configuration +CONFIG="/etc/valise.chapril.org/valisechaprilorg.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/valise.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