#!/bin/bash
# This script is used to gather information about the HW and SW of the system, in addition to information about the Nx AI Manager.
# The information is stored in a directory located in ~/nxai_troubleshooting and then compressed into a file named ~/nxai_troubleshooting.tgz.
# To get support, please attach the compressed file to your support request.
# NOTE: No sensitive information is collected, only the basic system information and the Nx AI Manager information.
# Enable debug mode
# set -x

# Create directory where the information will be stored
current_dir=$(pwd)
info_dir=~/nxai_troubleshooting
rm -rf $info_dir >/dev/null 2>&1
rm -rf $info_dir.tgz >/dev/null 2>&1
mkdir -p $info_dir

# Redirect all output and error streams to a log file
log_file="$info_dir/nxai_troubleshooting.log"
exec > >(tee -a "$log_file") 2>&1

############################### Basic System Information
lsb_release -a >$info_dir/lsb_release.txt
uname -a >$info_dir/uname.txt
lscpu >$info_dir/lscpu.txt
lspci >$info_dir/lspci.txt
df -h >$info_dir/df.txt
ldd --version >$info_dir/ldd_version.txt

############################### Check if the mediaserver is installed
plugins_dir=""
install_dir=""
if [ -d /opt/networkoptix-metavms/mediaserver/bin/plugins/ ]; then
    install_dir="/opt/networkoptix-metavms/mediaserver"
    plugins_dir="$install_dir/bin/plugins/"
elif [ -d /opt/networkoptix/mediaserver/bin/plugins/ ]; then
    install_dir="/opt/networkoptix/mediaserver"
    plugins_dir="$install_dir/bin/plugins/"
    echo "This is not a Meta installation"
else
    echo "Mediaserver is not installed."
fi

# get Mediaserver installed version
cat $install_dir/build_info.txt >$info_dir/mediaserver_info.txt

############################### Check if AI Plugin is installed
if [ -f $plugins_dir/nx_ai_manager_plugin/libnx_ai_manager_plugin.so ]; then
    echo "AI Plugin is installed."
else
    echo "AI Plugin is not installed."
fi
libnxai_plugin_dir=$plugins_dir/nx_ai_manager_plugin/

# Check file permissions
ls -lR $libnxai_plugin_dir/ >"$info_dir/nxai_plugin_permissions.txt"

# Check if tree is installed
if command -v tree >/dev/null 2>&1; then
    echo "Using tree to list files in the AI Plugin directory"
    tree -h --du "$libnxai_plugin_dir/" >"$info_dir/nxai_plugin_tree.txt"
else
    echo "Using du to list file sizes in the AI Plugin directory"
    du -ah "$libnxai_plugin_dir/" >"$info_dir/nxai_plugin_du.txt"
fi

# Set AI manager root directory
nxai_manager_dir=$install_dir/var/nx_ai_manager/nxai_manager/

############################### Check if AI Manager is installed
if [ -d $nxai_manager_dir/bin ]; then
    echo "AI Manager is installed in $nxai_manager_dir"
else
    echo "AI Manager is not installed."
fi

bin_dir=$nxai_manager_dir/bin/

# Gather all log files in the AI Manager directory and its parent dir (nxai_plugin logs are written there)
mkdir -p $info_dir/log
echo "Collecting log files from $nxai_manager_dir"
find "$nxai_manager_dir" "$(dirname "$nxai_manager_dir")" -name "*.log*" -exec cp {} "$info_dir/log" \;

############################### Check AI Manager configuration
if [ -f $bin_dir/installed_runtime.txt ]; then
    echo "Runtime might be installed."
else
    echo "Runtime not installed."
fi

# Get the installed runtime information
cp $bin_dir/installed_runtime.txt $info_dir/installed_runtime.txt

# Get all settings files
mkdir -p $info_dir/json
echo "Collecting json files from $nxai_manager_dir"
find $nxai_manager_dir -name "*.json" -exec cp {} $info_dir/json \;

############################### Check if AI Manager is running
# get running processes
ps aux | grep nxai >>$info_dir/nxai_manager_ps_aux.txt

############################### Check connectivity to the Nx AI Cloud
# Check if curl or wget is available
echo "Downloading test files from the Nx AI Cloud to measure download speed..."
if command -v curl >/dev/null 2>&1; then
    echo "Using curl"
    # check if Nx AI Cloud is reachable
    curl -s https://api.sclbl.nxvms.com/dev/ >$info_dir/nxai_cloud_connectivity.txt
    # Download a file from the Nx AI Cloud to measure the download speed
    curl -s -m 20 "https://cdn.sclbl.nxvms.com/benchmark.bin?size=10" -o /dev/null -w "%{speed_download}" |
        awk '{print "Model download speed: " $1/1048576 " MB/sec"}' \
            >$info_dir/nxai_cloud_download_speed_of_model.txt
    curl -s -m 20 "https://artifactory.nxvms.dev/artifactory/nxai_open/files/23MB.bin" -o /dev/null -w "%{speed_download}" |
        awk '{print "Runtime download speed: " $1/1048576 " MB/sec"}' \
            >$info_dir/nxai_cloud_download_speed_of_runtime.txt
elif command -v wget >/dev/null 2>&1; then
    echo "Using wget"
    wget -q -O "$info_dir/nxai_cloud_connectivity.txt" https://api.sclbl.nxvms.com/dev/
    wget --timeout=20 "https://cdn.sclbl.nxvms.com/benchmark.bin?size=10" -O /dev/null >$info_dir/nxai_cloud_download_speed_of_model.txt 2>&1
    wget --timeout=20 "https://artifactory.nxvms.dev/artifactory/nxai_open/files/23MB.bin" -O /dev/null >$info_dir/nxai_cloud_download_speed_of_runtime.txt 2>&1
else
    echo "ERROR: Neither curl nor wget is installed."
fi
# Get the latency to the Nx AI Cloud
ping -c 10 api.sclbl.nxvms.com >$info_dir/nxai_cloud_ping.txt

# Get information about DEEPX if available
# Checking if dxrt-cli is installed
if command -v dxrt-cli >/dev/null 2>&1; then
    echo "dxrt-cli is installed."
    dxrt-cli -s >$info_dir/dxrt_cli_version.txt 2>&1
else
    echo "dxrt-cli is not installed."
fi

# Get information about Nvidia if available
# Checking if nvidia-smi is installed
if command -v nvidia-smi >/dev/null 2>&1; then
    echo "nvidia-smi is installed."
    nvidia-smi >$info_dir/nvidia_smi.txt 2>&1
else
    echo "nvidia-smi is not installed."
fi

############################### tar compress the information
cd $info_dir/..
tar -cvf $info_dir.tgz "$(basename $info_dir)" >/dev/null || echo "ERROR: Failed to compress the information."
rm -rf $info_dir >/dev/null 2>&1

echo "System information gathering complete."
echo "The collected information is stored in $info_dir.tgz"
echo "Please attach this archive to your support request."
cd "$current_dir"
