influxdb.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "log"
  4. "time"
  5. "github.com/influxdb/influxdb-go"
  6. "github.com/kr/pretty"
  7. )
  8. func (zs *Zones) influxdbPoster() {
  9. log.Println("starting influxdb poster")
  10. if len(Config.InfluxDB.Database) == 0 {
  11. return
  12. }
  13. log.Println("going to post to influxdb")
  14. influxGroups := append(serverGroups, "total", serverID)
  15. lastCounts := map[string]int64{}
  16. lastEdnsCounts := map[string]int64{}
  17. for name, zone := range *zs {
  18. if zone.Logging.InfluxDB == true {
  19. lastCounts[name] = zone.Metrics.Queries.Count()
  20. lastEdnsCounts[name] = zone.Metrics.EdnsQueries.Count()
  21. }
  22. }
  23. influxConfig := &influxdb.ClientConfig{
  24. Host: "localhost:8086",
  25. Username: "geodns",
  26. Password: "foobartty",
  27. Database: "geodns",
  28. }
  29. inflx, err := influxdb.NewClient(influxConfig)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. for {
  34. time.Sleep(60 * time.Second)
  35. for name, zone := range *zs {
  36. count := zone.Metrics.Queries.Count()
  37. newCount := count - lastCounts[name]
  38. lastCounts[name] = count
  39. ednsCount := zone.Metrics.EdnsQueries.Count()
  40. newEdnsCount := ednsCount - lastEdnsCounts[name]
  41. lastEdnsCounts[name] = ednsCount
  42. if zone.Logging != nil && zone.Logging.StatHat == true {
  43. apiKey := zone.Logging.StatHatAPI
  44. if len(apiKey) == 0 {
  45. apiKey = Config.StatHat.ApiKey
  46. }
  47. if len(apiKey) == 0 {
  48. continue
  49. }
  50. srs := make([]*influxdb.Series, 0)
  51. cnt := int(newCount)
  52. ednsCnt := int(newEdnsCount)
  53. for _, group := range influxGroups {
  54. name := zone.Origin + "-queries-" + group
  55. s := &influxdb.Series{
  56. Name: name,
  57. Columns: []string{"queries", "edns-queries"},
  58. Points: [][]interface{}{
  59. []interface{}{cnt, ednsCnt},
  60. },
  61. }
  62. srs = append(srs, s)
  63. }
  64. pretty.Println("influx series", srs)
  65. err := inflx.WriteSeries(srs)
  66. if err != nil {
  67. log.Printf("Could not write to influxdb: %s", err)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. func influxdbPoster() {
  74. // lastQueryCount := qCounter.Count()
  75. // stathatGroups := append(serverGroups, "total", serverID)
  76. // suffix := strings.Join(stathatGroups, ",")
  77. // // stathat.Verbose = true
  78. // for {
  79. // time.Sleep(60 * time.Second)
  80. // if !Config.Flags.HasStatHat {
  81. // log.Println("No stathat configuration")
  82. // continue
  83. // }
  84. // log.Println("Posting to stathat")
  85. // current := qCounter.Count()
  86. // newQueries := current - lastQueryCount
  87. // lastQueryCount = current
  88. // stathat.PostEZCount("queries~"+suffix, Config.StatHat.ApiKey, int(newQueries))
  89. // stathat.PostEZValue("goroutines "+serverID, Config.StatHat.ApiKey, float64(runtime.NumGoroutine()))
  90. // }
  91. }