Reputation: 820
I'm working on a small python script that should return a list of data from an icinga server - I have it working with curl, but I need it in Python. This is the curl version:
$ curl -k -s -u karl:marx -H 'Accept: application/json' -H 'X-HTTP-Method-Override: GET' -X POST 'https://zenoss.hpc.ic.ac.uk:5665/v1/objects/services' -d '{ "filter": "service.state==state && match(pattern,service.name)", "filter_vars": { "state": 2, "pattern": "*checkmem" } }' | jq '.results[].attrs | [.host_name, .name, .last_check_result.output] | @csv' | sed -e 's/.cx1.hpc.ic.ac.uk//g' -e 's/\\n/\n/g' -e 's/\"\\\"//g' -e 's/\\\",\\\"/\n/g' -e 'a ------'
The python script that I believe implements this, is:
$ cat icinga_report
#!/usr/bin/python
import requests
import json
import sys
if len(sys.argv)==3:
state=int(sys.argv[1])
service=sys.argv[2]
else:
print 'Usage: icinga_report state service'
sys.exit()
hdr={
'Accept':'application/json',
'X-HTTP-Method-Override':'GET'
}
aut=(
'karl',
'marx'
)
url='https://zenoss.hpc.imperial.ac.uk:5665/v1/objects/services'
dat={
'filter':'service.state==state && match(pattern,service.name)',
'filter_vars':{
'state':int('%d'%state),
'pattern':'*%s'%service
}
}
res=requests.post(
url,
headers=hdr,
auth=aut,
data=json.dumps(dat)
)
print res
However, the output from running the script:
$ ./icinga_report 2 checkmem
<Response [200]>
And in fact, it looks like it is the same if I use any other, valid URL - I just get the response code. What am I doing wrong?
Upvotes: 1
Views: 34
Reputation: 106435
requests.post
returns a requests.Response
object. You should output its content
attribute instead if you want to see the content of response:
print res.content
Upvotes: 2