Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = [
"phpunit-integration.xml",
"locales/po/**",
"locales/LC_MESSAGES/**",
"templates/**",
]
precedence = "aggregate"
SPDX-FileCopyrightText = "2026 Thomas Vincent <thomasvincent@gmail.com>"
Expand Down
6 changes: 3 additions & 3 deletions docs/PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ The mapping, decided in [ADR 0010](decisions/0010-scope-devices-with-resource-da

| Object | Created from | Notes |
|---|---|---|
| Scope device | "Cloud Scope" device template; each device created with availability method None (`AVAIL_NONE`, 0) and the account's poller, because a Cacti 1.2 device template has no availability field | One per account and scope: a DigitalOcean team, an AWS account and region, a Google Cloud project, an Azure subscription and region |
| Scope device | A device template per provider, for example "Cloud: DigitalOcean Scope", carrying that provider's data queries; each device created with availability method None (`AVAIL_NONE`, 0) and the account's poller, because a Cacti 1.2 device template has no availability field | One per account and scope: a DigitalOcean team, an AWS account and region, a Google Cloud project, an Azure subscription and region |
| Resource data query | Script server data query per resource type, for example "Cloud: DigitalOcean Droplets" | Indexes are provider resource IDs; descriptions are resource names; reindexing follows Cacti's normal behavior |
| Data sources and graphs | Data and graph templates per resource type, generated from the metric catalog | Polled through the script server; reach Thold through `poller_output` |
| Data sources and graphs | A data template and a graph template per catalog graph, generated from the metric catalog | A graph's data source holds only that graph's metrics, so creating a graph collects nothing else ([ADR 0005](decisions/0005-collect-only-what-is-graphed.md)); polled through the script server; reach Thold through `poller_output` |

This follows plugin_cloudflare's pattern of one account device with resources as data query indexes. It keeps device counts proportional to accounts, not to resources, and Cacti's reindexing handles resources that come and go. The cost is that Maint suppresses a whole scope, not one resource, and a virtual machine's cloud graphs do not sit on the Cacti device that polls the same machine over SNMP. Mapping resources onto existing devices is a research issue.

Templates ship as Cacti template XML in the plugin and are imported with `import_xml_data()` at install and upgrade, matched by hash so re-imports update rather than duplicate.
Templates ship as Cacti template XML in `templates/`, generated from the catalog by `php tests/bin/generate-templates.php templates`; a unit test fails when the shipped files and the catalog disagree. Install and upgrade import them with `import_xml_data()` when the package changed or an object is missing, matched by fixed hashes so re-imports update rather than duplicate. Each data query definition is copied into `<path_cacti>/resource/script_server/`, because Cacti 1.2 refuses a data query file outside `<path_cacti>/resource`; uninstall leaves the templates and those files, because devices and graphs may still use them.

## Collection and measurement state

Expand Down
47 changes: 47 additions & 0 deletions lib/DigitalOceanCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,46 @@ final class DigitalOceanCatalog {
'dolb_queue' => [self::LOAD_BALANCER, 'droplets_queue_size', [], 'single', false],
];

/**
* The data source name of each resource type's collection age, which the
* script server computes from the cache (ADR 0006); no API request reads it.
*/
public const AGE = [self::DROPLET => 'do_age', self::LOAD_BALANCER => 'dolb_age'];

/**
* Graph templates: key => [type, title, vertical label, upper limit or
* null, data source names]. Each graph gets its own data template, so a
* graph collects only its own metrics (ADR 0005). Every metric and age
* name appears in exactly one graph; a template test checks it.
*
* @var array<string, array{0: string, 1: string, 2: string, 3: int|null, 4: list<string>}>
*/
public const GRAPHS = [
'do_cpu' => [self::DROPLET, 'CPU', 'percent', 100, ['do_cpu_pct']],
'do_bandwidth' => [self::DROPLET, 'Bandwidth', 'bits per second', null, [
'do_bw_pub_in', 'do_bw_pub_out', 'do_bw_prv_in', 'do_bw_prv_out',
]],
'do_load' => [self::DROPLET, 'Load Average', 'load', null, ['do_load_1', 'do_load_5', 'do_load_15']],
'do_memory' => [self::DROPLET, 'Memory', 'bytes', null, [
'do_mem_total', 'do_mem_avail', 'do_mem_free', 'do_mem_cached',
]],
'do_age' => [self::DROPLET, 'Collection Age', 'seconds', null, ['do_age']],
'dolb_connections' => [self::LOAD_BALANCER, 'Connections', 'connections', null, ['dolb_conn_cur', 'dolb_conn_lim']],
'dolb_cpu' => [self::LOAD_BALANCER, 'CPU', 'percent', 100, ['dolb_cpu_pct']],
'dolb_http_rps' => [self::LOAD_BALANCER, 'HTTP Requests', 'requests per second', null, ['dolb_http_rps']],
'dolb_http_resp' => [self::LOAD_BALANCER, 'HTTP Responses', 'responses', null, ['dolb_http_resp']],
'dolb_throughput' => [self::LOAD_BALANCER, 'Throughput', 'bytes per second', null, [
'dolb_http_Bps', 'dolb_tcp_Bps', 'dolb_udp_Bps', 'dolb_nlbtcp_Bps', 'dolb_nlbudp_Bps',
]],
'dolb_health' => [self::LOAD_BALANCER, 'Health Checks', 'status', null, ['dolb_hc_status']],
'dolb_downtime' => [self::LOAD_BALANCER, 'Downtime', 'status', null, ['dolb_downtime']],
'dolb_response' => [self::LOAD_BALANCER, 'Response Time', 'seconds', null, ['dolb_resp_avg_s', 'dolb_resp_p95_s']],
'dolb_backend' => [self::LOAD_BALANCER, 'Droplet Connections', 'connections', null, [
'dolb_back_conn', 'dolb_queue',
]],
'dolb_age' => [self::LOAD_BALANCER, 'Collection Age', 'seconds', null, ['dolb_age']],
];

private const FIELDS = ['type', 'path', 'query', 'convert', 'agent'];

/**
Expand All @@ -93,6 +133,13 @@ public static function metric(string $name): ?array {
return isset(self::METRICS[$name]) ? self::named(self::METRICS[$name]) : null;
}

/**
* @return list<string> every metric name of the catalog, in catalog order
*/
public static function names(): array {
return array_keys(self::METRICS);
}

/**
* Metrics read by default for a resource type: those that need no agent.
*
Expand Down
142 changes: 142 additions & 0 deletions lib/TemplateRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/*
* SPDX-FileCopyrightText: 2026 Thomas Vincent <thomasvincent@gmail.com>
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of plugin_cloud, a plugin for Cacti.
* https://github.com/somethingwithproof/plugin_cloud
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*/
namespace Somethingwithproof\Cloud;

/**
* The database side of the shipped Cacti templates: the import lock, the
* checksum of the last imported package, and the imported objects, found by
* the fixed hashes tests/bin/generate-templates.php gives them (ADR 0010).
*/
final class TemplateRepository {
private const SETTING = 'cloud_templates_sha256';
private const LOCK = 'plugin_cloud_templates';

public function __construct(private readonly \PDO|false $connection) {
}

/**
* Two processes importing at once would create every object twice. The
* lock is per connection, so it ends with the request at the latest.
*/
public function lock(int $timeout): bool {
$locked = db_fetch_cell_prepared('SELECT GET_LOCK(?, ?)', [self::LOCK, $timeout], '', true, $this->connection);

return (string) $locked === '1';
}

public function unlock(): void {
db_execute_prepared('DO RELEASE_LOCK(?)', [self::LOCK], true, $this->connection);
}

public function recordedChecksum(): string {
$value = db_fetch_cell_prepared(
'SELECT value FROM settings WHERE name = ?',
[self::SETTING],
'',
true,
$this->connection
);

return is_string($value) ? $value : '';
}

public function recordChecksum(string $sha256): void {
db_execute_prepared(
'REPLACE INTO settings (name, value) VALUES (?, ?)',
[self::SETTING, $sha256],
true,
$this->connection
);
}

public function defaultProfile(): int {
$id = db_fetch_cell_prepared(
"SELECT id FROM data_source_profiles WHERE `default` = 'on'",
[],
'',
true,
$this->connection
);

return is_numeric($id) ? (int) $id : 0;
}

/**
* Every object the package defines, as table => keys. A missing one means
* the import failed or an administrator deleted it.
*
* @return array<string, list<string>>
*/
public static function objects(): array {
$objects = [
'host_template' => ['host_template/digitalocean'],
'snmp_query' => [],
'data_template' => [],
'graph_templates' => [],
'snmp_query_graph' => [],
];

foreach ([DigitalOceanCatalog::DROPLET, DigitalOceanCatalog::LOAD_BALANCER] as $type) {
$objects['snmp_query'][] = 'data_query/' . $type;
}

foreach (array_keys(DigitalOceanCatalog::GRAPHS) as $key) {
$objects['data_template'][] = 'data_template/' . $key;
$objects['graph_templates'][] = 'graph_template/' . $key;
$objects['snmp_query_graph'][] = 'data_query_graph/' . $key;
}

return $objects;
}

/**
* Cacti identifies template objects by 32 hex digits; a truncated SHA-256
* of a fixed key gives each object a stable one. Not a security control.
*/
public static function hash(string $key): string {
return substr(hash('sha256', 'plugin_cloud/' . $key), 0, 32);
}

/**
* @return list<string> the keys of package objects not in the database
*/
public function missing(): array {
$missing = [];

// The table names come from objects(), never from input.
foreach (self::objects() as $table => $keys) {
$hashes = array_map(self::hash(...), $keys);
$found = db_fetch_assoc_prepared(
'SELECT hash FROM `' . $table . '` WHERE hash IN (' . implode(', ', array_fill(0, count($hashes), '?')) . ')',
$hashes,
true,
$this->connection
);
$have = array_column(is_array($found) ? $found : [], 'hash');

foreach ($keys as $i => $key) {
if (!in_array($hashes[$i], $have, true)) {
$missing[] = $key;
}
}
}

return $missing;
}
}
Loading
Loading