0

I´m using Check_MK and i was trying to implement a python script to run via cfengine to add automatically new installed hosts. I´m having some trouble with both using pycurl or running and external curl.

An example of the command i want to be able to pycurl is:

curl "http://10.20.30.40/mysite/check_mk/webapi.py?action=add_host&_username=autouser&_secret=mysecret" -d 'request={"attributes":{"alias": "Alias of winxp_1", "tag_agent": "cmk-agent", "tag_criticality": "prod", "ipaddress": "127.0.0.1"}, "hostname": "winxp_1", "folder": "os/windows"}'

This works fine from the terminal

but I cannot find the right syntax to make it working from within the python script.

thanks for any help.

codeforester
  • 39,467
  • 16
  • 112
  • 140

2 Answers2

0

this works for me.

NOTES: The user must exist. The "folder" must exist; i put "/" in sample.

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)

Sorry for my english.

-1

The same like Daniel, but with variables:

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))
  • Even if it is interesting, it's absolutly not an answer to the question asked. Please consider commenting the question instead. – abrunet Nov 02 '15 at 15:01