Преглед на файлове

Start InfluxDB support (work in progress)

Ask Bjørn Hansen преди 11 години
родител
ревизия
03384496cd
променени са 4 файла, в които са добавени 133 реда и са изтрити 2 реда
  1. 9 2
      config.go
  2. 1 0
      geodns.go
  3. 121 0
      influxdb.go
  4. 2 0
      zone.go

+ 9 - 2
config.go

@@ -1,18 +1,25 @@
 package main
 
 import (
-	"code.google.com/p/gcfg"
 	"fmt"
-	"github.com/howeyc/fsnotify"
 	"log"
 	"os"
 	"time"
+
+	"code.google.com/p/gcfg"
+	"github.com/howeyc/fsnotify"
 )
 
 type AppConfig struct {
 	StatHat struct {
 		ApiKey string
 	}
+	InfluxDB struct {
+		Host     string
+		Database string
+		Username string
+		Password string
+	}
 	Flags struct {
 		HasStatHat bool
 	}

+ 1 - 0
geodns.go

@@ -161,6 +161,7 @@ func main() {
 
 	go monitor(Zones)
 	go Zones.statHatPoster()
+	go Zones.influxdbPoster()
 
 	setupPgeodnsZone(Zones)
 

+ 121 - 0
influxdb.go

@@ -0,0 +1,121 @@
+package main
+
+import (
+	"log"
+	"time"
+
+	"github.com/influxdb/influxdb-go"
+	"github.com/kr/pretty"
+)
+
+func (zs *Zones) influxdbPoster() {
+
+	log.Println("starting influxdb poster")
+
+	if len(Config.InfluxDB.Database) == 0 {
+		return
+	}
+
+	log.Println("going to post to influxdb")
+
+	influxGroups := append(serverGroups, "total", serverID)
+	lastCounts := map[string]int64{}
+	lastEdnsCounts := map[string]int64{}
+
+	for name, zone := range *zs {
+		if zone.Logging.InfluxDB == true {
+			lastCounts[name] = zone.Metrics.Queries.Count()
+			lastEdnsCounts[name] = zone.Metrics.EdnsQueries.Count()
+		}
+	}
+
+	influxConfig := &influxdb.ClientConfig{
+		Host:     "localhost:8086",
+		Username: "geodns",
+		Password: "foobartty",
+		Database: "geodns",
+	}
+
+	inflx, err := influxdb.NewClient(influxConfig)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	for {
+		time.Sleep(60 * time.Second)
+
+		for name, zone := range *zs {
+
+			count := zone.Metrics.Queries.Count()
+			newCount := count - lastCounts[name]
+			lastCounts[name] = count
+
+			ednsCount := zone.Metrics.EdnsQueries.Count()
+			newEdnsCount := ednsCount - lastEdnsCounts[name]
+			lastEdnsCounts[name] = ednsCount
+
+			if zone.Logging != nil && zone.Logging.StatHat == true {
+
+				apiKey := zone.Logging.StatHatAPI
+				if len(apiKey) == 0 {
+					apiKey = Config.StatHat.ApiKey
+				}
+				if len(apiKey) == 0 {
+					continue
+				}
+
+				srs := make([]*influxdb.Series, 0)
+				cnt := int(newCount)
+				ednsCnt := int(newEdnsCount)
+
+				for _, group := range influxGroups {
+
+					name := zone.Origin + "-queries-" + group
+
+					s := &influxdb.Series{
+						Name:    name,
+						Columns: []string{"queries", "edns-queries"},
+						Points: [][]interface{}{
+							[]interface{}{cnt, ednsCnt},
+						},
+					}
+					srs = append(srs, s)
+				}
+
+				pretty.Println("influx series", srs)
+
+				err := inflx.WriteSeries(srs)
+				if err != nil {
+					log.Printf("Could not write to influxdb: %s", err)
+				}
+			}
+		}
+	}
+}
+
+func influxdbPoster() {
+
+	// lastQueryCount := qCounter.Count()
+	// stathatGroups := append(serverGroups, "total", serverID)
+	// suffix := strings.Join(stathatGroups, ",")
+	// // stathat.Verbose = true
+
+	// for {
+	// 	time.Sleep(60 * time.Second)
+
+	// 	if !Config.Flags.HasStatHat {
+	// 		log.Println("No stathat configuration")
+	// 		continue
+	// 	}
+
+	// 	log.Println("Posting to stathat")
+
+	// 	current := qCounter.Count()
+	// 	newQueries := current - lastQueryCount
+	// 	lastQueryCount = current
+
+	// 	stathat.PostEZCount("queries~"+suffix, Config.StatHat.ApiKey, int(newQueries))
+	// 	stathat.PostEZValue("goroutines "+serverID, Config.StatHat.ApiKey, float64(runtime.NumGoroutine()))
+
+	// }
+}

+ 2 - 0
zone.go

@@ -19,6 +19,8 @@ type ZoneOptions struct {
 type ZoneLogging struct {
 	StatHat    bool
 	StatHatAPI string
+
+	InfluxDB bool
 }
 
 type Record struct {