Files
Cactidocumentation/Plugin-Remote-Collection.md
T

109 lines
4.4 KiB
Markdown

# Handle Poller Output on a Remote Data Collector
This example records a single data source named `wf_demo` in a
collector-local plugin table. It illustrates three decisions a plugin
author must make: which hook supplies the data, which installation owns
the write, and how repeated delivery affects storage.
## Choose the hook and the writer
The poller builds a batch keyed by RRD path. Each entry carries a
`local_data_id` and a `times` array mapping Unix timestamps to data-source
names and values. Cacti passes that batch to the `poller_output` function
hook. The example registers a callback in its install function:
```php
api_plugin_register_hook('workflowdemo', 'poller_output', 'workflowdemo_output', 'setup.php');
```
On a remote collector, `remote_collect:1` in `INFO` enables this hook.
The separate `remote_poller` capability applies to `poller_bottom`.
Choose `poller_output` when you need the measurements themselves.
The main server also dispatches the hook. The example checks the collector
ID and returns immediately on collector 1, which keeps the write local to
the remote instance.
```php
$collector = defined('POLLER_ID') ? POLLER_ID : (int) $config['poller_id'];
if ($collector === 1) {
return $batch;
}
```
Within the callback, return the original batch on every normal path.
Other registered callbacks can receive the value your callback returns.
## Give each measurement a stable identity
For each timestamp, the example selects the `wf_demo` field. A unique key
covers collector ID, local data ID, timestamp, and field name. The table's
primary key enforces uniqueness across concurrent connections. Replaying
the same identity updates its reading. A new timestamp produces a new row.
```php
$key = hash('sha256', implode(':', [$collector, $rrd['local_data_id'], $time, 'wf_demo']));
db_execute_prepared('INSERT INTO plugin_workflowdemo_samples
(sample_key, collector, data_id, sample_time, reading) VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE reading = VALUES(reading)',
[$key, $collector, $rrd['local_data_id'], $time, $fields['wf_demo']]);
```
The example keeps the reading as text so an unknown sample (`U`) remains
distinguishable from zero. Its table lives on the collector. A plugin
that also displays these records centrally needs a separate transfer and
reconciliation design. Declaring a hook capability does not replicate
plugin-owned rows.
## Replay the same sample on two installations
Use a disposable main installation with collector ID 1 and a remote
installation with its own Cacti database and a configured collector ID
greater than 1. See [Remote Poller Setup](How-To-Setup-Remote-Pollers.md)
for the collector configuration. Deploy the same
[example plugin](resources/workflowdemo-1.zip) on both. Confirm the
plugin tables exist and the hook is enabled in both databases:
```sql
SELECT name, hook, status FROM plugin_hooks
WHERE name = 'workflowdemo' AND hook = 'poller_output';
SELECT COUNT(*) FROM plugin_workflowdemo_samples;
```
Start with hook status 1 and zero sample rows. From the Cacti root on the
main server, run this command once. Then run it twice on the collector:
```console
php plugins/workflowdemo/replay.php
```
The CLI fixture sends one synthetic batch to the real registered-hook
dispatcher. It checks that the returned batch is unchanged. It uses local
data ID 42, timestamp 1700000000, field `wf_demo`, and reading `7`. It does
not require an RRD file or an SNMP agent.
Run this query separately against the main and collector databases:
```sql
SELECT collector, data_id, sample_time, reading, COUNT(*) AS copies
FROM plugin_workflowdemo_samples
GROUP BY collector, data_id, sample_time, reading;
```
The main database should return no rows. For collector ID 2, the collector
should return exactly `2, 42, 1700000000, 7, 1`. If the collector has no
row, inspect its local plugin registration, `INFO`, and actual collector
ID. If it has two rows for one identity, inspect the key definition. For
a plugin that consumes live devices, also poll a configured test device
and trace its actual data-source name and timestamp through the callback.
The replay exercises dispatch and persistence downstream of collection.
For comparison, see the upstream [poller implementation][poller] and
[hook capability checks][hooks].
[poller]: <https://github.com/Cacti/cacti/blob/1.2.x/lib/poller.php>
[hooks]: <https://github.com/Cacti/cacti/blob/1.2.x/lib/plugins.php>
---
Copyright (c) 2004-2026 The Cacti Group