# tharkadharshana/cacti architecture

> Architecture diagram of the GitHub repository tharkadharshana/cacti, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tharkadharshana/cacti
- Repository: https://github.com/tharkadharshana/cacti

## Overview

##### Project type and purpose
- Cacti is a monolithic, full-stack PHP web application with an accompanying CLI-based poller service.
- Purpose: network graphing and monitoring platform leveraging RRDtool and SNMP.

##### Main architectural layers & components
A. Presentation Layer (Web UI)
- Web server (Apache/Nginx) serving PHP via CGI/FPM
- PHP scripts under root (e.g., index.php, auth_*.php, graph_*.php)
- Themes, CSS/JS in include/themes and include/js
- Localization under locales/LC_MESSAGES

B. Application Layer (Business Logic)
- Core libraries (`lib/*.php`): data modeling, API endpoints, graph/render logic, authentication, templating, caching utilities
- Plugins framework (`plugins/`)
- Automation and discovery modules (`automation_*.php`)

C. Data Collection Layer (Poller Service)
- Systemd service `cactid.service` or cron invoking `poller.php` and its sub-modules (`poller_*.php`)
- SNMP Agent (`snmpagent_*`, remote_agent.php)
- CLI tools (`cli/*.php`) for on-demand data import, upgrades, audit, automation

D. Data Storage & Processing
- MySQL/MariaDB database (tables defined in cacti.sql, manipulated via `cli/upgrade_database.php`)
- RRD files under `rra/` manipulated via RRDtool (`rrd.php`, `rrdcheck.php`, `rrdcleaner.php`)
- Cache files in `cache/` for speeding up queries and graphs

E. External Dependencies & Integrations
- RRDtool binary
- Net-SNMP library for SNMP data collection
- PHP modules (snmp, pdo_mysql, rrdtool, etc.)
- Optional remote poller nodes communicating over HTTP/MySQL replication

##### Relationships & data flows
1. User → Browser → Web Server → PHP-FPM → Application Layer
- Authentication → Session management → UI controllers → Lib APIs → DB/RRD
2. Application Layer ↔ MySQL/MariaDB
- Configuration, templates, permissions, graph metadata
3. Application Layer ↔ RRDtool
- Graph definitions → RRDtool generates PNG/SVG → Served to UI
4. Poller Service → SNMP (via Net-SNMP or php-snmp) → Device → poller logic writes to RRD & DB
5. Cache Layer sits between Lib APIs and disk to reduce repeated DB/RRD calls
6. Plugins and Automation modules plug into core lib APIs

##### Key architectural patterns & principles
- Modular monolith: single deployable codebase, logically modular via `lib/`, `plugins/`, `include/`
- MVC-inspired separation: scripts (controllers/view entry points) vs. library classes (models/business logic)
- Cron/Systemd-driven workers for long-running pollers separated from HTTP requests
- Plugin framework for extensibility
- Templating for reusable graph/data source definitions

##### Elements
- Component labels: e.g., “Web Server (Apache/Nginx)”, “PHP-FPM / HTTP Entry Points”, “Application Core Libraries”, “Cron/Systemd Poller Service”, “MySQL/MariaDB”, “RRDtool Engine”, “Cache Storage”.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        Browser["User Browser"]:::external
        WebServer["Web Server (Apache/Nginx)"]:::frontend
        PHPFPM["PHP-FPM / HTTP Entry Points"]:::frontend
        Themes["Themes & Static Assets"]:::frontend
        Localization["Localization (i18n)"]:::frontend
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        CoreLibs["Application Core Libraries"]:::backend
        Plugins["Plugins Framework"]:::backend
        Automation["Automation & Discovery Modules"]:::backend
    end

    %% Poller Service Layer
    subgraph "Poller Service"
        direction TB
        Poller["Cron/Systemd Poller Service"]:::poller
        SNMPAgent["SNMP Agent & Remote Agent"]:::poller
    end

    %% Data Storage Layer
    subgraph "Data Storage & Processing"
        direction TB
        MySQL["MySQL/MariaDB Database"]:::datastore
        RRD["RRDtool Engine & RRA Files"]:::datastore
        Cache["Cache Storage"]:::datastore
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        RRDtoolBin["RRDtool Binary"]:::external
        NetSNMP["Net-SNMP Library"]:::external
        PHPModules["PHP Modules"]:::external
    end

    %% Relationships
    Browser -->|"HTTP Request"| WebServer
    WebServer -->|"FastCGI"| PHPFPM
    PHPFPM -->|"HTTP API Calls"| CoreLibs
    Themes --> PHPFPM
    Localization --> PHPFPM

    CoreLibs -->|"PHP PDO (SQL Queries)"| MySQL
    CoreLibs -->|"RRDtool CLI (Graph Render)"| RRD
    CoreLibs -->|"Cache Read/Write"| Cache
    Plugins --> CoreLibs
    Automation --> CoreLibs

    Poller -->|"Cron Trigger"| SNMPAgent
    SNMPAgent -->|"SNMP GET/SET"| NetSNMP
    SNMPAgent -->|"Data Collection"| Poller
    Poller -->|"Write RRD"| RRD
    Poller -->|"Write SQL"| MySQL
    Poller -->|"Cache Populate"| Cache

    RRDtoolBin --> RRD
    NetSNMP --> Poller
    PHPModules --> CoreLibs
    PHPModules --> SNMPAgent

    %% Click Events
    click PHPFPM "https://github.com/tharkadharshana/cacti/blob/develop/index.php"
    click CoreLibs "https://github.com/tharkadharshana/cacti/blob/develop/lib/functions.php"
    click Plugins "https://github.com/tharkadharshana/cacti/tree/develop/plugins/"
    click Automation "https://github.com/tharkadharshana/cacti/blob/develop/automation_devices.php"
    click Poller "https://github.com/tharkadharshana/cacti/blob/develop/poller.php"
    click SNMPAgent "https://github.com/tharkadharshana/cacti/blob/develop/remote_agent.php"
    click MySQL "https://github.com/tharkadharshana/cacti/blob/develop/cacti.sql"
    click RRD "https://github.com/tharkadharshana/cacti/tree/develop/rra/"
    click Cache "https://github.com/tharkadharshana/cacti/tree/develop/cache/"
    click Themes "https://github.com/tharkadharshana/cacti/tree/develop/include/themes/"
    click Localization "https://github.com/tharkadharshana/cacti/tree/develop/locales/LC_MESSAGES/"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#333,stroke-width:1px
    classDef datastore fill:#FFA500,stroke:#333,stroke-width:1px
    classDef poller fill:#DDA0DD,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
```
