synapse/cmd.sh
author Peter Gervai <grin@grin.hu>
Mon, 24 Jan 2022 10:21:08 +0100
changeset 6 85afc1496738
parent 3 99e6c1cdae3b
child 7 6aea45bf033b
permissions -rwxr-xr-x
madmin shows v2 delete room example
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/bin/bash
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
#$Id: cmd.sh,v 7c8632f2b6be 2022/01/21 09:05:10 grin $
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
#
3
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     4
# This program queries PostgreSQL directly for various user statistics.
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     5
# Set psql access below (CMD=...), ensure that .pgpass lets user to login
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     6
#  without password entry.
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     7
# Start without arguments for help.
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
     8
#
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
# (c)Peter Gervai, 2020-21
3
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
    10
# @grin:grin.hu
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
# Licensed under GPLv3+ and CC_By-Sa-4.0-int
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
#
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
#CMD='sudo -u postgres psql synapse -tAc'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
CMD='psql -h 10.1.1.257 -U synapse synapse -c'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
MODE="$1"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
ARG="$2"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
if [ "x$MODE" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
	echo "Usage: $0 <mode> <args> ...."
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
	echo " modes: user @someuser:grin.hu       - user stats"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
	echo "        userevents @someuser:grin.hu - user events"
3
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
    24
	echo "        active1                      - last 1 day active users timeline"
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
    25
	echo "        active1u                     - last 1 day users (unique results)"
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
	echo "        lastreg [<n>]	           - last <n> (25) registrations"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
egy_arg() {
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
	local s=$1
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
	if [ "x$ARG" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
		echo "Missing arg ${s}!"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
		echo "$0 $MODE $s"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
		exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
	fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
}
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
if [ "$MODE" = "user" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
	egy_arg 'user'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
	echo "=== User data ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
	$CMD "SELECT display_name FROM user_directory WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
	$CMD "SELECT medium, address, unts(validated_at) AS validated_at, unts(added_at) AS added_at FROM user_threepids WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
	$CMD "SELECT joined_rooms FROM user_stats_current WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
 	echo "=== Logins ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
 	$CMD "SELECT user_id,device_id,ip,unts(last_seen) AS last_seen,user_agent FROM user_ips WHERE user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
 	$CMD "SELECT device_id, user_agent, unts(timestamp) FROM user_daily_visits WHERE user_id='$ARG' ORDER BY 1 ASC, 3 DESC LIMIT 10"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
 	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
 	echo "=== Shared private rooms ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
 	$CMD "select * from users_who_share_private_rooms where user_id='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
 	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
 	echo "=== Rooms the user has been ==="
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
 	$CMD "SELECT rm.room_id, rm.event_id, rm.sender, rm.membership, 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
       rss.canonical_alias, rss.join_rules, rss.encryption, 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
       unts(e.origin_server_ts), rss.name
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
 FROM room_memberships  rm
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
 JOIN room_stats_state rss USING(room_id) 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
 JOIN events e USING(event_id)
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
 WHERE user_id='$ARG'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
 ORDER BY e.origin_server_ts" | more
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
if [ "$MODE" = "userevents" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
        egy_arg 'user'
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
        $CMD "SELECT * FROM events WHERE sender='@sztg:grin.hu';
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
SELECT stream_ordering,type,e.room_id,content,sender,unts(origin_server_ts) AS orig_s,json 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
 FROM events e LEFT JOIN event_json j USING(event_id) 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
 WHERE sender='$ARG'"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
if [ "$MODE" = "active1" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
	$CMD "SELECT user_id,device_id,ip,unts(MAX(last_seen)) AS last_seen,user_agent FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
		 GROUP BY 1,2,3,5
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
		 ORDER BY last_seen DESC"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
if [ "$MODE" = "lastreg" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
	if [ "x$ARG" = "x" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    93
		ARG=25
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    94
	fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    95
	
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    96
	$CMD "SELECT name,unts(creation_ts*1000) AS created,is_guest,deactivated,shadow_banned,
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    97
		device_id,ip,unts(last_seen) AS last_seen, user_agent
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    98
		FROM users LEFT JOIN user_ips ON(name=user_id)
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    99
		ORDER BY 2 DESC 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   100
		LIMIT $ARG"
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   101
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   102
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   103
if [ "$MODE" = "active1u" ]; then
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   104
	$CMD "SELECT DISTINCT user_id FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   105
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
3
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   106
		 ORDER BY 1" | cat
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   107
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   108
	$CMD "SELECT DISTINCT user_id,device_id,ip FROM user_ips 
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   109
		 WHERE unts(last_seen)> NOW()-'1 day'::interval 
3
99e6c1cdae3b synapse/cmd.sh: add description comment
Peter Gervai <grin@grin.hu>
parents: 2
diff changeset
   110
		 ORDER BY 1" | cat
2
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   111
 	exit
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   112
fi
c3f49b58aff6 Add synapse/cmd.sh: query db for various user statistics
Peter Gervai <grin@grin.hu>
parents:
diff changeset
   113