Mar 29, 2012

Bash script : cacti add graph to tree

I had to add much graph to graph trees on cacti over 80 nodes.
So I made the following script. I hope this will help you.

Please edit awk pattern phrases like "/host pattern/" as your situation.

#!/bin/bash
#
#   CopyLeft 2012 Joongjin, Bae
#
#

CACTI_CLI_DIR=/var/www/cacti/cli

cd $CACTI_CLI_DIR

HOST_ID_LIST=`php -q add_tree.php --list-hosts | awk '/host pattern/ { print $1}'`
TREE_ID=`php -q add_tree.php --list-trees | awk '/graph tree name/ { print $1}'`
PARENT_ID_LIST=`php -q add_tree.php --list-nodes --tree-id=$TREE_ID | awk '/^Header/ {print $0}' | awk '/graph tree header node pattern/ {print $2}'`
TARGET_GRAPH_ID_LIST=()

for host_id in $HOST_ID_LIST
do
  TARGET_GRAPH_ID_LIST=("${TARGET_GRAPH_ID_LIST[@]}" "`php -q add_tree.php --list-graphs --host-id=$host_id | awk '/add target graph pattern/ { print $1 "\n"}'`")
done

idx=0

for parent_id in $PARENT_ID_LIST
do
  for graph_id in ${TARGET_GRAPH_ID_LIST[$idx]}
  do
    php -q add_tree.php --type=node --node-type=graph --tree-id=$TREE_ID --parent-node=$parent_id --graph-id=$graph_id
  done
  idx=$((idx+1))
done

echo Mission Completed!