add node header without duplicates

Templates, scripts for templates, scripts and requests for templates.

Moderators: Moderators, Developers

Post Reply
Author
Message
localspark
Posts: 2
Joined: Sun Dec 09, 2018 6:58 am

add node header without duplicates

#1 Post by localspark » Sun Dec 09, 2018 11:32 am

hi,

i'm new to cacti, it's an amazing system.

i'm having an issue when i'm trying to automate the adding process, in the " add_tree.php --type=node --node-type=header --tree-id=1 --name=test " script it adds node headers regardless of it exists or not.

is there any solution to this? i've combed google for this with no luck

what i'm trying to do is to output just the id if the header exists, and if not to create one.

thanks for the help

netniV
Cacti Guru User
Posts: 2973
Joined: Sun Aug 27, 2017 12:05 am

Re: add node header without duplicates

#2 Post by netniV » Mon Dec 10, 2018 6:57 am

It's likely that it is allowed as you can have the same name used repeatedly throughout a tree. However, I think it would be prudent to only allow the same name once at each tree level. What version of Cacti are you using?

localspark
Posts: 2
Joined: Sun Dec 09, 2018 6:58 am

Re: add node header without duplicates

#3 Post by localspark » Tue Dec 11, 2018 3:26 am

thanks for the reply,

i'm using 1.1.38,

i tried this code

parent_node_list=$(php -q /var/lib/cacti/cli/add_tree.php --list-nodes --tree-id=1)

if grep $name <<< "$parent_node_list"; then
1>$parent_node_id //this is the part that i can't figure, i can't get the matching found header id to an stdout so the device can use the
header id

else

parent_node_id=$(php -q /var/lib/cacti/cli/add_tree.php --type=node --node-type=header --tree-id=1 --name="$name")
fi

the code adds only once the $name but i get an echo output instead of it storing the id in a variable for the device to use

netniV
Cacti Guru User
Posts: 2973
Joined: Sun Aug 27, 2017 12:05 am

Re: add node header without duplicates

#4 Post by netniV » Wed Dec 12, 2018 3:37 am

Code: Select all

sudo -u www-data php -q cli/add_tree.php --list-nodes --tree-id=1 | grep "^Header" | cut -d'	' -f 4 | grep -qw "Machine"
Notes:
  • the -d value on the cut command is a tab not spaces.
  • the last grep returns its result (-q) which will be the process exit result ($? under most shells) and searches for the full name (-w)
  • to make grep return a count, change the -q to a -c and then capture the output (the $? result will then be sudo's execution status)

Post Reply