A terminal app that shows weather in form of Grafana dashboard
We are gonna create a Golang terminal app that shows the current location’s weather as the Grafana dashboard. As of now, it will only work for the ITerm terminal. A glimpse of the final app :
Some Ingredients needed to build this app:-
- Golang
- FreeGeoAPI to convert the IP addresses to lat long coordinates
- Openweather API (To get weather details based on latitude and longitude of current location)
- Grafana, Statsd, InFluxdb to build grafana dashboard
- Grafana image rendering plugin
- Imgcat to show image in the iTerm
We will be writing everything in Golang, so install Golang and set it up if you haven’t done it yet.
Since we are building a CLI app which will show the weather for the current location. In order to achieve that, we first need to convert the IP addresses to geo coordinates. And FreeGeoAP just does that 😎
We first define our geoAPI struct. It contains all attributes for the response from the FreeGeoAPI.
Code is simple, we first make an HTTP call to freegeoip API(If nothing else is added to URL, it returns IP details for the caller). Then we read the response into our variable and extract coordinates.
That was easy right.
Moving on to the next part…
https://github.com/briandowns/openweathermap. It’s a Go package for use with openweathermap.org’s API.
To be able to use this package, you will first need to obtain an API key from OpenWeatherMap(https://home.openweathermap.org/).
Once done, we are ready to write some code.
- Line
4
createsCurrentWeatherData
(a struct that contains a list of all-weather attributes like temperature and all) pointer with the parameters we pass where “C” is for Celcius, “en” is for the English language, and remember we talked about apiKey like 2 mins ago. This is where we use it. - Line
9-13
is where we pass the latitude and longitude for which we want to query the weather stats.
Now we can pass latitude, longitude retrieved from our first method, and pass it to this method.
For our next step, we will use the TIG stack(Telegraf, Influx, and Grafana)
Steps to be followed
- First, we will install
InfluxDb
and create a new database
$ brew install influxdb# Once installation is done, start service by using:
$ brew services start influxdb# Now we will create a new database, lauch influx shell using:$ influx
Connected to http://localhost:8086 version 1.8.x
InfluxDB shell version: 1.8.x
> CREATE DATABASE weather_db# now create new user name grafana and choose password for this user
> CREATE USER grafana WITH PASSWORD "password"
- Next, we will install
Telegraf
$ brew install telegraf# Once the installation is complete, start service by using:
$ brew services start telegraf# open telegraf.conf using any editor
$ vi /usr/local/etc/telegraf.conf# uncomment the following line and save the file.
[[outputs.influxdb]] [[inputs.statsd]]# restart telegraf
$ brew services restart telegraf
- Last we will install
Grafana
$ brew install grafana# Once the installation is complete, start service by using:
$ brew services start grafana
Since we are gonna need to export the Grafana dashboard as an image in our app, we will need the grafana-image-renderer
plugin
$ grafana-cli plugins install grafana-image-renderer
$ brew services restart grafana
Once all installation is done, go to http://localhost:3000
and login to Grafana(id and password both are an admin by default, you can change the password after you login).
Next, go to Datasources, and choose the Influxdb
as datasource
. Add DB details that we created earlier. It should look like this
Save it. Now let's go back to our code. We will continue with this later.
To be able to send our weather to Grafana
, we need statsd
client.
Our TIG is setup, but it won’t work until we send anything to it right. So let's get started with Statsd.
We will use https://github.com/DataDog/datadog-go library to create statsd
client
Add these lines at the end fetchWeatherInfoByCoordinates
method. You can read more about Statsd here https://github.com/statsd/statsd
Now go back to Grafana
and create a new dashboard. Create a new panel and choose influxdb
as datasource. These metrics will start reflecting here. It's up to you now, how you wanna design your dashboard.
Mine looks like this for now!!
Next, we will write a method, to fetch our dashboard as an image. Before this, go to Grafana and create a new API key. Create a new folder in your current directory to store the images. We save the image from HTTP response to /tmp/weather.png
In place apiKey, add the API key you generated.
Call this method at the end of fetchWeatherByCoordinates method
Here is the main method, which puts everything together. We use https://github.com/martinlindhe/imgcat to be able to show images in the iTerm .
And we are done 🎉 🎉