Skip to content

Commit b72cf93

Browse files
committed
initial import
0 parents  commit b72cf93

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sqlinflux
2+
*.swp
3+
*.yaml

sqlinflux.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"github.com/influxdata/influxdb/client/v2"
7+
_ "github.com/minus5/gofreetds"
8+
"gopkg.in/yaml.v2"
9+
"io/ioutil"
10+
"os"
11+
"time"
12+
)
13+
14+
type Config struct {
15+
Dsn string
16+
Query string
17+
Translations map[string]string
18+
Tags map[string]string
19+
}
20+
21+
func main() {
22+
filename := os.Args[1]
23+
24+
c := Config{}
25+
26+
s, err := ioutil.ReadFile(filename)
27+
if err != nil {
28+
panic(err)
29+
}
30+
31+
err = yaml.Unmarshal(s, &c)
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
db, err := sql.Open("mssql", c.Dsn)
37+
if err != nil {
38+
panic(err)
39+
}
40+
defer db.Close()
41+
42+
rows, err := db.Query(c.Query)
43+
if err != nil {
44+
panic(err)
45+
}
46+
defer rows.Close()
47+
48+
fields := make(map[string]interface{})
49+
for rows.Next() {
50+
var Variable string
51+
var Value uint
52+
err = rows.Scan(&Variable, &Value)
53+
if err != nil {
54+
panic(err)
55+
}
56+
if val, ok := c.Translations[Variable]; ok {
57+
Variable = val
58+
}
59+
fields[Variable] = Value
60+
}
61+
Pt, err := client.NewPoint("sqlValues", c.Tags, fields, time.Now().UTC().Round(time.Minute))
62+
if err != nil {
63+
panic(err)
64+
}
65+
fmt.Printf("%s\n", Pt.String())
66+
}

0 commit comments

Comments
 (0)