【问题标题】:check-mk check if hostname and/or ip already existcheck-mk 检查主机名和/或 ip 是否已经存在
【发布时间】:2020-07-12 06:59:07
【问题描述】:

我们有一个脚本,通过 curl 在 check_mk 中插入新主机

#!/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 

文件“file.conf”是使用 xmlstarlet 进行 nmap 扫描的已处理文件,该文件并不总是有主机名,因此使用 ip 地址作为主机名

file.conf 看起来像这样

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

所以对于某些主机,ip 地址作为主机名传递一次,逻辑上作为 ip 传递。现在是这样,员工输入正确的主机名并从字段主机名中删除 ip

如果再次执行上面提到的脚本,它将再次使用 ip 作为主机名创建主机(因为没有指定主机名),所以现在我们在 check_mk 1x 中使用手动添加的主机名来创建主机 2x 和一次使用ip 作为主机名

所有其他主机名从一开始就被 nmap 正确识别的主机没有被接管

我们现在需要一个函数,在脚本执行之前询问主机名的 IP 地址,如果它已经存在,则不应再次创建主机

使用 curl 只能获取主机

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

【问题讨论】:

  • 不幸的是,我没有任何 check_mk 可供检查,但在他们的官方网站上,您还可以找到“get_host”而不是“get_all_host”。然后您将能够获得有关特定主机(包括 IP)的更多信息。在这里查看checkmk.com/cms_web_api_references.html 如果您输入一个示例的问题输出,那么我们将知道下一步该做什么。
  • 如果我这样做: curl "myserver/mysite/check_mk/…" -d 'request={"hostname":"192.168.30.1"}' 我明白了:{"result": {"attributes “:{“tag_agent”:“cmk-agent”,“ipaddress”:“192.168.30.1”,“site”:“mysite”,“meta_data”:{“created_at”:1585659776.593514,“created_by”:“自动化”} },“主机名”:“192.168.30.1”,“路径”:“ansible”},“result_code”:0}
  • 但是你能把这部分 "request={"hostname":"192.168.30.1"}" 改成 "request={"ipaddress":"192.168.30.1"}" 并显示结果吗?
  • 是的,但是像这样他只能搜索以 ip 地址作为主机名的“主机名”,因为 curl 命令是 action:get_host,而你无法将其更改为 get_ip
  • 知道了,我会用我们现有的为你准备一些例子

标签: bash curl check-mk


【解决方案1】:

现在我们有另一个问题:我必须直接包含操作系统。到目前为止,这已经适用于相同的 add_host,我只需要包含另一个属性

 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"}}'

但是你必须手动将操作系统名称插入到 checkmk 界面中,有一个特殊的选项卡可以在其中定义它们

这已经在 Linux、Windows 和 LCOS 操作系统上完成

但是现在nmap扫描没有到处都包含一个操作系统,所以file.conf时不时看起来像这样:

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|

您可以看到主机上的操作系统完全丢失,或者有类似 android 的东西

我们现在希望没有 linux、windows 或 lcos 的主机具有“空”作为 tag_os

但是 curl 命令会为具有空操作系统的主机提供错误并且不会创建它们

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

【讨论】:

    【解决方案2】:

    首先你需要“jq”,所以 apt-get install jq。 (用于通过 bash 读取 json 文件)

    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
    

    【讨论】:

    • 听起来不错,但我收到此错误:jq: error (at :0): Cannot index string with string "attributes"
    • 但我也得到了这个回声:“主机名:在 check_mk 中已经存在”对于每个主机,问题是我已经删除了一个主机,但它没有再次设置,它应该按照到脚本
    • 关于您的第一个问题是因为当他在 check_mk 中没有找到这样的主机名时,json 看起来可能不同,为了防止出现此消息,您可以将 "jq -r '.result.attributes.ipaddress'" 更改为“jq -r '.result.attributes.ipaddress' 2>/dev/null”。我已经在我的示例中更改了这一点。
    • 但是我不明白你的第二个问题。你能提供更多细节吗?在您的问题中添加一些新的输出。如果脚本说“已经存在”,则意味着 curl 找到名称为 $ip 的主机名(因此来自您的文件名的列 ip )
    • 我已经在 checkmk 界面中用 ip 30.1 重命名了主机的主机名,但脚本仍然使用 ip 作为主机名和 ip 创建主机:C 这是我执行后得到的第一行脚本:{"result": null, "result_code": 0}Hostname: 192.168.30.1 添加到 check_mk
    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 2020-06-14
    • 2012-05-13
    • 1970-01-01
    相关资源
    最近更新 更多