This commit is contained in:
66
cmd/server/main.go
Normal file
66
cmd/server/main.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"ucs-exporter/internal/exporters"
|
||||
|
||||
"github.com/dnaeon/go-ucs/api"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var sha1ver string // sha1 revision used to build the program
|
||||
var buildTime string // when the executable was built
|
||||
|
||||
func main() {
|
||||
vURL := flag.String("url", "", "The URL of a UCS Manager, eg https://server.domain.example/nuova")
|
||||
vUser := flag.String("user", "ucspe", "The username to use when connecting to UCS Manager")
|
||||
vPass := flag.String("password", "ucspe", "The password to use when connecting to UCS Manager")
|
||||
listenPort := flag.String("listen-port", "9101", "The port to listen on for telemetry")
|
||||
flag.Parse()
|
||||
|
||||
// The following example shows how to retrieve all compute blades.
|
||||
|
||||
// Skip SSL certificate verification of remote endpoint.
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
httpClient := &http.Client{Transport: tr}
|
||||
|
||||
// Create a new Cisco UCS API client
|
||||
config := api.Config{
|
||||
Endpoint: *vURL,
|
||||
Username: *vUser,
|
||||
Password: *vPass,
|
||||
HttpClient: httpClient,
|
||||
}
|
||||
|
||||
ucsClient, err := api.NewClient(config)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create API client: %s", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
log.Printf("Logging in to %s\n", config.Endpoint)
|
||||
if _, err := ucsClient.AaaLogin(ctx); err != nil {
|
||||
log.Fatalf("Unable to login: %s\n", err)
|
||||
}
|
||||
defer ucsClient.AaaLogout(ctx)
|
||||
|
||||
log.Printf("Got authentication cookie: %s\n", ucsClient.Cookie)
|
||||
|
||||
// Register prometheus exporters
|
||||
exporter := exporters.NewUcsmTemperatureCollector(ucsClient, ctx)
|
||||
prometheus.MustRegister(exporter)
|
||||
|
||||
// Start prometheus exporter
|
||||
http.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
// TODO - run this in a go routine to avoid blocking, as per cbs code
|
||||
log.Fatal(http.ListenAndServe(*listenPort, nil))
|
||||
}
|
Reference in New Issue
Block a user