Zabbix API 연동을 위한 Python3 활용

Zabbix API를 호출하려면 인증 토큰을 먼저 획득해야 합니다. HTTP POST 요청으로 인증 프로세스를 시작합니다.

# Zabbix 인증 요청
import json
import requests

api_endpoint = 'http://172.10.10.2/zabbix/api_jsonrpc.php'
request_config = {'Content-Type': 'application/json'}
credentials = {
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1
}

response = requests.post(api_endpoint, data=json.dumps(credentials), headers=request_config)
print(response.text)

결과에서 인증 토큰을 추출합니다:

{
  "jsonrpc": "2.0",
  "result": "da336b04d376d914bf06bd2192c4ce3f",
  "id": 1
}

호스트 정보를 조회할 때는 획득한 토큰을 사용합니다:

host_query = {
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": ["hostid", "host"],
        "selectInterfaces": ["interfaceid", "ip"]
    },
    "id": 2,
    "auth": "da336b04d376d914bf06bd2192c4ce3f"
}

response = requests.post(api_endpoint, data=json.dumps(host_query), headers=request_config)
print(response.text)

호스트 그룹 정보를 필터링하여 검색합니다:

group_query = {
    "jsonrpc": "2.0",
    "method": "hostgroup.get",
    "params": {
        "output": "extend",
        "filter": {"name": ["Linux servers"]}
    },
    "auth": "da336b04d376d914bf06bd2192c4ce3f",
    "id": 1
}

response = requests.post(api_endpoint, data=json.dumps(group_query), headers=request_config)
print(response.text)

템플릿 정보를 확인하려면 host 필터를 적용합니다:

template_search = {
    "jsonrpc": "2.0",
    "method": "template.get",
    "params": {
        "output": "extend",
        "filter": {"host": ["Template OS Linux"]}
    },
    "auth": "da336b04d376d914bf06bd2192c4ce3f",
    "id": 1
}

response = requests.post(api_endpoint, data=json.dumps(template_search), headers=request_config)
print(response.text)

새 호스트를 생성할 때는 그룹 ID와 템플릿 ID를 활용합니다:

new_host_config = {
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "Linux server",
        "interfaces": [{
            "type": 1, "main": 1, "useip": 1,
            "ip": "192.168.3.1", "port": "10050"
        }],
        "groups": [{"groupid": "2"}],
        "templates": [{"templateid": "10001"}]
    },
    "auth": "da336b04d376d914bf06bd2192c4ce3f",
    "id": 1
}

response = requests.post(api_endpoint, data=json.dumps(new_host_config), headers=request_config)
print(response.text)

태그: ZabbixAPI Python3 통합모니터링 자동화스크립트

8월 17일 04:00에 게시됨