Solved

Centreon API - post request to set downtime NOT working

  • 29 November 2023
  • 2 replies
  • 126 views

Badge +1

Dear Centreon Community members,
 

I'm struggling with the centreon API usage for a specific task: "setting a downtime" 
 

1. I'm able to use the basic auth to get a token

1. use this token to do several API calls like checking the API version, get host id, etc.

1. BUT when I try to set a downtime for a specific host it does work and I received a 403 ; *(For test purpose the user as received admin privileges)*

 

To give you more details and to let you preproduce the issue I encountered, please find some curl commands below:

 

- get host information: OK

```bash

 curl -k  -H "Content-Type: application/json" -H "centreon-auth-token: XXXXXXXXXXXXXX" -X GET "https://centreon.srv.domain/centreon/api/index.php?object=centreon_realtime_hosts&action=list&search=myserver03&fields=id,name,alias,address"

```

 

- answer received:

```javascript

[{"id":2348,"name":"MYERVER03","alias":"MYERVER03","address":"1.222.111.23"}]

```


 

2. set a downtime: NOK

```javascript

curl -k  -H "Content-Type: application/json" -H "centreon-auth-token: XXXXXXXXXXXXXX" -X POST "https://centreon.srv.domain:443/centreon/api/monitoring/hosts/2348/downtimes" -d '{

  "author_id": 3,

  "comment": "Downtime set by ITPA",

  "duration": 3600,

  "end_time": "2023-11-29T10:38:34Z",

  "is_fixed": true,

  "start_time": "2023-11-29T10:23:34Z",

  "with_services": true

}'

```

- answer received:

```html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>403 Forbidden</title>

</head><body>

<h1>Forbidden</h1>

<p>You don't have permission to access this resource.</p>

</body></html>

```

 

> I received a 403 fordidden :/ BUT I suspect something no properly formatted like the date format in by request body.

 

Any idea what's wrong here ?

 

Thanks in advance for your time and help.

ps: The official documentation is available here: Centreon Web RestAPI

icon

Best answer by vcoum 29 November 2023, 15:39

View original

2 replies

Userlevel 4
Badge +10

Hello,

Your first and working request is an API v1 request, and the second one is an API v2 request

APIv2 request doesn’t used the same header for the token auth

To authenticate with APIv2, use this request

curl --location 'http://CENTREONADDRESS/centreon/api/latest/login' \
--header 'Content-Type: application/json' \
--data '{
"security": {
"credentials": {
"login": "USERNAME",
"password": "PASSWORD"
}
}
}'

You will get a payload like this

{
"contact": {
"id": 1,
"name": "John",
"alias": "john",
"email": "john@admin.fr",
"is_admin": true
},
"security": {
"token": "TOKEN"
}
}

And then, use the second request you posted to create your downtime with the token you just got, but change the header “centreon-auth-token” for “X-AUTH-TOKEN”

curl -k  -H "Content-Type: application/json" -H "X-AUTH-TOKEN: XXXXXXXXXXXXXX" -X POST "https://centreon.srv.domain:443/centreon/api/monitoring/hosts/2348/downtimes" -d '{
"author_id": 3,
"comment": "Downtime set by ITPA",
"duration": 3600,
"end_time": "2023-11-29T10:38:34Z",
"is_fixed": true,
"start_time": "2023-11-29T10:23:34Z",
"with_services": true

}'

Hope it helps

Badge +1

@vcoum Thanks a lot for your help.

  1. using the latest API to authenticate
  2. using the latest API and the new header and the token received in step 1 to set downtime works like a charms :) 

"https://${CENTREON_SRV}/centreon/api/latest/monitoring/hosts/2348/downtimes" gives me a 204 http code but I can manage that :) 

curl -k  -H "Content-Type: application/json" -H "X-AUTH-TOKEN: XXXXXX" -X POST "https://${CENTREON_SRV}/centreon/api/latest/monitoring/hosts/2348/downtimes" -d '{
"author_id": 3,
"comment": "Downtime set by ITPA",
"duration": 3600,
"end_time": "2023-11-30T10:38:34Z",
"is_fixed": true,
"start_time": "2023-11-30T10:23:34Z",
"with_services": true
}'

HTTP response status code 204 No Content is returned by the server to indicate that a HTTP request has been successfully completed, and there is no message body.

 

I will update my ansible script accordingly and let you know. 

Reply