LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-26-2016, 09:04 PM   #1
624867243@qq.com
Member
 
Registered: Nov 2015
Location: ShenZhen
Posts: 33
Blog Entries: 1

Rep: Reputation: Disabled
Seek a python script through zabbix api to list hosts?


hello everyone!I have met a question.I want to edit a script through zabbix api to list all the host?
Seek a python script through zabbix api to list host?

Last edited by 624867243@qq.com; 02-26-2016 at 09:06 PM.
 
Old 02-27-2016, 06:03 AM   #2
624867243@qq.com
Member
 
Registered: Nov 2015
Location: ShenZhen
Posts: 33

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
no one know that?
 
Old 02-27-2016, 09:21 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by 624867243@qq.com View Post
hello everyone!I have met a question.I want to edit a script through zabbix api to list all the host?
Seek a python script through zabbix api to list host?
...and...
Quote:
Originally Posted by 624867243@qq.com
no one know that?
Yes, I'm sure someone knows...there are many people who write python scripts. We will be happy to help you with a script or error(s), so post what you've written/tried/done on your own, and we can help. But we will NOT write things for you. Read the "Question Guidelines" link in my posting signature.
 
Old 02-27-2016, 10:01 AM   #4
624867243@qq.com
Member
 
Registered: Nov 2015
Location: ShenZhen
Posts: 33

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
below in my code

Quote:
Originally Posted by TB0ne View Post
...and...

Yes, I'm sure someone knows...there are many people who write python scripts. We will be happy to help you with a script or error(s), so post what you've written/tried/done on your own, and we can help. But we will NOT write things for you. Read the "Question Guidelines" link in my posting signature.
thank you for your remind,I have solved it .
Code:
#coding:utf-8
import json
import urllib2

url = "http://zabbix.knight.ren/api_jsonrpc.php"
header = {"Content-Type": "application/json"}
# request json
data = json.dumps(
{
    "jsonrpc":"2.0",
    "method":"host.get",
    "params":{
        "output":["hostid","name"],
        "filter":{"host":""}
    },
    "auth":"8c67fd696eb3a6a877569b9bc34d6c22",
    "id":1,
})
# create request object
request = urllib2.Request(url,data)
for key in header:
    request.add_header(key,header[key])
# get host list
try:
    result = urllib2.urlopen(request)
except URLError as e:
    if hasattr(e, 'reason'):
        print 'We failed to reach a server.'
        print 'Reason: ', e.reason
    elif hasattr(e, 'code'):
        print 'The server could not fulfill the request.'
        print 'Error code: ', e.code
else:
    response = json.loads(result.read())
    result.close()
    print "Number Of Hosts: ", len(response['result'])
    for host in response['result']:
        print "Host ID:",host['hostid'],"Host Name:",host['name']
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Write a shell script that pings a list of hosts and reports the unreachable ones. Beverly Linux - Newbie 8 11-25-2012 07:59 PM
Python script using tar from a list returns error: has no attribute 'startswith' linux1103 Linux - Newbie 3 04-05-2011 07:17 PM
Seek script to monitor sizes of list of files and send alert if size changes Jon_Roland Linux - Security 3 03-28-2011 04:37 PM
How to add hosts into Zabbix monitoring tool rawand Linux - Software 1 06-03-2010 12:33 AM
Bash/Perl script - provide list of hosts to choose from and ssh to chosen host loadedmind Programming 4 04-07-2010 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration