#!/bin/bash

OUT="/opt/hx-ki/inventory/hxki_inventory_$(hostname)_$(date +%Y%m%d-%H%M%S).txt"

echo "========================================" | tee "$OUT"
echo "      HX-KI SERVER INVENTORY V3" | tee -a "$OUT"
echo "========================================" | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> HOST SYSTEM" | tee -a "$OUT"
hostnamectl | tee -a "$OUT"
echo "" | tee -a "$OUT"
echo "Kernel: $(uname -r)" | tee -a "$OUT"
echo "Uptime: $(uptime -p)" | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> CPU / RAM" | tee -a "$OUT"
lscpu | tee -a "$OUT"
free -h | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> DISK STATUS" | tee -a "$OUT"
df -h / | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> NETWORKING" | tee -a "$OUT"
ip a | tee -a "$OUT"
ss -tulpen | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> DOCKER INFO" | tee -a "$OUT"
docker info | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> DOCKER CONTAINERS" | tee -a "$OUT"
docker ps -a | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> DOCKER NETWORKS" | tee -a "$OUT"
docker network ls | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> CONTAINERS BY NETWORK" | tee -a "$OUT"
for net in $(docker network ls --format "{{.Name}}"); do
  echo "" | tee -a "$OUT"
  echo "Network: $net" | tee -a "$OUT"
  docker network inspect "$net" --format '{{json .Containers}}' | jq . | tee -a "$OUT"
done

echo "" | tee -a "$OUT"
echo ">>> SYSTEMD SERVICES (TOP ACTIVE)" | tee -a "$OUT"
systemctl list-units --type=service --state=running | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> NATS STATUS" | tee -a "$OUT"
docker ps | grep nats | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> NODE EXPORTER STATUS" | tee -a "$OUT"
docker ps | grep exporter | tee -a "$OUT"

echo "" | tee -a "$OUT"
echo ">>> DONE" | tee -a "$OUT"
