xmpp.chapril.org-tools/metrology/collectd_exec_xmpp.sh

172 lines
6.6 KiB
Bash
Executable File

#!/bin/bash
HOSTNAME="${COLLECTD_HOSTNAME:-localhost}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
# required by some ejabberdctl commands
export HOME=/var/lib/ejabberd
export HTTP_UPLOAD_DATADIR="/var/www/xmpp.chapril.org/upload/"
main() {
# do-while hack found here: https://stackoverflow.com/questions/24420342/is-there-a-do-while-loop-in-bash#24421013
while
ejabberdctl_stats
http_upload_disk_usage
captcha_stats
_useragents_stats
sleep "$INTERVAL"
do true; done
}
ejabberdctl_stats() {
echo "PUTVAL \"${HOSTNAME}/xmpp_registered_users/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl registered_users chapril.org | wc -l)"
echo "PUTVAL \"${HOSTNAME}/xmpp_active_users/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl connected_users_number)"
echo "PUTVAL \"${HOSTNAME}/xmpp_active_distinct_users/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl connected_users | cut -d@ -f1 | sort -u | wc -l)"
echo "PUTVAL \"${HOSTNAME}/xmpp_s2s_in/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl incoming_s2s_number)"
echo "PUTVAL \"${HOSTNAME}/xmpp_s2s_out/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl outgoing_s2s_number)"
echo "PUTVAL \"${HOSTNAME}/xmpp_muc_total/count\" interval=$INTERVAL $(date +%s):$(ejabberdctl muc_online_rooms muc.chapril.org | wc -l)"
echo "PUTVAL \"${HOSTNAME}/xmpp_accounts_created_lastday/count\" interval=$INTERVAL $(date +%s):$(_count_accounts_created_lastday)"
echo "PUTVAL \"${HOSTNAME}/xmpp_accounts_created_lasthour/count\" interval=$INTERVAL $(date +%s):$(_count_accounts_created_lasthour)"
echo "PUTVAL \"${HOSTNAME}/xmpp_total_mam_rows/count\" interval=$INTERVAL $(date +%s):$(_count_mam_rows)"
echo "PUTVAL \"${HOSTNAME}/xmpp_total_mam_size/count\" interval=$INTERVAL $(date +%s):$(_estimate_mam_size)"
echo -n "PUTVAL \"${HOSTNAME}/xmpp_public_rooms_occupants/count\" interval=$INTERVAL $(date +%s):" ; _xmpp_public_rooms_total_occupants
}
http_upload_disk_usage() {
subfolders_count=$(find ${HTTP_UPLOAD_DATADIR} -mindepth 1 -maxdepth 1 -type d | wc -l)
total_size=$(du --exclude=lost+found -s ${HTTP_UPLOAD_DATADIR} | awk '{print $1}')
avg=$(( ${total_size} / ${subfolders_count} ))
echo "PUTVAL \"${HOSTNAME}/xmpp_upload_data_total/count\" interval=$INTERVAL $(date +%s):${total_size}"
echo "PUTVAL \"${HOSTNAME}/xmpp_upload_data_avg/count\" interval=$INTERVAL $(date +%s):${avg}"
}
captcha_stats() {
echo "PUTVAL \"${HOSTNAME}/xmpp_captcha_lastday/count\" interval=$INTERVAL $(date +%s):$(find /var/lib/ejabberd/chapril_captchas/ -mindepth 1 -maxdepth 1 -type f -mtime -1 | grep -v EXPERIMENTAL | wc -l)"
echo "PUTVAL \"${HOSTNAME}/xmpp_captcha_lasthour/count\" interval=$INTERVAL $(date +%s):$(find /var/lib/ejabberd/chapril_captchas/ -mindepth 1 -maxdepth 1 -type f -mmin -60 | grep -v EXPERIMENTAL | wc -l)"
}
_count_accounts_created_lastday() {
_sql_ejabberd "
select count(*)
from users
where created_at > current_timestamp - interval '1 day'
"
}
_count_accounts_created_lasthour() {
_sql_ejabberd "
select count(*)
from users
where created_at > current_timestamp - interval '1 hour'
"
}
_count_mam_rows() {
_sql_ejabberd "SELECT reltuples::bigint AS estimate FROM pg_class WHERE relname = 'archive'"
}
_estimate_mam_size() {
_sql_ejabberd "
select pg_relation_size(relid) as mam_size
from pg_catalog.pg_statio_user_tables
where relname = 'archive'
"
}
_xmpp_public_rooms_total_occupants() {
# keep results in a cache file for 5 minutes
local cache_file=/var/tmp/cache_collectd_xmpp_rooms.data
if [ ! -f ${cache_file} -o "$(find ${cache_file} -mmin +5 2>/dev/null)" ]; then
_xmpp_list_public_rooms > ${cache_file}
fi
total_occupants=0
while read line; do
total_occupants=$((total_occupants+$(echo ${line} | awk '{print $2}')))
done < ${cache_file}
echo ${total_occupants}
}
_xmpp_list_public_rooms() {
for room in $(ejabberdctl muc_online_rooms muc.chapril.org | cut -d@ -f1) ; do
if ejabberdctl get_room_options ${room} muc.chapril.org | egrep -q '^public\s*true' ; then
echo -n "${room}: "
ejabberdctl get_room_occupants ${room} muc.chapril.org | cut -d@ -f1 | sort -u | wc -l
fi
done | sort -k 2 -nr
}
_sql_ejabberd() {
# XXX the psql command can only works if file /var/lib/ejabberd/.pgpass exists
psql -U ejabberd -h localhost ejabberd -c "${1}" -t | grep -v '^$' | awk '{print $1}'
}
_useragents_stats() {
declare -A counters
items="$(ejabberdctl connected_users_info | sed -r 's/^(.*)\s+c2s_.*/\1/g' | cut -d/ -f2- | sed -r 's/\s+/_/g' )"
for line in $items; do
if echo ${line} | grep -qEi gajim ; then
ua=gajim
elif echo ${line} | grep -qEi dino ; then
ua=dino
elif echo ${line} | grep -qEi profanity ; then
ua=profanity
elif echo ${line} | grep -qEi conversations ; then
ua=conversations
elif echo ${line} | grep -qEi blabber ; then
ua=blabber
elif echo ${line} | grep -qEi cheogram ; then
ua=cheogram
elif echo ${line} | grep -qEi monocles ; then
ua=monocles
elif echo ${line} | grep -qEi monal ; then
ua=monal
elif echo ${line} | grep -qEi snikket ; then
ua=snikket
elif echo ${line} | grep -qEi movim ; then
ua=movim
elif echo ${line} | grep -qEi converse ; then
ua=converse
elif echo ${line} | grep -qEi unichat ; then
ua=unichat
elif echo ${line} | grep -qEi "poezio" ; then
ua=poezio
elif echo ${line} | grep -qEi mcabber ; then
ua=mcabber
elif echo ${line} | grep -qEi c0nnect ; then
ua=c0nnect
elif echo ${line} | grep -qEi "pix-art" ; then
ua=pix-art
elif echo ${line} | grep -qEi "bitlbee" ; then
ua=bitlbee
elif echo ${line} | grep -qEi "(pidgin|gaim)" ; then
ua=pidgin
elif echo ${line} | grep -qEi "iPhone" ; then
ua=siskin-probably
elif echo ${line} | grep -qEi "^[0-9]{21,26}" ; then
ua=pidgin-probably
elif echo ${line} | grep -qEi "macbook" ; then
ua=mac-computer
elif echo ${line} | grep -qEi "imac-de" ; then
ua=mac-computer
else
ua=other
fi
k=${counters[$ua]:-0}
((k++))
counters[$ua]=$k
done
ts=$(date +%s)
for i in "${!counters[@]}"; do
ua=$i
count=${counters[$i]}
echo "PUTVAL \"${HOSTNAME}/xmpp_useragent/count-${ua}\" interval=${INTERVAL} ${ts}:${count}"
done
}
main