Files
Cactidocumentation/Plugin-Creating-Plugins.md
T
2026-12-03 00:15:32 -08:00

110 lines
4.1 KiB
Markdown

# Creating a plugin
Build and test your plugin in a disposable Cacti installation. The
[reference plugin](Plugin-Reference.md) shows the smallest installable
example. The [downloadable example](Plugin-Guidelines.md#plugin-namespaces)
also registers a tab, a page, and a permission realm.
## Directory Structure
Choose a short lowercase directory name, such as `example`. Place the
directory directly under Cacti's `plugins/` directory. The result should
contain `plugins/example/setup.php` and `plugins/example/INFO`. Use the same
name for the `INFO` entry and the prefix of the setup functions.
## Setup File
Define the plugin's lifecycle functions in `setup.php`. Cacti loads this file
when it discovers or manages the plugin. Keep page output and database changes
inside the callbacks that need them.
## INFO File
Put the plugin metadata under `[info]`. Supply the directory name, display
name, version, author, homepage, and Cacti compatibility. Add `requires` for
plugin dependencies, `capabilities` for remote collector behavior, and
`nosync` for synchronization exclusions when applicable. See the field
definitions and examples in [Plugin Development](Plugin-Development.md).
## Mandatory functions
Cacti calls the version and installation functions by name. Replace
`PLUGINNAME` in the headings below with the plugin's directory name.
### plugin_PLUGINNAME_version
Return the metadata array from the `[info]` section. The installer reads
`longname`, `author`, and `version` from this array.
```php
function plugin_example_version() {
$info = parse_ini_file(__DIR__ . '/INFO', true, INI_SCANNER_RAW);
return $info['info'];
}
```
### plugin_PLUGINNAME_install
Register hooks and realms here. If the plugin needs database tables, create
them with Cacti's plugin database helpers. Make installation safe to retry.
A successful install callback can return without a value. On the current
branch, returning `false` or throwing an exception signals installation
failure.
```php
function plugin_example_install() {
api_plugin_register_hook('example', 'top_graph_header_tabs', 'example_show_tab', 'setup.php');
api_plugin_register_realm('example', 'example.php', 'View Example', true);
}
```
The downloadable example supplies `example_show_tab()` and the authenticated
page `example.php`. Install and enable the plugin to make its tab available to
users with the **View Example** realm.
On Cacti 1.2.x, the enabled plugin shows **Active**.
![Enabled example](images/plugin-example-management.png)
## Optional functions
### plugin_PLUGINNAME_check_config
Define this callback if the plugin needs settings before it can run. Return
`true` when it is ready and `false` when an administrator still needs to
configure it. Cacti treats an absent callback as ready. An empty callback
returns `null` and prevents the plugin from passing the check.
### plugin_PLUGINNAME_uninstall
Use this callback for plugin-specific cleanup. Cacti also processes the schema
changes it recorded through its plugin database API. Tell administrators which
data uninstalling will remove and how to back it up.
### Registering Hooks
The arguments to `api_plugin_register_hook()` identify the plugin, hook,
callback, and callback file relative to the plugin directory. Cacti loads
that file when it invokes the callback. Use the hook name and argument list
from the [Hook API Reference](Plugin-Hook-API-Ref.md) and its core call site.
Cacti registers `config_arrays`, `config_form`, and `config_settings` as
enabled by default so administrators can configure a newly installed plugin.
An optional fifth argument enables a hook at registration.
### Registering Realms
Pass the plugin name, page filename, and permission label to
`api_plugin_register_realm()`. A comma-separated filename list can share one
realm. The fourth argument controls whether the administrator receives the
realm at registration. Grant access to other users or groups explicitly.
Load Cacti's authentication bootstrap on the plugin page. When drawing its
tab, check `api_plugin_user_realm_auth('example.php')`. Test the page with
an account that has the realm and one that lacks it.
---
Copyright (c) 2004-2026 The Cacti Group