Skip to content

How to manage Alert Groups and Alerts via API

Last updated on

How to create a new Alert Group and Alert Rule

Section titled “How to create a new Alert Group and Alert Rule”

You always need an Alert Group to add an Alert Rule.

Below is an example of how to create a new Alert Group and Alert Rule using the STACKIT CLI. You will need to set the following environment variables (or replace them directly in the command): PROJECT_ID to your project ID and INSTANCE_ID to your instance ID.

API prerequisites: To learn how to execute these commands and locate your instance identifiers, refer to our guide on Interacting with the Observability API.

Example: Create a new Alert Group

Terminal window
stackit curl -X POST "https://argus.api.eu01.stackit.cloud/v1/projects/$PROJECT_ID/instances/$INSTANCE_ID/alertgroups" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
--data '{"name":"TestAlertGroup","interval":"60s","rules":[{"alert":"HostCheck","expr":"up == 0","for":"60s","labels":{"severity":"critical"},"annotations":{"testAnnotation":"testAnnotation"}}]}'

The request body in the command to create the Alert Group can be seen below:

{
"name": "TestAlertGroup",
"interval": "60s",
"rules": [
{
"alert": "HostCheck",
"expr": "up == 0",
"for": "60s",
"labels": {
"severity": "critical"
},
"annotations": {
"testAnnotation": "testAnnotation"
}
}
]
}

An explanation of the request body follows:

In the request body above, we used the expression up == 0. Below are more Kubernetes-specific examples:

Kubernetes Node not Ready

kube_node_status_condition{condition='Ready',status='true'} == 0

Expression to Alert on Failed Pods

kube_pod_status_phase{phase='Failed'} > 0

More Kubernetes examples can be found here.

You always need an Alert Group to modify an Alert Rule.

Below is an example of how to update an existing Alert Rule. You will need to set your PROJECT_ID and INSTANCE_ID as environment variables.

Example: Update an Alert Group config

Terminal window
stackit curl -X PUT "https://argus.api.eu01.stackit.cloud/v1/projects/$PROJECT_ID/instances/$INSTANCE_ID/alertgroups/TestAlertGroup" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
--data '{"interval": "60s","rules": [{"alert": "HostCheck","expr": "up == 0","for": "60s","labels": {"severity": "critical"},"annotations": {"testAnnotation":"testAnnotation"}},{"alert": "SystemLoad","expr": "system_load1{} >= 5", "for": "60s","labels": {"severity": "warning"}}]}'

The request body in the command to update the Alert Group can be seen below:

Example: Alert Group update request body

{
"interval": "60s",
"rules": [
{
"alert": "HostCheck",
"expr": "up == 0",
"for": "60s",
"labels": {
"severity": "critical"
},
"annotations": {
"testAnnotation": "testAnnotation"
}
},
{
"alert": "SystemLoad",
"expr": "system_load1{} >= 5",
"for": "60s",
"labels": {
"severity": "warning"
}
}
]
}

Unfortunately, it is not possible to change the name of an Alert Group directly. You have to delete it and create a new group using the desired name.

You can delete a specific Alert Rule from an Alert Group. Below is an example of deleting an Alert Rule. You will need to set your PROJECT_ID and INSTANCE_ID as environment variables:

Example: Delete an Alert Rule

Terminal window
stackit curl -X DELETE "https://argus.api.eu01.stackit.cloud/v1/projects/$PROJECT_ID/instances/$INSTANCE_ID/alertgroups/TestAlertGroup/alertrules/SystemLoad" \
-H "accept: application/json"

Below is an example of deleting an Alert Group. You will need to set your PROJECT_ID and INSTANCE_ID as environment variables:

Example: Delete an Alert Group

Terminal window
stackit curl -X DELETE "https://argus.api.eu01.stackit.cloud/v1/projects/$PROJECT_ID/instances/$INSTANCE_ID/alertgroups?groupName=TestAlertGroup" \
-H "accept: application/json"

An alternative way of deleting an Alert Group is:

Example: Delete an Alert Group (Alternative)

Terminal window
stackit curl -X DELETE "https://argus.api.eu01.stackit.cloud/v1/projects/$PROJECT_ID/instances/$INSTANCE_ID/alertgroups/TestAlertGroup" \
-H "accept: application/json"