Añadir get_hg.bash

This commit is contained in:
2025-07-30 09:15:51 +00:00
parent 9c92bdef60
commit ab156440b3

64
get_hg.bash Normal file
View File

@ -0,0 +1,64 @@
#!/bin/bash
ZBX_URL="https://zabbix.seat.vwg/api_jsonrpc.php"
API_TOKEN="508108e67eb8534e049e105d84e75ae1ac8673ac74a0a165e440a40a8e773362"
CSV_OUT="hosts_by_group.csv"
# Cabecera del CSV
echo "group_name,host_id,host_name" > "$CSV_OUT"
# Obtener grupos
group_response=$(curl -s -k -X POST "$ZBX_URL" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d '{
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": ["groupid", "name"]
},
"auth": "'"$API_TOKEN"'",
"id": 1
}')
group_count=$(echo "$group_response" | jq '.result | length')
echo "⏳ Obteniendo grupos de hosts..."
echo "✅ Se encontraron $group_count grupos. Procesando..."
for row in $(echo "$group_response" | jq -c '.result[]'); do
groupid=$(echo "$row" | jq -r '.groupid')
groupname=$(echo "$row" | jq -r '.name' | sed 's/"/""/g')
# Construir JSON para host.get
read -r -d '' HOST_PAYLOAD <<EOF
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["hostid", "host"],
"groupids": ["$groupid"]
},
"auth": "$API_TOKEN",
"id": 2
}
EOF
host_response=$(curl -s -k -X POST "$ZBX_URL" \
-H "Content-Type: application/json" \
-d "$HOST_PAYLOAD")
# Validar JSON antes de parsear
if ! echo "$host_response" | jq empty >/dev/null 2>&1; then
echo "❌ Error: respuesta inválida al obtener hosts del grupo '$groupname'"
continue
fi
for host in $(echo "$host_response" | jq -c '.result[]'); do
hostid=$(echo "$host" | jq -r '.hostid')
hostname=$(echo "$host" | jq -r '.host' | sed 's/"/""/g')
echo "\"$groupname\",\"$hostid\",\"$hostname\"" >> "$CSV_OUT"
done
done
echo -e "\n📄 Exportación completada: $CSV_OUT"