Tired off creating Grafana dashboards manually? Let’s automate it.

We have already discussed the creation of Grafana dashboards and alerts in my previous blogs. We were doing that manually. But think of, if we need to do that in more than 10 environments then we need to repeat that manual process again and again and sometimes we get frustrated by doing all these repetitive stuff.

We should have some automated process for doing this. So let’s discuss that.

Grafana provides the HTTP APIs for these type of stuff. It means, we can create dashboards, alerts and many more thing through APIs. Check out the full list here.

For the manual process, as we need to authenticate ourself by doing login, similarly we need to use some authentication mechanism with the APIs to access the stuff. Let’s see that.

Authentication:

Grafana provides 2 ways of authentication:

  • Token
  • Basic Auth

Here, we will use basic auth where APIs get authenticated with username and password.

We need to pass the username and password in the request itself in the following format:

username:password

For example:

As Grafana has default username and password as admin, so let’s use this for the request.

http://admin:admin@localhost:3000/api/dashboards/db

You can see in the above request, we have provided admin: admin as username and password for authentication at the very first place, then we need to pass host: port (where Grafana is running) and then need to provide the path of the APIs.

That’s all about the authentication. Now let’s discuss the dashboards.

Here, we will see the following things through APIs:

  • Creation of a notification channel
  • Creation of a dashboard with the alerts

Creation of a notification channel:

Here, we will create a slack alert.

POST       http://admin:admin@localhost:3000/api/alert-notifications/

Body: 

{
“name”: “demo_alerts”,
“type”: “slack”,
“isDefault”: true,
“sendReminder”: true,
“frequency” : “5m”,
“settings”: {
“url”:”incoming_webhook_url”
}
}

In url field, you need to provide slack incoming webhook URL which we have already discussed here. When we will hit the POST API for creating alert notification channel, we will get the following success response:

{
“id”: 1,
“name”: “demo_alerts”,
“type”: “slack”,
“isDefault”: true,
“sendReminder”: true,
“disableResolveMessage”: false,
“frequency”: “5m”,
“created”: “2019-05-14T11:45:40.020413745Z”,
“updated”: “2019-05-14T11:45:40.020413847Z”,
“settings”: {
“url”: “incoming_webhook_url”
}
}

Creation of a dashboard with the alerts:

To create the dashboard, you must have the exported dashboard json. So first we will see, how we can export the dashboard json for an existing dashboard.

We have an existing dashboard “User” for which we will export the json.

First, click on the dashboard -> Settings

settings

then click on JSON Model option at left-hand side and copy the json.

json_model

This above json is your dashboard json.

Now we need to put the above json as a value to the dashboard key:

{
“dashboard”: “dashboard_json”

}

for example:

{
“dashboard”: {
{
“annotations”: {
“list”: [
{
“builtIn”: 1,
“datasource”: “– Grafana –“,
“enable”: true,
“hide”: true,
“iconColor”: “rgba(0, 211, 255, 1)”,
“name”: “Annotations & Alerts”,
“type”: “dashboard”
}
]

}

}

Now the body of the dashboard API is ready. It’s time to hit the API now:

POST      http://admin:admin@localhost:3000/api/dashboards/db

Body:

{“dashboard”:{“annotations”:{“list”:[{“builtIn”:1,”datasource”:”– Grafana –“,”enable”:true,”hide”:true,”iconColor”:”rgba(0, 211, 255, 1)”,”name”:”Annotations & Alerts”,”type”:”dashboard”}]},”editable”:true,”gnetId”:null,”graphTooltip”:0,”links”:[],”panels”:[{“alert”:{“conditions”:[{“evaluator”:{“params”:[],”type”:”no_value”},”operator”:{“type”:”and”},”query”:{“params”:[“A”,”1m”,”now”]},”reducer”:{“params”:[],”type”:”last”},”type”:”query”}],”executionErrorState”:”keep_state”,”for”:”5m”,”frequency”:”1m”,”handler”:1,”message”:”user profile stream has died on local”,”name”:”User_details alert”,”noDataState”:”no_data”,”notifications”:[]},”aliasColors”:{},”bars”:false,”dashLength”:10,”dashes”:false,”datasource”:”Graphite”,”fill”:1,”gridPos”:{“h”:9,”w”:12,”x”:0,”y”:0},”id”:2,”legend”:{“avg”:false,”current”:false,”max”:false,”min”:false,”show”:true,”total”:false,”values”:false},”lines”:true,”linewidth”:1,”links”:[],”nullPointMode”:”null”,”percentage”:false,”pointradius”:5,”points”:false,”renderer”:”flot”,”seriesOverrides”:[],”spaceLength”:10,”stack”:false,”steppedLine”:false,”targets”:[{“refId”:”A”,”target”:”alias(sumSeries(user-streamapp.local.kafka.streams.type_stream-metrics.client-id_user-profiles-v1-*.process-rate), ‘user-profiles’)”,”textEditor”:false}],”thresholds”:[],”timeFrom”:null,”timeRegions”:[],”timeShift”:null,”title”:”User_details”,”tooltip”:{“shared”:true,”sort”:0,”value_type”:”individual”},”type”:”graph”,”xaxis”:{“buckets”:null,”mode”:”time”,”name”:null,”show”:true,”values”:[]},”yaxes”:[{“format”:”short”,”label”:null,”logBase”:1,”max”:null,”min”:null,”show”:true},{“format”:”short”,”label”:null,”logBase”:1,”max”:null,”min”:null,”show”:true}],”yaxis”:{“align”:false,”alignLevel”:null}}],”schemaVersion”:16,”style”:”dark”,”tags”:[],”templating”:{“list”:[]},”time”:{“from”:”now-6h”,”to”:”now”},”timepicker”:{“refresh_intervals”:[“5s”,”10s”,”30s”,”1m”,”5m”,”15m”,”30m”,”1h”,”2h”,”1d”],”time_options”:[“5m”,”15m”,”1h”,”6h”,”12h”,”24h”,”2d”,”7d”,”30d”]},”timezone”:””,”title”:”User”,”uid”:null,”id”:null,”version”:1}}

As you will hit the above request, you will get the following successful response:

{
“id”: 2,
“slug”: “user”,
“status”: “success”,
“uid”: “CjY2rCiWk”,
“url”: “/d/CjY2rCiWk/user”,
“version”: 1
}

Till now, we have discussed the usage of the APIs, we have not discussed how to make this process automated.

There are multiple ways to do this. Either we can create a Jenkins job which will hit these APIs on a single click or we can create postman collections and then we can automate that postman collection to be run. This is your choice that how you want to use the above Grafana APIs. The main crunch which is the Grafana APIs that we have already discussed above.

That’s it. I hope, this blog will help you to provide an understanding of the Grafana APIs and with the help of these APIs, you will be able to automate the process.

References:

https://grafana.com/docs/http_api/

 

Knoldus-blog-footer-image

 

Written by 

Rishi is a Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

1 thought on “Tired off creating Grafana dashboards manually? Let’s automate it.

Comments are closed.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!