check-mk check if hostname and/or ip already exist

1.1k Views Asked by At

we got a script that inserts new hosts in check_mk via curl

#!/bin/bash

cat file.conf | while read line
do

    HOSTNAME=$(echo $line | cut -d '|' -f1)
    IP=$(echo $line | cut -d '|' -f2)


curl "http://myserver/mysite/check_mk/webapi.py?action=add_host&_username=automation&_secret=myautomationsecret" -d 'request={"hostname":"'"$HOSTNAME"'","folder":"ansible","attributes":{"ipaddress":"'"$IP"'","site":"mysite","tag_agent":"cmk-agent"}}'
done 

the file "file.conf" is an already processed file from an nmap scan with xmlstarlet, this file does not always have the hostname, therefore the ip address was used as hostname

the file.conf looks like this

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

so for some hosts the ip address was passed once as hostname and logically as ip. now it is so that an employee enters the correct hostname and deletes the ip from the field hostname

if the above mentioned script is executed again, it will create the host again with the ip as hostname (because no hostname was specified), so now we have the host 2x in check_mk 1x with the manually added hostname and once with the ip as hostname

all other hosts where the hostname was already recognized correctly by nmap from start on were not taken over like it should be

we now need a function that asks for the ip address of the hostname before the script is executed and if it is already there the host should not be created again

with curl you can only get the hosts

curl "http://myserver/mysite/check_mk/webapi.py?action=get_all_hosts&_username=automation&_secret=myautomationsecret"  
2

There are 2 best solutions below

13
On BEST ANSWER

First you will need "jq", so apt-get install jq. ( is for reading json file by bash )

FILE="filename"
getHost="http://myserver/mysite/check_mk/webapi.py?action=get_all_host&_username=automation&_secret=myautomationsecret"

  while IFS='|' read -r host ip description
  do

    checkHost=$( curl -s "$getHost" | 
     jq -r '.result | keys[] as $k | "\(.[$k] | .attributes | select(.ipaddress=="'$ip'") | .ipaddress )"' | uniq )

    if [ "$checkHost" != "$ip" ]
    then
            # here you already know that this ip ( $ip ) not exist in check_mk
            # put your curl command with add_host action here
            echo "Hostname: $ip added to check_mk"
    else
            echo "Hostname: $ip is already exist in check_mk"

    fi


  done <$FILE
0
On

now we have another problem: i have to include the OS directly. this already works with the same add_host so far, i just had to include another attribute

 curl "http://myserver/mysite/check_mk/webapi.py?action=add_host&_username=automation&_secret=myautomationsecret" -d 'request={"hostname":"'"$HOSTNAME"'","folder":"ansible","attributes":{"ipaddress":"'"$IP"'","tag_os": "'"$OS"'","site":"mysite","tag_agent":"cmk-agent"}}'

but it is so that you have to insert OS names manually into the checkmk interface, there is a special tab where you can define them

this was already done with the OS Linux, Windows and LCOS

but now it is so that the nmap scan does not have an os included everywhere, so the file.conf looks like this from time to time:

host1|192.168.30.25|Windows
host2|192.168.30.90|Linux
host3|192.168.30.110|Linux
host4|192.168.30.111|Linux
192.168.30.130|192.168.30.130|
192.168.30.131|192.168.30.131|Android
192.168.30.155|192.168.30.155|Linux
192.168.30.157|192.168.30.157|

you can see that at hosts the os is completely missing or that there is something like android

we now want the hosts that do NOT have linux, windows or lcos to have "empty" as tag_os

but the curl command gives an error for hosts with empty os and does not create them

{"result": "Check_MK exception: Unknown tag ", "result_code": 1}{"result": "Check_MK exception: No such host", "result_code": 1}