Skip to main content

Hello people ! 

I have a csv list of hosts of which I have to change the events handler to every services affected to them.

I have that script for the moment : 

#!/bin/bash  

  

CLAPI=/usr/share/centreon/bin/centreon  

  

INPUT=/root/serverlist.csv

  

USER=admin  

  

PASS=password  

  

OLDIFS=$IFS  

  

IFS=$';'  

  

' ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }  

  

while read name

  

do  

  $CLAPI -u $USER -p $PASS -o SERVICE -a setparam -v "$name;$service;event_handler;event-handler-name"  

done < $INPUT  

  

IFS=$OLDIFS  

 

So, the problem is that I can’t find a way to get all service name from the hosts in the $service variable, every command I tried for that is giving me too much information and not only the service name.

Do you have any idea ? 

 

Thanks a lot !

Hi,

I’m not sure I fully understood your question but to get all the services from a particular host you can use this:

 

$ clapi -o service -a show | grep ';thehostname;' | cut -d';' -f4

and this:

 

$ clapi -o service -a show | grep ';thehostname;' | cut -d';' -f2,4

would give you both the hostname and service for all services of the host (this is probably what you want).


Hi,

I’m not sure I fully understood your question but to get all the services from a particular host you can use this:

 

$ clapi -o service -a show | grep ';thehostname;' | cut -d';' -f4

and this:

 

$ clapi -o service -a show | grep ';thehostname;' | cut -d';' -f2,4

would give you both the hostname and service for all services of the host (this is probably what you want).

 

Hello, thanks a lot for your answer.

When I execute that command on my centreon, I have nothing in answer, no error message, nothing being displayed.. Do you know what can cause that ? 

Moreover, where did you find that command, never saw that in clapi documentation.

 

Thank you !


Hello everyone,

Wishing you a great week, I’m permitting myself to up this topic because I still didn’t find any solution yet :


clapi was the old name of “centreon” (command line api), just use “centreon” instead of clapi, with your username and password like you did in your script

“centreon” should be in your path, but you can also use you “$CLAPI” variable, same thing.

 

For your original question : you have multiple options, for 1-shot massive change like you need, I usually dump all the services in a csv and work a bit in excel, then I do like I explained here :rename host | Community (centreon.com) with a bit of excel concatenation, then a copy/paste of the command in a big script with login/password in variables

 

there are other options of course, like using RestApi and python, but you can keep using bash with knowing how to use arrays and loops

 

For example, first : you can extract a specific list of service for a specific host with the centreon/clapi command and specifiying ‘-v “hostname;” ‘ (the semi colon is important)

no need to grep the whole service dump.

 

Then what you want is to nest a loop for each service, for each host, you can’t run clapi command on a list of services, you need to run each command individually

so :

let’s assume your hostname from the CSV is in a variable, let’s say $HOST (your script is not clear where the hostname is, maybe in $name, not sure)

you do something like this :

--- your script ater ...while
do


IFS=$'\n' my_services=( $(centreon -u $CENTUSER -p $CENTPASS -o SERVICE -a show -v "$HOST;") )
for svc in "${my_services @]}:1"
do
SVC=$( echo ${svc} | awk -F ";" '{print $4}' )
# now you have $HOST and $SVC, you can put your centreon command here
echo "$HOST -> $SVC"
done

done...
---

 

the IFS line will get the csv generate and store a bash array “my_services”, then the for loop syntax is strange but you read each instance of the array but skip the first line, I won’t enter in the syntax more, way too much google to read and i barely understand

then you awk to split the line get the 4th item in the line, the service name, and voila

 

this snippet will echo and do nothing, but the 2 variable host and svc should be sufficient for your need


clapi was the old name of “centreon” (command line api), just use “centreon” instead of clapi, with your username and password like you did in your script

“centreon” should be in your path, but you can also use you “$CLAPI” variable, same thing.

 

For your original question : you have multiple options, for 1-shot massive change like you need, I usually dump all the services in a csv and work a bit in excel, then I do like I explained here :rename host | Community (centreon.com) with a bit of excel concatenation, then a copy/paste of the command in a big script with login/password in variables

 

there are other options of course, like using RestApi and python, but you can keep using bash with knowing how to use arrays and loops

 

For example, first : you can extract a specific list of service for a specific host with the centreon/clapi command and specifiying ‘-v “hostname;” ‘ (the semi colon is important)

no need to grep the whole service dump.

 

Then what you want is to nest a loop for each service, for each host, you can’t run clapi command on a list of services, you need to run each command individually

so :

let’s assume your hostname from the CSV is in a variable, let’s say $HOST (your script is not clear where the hostname is, maybe in $name, not sure)

you do something like this :

--- your script ater ...while
do


IFS=$'\n' my_services=( $(centreon -u $CENTUSER -p $CENTPASS -o SERVICE -a show -v "$HOST;") )
for svc in "${my_services @]}:1"
do
SVC=$( echo ${svc} | awk -F ";" '{print $4}' )
# now you have $HOST and $SVC, you can put your centreon command here
echo "$HOST -> $SVC"
done

done...
---

 

the IFS line will get the csv generate and store a bash array “my_services”, then the for loop syntax is strange but you read each instance of the array but skip the first line, I won’t enter in the syntax more, way too much google to read and i barely understand

then you awk to split the line get the 4th item in the line, the service name, and voila

 

this snippet will echo and do nothing, but the 2 variable host and svc should be sufficient for your need

 

Hello,

Thanks a lot for your answer it is really good.

I don’t know but, when I do this command : centreon -u $CENTUSER -p $CENTPASS -o SERVICE -a show -v "$HOST;"

 

It only gives me this result : 

host id;host name;id;description;check command;check command arg;normal check interval;retry check interval;max check attempts;active checks enabled;passive checks enabled;activate

 

Without the result for the host I precise

Do you know what can cause that ? 


are you sure $HOST contains your host name? as I said, I don’t understand you script and where you get your host name, maybe in your script it is $name, I was not sure

try running the command manually and replace every $xxx by your values 

centreon -u jonn -p mypassw -o SERVICE -a show -v "TEST;" 

this should list you every service of the host TEST

also, add echo of your variable in your script if you are not sure


are you sure $HOST contains your host name? as I said, I don’t understand you script and where you get your host name, maybe in your script it is $name, I was not sure

try running the command manually and replace every $xxx by your values 

centreon -u jonn -p mypassw -o SERVICE -a show -v "TEST;" 

this should list you every service of the host TEST

also, add echo of your variable in your script if you are not sure

Hello,

My $name is fomr the CSV in the INPUT

And yes, I run that command manually, out of the script : 

centreon -u user -p passw -o SERVICE -a show -v "SERVERNAME;" 

And I’m still getting only this : 

host id;host name;id;description;check command;check command arg;normal check interval;retry check interval;max check attempts;active checks enabled;passive checks enabled;activate

 

Kind regards,

 

 


@Sohoshiro https://docs.centreon.com/docs/api/clapi/ 🚁🛶


Hello, 

I run the command with -v "SERVERNAME;" : i have a list of service

if I run the command with -v "SERVERNAME"  : I have an empty list with just the header like you

if I run the command with a wrong hostname, I also have an empty list 

 

so the only thing I can see is that you pass a wrong hostname, are you sure the hostname in your CSV matches exactly the hostname in centreon?

if you run this:

centreon -u xxx -p xxx -o HOST -a show > list.txt

are you finding your hostname in the file, and is it the exact same name, same upper/lower case?

 

 


Hello, 

I run the command with -v "SERVERNAME;" : i have a list of service

if I run the command with -v "SERVERNAME"  : I have an empty list with just the header like you

if I run the command with a wrong hostname, I also have an empty list 

 

so the only thing I can see is that you pass a wrong hostname, are you sure the hostname in your CSV matches exactly the hostname in centreon?

if you run this:

centreon -u xxx -p xxx -o HOST -a show > list.txt

are you finding your hostname in the file, and is it the exact same name, same upper/lower case?

 

 

Hello,

Thanks a lot for your help on this topic.

Even with or without the “;” at the end I still have an empty list :( 

And yes, I’m sure about the hostname, I tried with a lot of differents one

And it’s not about the script because I’m trying the command manually out of the script directly on the server

 


Quick question 

What's your centreon version?


Quick question 

What's your centreon version?

Hello, I have Centreon Engine 19.10.14


ah, well

what I gave you works in 22.x, not sure the syntax was working back then… (you should consider upgrading, I think 19.x is not supported anymore)

 

Anyway,you could do the brute force grep, like stéphane suggested 

 

in the IFS command, instead of 

centreon -u jonn -p mypassw -o SERVICE -a show -v "TEST;" 

you do

centreon -u jonn -p mypassw -o SERVICE -a show | grep “;TEST;”

I didn’t test it, should work 


ah, well

what I gave you works in 22.x, not sure the syntax was working back then… (you should consider upgrading, I think 19.x is not supported anymore)

 

Anyway,you could do the brute force grep, like stéphane suggested 

 

in the IFS command, instead of 

centreon -u jonn -p mypassw -o SERVICE -a show -v "TEST;" 

you do

centreon -u jonn -p mypassw -o SERVICE -a show | grep “;TEST;”

I didn’t test it, should work 

Hello, oh indeed it’s working, I have this result : 

996;TEST;8003;Disk-C;;;;;;2;2;1, and this for every services

Have to find a way to only have the “Disk-C” now ! 

Thanks a lot, don’t know why I didn’t notice that before.. 

 

And yes, I will consider about the upgrade, I need to read some doc to know how I can do that from 19, if it’s safe or no etc… If you have any information I’m not against get them haha


Reply