Initial version of jilo-cli

main
Yasen Pramatarov 2024-06-02 17:29:12 +03:00
parent 7bd5b9a253
commit 6e6baea905
1 changed files with 131 additions and 0 deletions

131
jilo-cli 100755
View File

@ -0,0 +1,131 @@
#!/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
db_stats_conferences_template="SELECT * FROM conferences;"
db_stats_participants_template="SELECT * FROM participants;"
help="Usage:\n\t$0 [OPTION]\nOptions:\n\t--conferences|-c - show conference stats\n\t--participants|-p - show participants stats\n\t--time|-t - show stats for a time interval"
###
# 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
while getopts ":cpt" opt; do
case $opt in
c)
cmd="--conferences"
;;
p)
cmd="--participants"
;;
t)
cmd="--time"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo -e "$help"
exit 1
;;
esac
done
shift $((OPTIND -1))
case "$cmd" in
--conferences)
db_stats_conferences=$(printf "$db_stats_conferences_template" )
db_query "$db_stats_conferences"
exit 0
;;
--participant|--participants)
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