-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvnx2graphite.go
160 lines (137 loc) · 3.91 KB
/
vnx2graphite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"os"
"bufio"
"bytes"
"io"
"fmt"
"strings"
"time"
"net"
)
import goopt "github.com/droundy/goopt"
import config "github.com/kless/goconfig/config"
var version = "0.1"
///////////////////////////////////
//
// Taken form goopt example
//
///////////////////////////////////
// The Flag function creates a boolean flag, possibly with a negating
// alternative. Note that you can specify either long or short flags
// naturally in the same list.
var amVerbose = goopt.Flag([]string{"-v", "--verbose"}, []string{"--quiet"},
"output verbosely this will also show the graphite data", "be quiet, instead")
// This is just a logging function that uses the verbosity flags to
// decide whether or not to log anything.
func log(x ...interface{}) {
if *amVerbose {
fmt.Println(x...)
}
}
///////////////////////////////////
///////////////////////////////////
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func readLines(path string) (lines []string, err error) {
var (
file *os.File
part []byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
defer file.Close()
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte, 0))
for {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
//replace slashes and whitespaces with underscore
func stringify(tstring string) (stringified string) {
str := strings.Replace(tstring, "\"", "", -1)
str = strings.Replace(str, "/", "_", -1)
stringified = strings.Replace(str, " ", "_", -1)
return
}
var opt_conf = goopt.String([]string{"-c", "--config"}, "config file", "path to config file")
var opt_data = goopt.String([]string{"-d", "--data"}, "data csv", "path to data csv file")
var opt_statsname = goopt.String([]string{"-s", "--statsname"}, "nfs", "extending name for the bucket: $basename.nfs")
var opt_mover = goopt.String([]string{"-m", "--datamover"}, "server_2", "extending name for the bucket: $basename.$movername.nfs")
func main() {
goopt.Version = version
goopt.Summary = "send emc vnx performance data to graphite"
goopt.Parse(nil)
if f, _ := exists(*opt_conf); f == false {
fmt.Print(goopt.Help())
fmt.Println("ERROR: config file " + *opt_conf + " doesn't exist")
return
}
c, _ := config.ReadDefault(*opt_conf)
host, _ := c.String("graphite", "host")
port, _ := c.Int("graphite", "port")
timeout, _ := c.Int("graphite", "timeout")
basename, _ := c.String("graphite", "basename")
//todo: we also can parse this from the output...
timestamp := time.Now().Unix()
log(fmt.Sprintf("using graphite with host %s:%d", host, port))
if f, _ := exists(*opt_data); f == false {
fmt.Print(goopt.Help())
fmt.Println("ERROR: data file " + *opt_data + " doesn't exist")
return
}
lines, err := readLines(*opt_data)
if err != nil {
fmt.Println("ERROR:", err)
return
}
//we only want to use the head and the first line of data
head := strings.Split(lines[0], ",")
data := strings.Split(lines[1], ",")
if len(head) != len(data) {
fmt.Println("ERROR: malformed csv (length of head != length of data")
return
}
//create the graphite connection.
// Todo: export this in a small pkg
cstr := fmt.Sprintf("%s:%d", host, port)
log("trying to connect to " + cstr)
conn, err := net.DialTimeout("tcp", cstr, time.Duration(timeout)*time.Second)
if err != nil {
fmt.Println("ERROR:", err)
return
}
for n, val := range head {
key := stringify(val)
value := stringify(data[n])
if key != "Timestamp" {
msg := fmt.Sprintf("%s.%s.%s.%s %s %d", basename, *opt_mover, *opt_statsname, key, value, timestamp)
log("sending: " + msg)
fmt.Fprint(conn, "\n"+msg+"\n")
} else {
log("Timestamp: ... next...")
}
}
conn.Close()
}