---
# System prepended metadata

title: zabbix_midware v2.0
tags: [By_Ivan]

---

# zabbix_midware v2.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.

[TOC]
## 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/1U57WXy.png)



## Usage

### Running the program
```bash 
$ python3 zabbix_main.py
usage:
	python3 zabbix_main.py start|restart|stop|daemon
```
Zabbix midware supports running in both background and foreground.

If "start" was given to the argument input, the midware would then run without daemonize. The program could be stopped by keyboard interruption.
```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)
```
If "daemon" was given to the arguemnt input, Daemonize process would then activates; the program would be detached from the current user and terminal, running in the background.
```bash
$ python3 zabbix_main.py daemon
Starting...
```
A daemonized-midware can only be stopped by execute the main module with "stop" argument.

```bash
$ python3 zabbix_main.py stop
Stopping...
Daemon_has_stoped
```
### 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.

```
RESTapi endpoint index:
[GET]    1. ip:port/zabbix/show
[POST]   2. ip:port/zabbix/update
[POST]   3. ip:port/zabbix/delete
```

"Show" would return the current setting of the midware.

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

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"}}
```

## Config Structure
```json!
{
    "zabbix": {
        "host": "Zabbix ip and port",
        "user": "#@#@#@", "password": "@#@#@#",
        "UI_address": {
            "host": "0.0.0.0 / endpoint for",
            "port": 8880
        },
        "out_Dir":"Out put file path",
        "csv_key":{"attr_entry":"arguments", "clock_entry":"for csv output"}
    },
    "probe": {
        "zabbix_probe": {
            "($dtype0)": {
                "($itemID)": {
                    "name": "Item Name, defined on the zabbix webpage",
                    "probe_server": "Defined by the name of 'Zabbix Application'"
                }
            },
            "($dtype3)":{
                "($itemID)": {
                    "name":"another item",
                    "probe_server": "another source"
                }
            }
        }
    }
}


```

## Framework Design

![](https://i.imgur.com/ePnyZuj.jpg)


### Alternitive tool: Systemd

```clike
[Unit]
Description=Sample Application

[Service]
Type=simple
ExecStart=/opt/test.py
Restart=always

[Install]
WantedBy=multi-user.target
```
Benefits:
1. Stable
2. Service handling: Auto-Reboot, Status, etc
3. Proccess monitor (recording status and logs)