This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
язык_программирования_python [2025/05/24 17:58] val [Zabbix API приложение] |
язык_программирования_python [2025/05/27 11:53] (current) val |
||
---|---|---|---|
Line 19: | Line 19: | ||
</code> | </code> | ||
+ | |||
+ | ===== Интерактивная оболочка ===== | ||
+ | <code> | ||
+ | (venv1) # pip install ipython | ||
+ | </code> | ||
===== Web приложение ===== | ===== Web приложение ===== | ||
Line 98: | Line 103: | ||
===== Zabbix API приложение ===== | ===== Zabbix API приложение ===== | ||
+ | * [[https://www.zabbix.com/documentation/current/en/manual/api]] | ||
* [[https://sbcode.net/zabbix/zabbix-api-python-example/]] | * [[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> | <code> | ||
(venv1) server:~# pip install requests | (venv1) server:~# pip install requests | ||
- | (venv1) server:~# cat zab_get_problem.py | + | (venv1) server:~# cat zab_set_map_name.py |
</code><code> | </code><code> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
import requests | import requests | ||
import json | import json | ||
+ | from sys import argv | ||
+ | |||
+ | if len(argv) != 3 : | ||
+ | print("You must set argument!!!\nExample: python zab_set_map_name.py 2 \"ISP 1\"") | ||
+ | quit() | ||
+ | |||
+ | MAPID = argv[1] | ||
+ | MAPNAME = argv[2] | ||
ZABBIX_API_URL = "http://127.0.0.1/zabbix/api_jsonrpc.php" | ZABBIX_API_URL = "http://127.0.0.1/zabbix/api_jsonrpc.php" | ||
Line 112: | Line 129: | ||
PWORD = "zabbix" | PWORD = "zabbix" | ||
+ | print("\nLogin user {} to Zabbiz API".format(UNAME)) | ||
r = requests.post(ZABBIX_API_URL, | r = requests.post(ZABBIX_API_URL, | ||
json={ | json={ | ||
Line 126: | Line 144: | ||
AUTHTOKEN = r.json()["result"] | AUTHTOKEN = r.json()["result"] | ||
- | print(AUTHTOKEN) | + | #print("Rename Map with ID", MAPID, "to", MAPNAME) |
+ | print(f"Rename Map with ID {MAPID} to {MAPNAME}") | ||
- | # Retrieve a list of problems | ||
- | print("\nRetrieve a list of problems") | ||
r = requests.post(ZABBIX_API_URL, | r = requests.post(ZABBIX_API_URL, | ||
headers={'Authorization': 'Bearer ' + AUTHTOKEN}, | headers={'Authorization': 'Bearer ' + AUTHTOKEN}, | ||
json={ | json={ | ||
- | "jsonrpc": "2.0", | + | "jsonrpc": "2.0", |
- | "method": "problem.get", | + | "method": "map.update", |
- | "params": {}, | + | "params": { |
- | "id": 2, | + | "sysmapid": MAPID, |
+ | "name": MAPNAME | ||
+ | }, | ||
+ | "id": 2 | ||
}) | }) | ||
print(json.dumps(r.json(), indent=4, sort_keys=True)) | print(json.dumps(r.json(), indent=4, sort_keys=True)) | ||
- | #Logout user | ||
print("\nLogout user") | print("\nLogout user") | ||
r = requests.post(ZABBIX_API_URL, | r = requests.post(ZABBIX_API_URL, | ||
Line 154: | Line 173: | ||
print(json.dumps(r.json(), indent=4, sort_keys=True)) | print(json.dumps(r.json(), indent=4, sort_keys=True)) | ||
</code><code> | </code><code> | ||
- | (venv1) server:~# python3 zab_get_problem.py | + | (venv1) server:~# chmod +x zab_set_map_name.py |
+ | |||
+ | (venv1) server:~# ./zab_set_map_name.py 2 "ISP 1" | ||
</code> | </code> | ||
+ | |||
+ | ===== Zabbix LLD приложение ===== | ||
+ | |||
+ | * [[https://www.geeksforgeeks.org/python-find-dictionary-matching-value-in-list/]] | ||
+ | * [[https://docs-python.ru/tutorial/vstroennye-funktsii-interpretatora-python/funktsija-next/]] | ||
+ | <code> | ||
+ | gate# apt install python3-xmltodict/stable | ||
+ | |||
+ | gate# cat /etc/zabbix/dhcp-pools.py | ||
+ | </code><code> | ||
+ | #!/usr/bin/env python3 | ||
+ | |||
+ | # Example usage: | ||
+ | # ./dhcp-pools.py | ||
+ | # ./dhcp-pools.py LAN1 defined|used | ||
+ | |||
+ | from sys import argv | ||
+ | import subprocess, xmltodict, json | ||
+ | |||
+ | p = subprocess.Popen("/usr/bin/dhcpd-pools -f x", stdout=subprocess.PIPE, shell=True) | ||
+ | (output, err) = p.communicate() | ||
+ | p_status = p.wait() | ||
+ | |||
+ | o = xmltodict.parse(output) | ||
+ | |||
+ | if len(argv)==1: | ||
+ | r=[] | ||
+ | for i in o['dhcpstatus']['shared-network']: | ||
+ | s={} | ||
+ | s["{#POOLNAME}"]=i['location'] | ||
+ | r.insert(1,s) | ||
+ | print(json.dumps(r)) | ||
+ | else: | ||
+ | LANNAME=argv[1] | ||
+ | USDEF=argv[2] | ||
+ | res = next((sub for sub in o['dhcpstatus']['shared-network'] if sub['location'] == LANNAME), None) | ||
+ | print(res[USDEF]) | ||
+ | </code> | ||
+ | |||
===== Дополнительные материалы ===== | ===== Дополнительные материалы ===== | ||
+ | |||
+ | * [[https://pypi.org/project/ansible-output-parser/]] | ||
+ | * [[https://www.cyberciti.biz/faq/python-run-external-command-and-get-output/]] | ||
==== Доступ к каталогу по http ==== | ==== Доступ к каталогу по http ==== | ||
Line 166: | Line 229: | ||
</code> | </code> | ||
+ | ==== Черновик ==== | ||