Files
Cactidocumentation/Command-Line-Scripts.md
T

15 KiB

Command Line Scripts (CLI)

Run the examples from the cli/ directory of your Cacti installation with PHP CLI and access to that installation's configuration. For commands that write RRD files, use the account that normally runs the poller.

The examples use shell variables for installation-specific IDs. First run the corresponding list command, then set the variable to the numeric ID from your installation. For example, if the device you want is listed as ID 11, run device_id=11 in that shell. Identifiers for devices, templates, graphs, queries, users, and tree nodes are separate.

Automation Scripts

Script Purpose
add_device.php Create a device and set its polling and SNMP options.
change_device.php Change an existing device's settings.
add_data_query.php Associate a data query with a device and set its reindex method.
add_datasource.php Create a device data source from a data template.
add_graph_template.php Associate a graph template with a device.
add_graphs.php Create graphs from graph templates or indexed data queries.
add_perms.php Add object permissions to a user's graph, device, tree, or template policy.
add_tree.php Create trees and add headers, devices, or graphs to them.
apply_automation_rules.php Apply automation rules to selected devices.
remove_device.php Remove devices selected by ID, description, or IP address.
plugin_manage.php Install, enable, disable, or uninstall plugins.
copy_user.php Create a local user from another local account.

Maintenance Scripts

Script Purpose
input_whitelist.php Review and manage approved data input methods.
poller_graphs_reapply_names.php Recalculate suggested titles for selected graphs.
poller_reindex_hosts.php Refresh data query indexes for selected devices.
analyze_database.php Refresh the database optimizer's table statistics.
batchgapfix.php Fill gaps in RRD data over the selected time range.
float_rrdfiles.php Adjust a range of values in selected RRD files.
host_update_template.php Update devices from their device template.
poller_data_sources_reapply_names.php Recalculate suggested names for selected data sources.
poller_replicate.php Synchronize data with remote data collectors.
refresh_csrf.php Rotate the configured CSRF secret during a maintenance window.
remove_broken_graphs.php Find and remove broken graphs.
remove_graphs.php Remove graphs matching the supplied device and graph criteria.
removespikes.php Remove spikes from selected RRD files. See Spike Kill.
reorder_data_query.php Reorder data queries on selected devices.
update_heartbeat.php Update the heartbeat in RRD files and the Cacti database.

Repair Scripts

Script Purpose
poller_output_empty.php Process pending poller output. This is a legacy recovery utility.
audit_graph_template_inputs.php Report integrity problems with graph template inputs.
push_out_hosts.php Legacy entry point. Use rebuild_poller_cache.php for cache rebuilds.
rebuild_poller_cache.php Rebuild the commands used to poll data sources.
repair_database.php Check and repair database tables.
repair_graphs.php Repair damaged graph records.
repair_templates.php Repair damaged template records.

Repair/Migration Scripts

Script Purpose
audit_database.php Compare the database schema with the Cacti schema and optionally repair differences.
convert_tables.php Convert database tables to the selected storage engine, character set, and collation.
fix_mediumint.php Widen integer columns that have outgrown their original range.
splice_rrd.php Merge RRD files or resample data into another archive layout.

Install/Upgrade Scripts

Script Purpose
import_package.php Import a signed Cacti package.
import_template.php Import templates from XML. See Template Import.
install_cacti.php Install Cacti from an initialized schema.
md5sum.php Create or verify an MD5 checksum file.
sqltable_to_php.php Generate a PHP table definition from an existing database table.
structure_rra_paths.php Migrate RRD files from a flat directory to device-specific paths.
upgrade_database.php Run database upgrade steps for this Cacti installation.

Rebuild Poller Cache

The cache stores the commands the collector runs for each data source. Inspect a device's entries in Console > Utilities > System Utilities > View Poller Cache. If the commands are stale after a data input or template change, rebuild the affected device and inspect its entries again.

php rebuild_poller_cache.php --host-id="$device_id" --debug

Omitting the device filter broadens the rebuild. Schedule a full rebuild outside a polling cycle. Debug output shows the work being done.

Re-Index Hosts

Reindexing refreshes the items a data query discovers, such as interfaces or mounted filesystems. The reindex method on the device's query association determines when Cacti automatically checks those items.

Table 20-1. Re-Index Methods

Method What triggers reindexing
None (0) The association has no automatic reindex trigger.
Uptime Goes Backwards (1) The device reports a lower uptime than on the previous check.
Index Count Changed (2) The number of indexed items changes.
Verify All Fields (3) Cacti detects a change in the query's monitored index fields.

Rediscover the selected query on one device after its interfaces change:

php poller_reindex_hosts.php --id="$device_id" --qid="$query_id" --debug

To reindex that query across matching devices, use --id=all and --host-descr='router%'. The description filter accepts SQL wildcards. Omitting --qid includes all associated queries.

Empty Poller Output Table

The collector stages readings in poller_output before Cacti processes them for RRDtool. A nonempty table during a polling cycle can be normal. If readings remain after polling finishes, check the poller log for the cause, such as an interrupted process. Once normal polling is idle, this legacy recovery utility processes the outstanding rows:

php poller_output_empty.php

The script prints the number of RRD updates processed. A count of zero is expected for an empty queue. It uses the poller's output processing code.

Reapply Suggested Names to Graphs

Changing the suggested title on a data query can leave existing graph titles unchanged. Select a device and a distinctive part of its current graph title or template name. Inspect the resulting titles in Graph Management.

php poller_graphs_reapply_names.php --host-id="$device_id" --filter='Traffic' --debug

The filter is a substring match. --debug prints progress while applying the change. See associated graph templates for the suggested-value settings.

Copy Local Cacti Users

Copy a local template account to create another local user. The first argument is the existing username and the second is the new username.

php copy_user.php guest cli-reader

Check the new account, sign-in settings, and permissions under Console > Configuration > Users before giving it to the user. See User Management for account and realm settings.

Add a New Device

The script uses host in option names for what the console calls a device. Use the description to identify the device in the console and the address or DNS name for polling.

List all Host Templates

php add_device.php --list-host-templates

Choose a device template by its local ID. Use --template=0 to start without a device template.

List all Community Strings

php add_device.php --list-communities lists configured SNMP communities. Its output can contain credentials. Run it in a private terminal when needed.

Create a New Device

This example creates a disabled device at a documentation address. It uses SNMP version zero so the fixture requires no SNMP credentials.

php add_device.php --description='CLI example' --ip=192.0.2.10 \
  --template=0 --version=0 --disable=1

For a monitored device, use its real address, select the matching template and SNMP settings, and enable polling. Record the new device ID.

Associate a Data Query to an existing Host

An association tells Cacti which query to run on a device. Choose the device and query before setting the reindex method.

List all Hosts

php add_data_query.php --list-hosts

List all Data Queries

php add_data_query.php --list-data-queries

Add a Data Query

php add_data_query.php --host-id="$device_id" --data-query-id="$query_id" \
  --reindex-method=fields

The method fields corresponds to Verify All Fields. Open the device and check Associated Data Queries for the selected query.

Associate a Graph Template to an existing Host

Use a device template to share graph template associations across devices. For a single device, select the IDs and add the association directly.

List all Hosts for a graph template

php add_graph_template.php --list-hosts

List all Graph Template

php add_graph_template.php --list-graph-templates

Add a Graph Template

php add_graph_template.php --host-id="$device_id" --graph-template-id="$graph_template_id"

The template now appears among the device's associated graph templates. Automation may already have created the graph. Check Graph Management before creating it separately.

Add a Data Source to an existing Host

Choose a data template in the console and record its ID. Data template IDs and graph template IDs identify different objects.

Add a Data Source

php add_datasource.php --host-id="$device_id" --data-template-id="$data_template_id"

Check the resulting entry under Data Sources. Use this command when you need a standalone data source. Creating a graph from a template can also create its required data sources.

Add a New Graph

Use --graph-type=cg for a graph created directly from a graph template. Use --graph-type=ds for graphs created from indexed data query results.

List Options for Associated Graph Templates

php add_graphs.php --list-hosts
php add_graphs.php --list-graph-templates
php add_graphs.php --list-graph-templates --host-template-id="$device_template_id"
php add_graphs.php --list-input-fields --graph-template-id="$graph_template_id"

The optional device template filter narrows the list. Check the input fields if the template needs values supplied at graph creation.

List Options for Associated Data Queries

php add_graphs.php --list-snmp-queries
php add_graphs.php --list-query-types --snmp-query-id="$query_id"
php add_graphs.php --list-snmp-fields --host-id="$device_id" --snmp-query-id="$query_id"
php add_graphs.php --list-snmp-values --host-id="$device_id" \
  --snmp-query-id="$query_id" --snmp-field=ifName

Use the field and values returned for the selected device. An empty list can mean the query has not yet discovered any items. Verify its query association and indexed results before attempting to create graphs.

Add Non-Indexed Graphs

php add_graphs.php --graph-type=cg --host-id="$device_id" \
  --graph-template-id="$graph_template_id"

Record the reported local graph ID for tree placement and permissions. For a template with custom inputs, consult --list-input-fields and the script's --input-fields option.

Add Indexed Graphs

Table 20-2. Parameters required for an Interface Traffic Graph

Value Where to find it for an interface traffic graph
Device ID The device with discovered interfaces.
Query ID The ID of SNMP - Interface Statistics.
Query type ID The traffic graph type offered by that query.
Graph template ID The graph template for the selected query type.
Index field and value A discovered field and value, such as ifName and eth0.
php add_graphs.php --graph-type=ds --host-id="$device_id" \
  --graph-template-id="$traffic_template_id" --snmp-query-id="$query_id" \
  --snmp-query-type-id="$query_type_id" --snmp-field=ifName --snmp-value=eth0

Replace eth0 with a value from your device. A field such as operational status can match many interfaces and create several graphs. Check Graph Management for the device and interface you intended.

Add Items to a Tree

A tree can contain headers, devices, and individual graphs. A tree ID identifies the tree. A parent node ID identifies a placement inside it.

List Hosts

php add_tree.php --list-hosts

List Trees

php add_tree.php --list-trees

List Nodes

php add_tree.php --list-nodes --tree-id="$tree_id"

List Graphs for given Hosts

php add_tree.php --list-graphs --host-id="$device_id"

Use the local graph ID from this list to place an individual graph.

Add a new Tree

php add_tree.php --type=tree --name='CLI inventory' --sort-method=manual

Record the new tree ID. The available sort modes are manual, alpha, natural, and numeric.

Add a new Header Node to a Tree

php add_tree.php --type=node --node-type=header --tree-id="$tree_id" --name=Lab

Use the new header's node ID as parent_id. Omit --parent-node to add at the root, or supply an existing header ID to nest the new item.

Add a new Host Node to a Tree

php add_tree.php --type=node --node-type=host --tree-id="$tree_id" \
  --parent-node="$parent_id" --host-id="$device_id" --host-group-style=1

Device grouping style 1 groups by graph template and 2 by data query index.

Add a new Graph Node to a Tree

php add_tree.php --type=node --node-type=graph --tree-id="$tree_id" \
  --parent-node="$parent_id" --graph-id="$graph_id"

Run --list-nodes again to verify the placement and parent.

Add Graph Permissions

add_perms.php adds an object entry to a user's graph, device, tree, or graph-template permission policy. Check the user's existing policy in User Management to determine the effect on access.

List Users

php add_perms.php --list-users

List Trees who accept permissions

php add_perms.php --list-trees

List Graph Templates

php add_perms.php --list-graph-templates

List Graphs for given Hosts where permissions can be set

php add_perms.php --list-graphs --host-id="$device_id"

Add Graph Permissions to specific Users

Choose the object type and the ID from its matching list. These examples show independent entries. Run only the ones intended for the account.

php add_perms.php --user-id="$user_id" --item-type=host --item-id="$device_id"
php add_perms.php --user-id="$user_id" --item-type=tree --item-id="$tree_id"
php add_perms.php --user-id="$user_id" --item-type=graph_template --item-id="$graph_template_id"
php add_perms.php --user-id="$user_id" --item-type=graph --item-id="$graph_id"

Check the account's object permissions and verify the result as that user.


Copyright (c) 2004-2026 The Cacti Group