Created
November 12, 2016 08:51
-
-
Save adejoux/b6972647a7618f05625e9a43186703ca to your computer and use it in GitHub Desktop.
simple example program to show use case of grafanaclient library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple example program to show use case of grafanaclient library: | |
// https://github.com/adejoux/grafanaclient | |
// author: Alain Dejoux | |
package main | |
import ( | |
"fmt" | |
"log" | |
"time" | |
"github.com/adejoux/grafanaclient" | |
) | |
func main() { | |
// account credentials and url to connect to grafana | |
user := "admin" | |
pass := "admin" | |
url := "http://192.168.56.101:3000" | |
// create grafana session | |
session := grafanaclient.NewSession(user, pass, url) | |
// logon on the current session | |
logonErr := session.DoLogon() | |
if logonErr != nil { | |
log.Fatal(logonErr) | |
} | |
// define a new datasource | |
// ds := grafanaclient.DataSource{Name: "testdata", | |
// Type: "influxdb", | |
// Access: "proxy", | |
// URL: "http://192.168.56.101:8086", | |
// User: "root", | |
// Password: "root", | |
// Database: "testdb", | |
// } | |
// create the new datasource | |
// createErr := session.CreateDataSource(ds) | |
// | |
// if createErr != nil { | |
// log.Fatal(createErr) | |
// } | |
// let's list the existing data sources | |
dataSources, listErr := session.GetDataSourceList() | |
if listErr != nil { | |
log.Fatal(listErr) | |
} | |
for _, dataSource := range dataSources { | |
fmt.Printf("name: %s type: %s url: %s\n", dataSource.Name, dataSource.Type, dataSource.URL) | |
} | |
// init a new dashboard and allows edition | |
dashboard := grafanaclient.Dashboard{Editable: true} | |
// let's set the title | |
dashboard.Title = "test dashboard" | |
// Creating a new row | |
textRow := grafanaclient.NewRow() | |
textRow.Title = "this row will contains only a text panel" | |
textRow.Collapse = true // it will be collapsed by default | |
// let's create a simple text panel supporting html format | |
textPanel := grafanaclient.Panel{Type: "text", Editable: true, Mode: "html"} | |
// Add some content | |
textPanel.Content = "<b> Hello World</b>" | |
// and add the panel to the row | |
textRow.AddPanel(textPanel) | |
// add the row to the dashboard itself | |
dashboard.AddRow(textRow) | |
//create a new row with charts | |
graphRow := grafanaclient.NewRow() | |
graphRow.Title = "charts" | |
// NewPanel will create a graph panel by default | |
graphPanel := grafanaclient.NewPanel() | |
// set panel title | |
graphPanel.Title = "chart1" | |
// let's specify the datasource | |
graphPanel.DataSource = "testdata" | |
// change panel span from default 12 to 6 | |
graphPanel.Span = 6 | |
// stack lines with a filling of 1 | |
graphPanel.Stack = true | |
graphPanel.Fill = 1 | |
// define a target | |
target := grafanaclient.NewTarget() | |
//specify the measurement to use | |
target.Measurement = "CPU_ALL" | |
// group stats by name tag values | |
target.GroupByTag("name") | |
target.GroupByTag("host") | |
// filter by host tag | |
target.FilterByTag("host", "test") | |
// Adding everything | |
graphPanel.AddTarget(target) | |
graphRow.AddPanel(graphPanel) | |
dashboard.AddRow(graphRow) | |
// Setup time frame | |
dashboard.SetTimeFrame(time.Now().Add(-24*time.Hour), time.Now()) | |
//upload the dashboard | |
session.UploadDashboard(dashboard, true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment