main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2017 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. // The ocprometheus implements a Prometheus exporter for OpenConfig telemetry data.
  5. package main
  6. import (
  7. "flag"
  8. "io/ioutil"
  9. "net/http"
  10. "sync"
  11. "notabug.org/themusicgod1/goarista/openconfig/client"
  12. "notabug.org/themusicgod1/glog"
  13. "github.com/prometheus/client_golang/prometheus"
  14. "github.com/prometheus/client_golang/prometheus/promhttp"
  15. )
  16. func main() {
  17. listenaddr := flag.String("listenaddr", ":8080", "Address on which to expose the metrics")
  18. url := flag.String("url", "/metrics", "URL where to expose the metrics")
  19. configFlag := flag.String("config", "",
  20. "Config to turn OpenConfig telemetry into Prometheus metrics")
  21. username, password, subscriptions, addrs, opts := client.ParseFlags()
  22. if *configFlag == "" {
  23. glog.Fatal("You need specify a config file using -config flag")
  24. }
  25. cfg, err := ioutil.ReadFile(*configFlag)
  26. if err != nil {
  27. glog.Fatalf("Can't read config file %q: %v", *configFlag, err)
  28. }
  29. config, err := parseConfig(cfg)
  30. if err != nil {
  31. glog.Fatal(err)
  32. }
  33. // Ignore the default "subscribe-to-everything" subscription of the
  34. // -subscribe flag.
  35. if subscriptions[0] == "" {
  36. subscriptions = subscriptions[1:]
  37. }
  38. // Add the subscriptions from the config file.
  39. subscriptions = append(subscriptions, config.Subscriptions...)
  40. coll := newCollector(config)
  41. prometheus.MustRegister(coll)
  42. wg := new(sync.WaitGroup)
  43. for _, addr := range addrs {
  44. wg.Add(1)
  45. c := client.New(username, password, addr, opts)
  46. go c.Subscribe(wg, subscriptions, coll.update)
  47. }
  48. http.Handle(*url, promhttp.Handler())
  49. glog.Fatal(http.ListenAndServe(*listenaddr, nil))
  50. }