bashcurlcheck-mk

insert hosts in check via for loop


following problem:

we have a file called "file.conf"

192.168.30.1|192.168.30.1|os
_gateway|192.168.30.2|Linux 2.6.18 - 2.6.22
...

first is the hostname second is the ipv4

now we have a script where he should automaticaly insert the hosts and ip's via automated user from checkMK

#!/bin/bash

FILE=filename
source $FILE

for i in ${FILE}
do

HOSTNAME=$(cat $i | cut -d '|' -f1)
IP=$(cat $i | cut -d '|' -f2)

curl "http://checkmkadress/check_mk/host&user" -d 'request={"hostname":"'"$HOSTNAME"'","folder":"ansible","attributes":{"ipaddress":"'"$IP"'","site":"sitename","tag_agent":"cmk-agent"}}'


done


but if we do it like that we get the following error cause he try's to put in every host in host and every ip in ip without going through all lines

{"result": "Check_MK exception: Failed to parse JSON request: '{\"hostname\":\"allhostnames":{\"ipaddress\":all_ips\",\"site\":\"sitename\",\"tag_agent\":\"cmk-agent\"}}': Invalid control character at: line 1 column 26 (char 25)", "result_code": 1}

how can we make the curl script go through each line to get host and ip individually


Solution

  • Using what you already done, try in this way.

    FILE="filename"
    source $FILE
    
    while read line
    do
    
        HOSTNAME=$(echo $line | cut -d '|' -f1)
        IP=$(echo $line | cut -d '|' -f2)
    
        #your curl command here
    
    done <$FILE 
    

    OR, which I prefer

    while IFS='|' read -r host ip description
    do 
       #your curl command here
       echo "$host : $ip : $description"
    done <$FILE