Solved

Massive import

  • 8 March 2022
  • 7 replies
  • 831 views

Userlevel 1
Badge +7

Hello the community !

I’ve learned how to use CLAPI commands to make a massive import of hosts, withe following line :

centreon -u [username] -p [password] -i ImportHost.csv

ImportHost.csv is created with the following format :
HOST;ADD;Device;Alias;IP;Template(s);Poller;HostGroup

 

Now, I’d like to remove massively devices from a hostGroup. The CLAPI is given below :
centreon -u [username] -p [password] -o HG -a delmember -v "HostGroup;Device"

 

Do you know the way to create a file with the list of devices to remove from the hostGroup, in order to execute the action with a unique command ?

(something like centreon -u [username] -p [password] -i DeviceToRemoveFromTheHostGroup.csv)

Thanks,

Pierre

icon

Best answer by Wisonic 14 March 2022, 11:04

View original

7 replies

Userlevel 1
Badge +4

I don’t think you can do it directly with a file but you can make a script that will do the same thing.

you file should looks like this (1 device per line) :
device1

device2

device3

….

script below :

#!/bin/bash

username=$1
password=$2
hostgroup=$3
file=$4

if [[ "x$username" == "x" ]]; then
echo "error : username not found"
echo "Usage : ./<script_name> <username> <password> <hostgroup> <file>"
exit 1
fi

if [[ "x$password" == "x" ]]; then
echo "error : password not found"
echo "Usage : ./<script_name> <username> <password> <hostgroup> <file>"
exit 1
fi

if [[ "x$hostgroup" == "x" ]]; then
echo "error : hostgroup not found"
echo "Usage : ./<script_name> <username> <password> <hostgroup> <file>"
exit 1
fi

if [[ "x$file" == "x" ]]; then
echo "error : file not found"
echo "Usage : ./<script_name> <username> <password> <hostgroup> <file>"
exit 1
fi

while read ligne
do
centreon -u $username -p $password -o HG -a delmember -v "$hostgroup;$line"
done < $file
exit 0

 

Userlevel 1
Badge +7

Thanks Wisonic, I’m checking your solution and I’ll send you a feedback

Userlevel 6
Badge +19

Wisonic solution is correct. 

You have to do several commands and script around it as it’s not possible to remove several member from a Hostgroup with a single command. 

Userlevel 1
Badge +4

I made a typo, it's not

while read ligne

but

while read line

 

Userlevel 1
Badge +7

Hello,

I’m newer on Centreon, and I really don’t know how to write a script :thinking:

Perhaps can you help me, or indicate where I can find a tutorial on it ?

Thanks,

Userlevel 1
Badge +4

1) connect to the terminal that hosts your Centreon
2) create a file (I advise you to create a folder where you will put all your Centreon scripts)
mkdir -p /usr/share/centreon/script/ #create folder

vi /usr/share/centreon/script/delete_device_from_hostgroup.sh #create file

3) paste the script in the file

check how to do that : https://staff.washington.edu/rells/R110/
4) make the file executable

chmod 755 /usr/share/centreon/script/delete_device_from_hostgroup.sh
5) run the file

/usr/share/centreon/script/delete_device_from_hostgroup.sh <user> <password> <hostgroup> <file>

(change the words between "<>" by the true values)

Userlevel 1
Badge +7

Thank you Wisonic, I just applied your code, and it works exactly as I expected !

Reply