Prosody

Manage your Prosody XMPP server via instant messages!

This page will give you some ideas on how to retreive real-time information from a Prosody XMPP server and send it via instant message.

How many logins each hour?

In this example I begin by showing how I modified mod_log_auth.lua to not associate XMPP IDs with IP their IP addresses.

-- a modified snippet from file mod_log_auth.lua
if mode == "success" or mode == "all" then
        module:hook("authentication-success", function (event)
                local session = event.session;
                session.log("info", "Successful authentication from IP: %s", session.ip); -- EDWARD
        end);
end

With that in mind, I created a script similar to the one I use for Apache2, that sends me data on the number of logins to prosody each hour.

#!/bin/bash
# by Edward Stoever
/usr/bin/sleep $((1 + $RANDOM %55)) # include this when running from cron
. /etc/profile
. /root/.profile

# How popular is my XMPP server?
# A quick bash script by Edward Stoever

THISHOUR=`date "+%k" | xargs`
if [ "$THISHOUR" = "0" ]; then
 LOG='/var/log/prosody/prosody.log.1'
else
 LOG='/var/log/prosody/prosody.log'
fi

HOUR=`date -d -1hour "+%k" | xargs`
if [ "$HOUR" = "1" ]; then
  ORDINAL='st';
elif [ "$HOUR" = "2" ]; then
  ORDINAL='nd';
elif [ "$HOUR" = "3" ]; then
  ORDINAL='rd';
elif [ "$HOUR" = "21" ]; then
  ORDINAL='st';
elif [ "$HOUR" = "22" ]; then
  ORDINAL='nd';
elif [ "$HOUR" = "23" ]; then
  ORDINAL='rd';
elif [ "$HOUR" = "0" ]; then
  ORDINAL='';
else
  ORDINAL='th';
fi

LOOK4='Successful authentication from IP'
LAST_COMPLETED_HOUR=`date -d -1hour "+%b %d %H:"`
UNIQ=`grep --binary-files=text --no-filename "$LAST_COMPLETED_HOUR" $LOG 2>/dev/null|grep --binary-files=text "$LOOK4"|awk '{ print $10 }'|sort | uniq| wc -l`
TOTAL=`grep --binary-files=text --no-filename "$LAST_COMPLETED_HOUR" $LOG 2>/dev/null|grep --binary-files=text "$LOOK4"|awk '{ print $10 }'| wc -l`
if [ "$TOTAL" = "1" ]; then
   VERB='was'; CONN='authentication';
else
   VERB='were'; CONN='authentications';
fi
if [ "$UNIQ" = "1" ]; then
   ADDR='address';
else
   ADDR='addresses';
fi

MSG="<GAMEDIE> There $VERB $TOTAL C2S $CONN from $UNIQ unique IP $ADDR established with the prosody server in the $HOUR$ORDINAL hour."

/usr/local/bin/monitor.chat.sh "$MSG"
echo "$MSG"

This message is sent by cron once every 4 hours.

How many chatrooms are there?

In this example, I use prosody’s admin telnet to find out how many chatrooms there are.

#!/bin/bash

HOST='localhost 5582'
CMD='s2s:show()'

(
echo open "$HOST"
sleep 1
echo "$CMD"
sleep 1
echo "bye"
) | telnet

The previous script is called by this one, which I list in cron to run once per hour:

#!/bin/bash
. /etc/profile
. /root/.profile
/usr/bin/sleep $((1 + $RANDOM %55)) # include this when running from cron

/root/bin/list_all_rooms.sh > /tmp/rooms.txt
HOWMANY=`grep --text e2e.chat /tmp/rooms.txt |wc -l`
if [ $HOWMANY = "1" ]; then
  VERB="is"
  CR="chatroom"
else
  VERB="are"
  CR="chatrooms"
fi

FRASE=`echo "<FIRSTQMOON> There $VERB $HOWMANY $CR on the e2e.chat domain."`

/usr/local/bin/monitor.chat.sh "$FRASE"
echo $FRASE

rm -f /tmp/rooms.txt

It is easy to keep an eye on how many chatrooms there are with the stat is sent once per hour by instant message.

Here is my crontab entry:

# How many chatrooms on the Prosody Server? Send once every 4 hours on the 22nd minute:
22 */4 * * * /root/bin/how_many_chatrooms.sh >> /root/bin/log/how_many_chatrooms.sh 2>&1

Advanced Queries for Prosody

The modfication mod_server_status.lua is an excellent way of querying real-time data from your prosody instance. I have modified mine to get some additional data such as uptime and current session counts. You can get a copy of my version here.

Lua

Send a message from one of Prosody’s lua files

Last modified December 15, 2020