jilo/jilo-cli

186 lines
5.9 KiB
Plaintext
Raw Normal View History

2024-06-02 14:29:12 +00:00
#!/usr/bin/env bash
###
# JItsi Log Observer - command line interface
#
# Bash script for Jitsi Meet components (Videobridge, Jicofo, etc.) logs parsing
# Command line interface (jilo-cli)
###
### Configuration file (overrides default configs)
CONFIG_FILE="./jilo.conf"
### Default configuration
# Default database type (sqlite, mysql|mariadb)
DEFAULT_DB_TYPE="sqlite"
# Default SQLite database file
DEFAULT_DB="./jilo.db"
# Default MySQL/MariaDB configuration
DEFAULT_MYSQL_HOST="localhost"
DEFAULT_MYSQL_USER="jilo"
DEFAULT_MYSQL_PASS="jilopass"
DEFAULT_MYSQL_DB="jilo_db"
# Load configurations from the config file if it exists
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
fi
# use default values if not overriden by config file
DB=${DB:-$DEFAULT_DB}
DB_TYPE=${DB_TYPE:-$DEFAULT_DB_TYPE}
MYSQL_HOST=${MYSQL_HOST:-$DEFAULT_MYSQL_HOST}
MYSQL_USER=${MYSQL_USER:-$DEFAULT_MYSQL_USER}
MYSQL_PASS=${MYSQL_PASS:-$DEFAULT_MYSQL_PASS}
MYSQL_DB=${MYSQL_DB:-$DEFAULT_MYSQL_DB}
###
# DB queries
2024-06-04 12:25:04 +00:00
db_stats_conferences_all_template="SELECT * FROM conferences;"
db_stats_conferences_name_template="SELECT * FROM conferences WHERE conference_name='%s';"
db_stats_conferences_id_template="SELECT * FROM conferences WHERE conference_id='%s';"
db_stats_participants_all_template="SELECT * FROM participants;"
db_stats_participants_conference_template="SELECT * FROM participants WHERE conference_id='%s';"
db_stats_participants_event_template="SELECT * FROM participants WHERE event_type LIKE '%%%s%%';"
db_stats_participants_endpoint_template="SELECT * FROM participants WHERE endpoint_id='%s';"
db_stats_participants_statsid_template="SELECT * FROM participants WHERE stats_id='%s';"
db_stats_participants_ip_template="SELECT * FROM participants WHERE participant_ip='%s';"
help="Usage:
$0 [OPTION]
Options:
--conference|-c [conference ID or name] - show specific conference, all of empty
--participant|-p [conference ID, participant IP or word from the event type] - show specific participant, all if empty
--time|-t - show stats for a time interval"
2024-06-02 14:29:12 +00:00
###
# First we check for requirements
check_requirements() {
# required programs, anything non-bash - edit as needed
# deb packages - sqlite3
local required_programs=("sqlite3")
local requirements_missing=''
for program in "${required_programs[@]}"; do
if ! command -v "$program" &> /dev/null; then
requirements_missing+="$program, "
fi
done
if [[ "$requirements_missing" != '' ]]; then
requirements_missing=${requirements_missing::-2}
echo "Error: $requirements_missing - not found. Please install to proceed."
fi
}
check_requirements
###
# DB functions for Sqlite3 and for MySQL/MariaDB
# execute a query and return the result
db_query() {
local query=$1
if [[ "$DB_TYPE" == "sqlite" ]]; then
sqlite3 "$DB" "$query"
elif [[ "$DB_TYPE" == "mysql" || "$DB_TYPE" == "mariadb" ]]; then
mysql -h "$MYSQL_HOST" -u "$MYSQL_USER" -p "$MYSQL_PASS" -D "$MYSQL_DB" -se "$query"
else
echo "Error: unknown database type $DB_TYPE."
exit 1
fi
}
### commandline options
2024-06-02 14:29:12 +00:00
cmd=""
2024-06-04 12:25:04 +00:00
conference_arg=""
participant_arg=""
while [[ $# -gt 0 ]]; do
case "$1" in
2024-06-04 12:25:04 +00:00
-c | --conference )
cmd="--conference"
2024-06-04 11:52:42 +00:00
if [[ -n "$2" && "$2" != -* ]]; then
2024-06-04 12:25:04 +00:00
conference_arg="$2"
2024-06-04 11:52:42 +00:00
shift 2
else
shift
fi
2024-06-02 14:29:12 +00:00
;;
2024-06-04 12:25:04 +00:00
-p | --participant)
cmd="--participant"
if [[ -n "$2" && "$2" != -* ]]; then
participant_arg="$2"
shift 2
else
shift
fi
2024-06-02 14:29:12 +00:00
;;
-t | --time)
2024-06-02 14:29:12 +00:00
cmd="--time"
sift
;;
-h | --help)
echo -e "$help"
exit 0
2024-06-02 14:29:12 +00:00
;;
*)
2024-06-04 11:52:42 +00:00
echo "Invalid option: $1" >&2
2024-06-02 14:29:12 +00:00
echo -e "$help"
exit 1
;;
esac
done
case "$cmd" in
2024-06-04 12:25:04 +00:00
--conference)
if [[ -n "$conference_arg" ]]; then
db_stats_conferences_id=$(printf "$db_stats_conferences_id_template" "$conference_arg")
db_query "$db_stats_conferences_id"
db_stats_conferences_name=$(printf "$db_stats_conferences_name_template" "$conference_arg")
db_query "$db_stats_conferences_name"
2024-06-04 11:52:42 +00:00
exit 0
else
2024-06-04 12:25:04 +00:00
db_stats_conferences_all=$(printf "$db_stats_conferences_all_template")
db_query "$db_stats_conferences_all"
2024-06-04 11:52:42 +00:00
exit 0
fi
2024-06-02 14:29:12 +00:00
;;
2024-06-04 12:25:04 +00:00
--participant)
if [[ -n "$participant_arg" ]]; then
db_stats_participants_conference=$(printf "$db_stats_participants_conference_template" "$participant_arg")
db_query "$db_stats_participants_conference"
db_stats_participants_event=$(printf "$db_stats_participants_event_template" "$participant_arg")
db_query "$db_stats_participants_event"
db_stats_participants_endpoint=$(printf "$db_stats_participants_endpoint_template" "$participant_arg")
db_query "$db_stats_participants_endpoint"
db_stats_participants_statsid=$(printf "$db_stats_participants_statsid_template" "$participant_arg")
db_query "$db_stats_participants_statsid"
db_stats_participants_ip=$(printf "$db_stats_participants_ip_template" "$participant_arg")
db_query "$db_stats_participants_ip"
exit 0
else
db_stats_participants_all=$(printf "$db_stats_participants_all_template" )
db_query "$db_stats_participants_all"
exit 0
fi
2024-06-02 14:29:12 +00:00
;;
--time)
db_stats_conferences=$(printf "$db_stats_conferences_template" )
db_query "$db_stats_conferences"
db_stats_participants=$(printf "$db_stats_participants_template" )
db_query "$db_stats_participants"
exit 0
;;
*)
echo -e "$help"
exit 1
;;
esac