2015-07-10 15:54:15 +00:00
|
|
|
package influxdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/client"
|
|
|
|
"github.com/influxdb/telegraf/outputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InfluxDB struct {
|
|
|
|
URL string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Database string
|
|
|
|
UserAgent string
|
2015-08-07 20:31:25 +00:00
|
|
|
Tags map[string]string
|
2015-07-10 15:54:15 +00:00
|
|
|
|
|
|
|
conn *client.Client
|
|
|
|
}
|
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
func (i *InfluxDB) Connect(host string) error {
|
2015-07-10 15:54:15 +00:00
|
|
|
u, err := url.Parse(i.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := client.NewClient(client.Config{
|
|
|
|
URL: *u,
|
|
|
|
Username: i.Username,
|
|
|
|
Password: i.Password,
|
|
|
|
UserAgent: i.UserAgent,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-07 20:31:25 +00:00
|
|
|
if i.Tags == nil {
|
|
|
|
i.Tags = make(map[string]string)
|
|
|
|
}
|
|
|
|
i.Tags["host"] = host
|
|
|
|
|
2015-07-10 15:54:15 +00:00
|
|
|
i.conn = c
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *InfluxDB) Write(bp client.BatchPoints) error {
|
|
|
|
bp.Database = i.Database
|
2015-08-07 20:31:25 +00:00
|
|
|
bp.Tags = i.Tags
|
2015-07-10 15:54:15 +00:00
|
|
|
if _, err := i.conn.Write(bp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
outputs.Add("influxdb", func() outputs.Output {
|
|
|
|
return &InfluxDB{}
|
|
|
|
})
|
|
|
|
}
|