Skip to main content
Question

script php pour afficher realtime services d'un host

  • Apr 28, 2022 11:59 AM UTC
  • 6 replies
  • 445 views

Forum|alt.badge.img+3

Bonjour ,

j’ai fait un script en php pour afficher l’état d’un service host avec le token,

la partie token fonctionne très bien, en revanche j’ai une erreur sur l’affichage de l’état de service cpu pour mon host

voir l’erreur

 

voici mon script 

<?php

 

// 1er appel API CENTREON pour obtenir le token de connexion.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://monserveur:port/centreon/api/index.php?action=authenticate");

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, "username=clapi-mon-poller&password=mdp");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response  = curl_exec($ch);

//echo $response;

curl_close($ch);

 

// Formatage du retour du cURL.

$response = substr("$response", 14);

//echo "<br> $response";

 

$token = substr("$response", 0, -2);

echo "<br> $token";

 

// 2eme appel API CENTREON pour obtenir le status d'un service particulier en utilisant le token obtenu.

 

$headers = array(

"Content-Type" => "application/json",

"centreon-auth-token" => "$token"

);

 

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://monserveur:port/centreon/api/index.php?object=centreon_realtime_services&action=list&limit=1&fields=state&search=CPU&searchHost=serveur1");

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_VERBOSE, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$status = curl_exec($ch);

echo "<br> $status";

curl_close($ch);

 

 

une idée sur l’erreur ou si quelqu’un a une meilleur proposition ?

6 replies

fgbetokpanou
Centreonian
Forum|alt.badge.img+12
  • Centreonian
  • Apr 28, 2022 12:07 PM UTC

Hi @idi, Please ask your problem in English. This will help all other non-french speaking users who have the same issue to benefit from the solutions that will be proposed. Thank you for your understanding


Forum|alt.badge.img+3
  • Author
  • Steward ***
  • Apr 28, 2022 12:34 PM UTC

Hello ,

I made a script in php to display the status of a host service with the token,

the token part works very well, however I have an error on the display of the cpu service status for my host

see error

here is my script

==============

<?php

 

// 1er appel API CENTREON pour obtenir le token de connexion.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://monserveur:port/centreon/api/index.php?action=authenticate");

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, "username=clapi-poller&password=mdp");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response  = curl_exec($ch);

//echo $response;

curl_close($ch);

 

// Formatage du retour du cURL.

$response = substr("$response", 14);

//echo "<br> $response";

 

$token = substr("$response", 0, -2);

echo "<br> $token";

 

// 2eme appel API CENTREON pour obtenir le status d'un service particulier en utilisant le token obtenu.

 

$headers = array(

"Content-Type" => "application/json",

"centreon-auth-token" => "$token"

);

 

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://monserveur:port/centreon/api/index.php?object=centreon_realtime_services&action=list&limit=1&fields=state&search=CPU&searchHost=serveur1");

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_HEADER, true);

curl_setopt($ch, CURLOPT_VERBOSE, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$status = curl_exec($ch);

echo "<br> $status";

curl_close($ch);

========================


Any idea what's wrong or if anyone has a better suggestion?


Forum|alt.badge.img+3
  • Author
  • Steward ***
  • Apr 29, 2022 9:39 AM UTC

Hello,

my need is to display the status of a service in real

CURL -s "http://$IP_CENTREON/centreon/api/index.php?object=centreon_realtime_services&action=list&limit=1&fields=state&search=CPU&searchHost=nomserveur" -H 'Content-Type: application/json' -H 'centreon-auth-token: '$TOKEN'' | $JQ 'length'`

 

 

 

my result is not correct

sduret
Centreonian
Forum|alt.badge.img+13
  • Centreonian
  • Apr 29, 2022 1:04 PM UTC

Hello @idi 

You can use this command to get all the real time information about your service.

curl --location --request GET 'https://IP_ADDRESS:443/centreon/api/latest/monitoring/hosts/YOUR_HOST_ID/services/YOUR_SERVICE_ID' \

--header 'X-AUTH-TOKEN: YOUR_TOKEN'

But first you will need to get the host and service IDs.


Forum|alt.badge.img+3
  • Author
  • Steward ***
  • Apr 29, 2022 3:04 PM UTC

Hello @sduret 

 
here is the error

curl --location --request GET 'http://myserveur:443/centreon/api/latest/monitoring/hosts/1589/services/8381' -H 'Content-Type: application/json' -H 'centreon-auth-token: 'zVLeK3XyUV9iWELUxnA59zFBtaPBO30jbcijN31Jv3o=''

 


sduret
Centreonian
Forum|alt.badge.img+13
  • Centreonian
  • Apr 29, 2022 3:10 PM UTC

Hello

Your server uses HTTP or HTTPS?
if it’s HTTP, you can remove “:443”

What is your current Centreon release?

rpm -qa centreon-web


Can you send us the result of this command?

grep -A2 -Ri '<LocationMatch' /opt/rh/httpd24/root/etc/httpd/conf.d/10-centreon.conf