Skip to main content

Hi =) 

I just upgraded from 24.10.2 to 24.10.3 last week, and I just found a change of behavior of the CLAPI.

 

I think I’m “stretching” the use of the CLAPI and maybe I’m not following best practices, so feel free to give me feedback on how you would do those tasks 


When I create a new project / new VMs in an existing project, I like to check if the host already exists via this CLAPI, to only apply my “auto_checks_conf” to newcommers and not get an error message like “This host already exists”
I used to check with commands like this one : 

centreon -u admin -p '*****' -o HOST -a show -v "{{ Hostname }}"

What I used to get as a return when there were no existing hosts : 

id;name;alias;address;activate

What I have now :

id;name;alias;address;activate
Return code end :

Here is a more complete show of what I try to do in the end (ansible, I’d understand to not have help on this part as this is not directly related to centreon) : 

 - name: check si host existe
shell: centreon -u admin -p '*****' -o HOST -a show -v "{{ Hostname }}"
delegate_to: centreon
register: command_output

- name: creation du host (si résultat précédent vide)
shell: centreon -u admin -p '*****' -o HOST -a add -v "{{ Hostname }};;{{ ipHost }};{{ templateHost }};Central;"
delegate_to: centreon
when: command_output.stdout == "id;name;alias;address;activate"

I check mainly if hosts and hostgroups exists, and i try to compare the stdout with a defined string that is returned, because the return wasn’t empty.

Is there a more elegant way to do so, that would be future proof as we can see the output can change ? 

Thanks in advance, and if anyone is automating the deployment of supervision, feel free to respond here or talk about it further in dm =) 

Just a note, i found a “workaround ”, not a solution : 


I now have a variable called “nbLignes”, at the beginning of my scripts, and I assign it the exact number of lines expected for an empty return of the commands “show”

Instead of comparing the output of the command to a defined string, i now count the number of lines of this output, and only make my actions when this number of lines equals my variable.

This way, even if the behavior changes back, I just have to edit the value of this variable and the scripts will work =) 

Still, i feel like this is a “fraud”, maybe there are more or different options if I try to use the API v2 instead of the CLAPI ? 
If anyone is currently using some similar scripts / jobs, feel free to DM me or post here your solutions / workaround on this kind of issue =) 

Thanks for your time, see you

EDIT : I also noted that this new line, “Return code end :” , is always present, not only on empty returns. Maybe the title isn’t a perfect match, since the behavior changed on every returns and not juste empty ones, but i didn’t find how to edit the title


Reply