Skip to content

Latest commit

 

History

History
492 lines (352 loc) · 17 KB

Command-Line-Scripts.md

File metadata and controls

492 lines (352 loc) · 17 KB

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_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.
copy_user.php Create a local user from another local account.
add_datasource.php Create a device data source from a data template.
add_group.php Create a user group or assign group permissions.
add_group_perms.php Set object permissions for a user group.
add_site.php Create a site and optionally associate matching devices with it.
apply_automation_rules.php Apply automation rules to selected devices.
clone_device_template.php Clone components of a device template.
host_update_template.php Update devices from their device template.
remove_device.php Remove devices selected by ID, description, or IP address.
remove_graphs.php Remove graphs matching the supplied device and graph criteria.

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.
audit_graph_template_inputs.php Report integrity problems with graph template inputs.
batchgapfix.php Fill gaps in RRD data over the selected time range.
float_rrdfiles.php Adjust a range of values in selected RRD files.
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 Compatibility entry point. It performs no rotation.
removespikes.php Remove spikes from selected RRD files.
reorder_data_query.php Reorder data queries on selected devices.
rrdresize.php Resize RRD files to match data template settings.
show_perms.php Report the database privileges the utility finds.
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.
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.
push_out_hosts.php Legacy entry point. Use rebuild_poller_cache.php for cache rebuilds.
remove_broken_graphs.php Find and remove broken graphs.

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.
splice_rrd.php Merge RRD files or resample data into another archive layout.
fix_mediumint.php Widen integer columns that have outgrown their original range.
migrate_poller.php Move devices from one data collector to another.

Install/Upgrade Scripts

Script Purpose
import_package.php Import a signed Cacti package.
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.
fetch_plugins.php Fetch official Cacti plugins and prepare them for installation.
genkey.php Generate package author key and certificate information.
genmanifest.php Generate a package manifest for a directory of packages.
import_template.php Import templates from XML.
plugin_manage.php Install, enable, disable, or uninstall plugins.
version.php Display Cacti version information.

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.

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.

On develop, -e sets the email address and -f sets the full name. Keep the command output private during account provisioning.

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 Device 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 Configured 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 with a Device

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

List Devices

php add_data_query.php --list-hosts

List 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 with a Device

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

List Devices for a Graph Template

php add_graph_template.php --list-hosts

List Available Graph Templates

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"

On the tested development revision this command writes the association, then stops with an undefined object_cache_get_totals() error before automation completes. Check Associated Graph Templates on the device before retrying. Use the device edit page to add new associations on affected installations.

Add a Data Source to a Device

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

On the tested develop revision, the field lookup returns an empty list even when interface records are cached. Value lookup and indexed CLI creation can then report an unknown field. Use the console's Create Graphs for this Device workflow if your installation shows this behavior.

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

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 on a Device

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 for Permission Entries

php add_perms.php --list-trees

List Graph Templates

php add_perms.php --list-graph-templates

List Graphs for Permission Entries

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

Add Object Permissions for a User

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