Centos7.4 deploys the LNMP platform and Zabbix to implement monitoring, and writes Python scripts to achieve fast control

1. Deploy LNMP platform

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

Install mysql, create zabbix library, authorization, etc.

Second, deploy 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

Go to the database directory to import the database table, and go to the front-end directory to copy the webpage content to nginx. Set permissions.

Modify zabbix server configuration file and agent configuration file

useradd -s /sbin/nologin zabbix

Visit nginx

http://192.168.122.10/index.php

Complete zabbix configuration

The account is admin and the password is zabbix

Three, write python script

1. Visit zabbix official website, find API related information, get zabbix version number

code show as below:

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. Visit zabbix official website, find API related information, get zabbix token

code show as below:

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. Visit zabbix official website, find API related information, and create a host

code show as below:

#!/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 ('Please enter the name of the monitoring host you want to create:')
    ip = input ('Please enter the IP of the monitoring host you want to create:')
    gid = input (' Please enter the GID of the monitoring host you want to create: ')
    tempid = input (' Please enter the TEMPID of the monitoring host you want to create: ')  
    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) lists existing hosts ID and host name
(1) List the group ID and host group
(2) List the template ID and template
(3) Create the monitoring host
(4) Exit
Please select the function you need from 1234, creating the monitoring host requires filling in the group ID , Template ID, IP address and other information: '' '
    while True:
        choice = input (prompt)
        if choice ==' 4 ':
            break
        if choice not in' 0123 ':
            print (' invalid !, please select the correct function ' )
        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)

 

Published 73 original articles · praised 4 · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_27592485/article/details/102490634