From cb9754cccfc08ad07f2855d7d2982ff919b197dd Mon Sep 17 00:00:00 2001 From: pitchum Date: Tue, 17 Mar 2020 20:27:40 +0100 Subject: [PATCH] Add scripts used for activity reporting. --- README.md | 12 +++++++ rapport_activite/rapport_activites.sh | 43 +++++++++++++++++++++++++ rapport_activite/update_ticket.sh | 46 +++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100755 rapport_activite/rapport_activites.sh create mode 100755 rapport_activite/update_ticket.sh diff --git a/README.md b/README.md index 5320331..912857d 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ # XMPP tools + +## Quickstart guide + + mkdir -p /srv/xmpp.chapril.org + cd /src/xmpp.chapril.org + git clone ssh://gitea@forge.april.org:222/Chapril/xmpp.chapril.org-tools.git tools + cd /usr/local/sbin + ln -s /srv/xmpp.chapril.org/tools/rapport_activite/rapport_activites.sh + ln -s /srv/xmpp.chapril.org/tools/rapport_activite/update_ticket.sh + cat /etc/cron.d/update-ticket-rapport-dactivite + 42 7 1 * * root API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx /usr/local/sbin/update_redmine_ticket + diff --git a/rapport_activite/rapport_activites.sh b/rapport_activite/rapport_activites.sh new file mode 100755 index 0000000..d4f4d28 --- /dev/null +++ b/rapport_activite/rapport_activites.sh @@ -0,0 +1,43 @@ +#! /bin/bash + +main() { + echo "h2. ## Global info" + echo "" + echo "* Total accounts: $(sudo -u ejabberd ejabberdctl registered_users chapril.org | egrep -v -e 'r\.giskard' -e '(lisa|bart)\.simpson' | wc -l)" + echo "* Total messages stored (MAM): $(count_archived_messages) " +# echo "Active users: $(sudo -u ejabberd ejabberdctl connected_users_number)" + echo "" + echo "h2. ## HTTP upload storage:" + echo "" + echo "* User quota: $(get_hard_quota)M" + echo "* Total disk usage: $(du -sh /srv/ejabberd/chapril.org/upload/ | awk '{print $1}')" + per_account_storage_usage +# echo "s2s outgoing connections: $(sudo -u ejabberd ejabberdctl incoming_s2s_number)" +# echo "s2s incoming connections: $(sudo -u ejabberd ejabberdctl outgoing_s2s_number)" +} + +get_hard_quota() { + grep hard_quota /etc/ejabberd/ejabberd.yml -A2 | grep all | sed -r 's/^\s*-\s([0-9]+).*/\1/' +} + +per_account_storage_usage() { + STORAGE_DIR="/srv/ejabberd/chapril.org/upload/" + # TODO Directories to ignore because they are used for tests accounts (bart and lisa simpsons) + IGNORE_DIRS="35eae3ddc83d72e4d3fad3160a400db688d7d936 9be859c77649f33d207cbe0d74a126b2fd31cb33" + + echo "Per user disk usage:" + tmp="$(du -sh ${STORAGE_DIR}* | sort -hr | awk '{print $1}')" + echo "** Max: "$(echo "${tmp}" | head -n 1) +# echo " Min: "$(echo "${tmp}" | tail -n 1) + + subfolders_count=$(find ${STORAGE_DIR} -mindepth 1 -maxdepth 1 -type d | wc -l) + total_size=$(du -s ${STORAGE_DIR} | awk '{print $1}') + avg=$(((${total_size} / 1024) / ${subfolders_count})) + echo "** Avg: ${avg}M" +} + +count_archived_messages() { + psql -U ejabberd -h localhost ejabberd -c "select count(*) from archive where peer not like 'irc%' and username not in ('r.giskard', 'lisa.simpson', 'bart.simpson')" -t | grep -v '^$' | awk '{print $1}' +} + +main diff --git a/rapport_activite/update_ticket.sh b/rapport_activite/update_ticket.sh new file mode 100755 index 0000000..86c292b --- /dev/null +++ b/rapport_activite/update_ticket.sh @@ -0,0 +1,46 @@ +#! /bin/bash + +REDMINE_BASE_URL="https://agir.april.org" +TICKET_ID=4047 +REPORT_COMMAND="/usr/local/sbin/display_activity_report" + +if [ 'z' = "z${API_KEY}" ]; then + echo "Missing environment variable API_KEY" >&2 + exit 1 +fi + +main() { + post_report +} + +post_report() { + datafile=$(mktemp /tmp/report_stats_$(date +%Y-%m-%d_%H%M)_XXXX.json) + cat < "${datafile}" +{ + "issue": { + "notes": "$(${REPORT_COMMAND} | sed -z 's/\n/\\n/g')" + } +} +EOF + + curl -s \ + -H "Content-Type: application/json" \ + -H "X-Redmine-API-Key: ${API_KEY}" \ + -X PUT --data-binary "@${datafile}" \ + "${REDMINE_BASE_URL}/issues/${TICKET_ID}.json" + rm "${datafile}" +} + +# debug function +last_comment_date() { + curl -H "X-Redmine-API-Key: ${API_KEY}" -s "${REDMINE_BASE_URL}/issues/${TICKET_ID}.json?include=journals" \ + | jq '.issue.journals | last | .created_on' +} + +# debug function +list() { + curl -H "X-Redmine-API-Key: ${API_KEY}" -s "${REDMINE_BASE_URL}/issues/${TICKET_ID}.json?include=journals" \ + | jq '.issue.journals[] | [.user.name, .notes]' +} + +main