TIL #3: Using xbar to build ArgoCD deployment monitor


This week I was going over the latest edition(Volume 27) of Thoughtworks Technology Radar and found the addition of xbar in their Tools section. xbar lets you put output from any script/program in your macOS menu bar. I first wrote about it in October 2021 when I showed how you can use it show WordPress page views analytics in your macOS menu bar.

From the Thoughtworks Radar entry on xbar

On remote teams, we sorely lack having a dedicated build monitor in the room; unfortunately, newer continuous integration (CI) tools lack support for the old CCTray format. The result is that broken builds aren’t always picked up as quickly as we’d like. To solve this problem, many of our teams have started using xbar for build monitoring. With xbar, one can execute a script to poll build status, displaying it on the menu bar.

We use Argo CD for deployments. Many times team was unaware of the failing deployments because no one looked at the Slack channel or ArgoCD UI. So, I decided to build a ArgoCD deployment monitor using xbar to solve this problem for myself and my team.

Xbar has a plugin based architecture. There are hundreds of pre-built plugins that help you view information about AWS, cryptocurrency, weather, date & time, and many others.

You can download and install the latest binary of xbar from the release page https://github.com/matryer/xbar/releases

xbar makes it dead simple to write a plugin. All plugins exist in the ~/Library/Application Support/xbar/plugins. You can open it either manually or by using the shortcut in your xbar app.

To create a plugin create a file name with the following format.

{name}.{time}.{ext}
  • name – The name of the file
  • time – The refresh rate (see below)
  • ext – The file extension

We created a new file argo.1m.sh inside the plugins folder ~/Library/Application Support/xbar/plugins

Make sure to make the script executable.

chmod +x argo.1m.sh

Now, we will use curl and jq to do the magic. You will need ArgoCD token to talk to the REST API.

#!/usr/bin/env bash

export PATH="/usr/local/bin:/usr/bin:$PATH"

token=<add token here>
argoUri=<add ArgoCD URI>

all_services=$(curl -s $argoUri/api/v1/applications -H "Authorization: Bearer $token" | jq '.items[] | .status.resources[2]' | jq -s | jq -r '.[] | select(. != null )' | jq -s | jq '.[] | {name,health}' | jq -s)

all_services_count=$(echo $all_services | jq length)

healthy_services=$(echo $all_services | jq -r '.[] | select(.health.status == "Healthy" )' | jq -s | jq length)

degraded_services=$(echo $all_services | jq -r '.[] | select(.health.status == "Degraded" )' | jq -s | jq length)

echo "Deployments: " $all_services_count"/H"$healthy_services"/D"$degraded_services

You will have to add valid values for token and argoUri for this script to work.

We first add the /usr/local/bin to the PATH. This will ensure jq is found in the path. Next, we make the cURL request passing the JSON output to jq. j

I am not an expert jq user. So, it could be possible to get the required result using less jq transformations.

Below is the screenshot of our build monitor. It tells there are total 14 services. Out of 14 services, 13 are healthy and 1 is in degraded mode. Developers can use this information to quickly fix degraded services.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: