2024-05-22 07:57:50 +00:00
#!/usr/bin/env bash
2024-05-27 12:41:43 +00:00
### Default configuration
2024-05-22 16:58:08 +00:00
2024-05-27 12:41:43 +00:00
# default log files
DEFAULT_LOGFILE="./jvb.log"
DEFAULT_ROTATED_LOGFILE="./jvb.log.1"
# Default SQLite database file
DEFAULT_DB="./jitsi-stats.db"
# Configuration file
CONFIG_FILE="./jilo.conf"
# 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
LOGFILE=${LOGFILE:-$DEFAULT_LOGFILE}
ROTATED_LOGFILE=${ROTATED_LOGFILE:-$DEFAULT_ROTATED_LOGFILE}
DB=${DB:-$DEFAULT_DB}
2024-05-22 16:58:08 +00:00
###
# SQLite queries
2024-05-26 19:01:54 +00:00
db_get_state="SELECT filename, filetime, filesize, position, inode FROM state WHERE jitsi_component = 'JVB';"
db_set_state_template="UPDATE state SET time=datetime('now'), filename='%s', filetime='%s', filesize='%s', position='%s', inode='%s' WHERE jitsi_component = 'JVB';"
2024-05-22 16:58:08 +00:00
db_insert_template="INSERT INTO conferences (conference_name, conference_id, start, end) VALUES ('%s', '%s', '%s', '%s');"
2024-05-23 09:05:54 +00:00
db_create="
2024-05-23 16:39:57 +00:00
DROP TABLE IF EXISTS conferences;
2024-05-23 09:05:54 +00:00
CREATE TABLE conferences (
id INTEGER PRIMARY_KEY,
conference_name TEXT,
conference_id TEXT,
start TEXT,
end TEXT
);
2024-05-23 16:39:57 +00:00
DROP TABLE IF EXISTS state;
2024-05-23 09:05:54 +00:00
CREATE TABLE state (
id INTEGER PRIMARY_KEY,
2024-05-26 19:01:54 +00:00
jitsi_component TEXT,
2024-05-23 09:05:54 +00:00
time TEXT,
filename TEXT,
filetime INTEGER,
2024-05-23 16:39:57 +00:00
filesize INTEGER,
2024-05-23 09:05:54 +00:00
position INTEGER CHECK(typeof(position)='integer'),
inode INTEGER
);
2024-05-26 19:01:54 +00:00
INSERT OR REPLACE INTO state (id, jitsi_component, time, filename, filetime, filesize, position, inode) VALUES (1, 'JVB', '1970-01-01 00:00:00.000', '', 0, 0, 0, 0);"
2024-05-23 09:05:54 +00:00
db_flush="
DELETE FROM conferences;
DELETE FROM state;
2024-05-26 19:01:54 +00:00
INSERT OR REPLACE INTO state (id, jitsi_component, time, filename, filetime, filesize, position, inode) VALUES (1, 'JVB', '1970-01-01 00:00:00.000', '', 0, 0, 0, 0);"
2024-05-23 16:39:57 +00:00
help="Usage:\n\t$0 [OPTION]\nOptions:\n\t--create-db|-d - create the database\n\t--flush|-f - flush the tables\n\t--check|-c - check for new data\n\t--parse|-p [-v] - parse the logs [verbosely]"
2024-05-23 09:05:54 +00:00
###
2024-05-24 08:15:23 +00:00
# First we check for requirements
check_requirements() {
# required programs, anything non-bash - edit as needed
# deb packages - sqlite3, coreutils(stat,dd)
local required_programs=("sqlite3" "stat" "dd")
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
###
2024-05-23 09:05:54 +00:00
# DB functions
2024-05-22 16:58:08 +00:00
# Get the last processed state from the database
get_state() {
sqlite3 "$DB" "$db_get_state"
}
# Update the state database
set_state() {
local filename=$1
local filetime=$2
2024-05-23 16:39:57 +00:00
local filesize=$3
local position=${4:-0}
local inode=$5
db_set_state=$(printf "$db_set_state_template" "$filename" "$filetime" "$filesize" "$position" "$inode")
2024-05-22 16:58:08 +00:00
sqlite3 "$DB" "$db_set_state"
}
2024-05-23 09:05:54 +00:00
###
2024-05-22 07:57:50 +00:00
2024-05-23 09:05:54 +00:00
# Main parsing funstion
2024-05-22 07:57:50 +00:00
jvb_log_parse() {
2024-05-22 16:58:08 +00:00
local file=$1
local start_pos=$2
2024-05-23 16:39:57 +00:00
new_last_pos="$start_pos"
2024-05-22 07:57:50 +00:00
2024-05-23 09:05:54 +00:00
# Local assoc array for conference events tracking
declare -A start_times
2024-05-23 16:39:57 +00:00
# Get size and position for progress tracking
local total_size=$(stat -c '%s' "$file")
local processed_lines=0
local procesed_bytes=0
2024-05-22 16:58:08 +00:00
# We open the file and start reading from $start_pos bytes
exec 3<"$file"
2024-05-22 07:57:50 +00:00
while IFS= read -r line; do
2024-05-22 16:58:08 +00:00
# save new position (previous plus bytes in current line plus 1 for the new line)
new_last_pos=$((new_last_pos + ${#line} + 1))
2024-05-23 16:39:57 +00:00
# increment progress stats
processed_lines=$((processed_lines + 1))
processed_bytes=$((processed_bytes + ${#line} + 1))
# show progress if in verbose mode
if [[ "$verbose" == true ]]; then
local percent=$((100 * processed_bytes / total_size))
echo -ne "Processing: $percent% ($processed_lines lines, $processed_bytes bytes) \r"
fi
2024-05-22 16:58:08 +00:00
# locate conference starting event
if [[ "$line" =~ ([0-9-]+\ [0-9:.]+)\ [A-Z]+:.*\ Videobridge\.createConference#[0-9]+:\ create_conf,\ id=([a-zA-Z0-9]+) ]]; then
2024-05-22 07:57:50 +00:00
timestamp="${BASH_REMATCH[1]}"
conferenceId="${BASH_REMATCH[2]}"
2024-05-22 16:58:08 +00:00
start_times["$conferenceId"]="$timestamp"
2024-05-22 07:57:50 +00:00
2024-05-22 16:58:08 +00:00
# locate the corresponding conference ending event
elif [[ "$line" =~ ([0-9-]+\ [0-9:.]+)\ [A-Z]+:.*\[confId=([a-zA-Z0-9]+)\ .*conf_name=([^ ]+)@.*\]\ Conference\.expire ]]; then
end_time="${BASH_REMATCH[1]}"
2024-05-22 07:57:50 +00:00
conferenceId="${BASH_REMATCH[2]}"
conferenceName="${BASH_REMATCH[3]}"
2024-05-22 16:58:08 +00:00
start_time="${start_times["$conferenceId"]}"
if [[ -n "$start_time" ]]; then
db_insert=$(printf "$db_insert_template" "$conferenceName" "$conferenceId" "$start_time" "$end_time")
sqlite3 "$DB" "$db_insert"
unset start_times["$conferenceId"]
2024-05-22 07:57:50 +00:00
fi
fi
2024-05-22 16:58:08 +00:00
# We don't use pipe, but process substitution '<(dd...)' to avoid running while loop in subshell and lose the 'new_last_pos' value
done < <(dd bs=1 skip="$start_pos" <&3 2>/dev/null)
2024-05-22 07:57:50 +00:00
2024-05-23 16:39:57 +00:00
# Close the file descriptor
2024-05-22 16:58:08 +00:00
exec 3<&-
}
2024-05-22 07:57:50 +00:00
2024-05-23 09:05:54 +00:00
###
# commandline options
2024-05-23 16:39:57 +00:00
while getopts ":dfcpv" opt; do
case $opt in
d)
cmd="--create-db"
;;
f)
cmd="--flush"
;;
c)
cmd="--check"
;;
p)
cmd="--parse"
;;
v)
verbose=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
echo -e "$help"
exit 1
;;
esac
done
shift $((OPTIND -1))
case "$cmd" in
--create-db)
2024-05-23 09:05:54 +00:00
sqlite3 "$DB" "$db_create"
echo "Database created."
exit 0
;;
2024-05-23 16:39:57 +00:00
--flush)
2024-05-23 09:05:54 +00:00
sqlite3 "$DB" "$db_flush"
echo "Tables flushed."
exit 0
;;
2024-05-23 16:39:57 +00:00
--check)
2024-05-23 09:05:54 +00:00
# Retrieve last log file and position inside it
2024-05-23 16:39:57 +00:00
IFS='|' read -r last_file last_filetime last_size last_pos last_inode <<< $(get_state)
2024-05-23 09:05:54 +00:00
# Initialize logfile vars
current_inode=$(stat -c '%i' "$LOGFILE")
current_filetime=$(stat -c '%Y' "$LOGFILE")
2024-05-23 16:39:57 +00:00
current_size=$(stat -c '%s' "$LOGFILE")
2024-05-23 09:05:54 +00:00
if [[ "$last_file" == '' || "$last_inode" == 0 ]]; then
echo "It looks like a fresh install. You can now run log parsing."
exit 0
fi
# report
echo "Last file: $last_file"
echo "Last filetime: $last_filetime"
echo "Last inode: $last_inode"
2024-05-23 16:39:57 +00:00
echo "Last size: $last_size"
2024-05-23 09:05:54 +00:00
echo "Last processed position: $last_pos"
echo "Current filetime: $current_filetime"
echo "Current inode: $current_inode"
2024-05-23 16:39:57 +00:00
echo "Current size: $current_size"
2024-05-23 09:05:54 +00:00
2024-05-23 16:39:57 +00:00
if [[ "$last_inode" == "$current_inode" && "$current_size" -lt "$last_pos" && -f "$ROTATED_LOGFILE" ]]; then
2024-05-23 09:05:54 +00:00
echo "Log file has rotated."
else
echo "Log file has not rotated."
fi
2024-05-23 16:39:57 +00:00
if [[ "$current_filetime" -ne "$last_filetime" || "$current_size" -ne "$last_size" ]]; then
2024-05-23 09:05:54 +00:00
echo "New lines have been added to the log."
else
echo "No new lines in the log."
fi
exit 0
;;
2024-05-23 16:39:57 +00:00
--parse)
2024-05-23 09:05:54 +00:00
# Retrieve last log file and position inside it
2024-05-23 16:39:57 +00:00
IFS='|' read -r last_file last_filetime last_size last_pos last_inode <<< $(get_state)
2024-05-23 09:05:54 +00:00
# Initialize logfile vars
last_pos=${last_pos:-0}
current_inode=$(stat -c '%i' "$LOGFILE")
current_filetime=$(stat -c '%Y' "$LOGFILE")
2024-05-23 16:39:57 +00:00
current_size=$(stat -c '%s' "$LOGFILE")
2024-05-23 09:05:54 +00:00
# Detect if the logfile was rotated (same inode, smaller size - copytruncate in logrotate)
# parse the rotated log file
if [[ "$last_inode" == "$current_inode" && "$current_size" -lt "$last_pos" && -f "$ROTATED_LOGFILE" ]]; then
echo "Logfile was rotated. Processing the rotated log file: $ROTATED_LOGFILE"
2024-05-23 16:39:57 +00:00
jvb_log_parse "$ROTATED_LOGFILE" 0 "$verbose"
2024-05-23 09:05:54 +00:00
last_file="$ROTATED_LOGFILE"
last_inode=$(stat -c '%i' "$ROTATED_LOGFILE")
last_filetime=$(stat -c '%Y' "$ROTATED_LOGFILE")
2024-05-23 16:39:57 +00:00
set_state "$last_file" "$last_filetime" "$last_size" "$last_pos" "$last_inode"
2024-05-23 09:05:54 +00:00
fi
# parse the current log file
echo "Processing the current log file: $LOGFILE"
2024-05-23 16:39:57 +00:00
jvb_log_parse "$LOGFILE" "$last_pos" "$verbose"
2024-05-22 07:57:50 +00:00
2024-05-23 16:39:57 +00:00
if [[ "$verbose" == true ]]; then
echo -e "\nNew last position after parsing: $new_last_pos"
fi
2024-05-22 16:58:08 +00:00
2024-05-23 09:05:54 +00:00
# update the state in db
2024-05-23 16:39:57 +00:00
set_state "$LOGFILE" "$current_filetime" "$current_size" "$new_last_pos" "$current_inode"
2024-05-22 16:58:08 +00:00
2024-05-23 09:05:54 +00:00
echo "Data import finished."
exit 0
;;
2024-05-22 07:57:50 +00:00
2024-05-23 09:05:54 +00:00
*)
2024-05-23 16:39:57 +00:00
echo -e "$help"
2024-05-23 09:05:54 +00:00
exit 1
;;
esac