【问题标题】:Check_MK syntax to to make python add new hostsCheck_MK 语法使 python 添加新主机
【发布时间】:2017-02-06 05:05:13
【问题描述】:

我正在使用 Check_MK,我正在尝试实现一个 python 脚本以通过 cfengine 运行以自动添加新安装的主机。 我在使用 pycurl 或运行和外部 curl 时遇到了一些问题。

我希望能够 pycurl 的命令示例是:

curl "http://10.20.30.40/mysite/check_mk/webapi.py?action=add_host&_username=autouser&_secret=mysecret" -d 'request={"attributes":{"alias": "winxp_1 的别名", "tag_agent": "cmk-agent", "tag_criticality": "prod", "ipaddress ": "127.0.0.1"}, "主机名": "winxp_1", "文件夹": "os/windows"}'

这在终端上可以正常工作

但我找不到正确的语法来使它在 python 脚本中工作。

感谢您的帮助。

【问题讨论】:

    标签: python api check-mk


    【解决方案1】:

    这对我有用。

    注意:用户必须存在。 “文件夹”必须存在;我在示例中输入了“/”。

    import urllib2
    
    req = urllib2.Request("http://localhost/mysite/check_mk/webapi.py?action=add_host&_username=autouser&_secret=mysecret",
                 headers = {"Content-Type": "application/x-www-form-urlencoded"},
                 data = 'request={"attributes":{"alias": "Alias of winxp_1", "tag_agent": "cmk-agent", "tag_criticality": "prod", "ipaddress": "127.0.0.1"}, "hostname": "winxp_1", "folder": "/"}')
    
    f = urllib2.urlopen(req)
    

    对不起我的英语。

    【讨论】:

      【解决方案2】:

      和 Daniel 一样,但是有变量:

      url = "/mysite/check_mk/webapi.py"
      
      request_url = "%s%s?action=add_host&_username=%s&_secret=%s" % ( check_mk_host, url, check_mk_username, check_mk_password )
      
      request_data = {}
      
      request_data['attributes'] = {}
      
      request_data['attributes']['tag_agent'] = 'cmk-agent'
      
      request_data['hostname'] = vm
      
      request_data['folder'] = "/Auto"
      
      request_data = json.dumps(request_data)
      
      data = "request=%s" % request_data
      
      br = mechanize.Browser()
      
      br.open(mechanize.Request(request_url, data=data))
      

      【讨论】:

      • 即使它很有趣,也绝对不能回答所提出的问题。请考虑改为评论该问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2013-02-02
      相关资源
      最近更新 更多