Skip to content

Commit

Permalink
[Logs onboarding] Getting elastic-agent state in a more reliably way (e…
Browse files Browse the repository at this point in the history
…lastic#165205)

Closes elastic#163163.

We were getting `elastic-agent` status using `human`
[output](https://www.elastic.co/guide/en/fleet/current/elastic-agent-cmd-options.html#_options_7),
this way of obtaining the state is very unreliable since human format
it's more likeable to vary from one version to other. e.g

- v8.0.0
<img width="1021" alt="image"
src="https://github.com/elastic/kibana/assets/1313018/7c8102a4-0785-4ab1-b690-ab62ec67644d">

- v8.9.0
<img width="1026" alt="image"
src="https://github.com/elastic/kibana/assets/1313018/b6acdfbd-6efa-4518-8855-0aba3662f07b">

### Changes
- Get `elastic-agent` status from json output.

#### Demo

##### v8.0.0


https://github.com/elastic/kibana/assets/1313018/6c507269-65d5-4c8a-9e9f-420698ca995d


##### v8.9.0


https://github.com/elastic/kibana/assets/1313018/acdab744-0bd7-43c8-9eb8-024e5a2eeae5
  • Loading branch information
yngrdyn authored Aug 31, 2023
1 parent d45347c commit 94bccbc
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,20 @@ waitForElasticAgentStatus
if [ "$?" -ne 0 ]; then
updateStepProgress "ea-status" "warning" "Unable to determine agent status"
fi
ELASTIC_AGENT_STATE="$(elastic-agent status | grep -m1 State | sed 's/State: //')"
ELASTIC_AGENT_MESSAGE="$(elastic-agent status | grep -m1 Message | sed 's/Message: //')"
if [ "${ELASTIC_AGENT_STATE}" = "HEALTHY" ] && [ "${ELASTIC_AGENT_MESSAGE}" = "Running" ]; then

# https://www.elastic.co/guide/en/fleet/current/elastic-agent-cmd-options.html#elastic-agent-status-command
ELASTIC_AGENT_STATES=(STARTING CONFIGURING HEALTHY DEGRADED FAILED STOPPING UPGRADING ROLLBACK)

# Get elastic-agent status in json format | removing extra states in the json | finding "state":value | removing , | removing "state": | trimming the result
ELASTIC_AGENT_STATE="$(elastic-agent status --output json | sed -n '/components/q;p' | grep state | sed 's/\(.*\),/\1 /' | sed 's/"state": //' | sed 's/\s//g')"
# Get elastic-agent status in json format | removing extra states in the json | finding "message":value | removing , | removing "message": | trimming the result | removing ""
ELASTIC_AGENT_MESSAGE="$(elastic-agent status --output json | sed -n '/components/q;p' | grep message | sed 's/\(.*\),/\1 /' | sed 's/"message": //' | sed 's/\s//g' | sed 's/\"//g')"
if [ "${ELASTIC_AGENT_STATE}" = "2" ] && [ "${ELASTIC_AGENT_MESSAGE}" = "Running" ]; then
echo "Elastic Agent running"
echo "Download and save configuration to ${cfg}"
updateStepProgress "ea-status" "complete"
else
updateStepProgress "ea-status" "warning" "Expected agent status HEALTHY / Running but got ${ELASTIC_AGENT_STATE} / ${ELASTIC_AGENT_MESSAGE}"
updateStepProgress "ea-status" "warning" "Expected agent status HEALTHY / Running but got ${ELASTIC_AGENT_STATES[ELASTIC_AGENT_STATE]} / ${ELASTIC_AGENT_MESSAGE}"
fi

downloadElasticAgentConfig() {
Expand Down

0 comments on commit 94bccbc

Please sign in to comment.