python zabbix批量添加主机 脚本

释放双眼,带上耳机,听听看~!

转载:https://www.cnblogs.com/momoshouhu/p/8053907.html
1.安装xlrd读取Excel文件

1.1. 下载setuptools-38.2.4.zip,上传至zabbix服务器解压安装,下载地址:https://pypi.python.org/packages/69/56/f0f52281b5175e3d9ca8623dadbc3b684e66350ea9e0006736194b265e99/setuptools-38.2.4.zip\#md5=e8e05d4f8162c9341e1089c80f742f64

[root@localhost temp]# ll #上传文件setuptools-38.2.4.zip
total 724
-rw-r–r– 1 root root 736683 Dec 17 22:37 setuptools-38.2.4.zip

[root@localhost temp]# unzip setuptools-38.2.4.zip #解压setuptools-38.2.4.zip

[root@localhost temp]# cd setuptools-38.2.4 #进入setuptools-38.2.4目录

[root@localhost temp]# cd setuptools-38.2.4 #安装

1.2下载pip-9.0.1.tar.gz,上传至zabbix服务器解压安装,下载地址:https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz\#md5=35f01da33009719497f01a4ba69d63c9

[root@localhost temp]# ll
total 1896
-rw-r–r– 1 root root 1197370 Dec 17 22:53 pip-9.0.1.tar.gz #上传文件pip-9.0.1.tar.gz
drwxr-xr-x 9 root root 4096 Dec 17 22:55 setuptools-38.2.4
-rw-r–r– 1 root root 736683 Dec 17 22:37 setuptools-38.2.4.zip

[root@localhost temp]# tar -xzvf pip-9.0.1.tar.gz #解压pip-9.0.1.tar.gz

[root@localhost pip-9.0.1]# python setup.py build #编译
[root@localhost pip-9.0.1]# python setup.py install #安装

[root@localhost pip-9.0.1]# pip install xlrd #安装xlrd

3.编辑Excel模板

3.1主机名、显示名、IP、主机组、模板

python zabbix批量添加主机 脚本

image

3.2将Execle表zabbix_host_add.xlsx上传至zabbix服务器

[root@localhost temp]# ll
total 1964
-rw-r–r– 1 root root 46079 Dec 17 23:28 zabbix_host_add.xlsx

4.编写Python脚本,参考http://www.361way.com/zabbix-api-2/3681.html

#!/usr/bin/python

#coding:utf-8

import json

import urllib2

from urllib2

1
1` ```` `

import URLError

import sys,argparse

import xlrd

defaultencoding

1
1` ```` `

= 'utf-8'

if sys.getdefaultencoding() !

1
1` ```` `

= defaultencoding:

reload

1
1` ```` `

(sys)

sys.setdefaultencoding(defaultencoding)

class zabbix_api:

def init(

1
1` ```` `

self

1
1` ```` `

):

self

1
1` ```` `

.url

1
1` ```` `

= 'http://zabbix服务器IP地址/zabbix/api_jsonrpc.php' #修改URL

self

1
1` ```` `

.header

1
1` ```` `

= {

1
1` ```` `

"Content-Type"

1
1` ```` `

:

1
1` ```` `

"application/json"

1
1` ```` `

}

def user_login(

1
1` ```` `

self

1
1` ```` `

):

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"user.login"

1
1` ```` `

,

"params"

1
1` ```` `

: {

"user"

1
1` ```` `

:

1
1` ```` `

"Admin"

1
1` ```` `

,

1
1` ```` `

#web页面登录用户名

"password"

1
1` ```` `

:

1
1` ```` `

"zabbix" #web页面登录密码

},

"id"

1
1` ```` `

:

1
1` ```` `

0

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url, data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "\033[041m 用户认证失败,请检查 !\033[0m"

1
1` ```` `

, e.code

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

#print response['result']

self

1
1` ```` `

.authID

1
1` ```` `

= response[

1
1` ```` `

'result'

1
1` ```` `

]

return self

1
1` ```` `

.authID

def host_get(

1
1` ```` `

self

1
1` ```` `

,hostName

1
1` ```` `

=

1
1` ```` `

''):

data

1
1` ```` `

=

1
1` ```` `

json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"host.get"

1
1` ```` `

,

"params"

1
1` ```` `

: {

"output"

1
1` ```` `

:

1
1` ```` `

"extend"

1
1` ```` `

,

"filter"

1
1` ```` `

:{

1
1` ```` `

"host"

1
1` ```` `

:hostName}

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url,data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

if hasattr

1
1` ```` `

(e,

1
1` ```` `

'reason'

1
1` ```` `

):

print 'We failed to reach a server.'

print 'Reason: '

1
1` ```` `

, e.reason

elif hasattr

1
1` ```` `

(e,

1
1` ```` `

'code'

1
1` ```` `

):

print 'The server could not fulfill the request.'

print 'Error code: '

1
1` ```` `

, e.code

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

#print response

result.close()

print "主机数量: \033[31m%s\033[0m"

1
1` ```` `

%

1
1` ```` `

(

1
1` ```` `

len

1
1` ```` `

(response[

1
1` ```` `

'result'

1
1` ```` `

]))

for host

1
1` ```` `

in response[

1
1` ```` `

'result'

1
1` ```` `

]:

status

1
1` ```` `

=

1
1` ```` `

{

1
1` ```` `

"0"

1
1` ```` `

:

1
1` ```` `

"OK"

1
1` ```` `

,

1
1` ```` `

"1"

1
1` ```` `

:

1
1` ```` `

"Disabled"

1
1` ```` `

}

available

1
1` ```` `

=

1
1` ```` `

{

1
1` ```` `

"0"

1
1` ```` `

:

1
1` ```` `

"Unknown"

1
1` ```` `

,

1
1` ```` `

"1"

1
1` ```` `

:

1
1` ```` `

"available"

1
1` ```` `

,

1
1` ```` `

"2"

1
1` ```` `

:

1
1` ```` `

"Unavailable"

1
1` ```` `

}

#print host

if len

1
1` ```` `

(hostName)

1
1` ```` `

=

1
1` ```` `

=

1
1` ```` `

0

1
1` ```` `

:

print "HostID : %s\t HostName : %s\t Status :\033[32m%s\033[0m \t Available :\033[31m%s\033[0m"

1
1` ```` `

%

1
1` ```` `

(host[

1
1` ```` `

'hostid'

1
1` ```` `

],host[

1
1` ```` `

'name'

1
1` ```` `

],status[host[

1
1` ```` `

'status'

1
1` ```` `

]],available[host[

1
1` ```` `

'available'

1
1` ```` `

]])

else

1
1` ```` `

:

print "HostID : %s\t HostName : %s\t Status :\033[32m%s\033[0m \t Available :\033[31m%s\033[0m"

1
1` ```` `

%

1
1` ```` `

(host[

1
1` ```` `

'hostid'

1
1` ```` `

],host[

1
1` ```` `

'name'

1
1` ```` `

],status[host[

1
1` ```` `

'status'

1
1` ```` `

]],available[host[

1
1` ```` `

'available'

1
1` ```` `

]])

return host[

1
1` ```` `

'hostid'

1
1` ```` `

]

def hostgroup_get(

1
1` ```` `

self

1
1` ```` `

, hostgroupName

1
1` ```` `

=

1
1` ```` `

''):

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"hostgroup.get"

1
1` ```` `

,

"params"

1
1` ```` `

:{

"output"

1
1` ```` `

:

1
1` ```` `

"extend"

1
1` ```` `

,

"filter"

1
1` ```` `

: {

"name"

1
1` ```` `

: hostgroupName

}

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url,data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

#print result.read()

response

1
1` ```` `

= json.loads(result.read())

result.close()

#print response()

for group

1
1` ```` `

in response[

1
1` ```` `

'result'

1
1` ```` `

]:

if len

1
1` ```` `

(hostgroupName)

1
1` ```` `

=

1
1` ```` `

=

1
1` ```` `

0

1
1` ```` `

:

print "hostgroup: \033[31m%s\033[0m \tgroupid : %s" %

1
1` ```` `

(group[

1
1` ```` `

'name'

1
1` ```` `

],group[

1
1` ```` `

'groupid'

1
1` ```` `

])

else

1
1` ```` `

:

print "hostgroup: \033[31m%s\033[0m\tgroupid : %s" %

1
1` ```` `

(group[

1
1` ```` `

'name'

1
1` ```` `

],group[

1
1` ```` `

'groupid'

1
1` ```` `

])

self

1
1` ```` `

.hostgroupID

1
1` ```` `

= group[

1
1` ```` `

'groupid'

1
1` ```` `

]

return group[

1
1` ```` `

'groupid'

1
1` ```` `

]

def template_get(

1
1` ```` `

self

1
1` ```` `

,templateName

1
1` ```` `

=

1
1` ```` `

''):

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"template.get"

1
1` ```` `

,

"params"

1
1` ```` `

: {

"output"

1
1` ```` `

:

1
1` ```` `

"extend"

1
1` ```` `

,

"filter"

1
1` ```` `

: {

"name"

1
1` ```` `

:templateName

}

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url, data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

#print response

for template

1
1` ```` `

in response[

1
1` ```` `

'result'

1
1` ```` `

]:

if len

1
1` ```` `

(templateName)

1
1` ```` `

=

1
1` ```` `

=

1
1` ```` `

0

1
1` ```` `

:

print "template : \033[31m%s\033[0m\t id : %s" % (template[

1
1` ```` `

'name'

1
1` ```` `

], template[

1
1` ```` `

'templateid'

1
1` ```` `

])

else

1
1` ```` `

:

self

1
1` ```` `

.templateID

1
1` ```` `

= response[

1
1` ```` `

'result'

1
1` ```` `

][

1
1` ```` `

0

1
1` ```` `

][

1
1` ```` `

'templateid'

1
1` ```` `

]

print "Template Name : \033[31m%s\033[0m "

1
1` ```` `

%

1
1` ```` `

templateName

return response[

1
1` ```` `

'result'

1
1` ```` `

][

1
1` ```` `

0

1
1` ```` `

][

1
1` ```` `

'templateid'

1
1` ```` `

]

def hostgroup_create(

1
1` ```` `

self

1
1` ```` `

,hostgroupName):

if self

1
1` ```` `

.hostgroup_get(hostgroupName):

print "hostgroup \033[42m%s\033[0m is exist !"

1
1` ```` `

%

1
1` ```` `

hostgroupName

sys.exit(

1
1` ```` `

1

1
1` ```` `

)

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"hostgroup.create"

1
1` ```` `

,

"params"

1
1` ```` `

: {

"name"

1
1` ```` `

: hostgroupName

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

=

1
1` ```` `

urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url,data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

print "\033[042m 添加主机组:%s\033[0m hostgroupID : %s"

1
1` ```` `

%

1
1` ```` `

(hostgroupName,response[

1
1` ```` `

'result'

1
1` ```` `

][

1
1` ```` `

'groupids'

1
1` ```` `

])

def host_create_andy(

1
1` ```` `

self

1
1` ```` `

,hostName,visibleName, hostip, hostgroupName, templateName):

if self

1
1` ```` `

.host_get(hostip):

print "\033[041m该主机已经添加!\033[0m"

sys.exit(

1
1` ```` `

1

1
1` ```` `

)

group_list

1
1` ```` `

=

1
1` ```` `

[]

template_list

1
1` ```` `

=

1
1` ```` `

[]

for i

1
1` ```` `

in hostgroupName.split(

1
1` ```` `

','

1
1` ```` `

):

var

1
1` ```` `

= {}

var[

1
1` ```` `

'groupid'

1
1` ```` `

]

1
1` ```` `

= self

1
1` ```` `

.hostgroup_get(i)

group_list.append(var)

for i

1
1` ```` `

in templateName.split(

1
1` ```` `

','

1
1` ```` `

):

var

1
1` ```` `

=

1
1` ```` `

{}

var[

1
1` ```` `

'templateid'

1
1` ```` `

]

1
1` ```` `

=

1
1` ```` `

self

1
1` ```` `

.template_get(i)

template_list.append(var)

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"host.create"

1
1` ```` `

,

"params"

1
1` ```` `

:{

"host"

1
1` ```` `

: hostName,

"name"

1
1` ```` `

: visibleName,

"interfaces"

1
1` ```` `

: [

{

"type"

1
1` ```` `

:

1
1` ```` `

2

1
1` ```` `

,

1
1` ```` `

#1:表示IP;2表示SNMP

"main"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

"useip"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

"ip"

1
1` ```` `

: hostip,

"dns"

1
1` ```` `

: "",

"port"

1
1` ```` `

:

1
1` ```` `

"161" #IP端口10051;SNMP端口161

}

],

"groups"

1
1` ```` `

: group_list,

"templates"

1
1` ```` `

: template_list,

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url, data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

print "添加主机 : \033[42m%s\031[0m \tid :\033[31m%s\033[0m" % (hostip, response[

1
1` ```` `

'result'

1
1` ```` `

][

1
1` ```` `

'hostids'

1
1` ```` `

])

def host_create(

1
1` ```` `

self

1
1` ```` `

, hostip, hostgroupName, templateName):

if self

1
1` ```` `

.host_get(hostip):

print "\033[041m该主机已经添加!\033[0m"

sys.exit(

1
1` ```` `

1

1
1` ```` `

)

group_list

1
1` ```` `

=

1
1` ```` `

[]

template_list

1
1` ```` `

=

1
1` ```` `

[]

for i

1
1` ```` `

in hostgroupName.split(

1
1` ```` `

','

1
1` ```` `

):

var

1
1` ```` `

= {}

var[

1
1` ```` `

'groupid'

1
1` ```` `

]

1
1` ```` `

= self

1
1` ```` `

.hostgroup_get(i)

group_list.append(var)

for i

1
1` ```` `

in templateName.split(

1
1` ```` `

','

1
1` ```` `

):

var

1
1` ```` `

=

1
1` ```` `

{}

var[

1
1` ```` `

'templateid'

1
1` ```` `

]

1
1` ```` `

=

1
1` ```` `

self

1
1` ```` `

.template_get(i)

template_list.append(var)

data

1
1` ```` `

= json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"host.create"

1
1` ```` `

,

"params"

1
1` ```` `

:{

"host"

1
1` ```` `

: hostip,

"interfaces"

1
1` ```` `

: [

{

"type"

1
1` ```` `

:

1
1` ```` `

2

1
1` ```` `

,

"main"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

"useip"

1
1` ```` `

:

1
1` ```` `

1

1
1` ```` `

,

"ip"

1
1` ```` `

: hostip,

"dns"

1
1` ```` `

: "",

"port"

1
1` ```` `

:

1
1` ```` `

"161"

}

],

"groups"

1
1` ```` `

: group_list,

"templates"

1
1` ```` `

: template_list,

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url, data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

print "添加主机 : \033[42m%s\031[0m \tid :\033[31m%s\033[0m" % (hostip, response[

1
1` ```` `

'result'

1
1` ```` `

][

1
1` ```` `

'hostids'

1
1` ```` `

])

def host_disable(

1
1` ```` `

self

1
1` ```` `

,hostip):

data

1
1` ```` `

=

1
1` ```` `

json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"host.update"

1
1` ```` `

,

"params"

1
1` ```` `

: {

"hostid"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.host_get(hostip),

"status"

1
1` ```` `

:

1
1` ```` `

1

},

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url,data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except URLError as e:

print "Error as "

1
1` ```` `

, e

else

1
1` ```` `

:

response

1
1` ```` `

= json.loads(result.read())

result.close()

print '—-主机现在状态————'

print self

1
1` ```` `

.host_get(hostip)

def host_delete(

1
1` ```` `

self

1
1` ```` `

,hostid):

hostid_list

1
1` ```` `

=

1
1` ```` `

[]

#print type(hostid)

for i

1
1` ```` `

in hostid.split(

1
1` ```` `

','

1
1` ```` `

):

var

1
1` ```` `

= {}

var[

1
1` ```` `

'hostid'

1
1` ```` `

]

1
1` ```` `

= self

1
1` ```` `

.host_get(i)

hostid_list.append(var)

data

1
1` ```` `

=

1
1` ```` `

json.dumps({

"jsonrpc"

1
1` ```` `

:

1
1` ```` `

"2.0"

1
1` ```` `

,

"method"

1
1` ```` `

:

1
1` ```` `

"host.delete"

1
1` ```` `

,

"params"

1
1` ```` `

: hostid_list,

"auth"

1
1` ```` `

:

1
1` ```` `

self

1
1` ```` `

.user_login(),

"id"

1
1` ```` `

:

1
1` ```` `

1

})

request

1
1` ```` `

= urllib2.Request(

1
1` ```` `

self

1
1` ```` `

.url,data)

for key

1
1` ```` `

in self

1
1` ```` `

.header:

request.add_header(key,

1
1` ```` `

self

1
1` ```` `

.header[key])

try

1
1` ```` `

:

result

1
1` ```` `

= urllib2.urlopen(request)

except Exception,e:

print e

else

1
1` ```` `

:

result.close()

print "主机 \033[041m %s\033[0m 已经删除 !"

1
1` ```` `

%

1
1` ```` `

hostid

if name

1
1` ```` `

=

1
1` ```` `

= "main"

1
1` ```` `

:

zabbix

1
1` ```` `

=

1
1` ```` `

zabbix_api()

parser

1
1` ```` `

=

1
1` ```` `

argparse.ArgumentParser(description

1
1` ```` `

=

1
1` ```` `

'zabbix api '

1
1` ```` `

,usage

1
1` ```` `

=

1
1` ```` `

'%(prog)s [options]'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-H'

1
1` ```` `

,

1
1` ```` `

'–host'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

'?'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'listhost'

1
1` ```` `

,default

1
1` ```` `

=

1
1` ```` `

'host'

1
1` ```` `

,

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'查询主机'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-G'

1
1` ```` `

,

1
1` ```` `

'–group'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

'?'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'listgroup'

1
1` ```` `

,default

1
1` ```` `

=

1
1` ```` `

'group'

1
1` ```` `

,

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'查询主机组'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-T'

1
1` ```` `

,

1
1` ```` `

'–template'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

'?'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'listtemp'

1
1` ```` `

,default

1
1` ```` `

=

1
1` ```` `

'template'

1
1` ```` `

,

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'查询模板信息'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-A'

1
1` ```` `

,

1
1` ```` `

'–add-group'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

1

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'addgroup'

1
1` ```` `

,

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'添加主机组'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-C'

1
1` ```` `

,

1
1` ```` `

'–add-host'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'addhost'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

3

1
1` ```` `

,metavar

1
1` ```` `

=

1
1` ```` `

(

1
1` ```` `

'192.168.2.1'

1
1` ```` `

,

1
1` ```` `

'test01,test02'

1
1` ```` `

,

1
1` ```` `

'Template01,Template02'

1
1` ```` `

),

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'添加主机,多个主机组或模板使用分号'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-d'

1
1` ```` `

,

1
1` ```` `

'–disable'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'disablehost'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

1

1
1` ```` `

,metavar

1
1` ```` `

=

1
1` ```` `

(

1
1` ```` `

'192.168.2.1'

1
1` ```` `

),

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'禁用主机'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-L'

1
1` ```` `

,

1
1` ```` `

'–allin'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'allin'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

'?'

1
1` ```` `

,default

1
1` ```` `

=

1
1` ```` `

'allin'

1
1` ```` `

,

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'从Excel批量导入主机'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-D'

1
1` ```` `

,

1
1` ```` `

'–delete'

1
1` ```` `

,dest

1
1` ```` `

=

1
1` ```` `

'deletehost'

1
1` ```` `

,nargs

1
1` ```` `

=

1
1` ```` `

'+'

1
1` ```` `

,metavar

1
1` ```` `

=

1
1` ```` `

(

1
1` ```` `

'192.168.2.1'

1
1` ```` `

),

1
1` ```` `

help

1
1` ```` `

=

1
1` ```` `

'删除主机,多个主机之间用分号'

1
1` ```` `

)

parser.add_argument(

1
1` ```` `

'-v'

1
1` ```` `

,

1
1` ```` `

'–version'

1
1` ```` `

, action

1
1` ```` `

=

1
1` ```` `

'version'

1
1` ```` `

, version

1
1` ```` `

=

1
1` ```` `

'%(prog)s 1.0'

1
1` ```` `

)

if len

1
1` ```` `

(sys.argv)

1
1` ```` `

=

1
1` ```` `

=

1
1` ```` `

1

1
1` ```` `

:

print parser.print_help()

else

1
1` ```` `

:

args

1
1` ```` `

=

1
1` ```` `

parser.parse_args()

if args.listhost !

1
1` ```` `

= 'host' :

if args.listhost:

zabbix.host_get(args.listhost)

else

1
1` ```` `

:

zabbix.host_get()

if args.listgroup !

1
1` ```` `

=

1
1` ```` `

'group'

1
1` ```` `

:

if args.listgroup:

zabbix.hostgroup_get(args.listgroup)

else

1
1` ```` `

:

zabbix.hostgroup_get()

if args.listtemp !

1
1` ```` `

= 'template'

1
1` ```` `

:

if args.listtemp:

zabbix.template_get(args.listtemp)

else

1
1` ```` `

:

zabbix.template_get()

if args.addgroup:

zabbix.hostgroup_create(args.addgroup[

1
1` ```` `

0

1
1` ```` `

])

if args.addhost:

zabbix.host_create(args.addhost[

1
1` ```` `

0

1
1` ```` `

], args.addhost[

1
1` ```` `

1

1
1` ```` `

], args.addhost[

1
1` ```` `

2

1
1` ```` `

])

if args.disablehost:

zabbix.host_disable(args.disablehost)

if args.deletehost:

zabbix.host_delete(args.deletehost[

1
1` ```` `

0

1
1` ```` `

])

if args.allin !

1
1` ```` `

= 'allin'

1
1` ```` `

:

workbook

1
1` ```` `

= xlrd.open_workbook(

1
1` ```` `

'zabbix_host_add.xlsx'

1
1` ```` `

)

1
1` ```` `

#Excel名

for row

1
1` ```` `

in xrange

1
1` ```` `

(workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].nrows):

hostname

1
1` ```` `

= workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].cell(row,

1
1` ```` `

0

1
1` ```` `

).value

visible

1
1` ```` `

= workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].cell(row,

1
1` ```` `

1

1
1` ```` `

).value

hostip

1
1` ```` `

= workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].cell(row,

1
1` ```` `

2

1
1` ```` `

).value

hostgroup

1
1` ```` `

= workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].cell(row,

1
1` ```` `

3

1
1` ```` `

).value

hosttemp

1
1` ```` `

= workbook.sheets()[

1
1` ```` `

0

1
1` ```` `

].cell(row,

1
1` ```` `

4

1
1` ```` `

).value

zabbix.host_create_andy(hostname,visible,hostip,hostgroup, hosttemp)

|

上面红色标注,请根据实际情况修改。

上传Python脚本zabbix_host.py至zabbix服务器

[root@localhost temp]# ll
total 1964
-rwxr-xr-x 1 root root 14644 Dec 17 23:28 zabbix_host.py

[root@localhost temp]# chown zabbix:zabbix zabbix_host.py #修改属组属主
[root@localhost temp]# chmod +x zabbix_host.py #添加执行权限

[root@localhost temp]# python zabbix_host.py -L #执行zabbix_host.py进行主机添加

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全经验

SpringBoot微服务 +tomcat集群+Ngnix负载均衡+Mysql主从复制,读写分离(1)

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索