---
# System prepended metadata

title: Zabbix Query by Python (first edition zabbix_query.py)
tags: [By_Ivan]

---

# Zabbix Query by Python (first edition zabbix_query.py)
###### tags: `By_Ivan`
##### Using tools: json, requests

Zabbix_query.py is a python module designed to work with Zabbix' Http API. User can interact with Zabbix and its data from python environment.

This document contains the basic work flow of how to use, and a list of all functions.

[TOC]

## Basic Work Flow

![](https://i.imgur.com/Sf7K2ww.png)

### core function (send query message)

Zabbix' api works under the jsonrpc protocol. The query message can be sent to zabbix using "requests" module.

```python!
def query(jsonrpc, host):
    url = "http://"+host+"/zabbix/api_jsonrpc.php"
    headers = {"Content-Type": "application/json"}
    
    response = requests.post(url, data=json.dumps(jsonrpc), headers=headers).json()
    return response
```

## Function lists
```python!
#########################################################
### functions designed for AIPOs midware

def query(jsonrpc):
# the core function, usually used in other functions

def login(host = "", user = "", password=""):
# log in the given zabbix host, the query would return the token for Authentication

def extend_lifetime():
# extend the current section's lifetime

def item_hist_get(itemid, dtype, limit=None, time_from=0, time_till = None):
# query for the newest <limit> message(s) within the window defined by <time_from> and <time_till>
""" 
Possible dtype values:
0 - numeric float;
1 - character;
2 - log;
3 - numeric unsigned;
4 - text. 
"""

def bulk_query(dict c):
# read from the config json, 
# Todo

#########################################################
```
```python!
#########################################################
### functions for inspecting data

def hostid_get(host_name_list):
# given list of host name, return their ids

def itemlist_get(host_id):
# given host's id, return the host's every itemid, item name, and thier dtype

def item_get(item_id):
# given item id, return its every attribute

def item_attr_get(item_name, host_name):
# similar to item_get, but take the item's name as input

#########################################################
```