core/v2/hooks

NOTE: Requests to core/v2/hooks API endpoints require you to authenticate with a Sensu API key or access token. The code examples in this document use the environment variable $SENSU_API_KEY to represent a valid API key in API requests.

Get all hooks

The /hooks API endpoint provides HTTP GET access to hook data.

Example

The following example demonstrates a GET request to the /hooks API endpoint:

curl -X GET \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hooks \
-H "Authorization: Key $SENSU_API_KEY"

The request results in a successful HTTP/1.1 200 OK response and a JSON array that contains the hook definitions in the default namespace:

[
  {
    "metadata": {
      "name": "nginx-log",
      "namespace": "default",
      "created_by": "admin"
    },
    "command": "tail -n 100 /var/log/nginx/error.log",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  },
  {
    "metadata": {
      "name": "process-tree",
      "namespace": "default",
      "created_by": "admin"
    },
    "command": "ps -eo user,pid,cmd:50,%cpu --sort=-%cpu | head -n 6",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  }
]

API Specification

/hooks (GET)
description Returns the list of hooks.
example url http://hostname:8080/api/core/v2/namespaces/default/hooks
pagination This endpoint supports pagination using the limit and continue query parameters.
response filtering This endpoint supports API response filtering.
response type Array
response codes
  • Success: 200 (OK)
  • Error: 500 (Internal Server Error)
output
[
  {
    "metadata": {
      "name": "nginx-log",
      "namespace": "default",
      "created_by": "admin"
    },
    "command": "tail -n 100 /var/log/nginx/error.log",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  },
  {
    "metadata": {
      "name": "process-tree",
      "namespace": "default",
      "created_by": "admin"
    },
    "command": "ps -eo user,pid,cmd:50,%cpu --sort=-%cpu | head -n 6",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  }
]

Create a new hook

The /hooks API endpoint provides HTTP POST access to create a hook.

Example

In the following example, an HTTP POST request is submitted to the /hooks API endpoint to create the hook process-tree:

curl -X POST \
-H "Authorization: Key $SENSU_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "metadata": {
    "name": "process-tree",
    "namespace": "default",
    "labels": null,
    "annotations": null
  },
  "command": "ps -eo user,pid,cmd:50,%cpu --sort=-%cpu | head -n 6",
  "timeout": 10,
  "stdin": false
}' \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hooks

The request will return a successful HTTP/1.1 201 Created response.

API Specification

/hooks (POST)
description Creates a Sensu hook.
example URL http://hostname:8080/api/core/v2/namespaces/default/hooks
payload
{
  "metadata": {
    "name": "process-tree",
    "namespace": "default",
    "labels": null,
    "annotations": null
  },
  "command": "ps aux",
  "timeout": 10,
  "stdin": false
}
response codes
  • Success: 201 (Created)
  • Malformed: 400 (Bad Request)
  • Error: 500 (Internal Server Error)

Get a specific hook

The /hooks/:hook API endpoint provides HTTP GET access to hook data for specific :hook definitions, by hook name.

Example

The following example queries the /hooks/:hook API endpoint for the :hook named process-tree:

curl -X GET \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hooks/process-tree \
-H "Authorization: Key $SENSU_API_KEY"

The request will return a successful HTTP/1.1 200 OK response and a JSON map that contains the requested :hook definition (in this example, process-tree):

{
  "metadata": {
    "name": "process-tree",
    "namespace": "default",
    "created_by": "admin",
    "labels": null,
    "annotations": null
  },
  "command": "ps aux",
  "timeout": 10,
  "stdin": false
}

API Specification

/hooks/:hook (GET)
description Returns the specified hook.
example url http://hostname:8080/api/core/v2/namespaces/default/hooks/process-tree
response type Map
response codes
  • Success: 200 (OK)
  • Missing: 404 (Not Found)
  • Error: 500 (Internal Server Error)
output
{
  "metadata": {
    "name": "process-tree",
    "namespace": "default",
    "created_by": "admin",
    "labels": null,
    "annotations": null
  },
  "command": "ps aux",
  "timeout": 10,
  "stdin": false
}

Create or update a hook

The /hooks/:hook API endpoint provides HTTP PUT access to create or update specific :hook definitions, by hook name.

Example

In the following example, an HTTP PUT request is submitted to the /hooks/:hook API endpoint to create the hook nginx-log:

curl -X PUT \
-H "Authorization: Key $SENSU_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
  "metadata": {
    "name": "nginx-log",
    "namespace": "default",
    "labels": null,
    "annotations": null
  },
  "command": "tail -n 100 /var/log/nginx/error.log",
  "timeout": 10,
  "stdin": false
  }' \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hooks/nginx-log

The request will return a successful HTTP/1.1 201 Created response.

API Specification

/hooks/:hook (PUT)
description Creates or updates the specified Sensu hook.
example URL http://hostname:8080/api/core/v2/namespaces/default/hooks/nginx-log
payload
{
  "metadata": {
    "name": "nginx-log",
    "namespace": "default",
    "labels": null,
    "annotations": null
  },
  "command": "tail -n 100 /var/log/nginx/error.log",
  "timeout": 10,
  "stdin": false
  }
response codes
  • Success: 201 (Created)
  • Malformed: 400 (Bad Request)
  • Error: 500 (Internal Server Error)

Update a hook with PATCH

The /hooks/:hook API endpoint provides HTTP PATCH access to update :hook definitions, specified by hook name.

NOTE: You cannot change a resource’s name or namespace with a PATCH request. Use a PUT request instead.

Also, you cannot add elements to an array with a PATCH request — you must replace the entire array.

Example

In the following example, an HTTP PATCH request is submitted to the /hooks/:hook API endpoint to update the timeout for the process-tree hook, resulting in an HTTP/1.1 200 OK response and the updated hook definition.

We support JSON merge patches, so you must set the Content-Type header to application/merge-patch+json for PATCH requests.

curl -X PATCH \
-H "Authorization: Key $SENSU_API_KEY" \
-H 'Content-Type: application/merge-patch+json' \
-d '{
  "timeout": 20
}' \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hook/process-tree

API Specification

/hooks/:hook (PATCH)
description Updates the specified Sensu hook.
example URL http://hostname:8080/api/core/v2/namespaces/default/hooks/process-tree
payload
{
  "timeout": 20
}
response codes
  • Success: 200 (OK)
  • Malformed: 400 (Bad Request)
  • Error: 500 (Internal Server Error)

Delete a hook

The /hooks/:hook API endpoint provides HTTP DELETE access to delete a check hook from Sensu (specified by the hook name).

Example

The following example shows a request to the /hooks/:hook API endpoint to delete the hook process-tree, resulting in a successful HTTP/1.1 204 No Content response:

curl -X DELETE \
http://127.0.0.1:8080/api/core/v2/namespaces/default/hooks/process-tree \
-H "Authorization: Key $SENSU_API_KEY"

API Specification

/hooks/:hook (DELETE)
description Removes the specified hook from Sensu.
example url http://hostname:8080/api/core/v2/namespaces/default/hooks/process-tree
response codes
  • Success: 204 (No Content)
  • Missing: 404 (Not Found)
  • Error: 500 (Internal Server Error)

Get a subset of hooks with response filtering

The /hooks API endpoint supports response filtering for a subset of hook data based on labels and the following fields:

  • hook.name
  • hook.namespace

Example

The following example demonstrates a request to the /hooks API endpoint with response filtering for only hook definitions in the production namespace:

curl -H "Authorization: Key $SENSU_API_KEY" http://127.0.0.1:8080/api/core/v2/hooks -G \
--data-urlencode 'fieldSelector=hook.namespace == production'

The example request will result in a successful HTTP/1.1 200 OK response and a JSON array that contains only hook definitions in the production namespace:

[
  {
    "metadata": {
      "name": "process_tree",
      "namespace": "production",
      "created_by": "admin"
    },
    "command": "ps aux",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  },
  {
    "metadata": {
      "name": "restart_nginx",
      "namespace": "production",
      "labels": {
        "sensu.io/managed_by": "sensuctl"
      },
      "created_by": "admin"
    },
    "command": "sudo systemctl start nginx",
    "timeout": 60,
    "stdin": false,
    "runtime_assets": null
  }
]

NOTE: Read API response filtering for more filter statement examples that demonstrate how to filter responses using different operators with label and field selectors.

API Specification

/hooks (GET) with response filters
description Returns the list of hooks that match the response filters applied in the API request.
example url http://hostname:8080/api/core/v2/hooks
pagination This endpoint supports pagination using the limit and continue query parameters.
response type Array
response codes
  • Success: 200 (OK)
  • Error: 500 (Internal Server Error)
output
[
  {
    "metadata": {
      "name": "process_tree",
      "namespace": "production",
      "created_by": "admin"
    },
    "command": "ps aux",
    "timeout": 10,
    "stdin": false,
    "runtime_assets": null
  },
  {
    "metadata": {
      "name": "restart_nginx",
      "namespace": "production",
      "labels": {
        "sensu.io/managed_by": "sensuctl"
      },
      "created_by": "admin"
    },
    "command": "sudo systemctl start nginx",
    "timeout": 60,
    "stdin": false,
    "runtime_assets": null
  }
]