Restarting Wazuh agents via the API: parameters, responses and agent_control
Restarting Wazuh agents via the server API: endpoints, parameters, partial error responses, daemon statistics, and agent_control limits in a cluster.
The Wazuh 4.14 server API exposes two agent restart endpoints: one for a single agent and one for a list of agents or all of them. I cover both, the shape of a response with a partial error, the daemon statistics endpoint, and the console tools whose reach in a cluster is limited.
Addresses, identifiers and host names in the examples are illustrative: manager.example.com, agent agent-01. I removed the timestamp and uptime fields from the sample responses, because the specification gives them a date format with a timezone offset. The rest of the JSON is copied from the specification unchanged.
Two restart endpoints
The API specification defines two PUT operations. The first requires the RBAC permission agent:restart, and so does the second.
| Endpoint | Description in the specification |
|---|---|
PUT /agents/{agent_id}/restart | Restart the specified agent |
PUT /agents/restart | Restart all agents or a list of them |
The first takes the agent identifier in the path. The second takes the agents_list parameter. When I omit it, the call applies to all agents, so a call without the parameter restarts the whole fleet. With a larger number of agents I pass the list explicitly.
Both return the same status codes: 200, 400, 401, 403, 405 and 429.
Parameters
| Parameter | Where | Description in the specification | Default |
|---|---|---|---|
agent_id | path | Agent ID. All possible values from 000 onwards | required |
agents_list | query | List of agent IDs (separated by comma), all agents selected by default if not specified | all agents |
wait_for_complete | query | Disable timeout response | false |
pretty | query | Show results in human-readable format | false |
The value all belongs to a different definition of the agents_list parameter, used by other endpoints. In PUT /agents/restart the list has elements of type AgentID: a string with a minimum length of 3 in the numbers format, for example 001.
Authentication
The JWT token is issued by POST /security/user/authenticate with Basic authentication. According to the specification the token expires after auth_token_exp_timeout seconds, 900 by default. The value is changed with PUT /security/config. The Wazuh API listens on port 55000.
POST /security/user/authenticate HTTP/1.1
Host: manager.example.com:55000
Authorization: Basic <base64 of user:password>
I send the token from the response in the Authorization: Bearer header. The body of the token response is not described in the parts of the specification I used, so I do not quote it.
Restarting a single agent
PUT /agents/002/restart?pretty=true HTTP/1.1
Host: manager.example.com:55000
Authorization: Bearer <token>
Example 200 response from the specification:
{
"data": {
"affected_items": [
"002"
],
"total_affected_items": 1,
"total_failed_items": 0,
"failed_items": []
},
"message": "Restart command was sent to all agents",
"error": 0
}
The values error: 0 and total_failed_items: 0 mean that the restart command was sent. The message “Restart command was sent to all agents” points to this. The specification does not describe how to check that the agent actually came back after the restart.
Restarting a list of agents and a partial error
PUT /agents/restart?agents_list=001,002,003,004,005,006,007,008,009,010,011 HTTP/1.1
Host: manager.example.com:55000
Authorization: Bearer <token>
Example 200 response with a partial error from the specification:
{
"data": {
"affected_items": [
"001",
"002",
"003",
"004",
"005",
"006",
"007",
"008"
],
"total_affected_items": 8,
"total_failed_items": 3,
"failed_items": [
{
"error": {
"code": 1707,
"message": "Cannot send request, agent is not active",
"remediation": "Please, check non-active agents connection and try again. Visit https://documentation.wazuh.com/4.14/user-manual/registering/index.html and https://documentation.wazuh.com/4.14/user-manual/agents/agent-connection.html to obtain more information on registering and connecting agents"
},
"id": [
"009",
"010",
"011"
]
}
]
},
"message": "Restart command was not sent to some agents",
"error": 2
}
With a partial result the error field is non-zero and message reads “Restart command was not sent to some agents”. The agents in failed_items have error 1707, “Cannot send request, agent is not active”. The remediation field points to the documentation on registering and connecting agents. A script that automates restarts therefore checks error and total_failed_items.
The message and error fields sit at the top of the response, while total_affected_items, total_failed_items and failed_items are in data. Error 1707 only says that the manager considers the agent inactive. The Disconnected status and the time after which the manager sets it are described in the post on disconnected Wazuh agents.
Error responses
| Code | Title in the specification example | Detail |
|---|---|---|
| 400 | Bad Request | parameter validation message |
| 401 | Unauthorized | No authorization token provided |
| 403 | Permission Denied | Permission denied: Resource type: : |
| 405 | Method Not Allowed | Specified method is invalid for this resource |
| 429 | Wazuh Error | Maximum number of requests per minute reached |
In the specification the 429 response has code 6001 and a hint that the limit is changed in the api.yaml file. The 403 response has code 4000 and points to the RBAC configuration. If a script restarts agents in a loop, the requests-per-minute limit can stop it. A single PUT /agents/restart with a list is better.
Agent daemon statistics
GET /agents/{agent_id}/daemons/stats returns statistics of the selected daemons for one agent. It requires the agent:read permission. The daemons_list parameter accepts wazuh-analysisd and wazuh-remoted, and without it both are returned.
GET /agents/001/daemons/stats?daemons_list=wazuh-remoted,wazuh-analysisd HTTP/1.1
Host: manager.example.com:55000
Authorization: Bearer <token>
Example response from the specification:
{
"data": {
"affected_items": [
{
"name": "wazuh-remoted",
"agents": [
{
"id": 1,
"metrics": {
"messages": {
"received_breakdown": {
"control": 2,
"control_breakdown": {
"keepalive": 1,
"request": 0,
"shutdown": 0,
"startup": 1
},
"event": 195
},
"sent_breakdown": {
"ack": 2,
"ar": 0,
"cfga": 2,
"discarded": 0,
"request": 0,
"shared": 0
}
}
}
}
]
},
{
"name": "wazuh-analysisd",
"agents": [
{
"id": 1,
"metrics": {
"events": {
"processed": 195,
"received_breakdown": {
"decoded_breakdown": {
"agent": 0,
"dbsync": 0,
"integrations_breakdown": {
"virustotal": 0
},
"modules_breakdown": {
"aws": 0,
"azure": 0,
"ciscat": 0,
"command": 0,
"docker": 0,
"gcp": 0,
"github": 0,
"logcollector_breakdown": {
"eventchannel": 0,
"eventlog": 0,
"macos": 0,
"others": 0
},
"office365": 0,
"ms-graph": 0,
"oscap": 0,
"osquery": 0,
"rootcheck": 1,
"sca": 194,
"syscheck": 0,
"syscollector": 0,
"upgrade": 0,
"vulnerability": 0
},
"monitor": 0,
"remote": 0
}
},
"written_breakdown": {
"alerts": 191,
"archives": 0,
"firewall": 0
}
}
}
}
]
}
],
"total_affected_items": 2,
"total_failed_items": 0,
"failed_items": []
},
"message": "Statistical information for each daemon was successfully read",
"error": 0
}
Field descriptions from the schema definition:
| Field | Description in the specification |
|---|---|
uptime | When the count of the metrics started |
control | Control messages received from agent |
keepalive | Keepalive messages from agent |
event | Event messages (syscheck, syscollector, logcollector, etc.) received from agent |
ar | Active response messages sent to agent |
ack | ACK messages (response to keepalive, startup and shutdown) sent to agent |
discarded | Messages discarded because the send queue was full (for this agent) |
processed | Total processed events (analyzed by rules) from agent |
The event counter from wazuh-remoted and processed from wazuh-analysisd show how many events from a given agent reached the manager and how many went through the rules. Apart from the uptime description, the specification does not define the units or the moment the counters reset, so alert thresholds have to be set from your own measurements.
agent_control in a cluster
The agent_control tool queries the manager about agents and triggers a syscheck or rootcheck scan at the agent’s next contact with the manager. The documentation lists four statuses: Active, Pending, Disconnected and Never connected.
| Option | Description in the documentation |
|---|---|
-l | List available agents whether they are active or not |
-lc | List only the currently connected agents |
-ln | List only the currently disconnected agents |
-i <agent_id> | Extract information from an agent |
-R <agent_id> | Restart the Wazuh processes on the agent |
-r | Run the integrity/rootcheck checking on agents, together with -a or -u |
-a | Utilizes all agents |
-u <agent_id> | Perform the requested action on the specified agent |
-b <IP> | Blocks the specified IP address (Active Response) |
-f <ar> | Used with -b, specifies which response to run |
-L | List available active responses |
-s | Change the output to CSV format |
-j | Change the output to JSON format |
The documentation states that in a cluster I run agent_control on the master node, because it does not work on workers. The options -r, -R, -b and -f reach only agents connected to the master. For agents connected to a worker the documentation says to use the server API. This is the main reason a restart through the API is the right way in a cluster. Node handling is described in the post on Wazuh cluster operations.
Restart example from the documentation and the message the tool prints:
/var/ossec/bin/agent_control -R -u 001
Wazuh agent_control: Restarting agent: 001
The response identifier for the -f option consists of the command name and the timeout value from the active-response block. When timeout_allowed in the command block is no, or no timeout is given, the number is 0. The identifier is visible in the first column of /var/ossec/etc/shared/ar.conf. The mechanism itself is described in the post on Active Response in Wazuh.
wazuh-control: restart and reload
The wazuh-control script starts, stops and checks Wazuh processes. The documentation recommends systemctl or service for starting, stopping and restarting the service, however, to avoid inconsistency between the service state and the process state.
| Command | Description in the documentation |
|---|---|
start | Start the Wazuh processes |
stop | Stop the Wazuh processes |
restart | Restart the Wazuh processes |
reload | Restart all Wazuh processes except wazuh-execd |
status | Determine which Wazuh processes are running |
reload restarts all processes except wazuh-execd. The documentation notes that this option is not available in a local installation.
What the documentation does not specify
- Whether
wait_for_completetimes out after some time and what that time is: the description is only “Disable timeout response”. - An upper limit on the number of identifiers in
agents_list.