jilo/jilo-cli

152 lines
3.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 11:52:42 +00:00
db_stats_conferences_template="SELECT * FROM conferences WHERE conference_id='%s';"
2024-06-02 14:29:12 +00:00
db_stats_participants_template="SELECT * FROM participants;"
2024-06-04 11:52:42 +00:00
help="Usage:\n\t$0 [OPTION]\nOptions:\n\t--conferences|-c [conference ID] - show conference stats\n\t--participants|-p - show participants stats\n\t--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 11:52:42 +00:00
conference_id=""
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --conferences )
2024-06-02 14:29:12 +00:00
cmd="--conferences"
2024-06-04 11:52:42 +00:00
if [[ -n "$2" && "$2" != -* ]]; then
conference_id="$2"
shift 2
else
shift
fi
2024-06-02 14:29:12 +00:00
;;
-p | --participants)
2024-06-02 14:29:12 +00:00
cmd="--participants"
shift
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
--conferences)
2024-06-04 11:52:42 +00:00
if [[ -n "$conference_id" ]]; then
db_stats_conferences=$(printf "$db_stats_conferences_template" "$conference_id")
db_query "$db_stats_conferences"
exit 0
else
db_stats_conferences=$(printf "$db_stats_conferences_template" ".*")
db_query "$db_stats_conferences"
exit 0
fi
2024-06-02 14:29:12 +00:00
;;
--participants)
2024-06-02 14:29:12 +00:00
db_stats_participants=$(printf "$db_stats_participants_template" )
db_query "$db_stats_participants"
exit 0
;;
--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