一、部署LNMP平台
yum -y install gcc gcc-c++ make automake php php-mysql openssl-devel pcre-devel
cd nginx-1.12.2
useradd -s /sbin/nologin nginx
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install
rpm -ivh php-fpm-5.4.16-42.el7.x86_64.rpm
systemctl restart php-fpm
systemctl enable php-fpm
cd /usr/local/nginx/conf/
vim nginx.conf
ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
vim /usr/local/nginx/html/test.php
安装mysql,创建zabbix的库,授权等
二、部署zabbix
tar -xf zabbix-3.4.4.tar.gz
cd zabbix-3.4.4/
./configure --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_conf --with-net-snmp --with-libcurl
make && make install
去database目录把数据库表格导入,去front前端目录把网页内容,复制到nginx下。设定权限。
修改zabbix的server配置文件和agent配置文件
useradd -s /sbin/nologin zabbix
访问nginx
http://192.168.122.10/index.php
完成zabbix的配置
账号为admin,密码为zabbix
三、编写python脚本
1、访问zabbix官网,查找API相关的资料,获取zabbix的版本号
代码如下:
import requests
import json
url = 'http://192.168.122.10/api_jsonrpc.php'
headers = {'Content-Type': 'application/json-rpc'}
data = {
"jsonrpc": "2.0",
"method": "apiinfo.version",
"params": [],
"id": 1
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
result = r.json()
print('The Zabbix version is %s' % result['result'])
2、访问zabbix官网,查找API相关的资料,获取zabbix的令牌
代码如下:
import requests
import json
url = 'http://192.168.122.10/api_jsonrpc.php'
headers = {'Content-Type': 'application/json-rpc'}
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
result = r.json()
print('The Zabbix Token is %s' % result['result'])
1、访问zabbix官网,查找API相关的资料,创建主机
代码如下:
#!/usr/bin/env python3
import requests
import json
def get_token(url, headers):
data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
return r.json()['result']
def get_hosts(token, url, headers):
data = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
}
},
"auth": token,
"id": 5
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
n = r.json()['result']
for item in n:
print(item['hostid'], item['host'])
def get_groups(token, url, headers):
data = {
"jsonrpc": "2.0",
"method": "hostgroup.get",
"params": {
"output": "extend",
"filter": {
}
},
"auth": token,
"id": 2
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
b = r.json()['result']
for item in b:
print(item['groupid'], item['name'])
def get_tamp(token, url, headers):
data = {
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
}
},
"auth": token,
"id": 1
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
c = r.json()['result']
for item in c:
print(item['templateid'], item['host'])
def create_host(token, url, headers):
hostname = input('请输入你要创建的监控主机名:')
ip = input('请输入你要创建的监控主机的IP:')
gid = input('请输入你要创建的监控主机的GID:')
tempid = input('请输入你要创建的监控主机的TEMPID:')
data = {
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": hostname,
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": ip,
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": gid
}
],
"templates": [
{
"templateid": tempid
}
],
"inventory_mode": 0,
"inventory": {
}
},
"auth": token,
"id": 4
}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
def show_memu(token, url, headers):
cmds = {'1': get_groups, '2': get_tamp, '3': create_host, '0': get_hosts}
prompt = '''(0)列出已存在主机ID和主机名
(1)列出组ID和主机组
(2)列出模板ID和模板
(3)创建监控主机
(4)退出
请从1234中选择你需要的功能,创建监控主机需要填写组ID,模板ID,IP地址等信息: '''
while True:
choice = input(prompt)
if choice == '4':
break
if choice not in '0123':
print('无效!,请选择正确的功能')
cmds[choice](token, url, headers)
if __name__ == '__main__':
url = 'http://172.17.2.187/api_jsonrpc.php'
headers = {'Content-Type': 'application/json-rpc'}
token = get_token(url, headers)
show_memu(token, url, headers)