Add hosts and set contact groups with REST API

Goodmorning everyone.
I would kindly need help improving my script.
At the moment it takes the information from a series of files and creates the hosts in Check_MK only if they are not already entered.
My problem is how to associate these hosts at groups.

So could someone kindly give me an example of a curl request with inserting a group?

My code without group attributes (It work well):

#!/bin/bash
#Configuro
HOST_NAME="localhost"
SITE_NAME="famarcheck"
API_URL="http://$HOST_NAME/$SITE_NAME/check_mk/api/1.0"

USERNAME="automation"
PASSWORD="UkJq9dseSc6Z32432FedtL"

ip=mk/mkip
descrizione=mk/mkde
gruppo=mk/mkgr
utente=mk/mkut
id=mk/mkid
tipologia=mk/mkti
cmk -l > mk.csv

#Rimuovo caratteri strani perchè Moscatelli è sbadato

sed -i 's/à/a/g' mk/mkde;sed -i 's/ù/u/g' mk/mkde;sed -i 's/à/a/g' mk/mkde;sed -i 's/è/e/g' mk/mkde;sed -i 's+/+ +g' mk/mkde;sed -i "s+'+ +g" mk/mkde

# Conto le righe e per ogni riga verifico se esiste già
numline=`wc -l "mk/mkip" | awk '{print $1}'`

for ((line = 1; line<=numline;line +=1)); do

ip=`awk "NR==$line {print; exit}" mk/mkip`
descrizione=`awk "NR==$line {print; exit}" mk/mkde`
gruppo=`awk "NR==$line {print; exit}" mk/mkgr`
utente=`awk "NR==$line {print; exit}" mk/mkut`
id=`awk "NR==$line {print; exit}" mk/mkid`
tipologia=`awk "NR==$line {print; exit}" mk/mkti`


grep -R "$ip" "mk.csv" && aggiungi="0" || aggiungi="1"

if [ $aggiungi = "1" ];
then

#IMPORTO GLI HOSTS DESIDERATI

desc=''$id'  -  '$ip'  -  '$descrizione'  -  GR:'$gruppo' UT:'$utente' - '$tipologia''

out=$(
curl \
    --request POST \
    --write-out "\nxxx-status_code=%{http_code}\n" \
    --header "Authorization: Bearer $USERNAME $PASSWORD" \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '{
          "attributes": {
        "alias":"'"$desc"'",
             "ipaddress":"'"$ip"'"
          },
          "folder": "~Nuovi Hosts",
          "host_name":"'"$ip"'"
        }' \
    "$API_URL/domain-types/host_config/collections/all")

resp=$( echo "${out}" | grep -v "xxx-status_code" )
code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}')

# For indentation, please install 'jq' (JSON query tool)
echo "$resp" | jq
# echo "$resp"

if [[ $code -lt 400 ]]; then
    echo "OK"
sleep 5
./activatechanges.sh
#exit 0
else
    echo "Request error"
    #exit 1
fi

#fine se
fi
#fine for
done

What I would like to do but it doesn’t work:

out=$(
curl \
    --request POST \
    --write-out "\nxxx-status_code=%{http_code}\n" \
    --header "Authorization: Bearer $USERNAME $PASSWORD" \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '{
          "attributes": {
        "alias":"'"$desc"'",
        "contactgroups": {
        "group": [
          "'"$gruppo"'"
        ]
      },
             "ipaddress":"'"$ip"'"
          },
          "folder": "~Nuovi Hosts",
          "host_name":"'"$ip"'"
        }' \

Thanks everyone in advance

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.