xmpp.chapril.org-tools/rapport_activite/rapport_activites.sh
2020-12-13 13:46:13 +01:00

142 lines
6.3 KiB
Bash
Executable File

#! /bin/bash
STORAGE_DIR="/var/www/xmpp.chapril.org/upload/"
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 "** Active: $(count_active_accounts)"
echo "** Inactive: $(count_inactive_accounts '1 MONTH')"
echo "** Removable: $(count_removable_accounts '6 MONTHS')"
echo "* Total messages stored (MAM): $(count_archived_messages) "
echo "* Total rooms active: $(sudo -u ejabberd ejabberdctl muc_online_rooms global | wc -l)"
echo ""
echo "_* Inactive accounts:_ no connection during last month"
echo "_* Removable accounts:_ no connection in the last 6 months *and* didn't read the welcome message"
echo ""
echo "h2. ## HTTP upload storage:"
echo ""
echo "* User quota: $(get_hard_quota)M"
echo "* Total disk usage: $(du -sh ${STORAGE_DIR} | awk '{print $1}')"
per_account_storage_usage
echo ""
echo "h2. ## Audio/Video Call usage:"
# Dans la config actuelle de logrotate, le log du mois dernier est stocké en tete clair, et tous les autres sont compressés par gzip. Un cat ne sortira de texte lisible que pour les fichiers en clair soit forcément celui du mois précédent
echo ""
echo "* Number of calls relayed: $(ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Offering stun" | wc -l)"
echo "* Data relayed per protocol per call:"
echo "** UDP: avg $(get_avg_UDP_turn_relayed_data) KiB, max $(get_max_UDP_turn_relayed_data) KiB, total $(get_total_UDP_turn_relayed_data) KiB"
echo "** TCP: avg $(get_avg_TCP_turn_relayed_data) KiB, max $(get_max_TCP_turn_relayed_data) KiB, total $(get_total_TCP_turn_relayed_data) KiB"
echo "** TLS: avg $(get_avg_TLS_turn_relayed_data) KiB, max $(get_max_TLS_turn_relayed_data) KiB, total $(get_total_TLS_turn_relayed_data) KiB"
}
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() {
# 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_active_accounts() {
since=${1:-'1 MONTH'}
sql="select count(distinct l.username)
from last l
join users u on l.username = u.username
where to_timestamp(cast(l.seconds as int)) > current_timestamp - interval '${since}'
and l.username not in ('r.giskard', 'lisa.simpson', 'bart.simpson')"
psql -U ejabberd -h localhost ejabberd -c "${sql}" -t | grep -v '^$' | awk '{print $1}'
}
count_inactive_accounts() {
since=${1:-'1 MONTH'}
sql="select count(distinct l.username)
from last l
join users u on l.username = u.username
where to_timestamp(cast(l.seconds as int)) < current_timestamp - interval '${since}'
and l.username not in ('r.giskard', 'lisa.simpson', 'bart.simpson')"
psql -U ejabberd -h localhost ejabberd -c "${sql}" -t | grep -v '^$' | awk '{print $1}'
}
count_removable_accounts() {
# Accounts inactive *and* who never read the welcome message.
since=${1:-'6 MONTHS'}
sql="select count(distinct l.username)
from last l
join users u on l.username = u.username
join spool s on l.username = s.username
where to_timestamp(cast(l.seconds as int)) < current_timestamp - interval '${since}'
and s.xml like '%from=''chapril.org'' xmlns=''jabber:client''%Bienvenue sur le serveur%'
and s.username not in ('r.giskard', 'lisa.simpson', 'bart.simpson')"
psql -U ejabberd -h localhost ejabberd -c "${sql}" -t | grep -v '^$' | awk '{print $1}'
}
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}'
}
get_stunstats_from_log() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Offering stun"
}
get_total_UDP_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "UDP" | cut -d" " -f6-6 | paste -sd+ - | bc
}
get_total_TCP_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TCP" | cut -d" " -f6-6 | paste -sd+ - | bc
}
get_total_TLS_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TLS" | cut -d" " -f6-6 | paste -sd+ - | bc
}
get_max_UDP_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "UDP" | cut -d" " -f6-6 | sort | tail -n 1
}
get_max_TCP_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TCP" | cut -d" " -f6-6 | sort | tail -n 1
}
get_max_TLS_turn_relayed_data() {
ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TLS" | cut -d" " -f6-6 | paste -sd+ - | sort | tail -n 1
}
get_avg_UDP_turn_relayed_data() {
num=$(ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "UDP" | wc -l)
total=$(get_total_UDP_turn_relayed_data)
echo "scale=2; $total / $num" | bc -l
}
get_avg_TCP_turn_relayed_data() {
num=$(ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TCP" | wc -l)
total=$(get_total_TCP_turn_relayed_data)
echo "scale=2; $total / $num" | bc -l
}
get_avg_TLS_turn_relayed_data() {
num=$(ls -1tr /var/log/ejabberd/ejabberd.log* | tail -n 5 | xargs zcat -f | grep -i -e "Relayed" | grep "TLS" | wc -l)
total=$(get_total_TLS_turn_relayed_data)
echo "scale=2; $total / $num" | bc -l
}
main