jilo/jilo-cli

152 lines
3.9 KiB
Bash

#!/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 WHERE conference_id='%s';"
db_stats_participants_template="SELECT * FROM participants;"
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"
###
# 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
cmd=""
conference_id=""
while [[ $# -gt 0 ]]; do
case "$1" in
-c | --conferences )
cmd="--conferences"
if [[ -n "$2" && "$2" != -* ]]; then
conference_id="$2"
shift 2
else
shift
fi
;;
-p | --participants)
cmd="--participants"
shift
;;
-t | --time)
cmd="--time"
sift
;;
-h | --help)
echo -e "$help"
exit 0
;;
*)
echo "Invalid option: $1" >&2
echo -e "$help"
exit 1
;;
esac
done
case "$cmd" in
--conferences)
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
;;
--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