161 lines
4.8 KiB
Bash
161 lines
4.8 KiB
Bash
#!/bin/bash
|
|
|
|
# Default Konfiguration
|
|
SSH_USER="root"
|
|
INTERNAL_DOMAIN=""
|
|
USE_LOCAL=false
|
|
USE_INTERNAL=false
|
|
OUTPUT_FORMAT="cli"
|
|
NODES=()
|
|
|
|
# Argumente parsen
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
--local) USE_LOCAL=true ;;
|
|
--internal) USE_INTERNAL=true ;;
|
|
--nodes) shift; IFS=',' read -r -a NODES <<< "$1" ;;
|
|
--output) shift; OUTPUT_FORMAT="$1" ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# Hilfsfunktionen
|
|
run_cmd() {
|
|
local node="$1"
|
|
local cmd="$2"
|
|
if $USE_LOCAL; then
|
|
bash -c "$cmd"
|
|
else
|
|
ssh "${SSH_USER}@${node}" "$cmd"
|
|
fi
|
|
}
|
|
|
|
resolve_dns() {
|
|
local hostname="$1"
|
|
getent hosts "$hostname" | awk '{print $1}'
|
|
}
|
|
|
|
format_bytes() {
|
|
local bytes="$1"
|
|
echo "$((bytes / 1024))M"
|
|
}
|
|
|
|
parse_storage() {
|
|
local config="$1"
|
|
echo "$config" | grep -E '(^scsi|^virtio|^rootfs|^mp[0-9]+)' | \
|
|
grep -v none | grep -oP 'size=\K[0-9]+[A-Z]+' | paste -sd "+" -
|
|
}
|
|
|
|
# Daten erfassen
|
|
declare -a DATA_ROWS
|
|
|
|
gather_node_data() {
|
|
local node="$1"
|
|
local hostname fqdn ip cpu ram
|
|
hostname=$(run_cmd "$node" "hostname")
|
|
fqdn="${hostname}.${INTERNAL_DOMAIN}"
|
|
ip=$(resolve_dns "$fqdn")
|
|
cpu=$(run_cmd "$node" "nproc")
|
|
ram=$(run_cmd "$node" "grep MemTotal /proc/meminfo | awk '{print int(\$2 / 1024)}'")
|
|
DATA_ROWS+=("$hostname|$fqdn|$ip|Node|$cpu|${ram}M|-")
|
|
}
|
|
|
|
gather_vm_data() {
|
|
local node="$1"
|
|
local vms
|
|
vms=$(run_cmd "$node" "qm list | awk 'NR>1 {print \$1}'")
|
|
for vm in $vms; do
|
|
local config name ip cpu ram storage
|
|
config=$(run_cmd "$node" "qm config $vm")
|
|
name=$(echo "$config" | grep "^name:" | cut -d' ' -f2-)
|
|
ip=$(echo "$config" | grep -oP "ip=\K[^,]+" | head -n1)
|
|
cpu=$(echo "$config" | grep "^cores:" | awk '{print $2}')
|
|
ram=$(echo "$config" | grep "^memory:" | awk '{print $2"M"}')
|
|
storage=$(parse_storage "$config")
|
|
DATA_ROWS+=("$vm|$name|${ip:-"-"}|VM|${cpu:-"-"}|${ram:-"-"}|$storage")
|
|
done
|
|
}
|
|
|
|
gather_lxc_data() {
|
|
local node="$1"
|
|
local lxcs
|
|
lxcs=$(run_cmd "$node" "pct list | awk 'NR>1 {print \$1}'")
|
|
for ct in $lxcs; do
|
|
local config name ip cpu ram storage
|
|
config=$(run_cmd "$node" "pct config $ct")
|
|
name=$(echo "$config" | grep "^hostname:" | cut -d' ' -f2-)
|
|
ip=$(echo "$config" | grep -oP "ip=\K[^,]+" | head -n1)
|
|
cpu=$(echo "$config" | grep "^cores:" | awk '{print $2}')
|
|
ram=$(echo "$config" | grep "^memory:" | awk '{print $2"M"}')
|
|
storage=$(parse_storage "$config")
|
|
DATA_ROWS+=("$ct|$name|${ip:-"-"}|CT|${cpu:-"-"}|${ram:-"-"}|$storage")
|
|
done
|
|
}
|
|
|
|
# Nodes abfragen
|
|
if $USE_LOCAL; then
|
|
gather_node_data "localhost"
|
|
gather_vm_data "localhost"
|
|
gather_lxc_data "localhost"
|
|
else
|
|
for n in "${NODES[@]}"; do
|
|
gather_node_data "$n"
|
|
gather_vm_data "$n"
|
|
gather_lxc_data "$n"
|
|
done
|
|
fi
|
|
|
|
# Dynamische Spaltenberechnung
|
|
calculate_widths() {
|
|
max_id=2; max_host=8; max_ip=10; max_typ=5; max_cpu=3; max_ram=3; max_storage=7
|
|
for row in "${DATA_ROWS[@]}"; do
|
|
IFS='|' read -r id hn ip typ cpu ram sto <<< "$row"
|
|
[[ ${#id} -gt $max_id ]] && max_id=${#id}
|
|
[[ ${#hn} -gt $max_host ]] && max_host=${#hn}
|
|
[[ ${#ip} -gt $max_ip ]] && max_ip=${#ip}
|
|
[[ ${#typ} -gt $max_typ ]] && max_typ=${#typ}
|
|
[[ ${#cpu} -gt $max_cpu ]] && max_cpu=${#cpu}
|
|
[[ ${#ram} -gt $max_ram ]] && max_ram=${#ram}
|
|
[[ ${#sto} -gt $max_storage ]] && max_storage=${#sto}
|
|
done
|
|
}
|
|
|
|
print_cli_output() {
|
|
calculate_widths
|
|
printf "%-${max_id}s %-${max_host}s %-${max_ip}s %-${max_typ}s %${max_cpu}s %${max_ram}s %-${max_storage}s\n" \
|
|
"ID" "Hostname" "IP" "Typ" "CPU" "RAM" "Storage"
|
|
printf -- "%0.s-" $(seq 1 $((max_id + max_host + max_ip + max_typ + max_cpu + max_ram + max_storage + 18)))
|
|
echo
|
|
for row in "${DATA_ROWS[@]}"; do
|
|
IFS='|' read -r id hn ip typ cpu ram sto <<< "$row"
|
|
printf "%-${max_id}s %-${max_host}s %-${max_ip}s %-${max_typ}s %${max_cpu}s %${max_ram}s %-${max_storage}s\n" \
|
|
"$id" "$hn" "$ip" "$typ" "$cpu" "$ram" "$sto"
|
|
done
|
|
}
|
|
|
|
print_csv_output() {
|
|
echo "ID,Hostname,IP,Typ,CPU,RAM,Storage"
|
|
for row in "${DATA_ROWS[@]}"; do
|
|
echo "$row" | tr '|' ','
|
|
done
|
|
}
|
|
|
|
print_json_output() {
|
|
echo "["
|
|
for i in "${!DATA_ROWS[@]}"; do
|
|
IFS='|' read -r id hn ip typ cpu ram sto <<< "${DATA_ROWS[i]}"
|
|
printf ' {"id": "%s", "hostname": "%s", "ip": "%s", "type": "%s", "cpu": "%s", "ram": "%s", "storage": "%s"}' \
|
|
"$id" "$hn" "$ip" "$typ" "$cpu" "$ram" "$sto"
|
|
[[ $i -lt $((${#DATA_ROWS[@]} - 1)) ]] && echo "," || echo
|
|
done
|
|
echo "]"
|
|
}
|
|
|
|
# Ausgabe
|
|
case "$OUTPUT_FORMAT" in
|
|
cli) print_cli_output ;;
|
|
csv) print_csv_output ;;
|
|
json) print_json_output ;;
|
|
*) echo "Unbekanntes Ausgabeformat: $OUTPUT_FORMAT" ;;
|
|
esac
|