This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
язык_программирования_python [2025/04/08 13:53] val [Дополнительные материалы] |
язык_программирования_python [2025/05/25 09:14] (current) val [Zabbix API приложение] |
||
---|---|---|---|
Line 96: | Line 96: | ||
* [[https://dev.to/francescoxx/python-crud-rest-api-using-flask-sqlalchemy-postgres-docker-docker-compose-3kh4|Python CRUD Rest API using Flask, SQLAlchemy, Postgres, Docker, Docker Compose]] | * [[https://dev.to/francescoxx/python-crud-rest-api-using-flask-sqlalchemy-postgres-docker-docker-compose-3kh4|Python CRUD Rest API using Flask, SQLAlchemy, Postgres, Docker, Docker Compose]] | ||
+ | ===== Zabbix API приложение ===== | ||
+ | |||
+ | * [[https://www.zabbix.com/documentation/current/en/manual/api]] | ||
+ | * [[https://sbcode.net/zabbix/zabbix-api-python-example/]] | ||
+ | * [[https://forum.checkmk.com/t/rest-api-python-question-how-to-use-variables-in-json-post-in-key-and-value/34652]] | ||
+ | | ||
+ | <code> | ||
+ | (venv1) server:~# pip install requests | ||
+ | |||
+ | (venv1) server:~# cat zab_get_problem.py | ||
+ | </code><code> | ||
+ | import requests | ||
+ | import json | ||
+ | |||
+ | ZABBIX_API_URL = "http://127.0.0.1/zabbix/api_jsonrpc.php" | ||
+ | UNAME = "Admin" | ||
+ | PWORD = "zabbix" | ||
+ | |||
+ | r = requests.post(ZABBIX_API_URL, | ||
+ | json={ | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "user.login", | ||
+ | "params": { | ||
+ | "username": UNAME, | ||
+ | "password": PWORD}, | ||
+ | "id": 1 | ||
+ | }) | ||
+ | |||
+ | print(json.dumps(r.json(), indent=4, sort_keys=True)) | ||
+ | |||
+ | AUTHTOKEN = r.json()["result"] | ||
+ | |||
+ | print(AUTHTOKEN) | ||
+ | |||
+ | # Retrieve a list of problems | ||
+ | print("\nRetrieve a list of problems") | ||
+ | r = requests.post(ZABBIX_API_URL, | ||
+ | headers={'Authorization': 'Bearer ' + AUTHTOKEN}, | ||
+ | json={ | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "problem.get", | ||
+ | "params": {}, | ||
+ | "id": 2, | ||
+ | }) | ||
+ | |||
+ | print(json.dumps(r.json(), indent=4, sort_keys=True)) | ||
+ | |||
+ | #Logout user | ||
+ | print("\nLogout user") | ||
+ | r = requests.post(ZABBIX_API_URL, | ||
+ | headers={'Authorization': 'Bearer ' + AUTHTOKEN}, | ||
+ | json={ | ||
+ | "jsonrpc": "2.0", | ||
+ | "method": "user.logout", | ||
+ | "params": {}, | ||
+ | "id": 2, | ||
+ | }) | ||
+ | |||
+ | print(json.dumps(r.json(), indent=4, sort_keys=True)) | ||
+ | </code><code> | ||
+ | (venv1) server:~# python3 zab_get_problem.py | ||
+ | </code> | ||
===== Дополнительные материалы ===== | ===== Дополнительные материалы ===== | ||