# bgmot/zabbix-module-hosts-tree architecture

> Architecture diagram of the GitHub repository bgmot/zabbix-module-hosts-tree, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bgmot/zabbix-module-hosts-tree
- Repository: https://github.com/bgmot/zabbix-module-hosts-tree

## Overview

##### Project Type and Purpose
- This is a Zabbix extension module (plugin) written in PHP and JavaScript.
- Its purpose is to integrate with the Zabbix server UI and provide a new “Hosts Tree” view under Monitoring → Hosts Tree.

##### High-Level Components
a. Zabbix Core
- The host monitoring platform that loads modules via its module API.
b. Module Loader & Manifest
- manifest.json: informs Zabbix about menu items, entry points, permissions.
- Module.php: bootstraps the module, registers hook handlers.
c. Controllers (actions/)
- CControllerBGHost.php: primary controller for host tree data.
- CControllerBGHostView.php: handles initial page render.
- CControllerBGHostViewRefresh.php: AJAX refresh endpoint.
d. Views & Partials
- views/module.monitoring.bghost.view.php: template for main page.
- views/module.monitoring.bghost.view.refresh.php: partial for live updates.
- partials/js/monitoring.host.view.refresh.js.php: JS function to poll/refresh.
- partials/module.monitoring.host.view.html.php: HTML snippet for tree.
e. Static Assets
- CSS: views/css/bghost.css
- JS: views/js/monitoring.host.view.js.php

##### Interactions and Data Flow
1) User clicks “Hosts Tree” in Zabbix UI → Zabbix invokes Module.php to handle the menu route.
2) Zabbix module loader dispatches to CControllerBGHostView → gathers group/host data via Zabbix API/DB.
3) Controller returns a view template (module.monitoring.bghost.view.php) plus CSS/JS links to browser.
4) Front-end JS (monitoring.host.view.js) runs in user’s browser, draws the tree.
5) Periodically or on demand, JS calls the refresh endpoint (CControllerBGHostViewRefresh) via AJAX → returns updated HTML from partials/js/monitoring.host.view.refresh.js.php.
6) Browser injects updated tree fragment.

##### Architectural Patterns & Design Principles
- Plugin Architecture: self-contained module loaded by Zabbix at startup.
- MVC-style separation:
- Model: Zabbix API/DB calls within controllers.
- View: PHP templates & partials.
- Controller: action classes.
- Progressive Enhancement: initial full render + AJAX partial refresh.
- Single Responsibility: each controller and partial serves one purpose (render vs. refresh).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Entities
    Browser["Browser Client"]:::browser
    API["Zabbix API/DB"]:::db

    %% Zabbix Core & Module Loading
    subgraph "Zabbix Core"
        ZabbixCore["Zabbix Core\n(Server & UI)"]:::core
        Manifest["manifest.json"]:::manifest
        ModulePHP["Module.php"]:::bootstrap
    end

    %% Controllers
    subgraph "Controllers"
        DataCtrl["CControllerBGHost.php\n(Primary Data)"]:::controller
        ViewCtrl["CControllerBGHostView.php\n(Initial Render)"]:::controller
        RefreshCtrl["CControllerBGHostViewRefresh.php\n(AJAX Refresh)"]:::controller
    end

    %% Views & Partials
    subgraph "Views & Partials"
        MainView["module.monitoring.bghost.view.php"]:::view
        RefreshView["module.monitoring.bghost.view.refresh.php"]:::view
        HTMLPartial["module.monitoring.host.view.html.php"]:::partial
        RefreshJSPartial["monitoring.host.view.refresh.js.php"]:::partialjs
    end

    %% Static Assets
    subgraph "Static Assets"
        CSS["bghost.css"]:::css
        MainJS["monitoring.host.view.js.php"]:::js
    end

    %% Data Flows
    Browser -->|HTTP GET Hosts Tree| ZabbixCore
    ZabbixCore -->|menu route| ModulePHP
    ModulePHP -->|dispatch to| ViewCtrl
    ViewCtrl -->|retrieves data| API
    ViewCtrl -->|renders template| MainView
    MainView -->|includes CSS| CSS
    MainView -->|includes JS| MainJS
    MainJS -->|draws tree| Browser

    Browser -->|AJAX POST refresh| RefreshCtrl
    RefreshCtrl -->|retrieves data| API
    RefreshCtrl -->|renders partial| RefreshView
    RefreshView -->|HTML snippet| HTMLPartial
    RefreshView -->|JS poll function| RefreshJSPartial
    RefreshView -->|injects update| Browser

    %% Click Events
    click Manifest "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/manifest.json"
    click ModulePHP "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/Module.php"
    click DataCtrl "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/actions/CControllerBGHost.php"
    click ViewCtrl "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/actions/CControllerBGHostView.php"
    click RefreshCtrl "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/actions/CControllerBGHostViewRefresh.php"
    click MainView "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/views/module.monitoring.bghost.view.php"
    click RefreshView "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/views/module.monitoring.bghost.view.refresh.php"
    click HTMLPartial "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/partials/module.monitoring.host.view.html.php"
    click RefreshJSPartial "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/partials/js/monitoring.host.view.refresh.js.php"
    click CSS "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/views/css/bghost.css"
    click MainJS "https://github.com/bgmot/zabbix-module-hosts-tree/blob/master/views/js/monitoring.host.view.js.php"

    %% Styles
    classDef core fill:#f9f,stroke:#333,stroke-width:1px
    classDef manifest fill:#fcf8e3,stroke:#b7950b,stroke-width:1px
    classDef bootstrap fill:#e7f3fe,stroke:#3498db,stroke-width:1px
    classDef controller fill:#d5f5e3,stroke:#27ae60,stroke-width:1px
    classDef view fill:#fdebd0,stroke:#d35400,stroke-width:1px
    classDef partial fill:#e8daef,stroke:#8e44ad,stroke-width:1px
    classDef partialjs fill:#e8daef,stroke:#9b59b6,stroke-width:1px
    classDef css fill:#d6ecef,stroke:#1abc9c,stroke-width:1px
    classDef js fill:#f1c40f,stroke:#b7950b,stroke-width:1px
    classDef browser fill:#aed6f1,stroke:#2980b9,stroke-width:1px
    classDef db fill:#fadbd8,stroke:#c0392b,stroke-width:1px
```
