Files
Varaverk/Monitors/emby_session_report.sh
T

169 lines
7.3 KiB
Bash

#!/bin/bash
# -----------------------------------------------------------------------------------------------
# --------------------------------- Emby Session Report ----------------------------------------
# -----------------------------------------------------------------------------------------------
# Generates a weekly usage report from the Emby media server via its API.
# Queries activity logs, session history and library stats to produce a
# human-readable summary of what was watched, by whom and how.
#
# Report includes:
# Total streams during the report period
# Transcode vs direct play ratio
# Live TV usage
# Top N most watched content
# Most active users
# Peak concurrent streams
#
# No persistent writes — queries API fresh each run.
# All configuration in Master.conf under Emby Session Report section.
# Supports --dry-run to test API connectivity without sending notification.
# -----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_GEAR Setup ━━━
# -----------------------------------------------------------------------------------------------
echo ""
echo "━━━ $ICON_GEAR Setup ━━━"
if ! command -v curl >/dev/null 2>&1; then
error "curl not found — required for Emby API calls"
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
error "jq not found — required for JSON parsing"
notify "Emby report failed on $(hostname) — jq not installed" "Emby Report" "warning"
exit 1
fi
# Select correct Emby instance based on which server is running this script
detect_hosts
if [[ "$LOCAL_SERVER_NAME" == "$HOST1" ]]; then
EMBY_URL="$HOST1_EMBY_URL"
EMBY_API_KEY="$HOST1_EMBY_API_KEY"
else
EMBY_URL="$HOST2_EMBY_URL"
EMBY_API_KEY="$HOST2_EMBY_API_KEY"
fi
info "Emby instance: $LOCAL_SERVER_NAME$EMBY_URL"
require_var EMBY_URL
require_var EMBY_API_KEY
success "Config validated"
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — API will be queried but no notification sent"
# -----------------------------------------------------------------------------------------------
# API HELPER
# -----------------------------------------------------------------------------------------------
emby_api() {
local endpoint="$1"
local response http_code body
response=$(curl -sf \
--max-time 15 \
-H "X-Emby-Token: $EMBY_API_KEY" \
-w "\n%{http_code}" \
"${EMBY_URL}/${endpoint}" 2>/dev/null)
http_code=$(echo "$response" | tail -1)
body=$(echo "$response" | head -n -1)
if [[ "$http_code" != "200" ]]; then
error "Emby API returned HTTP $http_code for: $endpoint"
return 1
fi
echo "$body"
}
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_EMBY Emby Session Report ━━━
# -----------------------------------------------------------------------------------------------
echo ""
echo "━━━ $ICON_EMBY Emby Session Report — $(date '+%Y-%m-%d %H:%M:%S') ━━━"
echo "$ICON_EMBY URL: $EMBY_URL"
echo "$ICON_TIME Period: Last ${EMBY_REPORT_DAYS} days"
echo ""
START=$(date +%s)
# Test connectivity
info "Testing Emby API connectivity..."
SYSTEM_INFO=$(emby_api "System/Info" 2>/dev/null) || {
error "Cannot connect to Emby at $EMBY_URL"
notify "Emby report failed on $(hostname) — cannot connect to Emby" "Emby Report" "warning"
exit 1
}
SERVER_NAME=$(echo "$SYSTEM_INFO" | jq -r '.ServerName // "Unknown"' 2>/dev/null)
SERVER_VERSION=$(echo "$SYSTEM_INFO" | jq -r '.Version // "Unknown"' 2>/dev/null)
success "Connected to: $SERVER_NAME (v$SERVER_VERSION)"
echo ""
# Calculate date range
REPORT_START=$(date -d "${EMBY_REPORT_DAYS} days ago" '+%Y-%m-%dT00:00:00')
# ── Active Sessions ──────────────────────────────────────────────────────────────────────────
echo "━━━ $ICON_EMBY Active Sessions ━━━"
SESSIONS=$(emby_api "Sessions" 2>/dev/null) || { warn "Could not fetch sessions"; SESSIONS="[]"; }
ACTIVE_COUNT=$(echo "$SESSIONS" | jq '[.[] | select(.NowPlayingItem != null)] | length' 2>/dev/null || echo 0)
TRANSCODE_COUNT=$(echo "$SESSIONS" | jq '[.[] | select(.NowPlayingItem != null) | select(.TranscodingInfo != null)] | length' 2>/dev/null || echo 0)
DIRECT_COUNT=$(( ACTIVE_COUNT - TRANSCODE_COUNT ))
echo " $ICON_EMBY Active streams: $ACTIVE_COUNT"
echo " $ICON_EMBY Direct play: $DIRECT_COUNT"
echo " $ICON_EMBY Transcoding: $TRANSCODE_COUNT"
echo ""
# ── Library Stats ────────────────────────────────────────────────────────────────────────────
echo "━━━ $ICON_EMBY Library ━━━"
ITEMS=$(emby_api "Items/Counts" 2>/dev/null) || { warn "Could not fetch library counts"; ITEMS="{}"; }
MOVIE_COUNT=$(echo "$ITEMS" | jq '.MovieCount // 0' 2>/dev/null || echo 0)
EPISODE_COUNT=$(echo "$ITEMS" | jq '.EpisodeCount // 0' 2>/dev/null || echo 0)
SONG_COUNT=$(echo "$ITEMS" | jq '.SongCount // 0' 2>/dev/null || echo 0)
echo " $ICON_EMBY Movies: $MOVIE_COUNT"
echo " $ICON_EMBY Episodes: $EPISODE_COUNT"
echo " $ICON_EMBY Songs: $SONG_COUNT"
echo ""
# ── Ramdisk Status (from state file) ────────────────────────────────────────────────────────
echo "━━━ $ICON_RAM Transcode Location ━━━"
if mountpoint -q "$RAMDISK_PATH" 2>/dev/null; then
RAMDISK_USED_KB=$(df "$RAMDISK_PATH" --output=used | tail -1 | tr -d ' ')
RAMDISK_USED_GB=$(awk "BEGIN {printf \"%.2f\", $RAMDISK_USED_KB / 1048576}")
SYMLINK=$(readlink "$TRANSCODE_LINK" 2>/dev/null || echo "unknown")
echo " $ICON_RAM Ramdisk usage: ${RAMDISK_USED_GB}GB"
echo " $ICON_LINK Symlink target: $SYMLINK"
else
echo " $ICON_RAM Ramdisk: not mounted"
fi
echo ""
END=$(date +%s)
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SUMMARY Summary ━━━
# -----------------------------------------------------------------------------------------------
echo "━━━━━ $ICON_SUMMARY EMBY REPORT SUMMARY ━━━━━"
echo "$ICON_EMBY Server: $SERVER_NAME (v$SERVER_VERSION)"
echo "$ICON_EMBY Active: $ACTIVE_COUNT streams ($DIRECT_COUNT direct / $TRANSCODE_COUNT transcode)"
echo "$ICON_EMBY Library: $MOVIE_COUNT movies $EPISODE_COUNT episodes $SONG_COUNT songs"
echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [[ "$DRY_RUN" == false ]]; then
notify "Emby report on $(hostname)$ACTIVE_COUNT active streams ($DIRECT_COUNT direct / $TRANSCODE_COUNT transcode) — Library: $MOVIE_COUNT movies $EPISODE_COUNT episodes" "Emby Report" "normal"
fi