/
influx.go
146 lines (125 loc) · 3.09 KB
/
influx.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/influxdb/influxdb/models"
"github.com/mitchellh/ioprogress"
)
var (
points chan string // communication channel for store function
)
// openFile writer which stores (appends) data to influx.data file
func openFile(filename string) io.Writer {
log.Println("influx out file:", filename)
writer, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
ok(err)
return writer
}
// openInflux goroutine and returns writer
func openInflux(url string) io.Writer {
log.Println("openInflux:", url)
pr, pw := io.Pipe()
scanner := bufio.NewScanner(pr)
scanner.Split(multiScanLinesFactory(*influxBatch))
client := http.Client{}
go func() {
// wg.Add(1)
// defer wg.Done()
for scanner.Scan() {
body := bytes.NewBufferString(scanner.Text())
// log.Println("influx push: ", body.Len())
req, err := http.NewRequest("POST", url, body)
ok(err)
resp, err := client.Do(req)
if (warn(err) && resp != nil) || resp.StatusCode != 204 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("influx resposone warn: %s\n", b)
}
log.Fatalf("err: %q %q\n", resp.Status, string(b))
}
}
}()
return pw
}
// start goroutines that move points to file and optionally to influxdb
func initInflux(influxUrl string) {
points = make(chan string)
var writer io.Writer
switch influxUrl {
case "null":
writer = ioutil.Discard
case "stdout":
writer = os.Stdout
default:
u, err := url.Parse(influxUrl)
ok(err)
if u.Scheme == "http" {
writer = openInflux(influxUrl)
} else {
writer = openFile(u.Host)
}
}
go func() {
for {
select {
case s := <-points:
_, err := io.WriteString(writer, s)
ok(err)
case <-quit:
wg.Done()
return
}
}
}()
}
// store point in points channel serialized as line protocol from influx
func store(name string, tags map[string]string, fields map[string]interface{}) {
if tags == nil {
tags = map[string]string{"name": *NAME}
} else {
tags["name"] = *NAME
}
p, err := models.NewPoint(name, tags, fields, time.Now())
if err != nil {
panic(err)
}
points <- p.String() + "\n"
}
func storeLog(values ...string) {
msg := strings.Join(values, " ")
log.Println(msg)
store("logs", nil, map[string]interface{}{"message": msg})
}
// influxFlush copies reads data from reader into influx given by
// uses store function (line by line)
func feedInflux(srcFilename, dstUrl string) {
if strings.Contains(dstUrl, srcFilename) {
fmt.Printf("please specify other destination through influxUrl=%q than src file=%q\n", dstUrl, srcFilename)
os.Exit(1)
}
srcFile, err := os.Open(srcFilename)
ok(err)
finfo, err := srcFile.Stat()
ok(err)
size := finfo.Size()
// Create the progress reader
src := &ioprogress.Reader{
Reader: srcFile,
Size: size,
DrawFunc: ioprogress.DrawTerminalf(os.Stderr, ioprogress.DrawTextFormatBar(80)),
}
dst := openInflux(dstUrl)
n, err := io.Copy(dst, src)
ok(err)
fmt.Printf("copied %d bytes from %q to %q\n", n, srcFilename, dstUrl)
}