Curl blank in Service Namen

CMK version:2.0.0p23 (CEE)
OS version:debian 11

Error message:
“title”: “The requested service was not found”,
“status”: 404,
“detail”: “The service description CPU did not match any service”
**
Hallo,
ich versuche eien Abfrage zu erstellen (REST-API) die mir den Service-Status liefern soll.
Dabei ist HOST=$1 und Service =$2 .
Solange der Service-Name kein Leerzeichen (Uptime) enthält, ist alles in Butter. Sobald ein Leerzeichen (CPU load) enthalten ist, läuf das Script auf einen Fehler.
Wie kann ich den Service-Namen übergeben? lst Doku sollte Leerzeichen in Curl kein Problem sein.

#!/bin/bash
HOST_NAME="MONHOST"
SITE_NAME="MONSITE"
API_URL="https://$HOST_NAME/$SITE_NAME/check_mk/api/1.0"
HOST=$1
SERVICE=$2

USERNAME="automation"
PASSWORD="PASS"

out=$(
  curl \
    -k \
    -G \
   --write-out "\nxxx-status_code=%{http_code}\n" \
    --header "Authorization: Bearer $USERNAME $PASSWORD" \
    --header "Accept: application/json" \
    --data-urlencode 'service_description='$2' ' \
    "$API_URL/objects/host/$HOST/actions/show_service/invoke")

resp=$( echo "${out}" | grep -v "xxx-status_code" )
code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}')

# For indentation, please install 'jq' (JSON query tool)
echo "$resp" | jq
# echo "$resp"

if [[ $code -lt 400 ]]; then
    echo "OK"
    exit 0
else
    echo "Request error"
    exit 1
fi

Please use the preformatted text format for code for better readability. You can do that by highlighting the text, then pressing Ctrl+E.

restAPI-show_service.sh HOST Uptime
Ergebnis:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   432  100   432    0     0   4595      0 --:--:-- --:--:-- --:--:--  4595
{
  "links": [
    {
      "domainType": "link",
      "rel": "self",
      "href": "http://MONHOST/MONSITE/check_mk/api/1.0/objects/service/HOST-Uptime",
      "method": "GET",
      "type": "application/json"
    }
  ],
  "domainType": "service",
  "id": "HOST-Uptime",
  "title": "Service Uptime",
  "members": {},
  "extensions": {
    "description": "Uptime",
    "host_name": "HOST",
    "state_type": 1,
    "state": 0,
    "last_check": 1638533067
  }
}
OK

restAPI-show_service.sh HOST CPU load
Ergebnis:


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   130  100   130    0     0   1397      0 --:--:-- --:--:-- --:--:--  1413
{
  "title": "The requested service was not found",
  "status": 404,
  "detail": "The service description CPU did not match any service"
}
Request error

Bei Substtution des Service-Namen :

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: load
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   130  100   130    0     0   1340      0 --:--:-- --:--:-- --:--:--  1340
{
  "title": "The requested service was not found",
  "status": 404,
  "detail": "The service description CPU did not match any service"
}
restAPI-show_service.sh: Zeile 33: [[: 000
404: Syntaxfehler im Ausdruck. (Fehlerverursachendes Zeichen ist "404").
Request error

Ich denke mal, daß das Problem mal wieder der “Wald mit den vielen Bäumen” ist :roll_eyes:

Das Problem dürfte die Expansion an dieser Stelle sein:

--data-urlencode 'service_description='$2'

Das expandiert letztlich zu drei statt zwei Parametern, wenn ein Leerzeichen involviert ist: $2 ist dann nicht innerhalb von Anführungszeichen. Bitte mal so versuchen:

--data-urlencode "service_description=$2" 

oder

--data-urlencode "service_description=$2 " 

Danke,
:smiley: Genau das war es. nun nur noch das Leerzeichen mit \ maskieren ( im Shell-Aufruf)und schon passt es. :sunglasses:

Gruß
Sven

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.