From ab156440b35b239acce127f7b2c28e79eef5c78a Mon Sep 17 00:00:00 2001 From: juanlf Date: Wed, 30 Jul 2025 09:15:51 +0000 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20get=5Fhg.bash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- get_hg.bash | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 get_hg.bash diff --git a/get_hg.bash b/get_hg.bash new file mode 100644 index 0000000..002f950 --- /dev/null +++ b/get_hg.bash @@ -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 </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"