在放入grafana后如何解决400错误

时间:2018-12-17 12:40:54

标签: go grafana

我正在尝试使用Go并使用此库“ github.com/adejoux/grafanaclient”自动在grafana中推送仪表板。我不是在使用其会话功能,而是在使用证书制作自己的客户端进行身份验证。 下面是我尝试过的。

package main
import (
    "fmt"
    "time"
    "github.com/adejoux/grafanaclient"
    "bytes"
    "encoding/json"
    "crypto/tls"
    "crypto/x509"
    "io/ioutil"
    "net/http"
    "net/url"
    "os"
)

func CreateGraphanaClient()(client *http.Client){
    test_telemetry_cert_file:="/vivek/certfile"
    test_telemetry_bridge_cert_file := "/vivek/bridge_certfile"
    proxyUrl, err := url.Parse("http://www-proxy-obc.us.myurl.com:80")
    test_telemetry_keystore_path := "/vivek/keystore"

    cert, err := tls.LoadX509KeyPair(test_telemetry_cert_file, test_telemetry_keystore_path)
    if err != nil {
        os.Exit(2)
    }

    clientCACert, err := ioutil.ReadFile(test_telemetry_bridge_cert_file)
    if err != nil {
        os.Exit(2)
    }

    clientCertPool := x509.NewCertPool()
    clientCertPool.AppendCertsFromPEM(clientCACert)

    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
        RootCAs:      clientCertPool,
    }

    tlsConfig.BuildNameToCertificate()

    client = &http.Client{
            Transport: &http.Transport{TLSClientConfig: tlsConfig,  Proxy: http.ProxyURL(proxyUrl) ,}, 
            }
    return
}

func main(){

    grafana_url := "http://192.168.56.101:3000"
    // init a new dashboard and allows edition
    dashboard := grafanaclient.Dashboard{Editable: true}
    // let's set the title
    dashboard.Title = "OPC-OCI-apicanary"

    // 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)
    dash[oard.AddRow(graphRow)

    // Setup time frame
    dashboard.SetTimeFrame(time.Now().Add(-24*time.Hour), time.Now())

    client := CreateGraphanaClient()
    out, _ := json.Marshal(dashboard)
    req, _ := http.NewRequest("PUT", grafana_url, bytes.NewBuffer(out))
    req.Header.Add("Content-Type", "application/json")
    resp,err := client.Do(req)
    if err != nil{
        fmt.Println("Err :",err)
    }
    fmt.Println("resp :",resp)
}

但是我收到以下错误:

resp : &{400 Bad Request 400 HTTP/1.1 1 1 map[Okc-Request-Id:[b2m0252c-7jr9-4bnx-l937-34hk1x11q102] Content-Type:[application/json] Content-Length:[2457] Date:[Mon, 17 Dec 2018 12:37:31 GMT]] 0xc0002b20c0 2457 [] false false map[] 0xc000164100 0xc0000dc4d0}

0 个答案:

没有答案