---
# System prepended metadata

title: zabbix_midware v3.0
tags: [By_Ivan]

---

# zabbix_midware v3.0
###### tags: `By_Ivan`
zabbix_midware is a service, designed for connecting AIOPs and Zabbix. 

This document explains how to use the program, and shows the architecture of the midware framework.



## Hierarchy and Architecture

modules and contents:
```perl=
- midware_zabbix/
# main modules :
    - zabbix_main.py
    - zabbix_query.py
    - zabbix_UI.py
# function module, portable designed:
    - daemonize.py
    - j2c.py
# config and log file:
    - midware.conf
    - .midware.log
# intended output directory:
    - probe/
```
Brief workflow：
![](https://i.imgur.com/JTuloTa.png)

```plantuml
start
-Config Setup (user/passwd)
-Start Program
-Init (endpoint/init)
-Query Continuously
```

## Program Usage

### Run Directly
```bash 
$ python3 zabbix_main.py -h
usage: zabbix_main.py [-h] [options] {start,stop,restart,daemon}

Transform Zabbix data to AIOPs format.

positional arguments:
  {start,stop,restart,daemon}
                        Instruction for the program

optional arguments:
  -h, --help                 show this help message and exit
  -c path, --config path     config file path
  -l path, --log path        log file path
  -o path, --out_Dir path    output directory path
  -p path, --pidfile path    pidfile path
  -v                         logging noise level
```
Zabbix midware supports running in both background and foreground.

"Start" argument would run the midware normally, </br>
The program could be stopped by keyboard interruption (Ctrl+C).
```bash
$ python3 zabbix_main.py start
Starting...
Daemonize_off
 * Serving Flask app "zabbix_UI" (lazy loading)
 * Environment: production
...
   
 * Running on http://0.0.0.0:8880/ (Press CTRL+C to quit)
```
"Daemon" arguemnt input would daemonize the process using the self-implement daemonize module. The program would be detached from the current user and terminal, then run in the background.</br> Run with "Stop" argument to kill the background process. (Only one daemonized process can exist at the same time.)
```bash
$ python3 zabbix_main.py daemon
Starting...
$ python3 zabbix_main.py stop
Stopping...
Daemon_has_stoped
```
### Run by systemd
```.service=
# /usr/lib/systemd/system/zabbix-midware.service
[Unit]
Description=Midware between zabbix and AIOPs

[Service]
ExecStart=/home/ivan/midware_zabbix/zabbix_main.py -o /home/ivan/share2/probe start
Restart=on-abnormal
RestartSec=3min

[Install]
WantedBy=multi-user.target
```
Generally a better option.</br>
Running as a systemd service has several benefits, including :
1. auto-load at boot-up
2. auto-restart
3. log handling

### Output

According to AIOPs' regulation, datas should be grouped by their datetime (one group for each minute), and stored in distinct files.

>zabbix_probe@${datetime}.csv

The format of the datetime is set to "%Y%m%d_%H_%M" by default.

## UI Endpoints
A sub-thread would be created when starting the midware, dedicated to http request handling.

```bash
% curl 172.16.1.70:8880
zabbix midware UI:
    index:
[GET]    1. ip:port/zabbix/show
[GET]    2. ip:port/zabbix/init
[POST]   3. ip:port/zabbix/update
[POST]   4. ip:port/zabbix/delete
```

"Show" would return the list of IDs of the monitored items.

"Init" would set the config to default. In zabbix' case, all of the available items would be added to the monitor list.

"Update" and "Delete" would add or remove a item of the given "itemID" from the form table.

For example:
```bash
$ curl -X POST --data "itemID=32142" localhost:8880/delete
{"message":"delete complete"}
$ curl -X POST --data "itemID=32142" localhost:8880/update
{"message":"update complete"}
```
If the given "itemID" is invalid or the item does not exist, the endpoint would then return with error code "0".
```bash
{"error":{"code":0,"message":"EMPTY_RESULT:item_DNE"}}
```
The update endpoint also accept "probe_server" 



## Config Structure
```json!
{
    "zabbix": {
        "host": "Zabbix ip and port",
        "user": "SecretUser", "password": "SecretPwd",
        "UI_address": {
            "host": "0.0.0.0",
            "port": 8880
        }
    },
    "probe": {
        "zabbix_probe": {
            "<$dtype0>": {
                "<$itemID>": {
                    "name": "Item Name, defined on the zabbix webpage",
                    "probe_server": "Default set to '-'"
                },
                "<$itemID>": {
                    "name":"another item",
                    "probe_server": "another source"
                }
            },
            "<$dtype3>":{
                "($itemID)": {
                    "name":"another item",
                    "probe_server": "another source"
                }
            }
        }
    }
}
```

## Framework Design
![](https://i.imgur.com/5U8viQk.jpg)

The source file of the graph is uploaded to cloud-drive.