Browse Source

Initial commit

tags/v0.1
Bernd Zeimetz 3 years ago
commit
12650f51c2
5 changed files with 176 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +6
    -0
      .travis.yml
  3. +21
    -0
      LICENSE.md
  4. +18
    -0
      Makefile
  5. +128
    -0
      dump.go

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
whisper-to-graphite
*.swp
_vendor

+ 6
- 0
.travis.yml View File

@@ -0,0 +1,6 @@
language: go
go:
- 1.8

script:
- make test

+ 21
- 0
LICENSE.md View File

@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Bernd Zeimetz <bernd@bzed.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

+ 18
- 0
Makefile View File

@@ -0,0 +1,18 @@
#!/usr/bin/make -f

NAME:=whisper-to-graphite

GO ?= go

all: $(NAME)

$(NAME): *.go
$(GO) get
$(GO) build

clean:
rm -f $(NAME)
test: $(NAME)
go vet *.go

.PHONY: clean test

+ 128
- 0
dump.go View File

@@ -0,0 +1,128 @@
package main

import (
"errors"
"flag"
"github.com/lomik/go-whisper"
"github.com/marpaia/graphite-golang"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)

func convertFilename(filename string, baseDirectory string) (string, error) {
absFilename, err := filepath.Abs(filename)
if err != nil {
return "", err
}
absBaseDirectory, err := filepath.Abs(baseDirectory)
if err != nil {
return "", err
}
err = nil
if strings.HasPrefix(absFilename, absBaseDirectory) {
metric := strings.Replace(
strings.TrimPrefix(
strings.TrimPrefix(
absFilename,
absBaseDirectory),
"/"),
"/",
".",
-1)
return metric, err
}
err = errors.New("Path for whisper file does not live in BasePath")
return "", err
}

func sendWhisperData(filename string, baseDirectory string, graphiteHost string, graphitePort int, graphiteProtocol string) error {
metricName, err := convertFilename(filename, baseDirectory)
if err != nil {
return err
}

graphiteConn, err := graphite.GraphiteFactory(graphiteProtocol, graphiteHost, graphitePort, "")
if err != nil {
return err
}

whisperData, err := whisper.Open(filename)
if err != nil {
return err
}

timeSeriesdata, err := whisperData.Fetch(whisperData.StartTime(), int(time.Now().Unix()))
if err != nil {
return err
}

point_count := 0
metrics := make([]graphite.Metric, 1000)
for _, point := range timeSeriesdata.Points() {
v := strconv.FormatFloat(point.Value, 'f', -1, 64)
metrics[point_count] = graphite.NewMetric(metricName, v, int64(point.Time))
point_count++
if point_count >= 1000 {
err = graphiteConn.SendMetrics(metrics)
if err != nil {
return err
}
point_count = 0
metrics = make([]graphite.Metric, 1000)
}
}

err = nil
return err
}

func findWhisperFiles(directory string) chan string {
ch := make(chan string)
visit := func(path string, info os.FileInfo, err error) error {
if (info != nil) && !info.IsDir() {
if strings.HasSuffix(path, ".wsp") {
ch <- path
}
}
return nil
}
err := filepath.Walk(directory, visit)
if err != nil {
log.Fatalln(err)
}
close(ch)
return ch
}

func main() {

baseDirectory := flag.String(
"basedirectory",
"/var/lib/graphite/whisper",
"Base directory where whisper files are located. Used to retrieve the metric name from the filename.")
directory := flag.String(
"directory",
"/var/lib/graphite/whisper/collectd",
"Directory containing the whisper files you want to send to graphite again")
graphiteHost := flag.String(
"host",
"127.0.0.1",
"Hostname/IP of the graphite server")
graphitePort := flag.Int(
"port",
2003,
"graphite Port")
graphiteProtocol := flag.String(
"protocol",
"tcp",
"Protocol to use to transfer graphite data (tcp/udp/nop)")
flag.Parse()

for wspFile := range findWhisperFiles(*directory) {
sendWhisperData(wspFile, *baseDirectory, *graphiteHost, *graphitePort, *graphiteProtocol)
}
}

Loading…
Cancel
Save