Adding Devices to Cacti in Bulk

Cacti LogoI recently had to add over 100 new devices into Cacti. Believe it or not, you can do this in bulk using the command-line tools provided with Cacti. I used to add_device.php, add_tree.php, and add_graphs.php scripts to get the job done.

First, I created a file (/tmp/slap) with a list of host names, one per line. From inside the cacti directory, I used the following command line to add the hosts into cacti:

for i in `cat /tmp/slap`; do
    php cli/add_device.php --description=${i} --ip=${i} --community=public --template=5;

Template 5 is the cisco router template that comes with cacti. (Note: I do not use "public" as the community string. That's just for the purpose of this example.)

The output from the command included device id numbers. In this example, we'll say that those device ids ranged from 91 to 107. I had already created my graph trees and all of these hosts were going into the same tree. I added the hosts to that tree with:

for i in {91..107} ; do
    php cli/add_tree.php --type=node --node-type=host --tree-id=8 --parent-node=51 --host-id=${i};

The tree id can either be gotten from the cacti web interface, or php cli/add_tree.php --list-trees. The parent node is a header under my tree. You can also get that node id with the web interface or using the CLI: php cli/add_tree.php --list-nodes --tree-id=<tree_id>.

The final piece of the puzzle is adding graphs to the devices. This part was the most complicated by far, and I really recommend you read the add_graphs.php documentation to get an idea of the process. To give you an idea of what can be done with this, here's what I did to add graphs to the devices. These graphs include CPU, RAM, and interface traffic, errors, and packets. The interface graphs query the device and only enable graphs for interfaces that are up.

for i in {91..107}; do
    php cli/add_graphs.php --host-id=${i} --graph-type=cg --graph-template-id=18;
    php cli/add_graphs.php --host-id=${i} --graph-type=cg --graph-template-id=35;
    php cli/add_graphs.php --host-id=${i} --graph-type=ds --snmp-query-id=1 --snmp-query-type-id=2 --snmp-field=ifOperStatus --snmp-value=Up;
    php cli/add_graphs.php --host-id=${i} --graph-type=ds --graph-template-id=22 --snmp-query-id=1 --snmp-query-type-id=2 --snmp-field=ifOperStatus --snmp-value=Up;
    php cli/add_graphs.php --host-id=${i} --graph-type=ds --graph-template-id=23 --snmp-query-id=1 --snmp-query-type-id=4 --snmp-field=ifOperStatus --snmp-value=Up;

My Bookshelf

Reading Now

Other Stuff