func main() { numberOfSeries := 50000 if len(os.Args) > 1 { numberOfSeries, _ = strconv.Atoi(os.Args[1]) } fmt.Printf("Benchmarking writing %d series\n", numberOfSeries) client, err := influxdb.NewClient(&influxdb.ClientConfig{}) if err != nil { panic(err) } before := time.Now() client.DeleteDatabase("performance") fmt.Printf("Deleting took %s\n", time.Now().Sub(before)) os.Exit(0) if err := client.CreateDatabase("performance"); err != nil { panic(err) } client, err = influxdb.NewClient(&influxdb.ClientConfig{ Database: "performance", }) if err != nil { panic(err) } before = time.Now() for i := 0; i < 10; i++ { series := []*influxdb.Series{} for i := 0; i < numberOfSeries; i++ { name := fmt.Sprintf("series_%d", i+1) series = append(series, &influxdb.Series{ Name: name, Columns: []string{"value"}, Points: [][]interface{}{ {rand.Float64()}, }, }) } if err := client.WriteSeries(series); err != nil { panic(err) } } fmt.Printf("Writing took %s\n", time.Now().Sub(before)) }
func (self *SingleServerSuite) TestUserWritePermissions(c *C) { rootUser := self.server.GetClient("", c) // create two users one that can only read and one that can only write. both can access test_should_read // series only /* c.Assert(rootUser.CreateDatabase("db1"), IsNil) */ c.Assert(rootUser.CreateDatabaseUser("db1", "limited_user", "pass", "^$", "^$"), IsNil) config := &influxdb.ClientConfig{ Username: "******", Password: "******", Database: "db1", } user, err := influxdb.NewClient(config) c.Assert(err, IsNil) data := ` [ { "points": [ [1] ], "name": "test_should_write", "columns": ["value"] } ]` invalidData := ` [ { "points": [ [2] ], "name": "test_should_not_write", "columns": ["value"] } ]` series := []*influxdb.Series{} c.Assert(json.Unmarshal([]byte(data), &series), IsNil) // readUser shouldn't be able to write c.Assert(user.WriteSeries(series), NotNil) content := self.server.RunQueryAsRoot("select * from test_should_write", "m", c) c.Assert(content, HasLen, 0) rootUser.ChangeDatabaseUser("db1", "limited_user", "pass", false, "^$", "test_should_write") // write the data to test the write permissions c.Assert(user.WriteSeries(series), IsNil) self.server.WaitForServerToSync() invalidSeries := []*influxdb.Series{} content = self.server.RunQueryAsRoot("select * from test_should_write", "m", c) c.Assert(content, HasLen, 1) c.Assert(json.Unmarshal([]byte(invalidData), &invalidSeries), IsNil) c.Assert(user.WriteSeries(invalidSeries), NotNil) self.server.WaitForServerToSync() content = self.server.RunQueryAsRoot("select * from test_should_not_write", "m", c) c.Assert(content, HasLen, 0) rootUser.ChangeDatabaseUser("db1", "limited_user", "pass", false, "^$", "test_.*") c.Assert(user.WriteSeries(invalidSeries), IsNil) self.server.WaitForServerToSync() content = self.server.RunQueryAsRoot("select * from test_should_not_write", "m", c) c.Assert(content, HasLen, 1) }
func (self *Server) WriteData(data interface{}, c *C, precision ...influxdb.TimePrecision) { client, err := influxdb.NewClient(&influxdb.ClientConfig{ Username: "******", Password: "******", Database: "db1", }) c.Assert(err, IsNil) var series []*influxdb.Series switch x := data.(type) { case string: c.Assert(json.Unmarshal([]byte(x), &series), IsNil) case []*influxdb.Series: series = x default: c.Fatalf("Unknown type: %T", x) } if len(precision) == 0 { err = client.WriteSeries(series) } else { err = client.WriteSeriesWithTimePrecision(series, precision[0]) } c.Assert(err, IsNil) self.WaitForServerToSync() }
func (self *DataTestClient) RunInvalidQuery(query string, c *C, timePrecision ...influxdb.TimePrecision) []*influxdb.Series { client, err := influxdb.NewClient(&influxdb.ClientConfig{Database: self.db}) c.Assert(err, IsNil) _, err = client.Query(query, timePrecision...) c.Assert(err, NotNil) return nil }
func (self *SingleServerSuite) createUser(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabase("db1"), IsNil) c.Assert(client.CreateDatabaseUser("db1", "user", "pass"), IsNil) c.Assert(client.AlterDatabasePrivilege("db1", "user", true), IsNil) }
func (self *SingleServerSuite) TestSslOnly(c *C) { self.server.Stop() // TODO: don't hard code the path here c.Assert(os.RemoveAll("/tmp/influxdb/development"), IsNil) server := NewSslServer("src/integration/test_ssl_only.toml", c) server.WaitForServerToStart() defer func() { server.Stop() self.server = NewServer("src/integration/test_config_single.toml", c) }() client, err := influxdb.NewClient(&influxdb.ClientConfig{ IsSecure: true, HttpClient: &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, }, }) c.Assert(err, IsNil) c.Assert(client.Ping(), IsNil) }
func (self *DataTestClient) RunQueryWithNumbers(query string, c *C, timePrecision ...influxdb.TimePrecision) []*influxdb.Series { client, err := influxdb.NewClient(&influxdb.ClientConfig{Database: self.db}) c.Assert(err, IsNil) series, err := client.QueryWithNumbers(query, timePrecision...) c.Assert(err, IsNil) return series }
// optional db func (self *Server) GetClient(db string, c *C) *influxdb.Client { client, err := influxdb.NewClient(&influxdb.ClientConfig{ Host: fmt.Sprintf("localhost:%d", self.apiPort), Database: db, }) c.Assert(err, IsNil) return client }
func (self *DataTestClient) WriteData(series []*influxdb.Series, c *C, timePrecision ...influxdb.TimePrecision) { client, err := influxdb.NewClient(&influxdb.ClientConfig{Database: self.db}) c.Assert(err, IsNil) if len(timePrecision) == 0 { c.Assert(client.WriteSeries(series), IsNil) } else { c.Assert(client.WriteSeriesWithTimePrecision(series, timePrecision[0]), IsNil) } }
func (self *SingleServerSuite) TestShortPasswords(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabaseUser("shahid", "shahid", "1"), Not(ErrorMatches), ".*blowfish.*") // should be able to recreate the user c.Assert(client.CreateDatabaseUser("shahid", "shahid", "shahid"), IsNil) c.Assert(client.AuthenticateDatabaseUser("shahid", "shahid", "shahid"), IsNil) }
func (self *Server) GetClientWithUser(db, username, password string, c *C) *influxdb.Client { client, err := influxdb.NewClient(&influxdb.ClientConfig{ Host: fmt.Sprintf("localhost:%d", self.apiPort), Username: username, Password: password, Database: db, }) c.Assert(err, IsNil) return client }
func NewCounterClient(httpClient *http.Client) (*CounterClient, error) { client, err := influx.NewClient(&influx.ClientConfig{ Host: fmt.Sprintf("%s:%d", InfluxConfig.Host, InfluxConfig.Port), Username: InfluxConfig.Username, Password: InfluxConfig.Password, Database: InfluxConfig.Database, IsSecure: InfluxConfig.IsSecure, HttpClient: httpClient, }) return &CounterClient{influxClient: client}, err }
func (self *Server) RunQueryAsUser(query string, precision influxdb.TimePrecision, username, password string, isValid bool, c *C) []*influxdb.Series { client, err := influxdb.NewClient(&influxdb.ClientConfig{ Username: username, Password: password, Database: "db1", }) c.Assert(err, IsNil) series, err := client.Query(query, precision) if isValid { c.Assert(err, IsNil) } else { c.Assert(err, NotNil) } return series }
// issue #378 func (self *SingleServerSuite) TestDeletingNewDatabase(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabase("delete0"), IsNil) s := CreatePoints("data_resurrection", 1, 10) self.server.WriteData(s, c) self.server.WaitForServerToSync() fmt.Printf("wrote some data\n") for i := 0; i < 2; i++ { c.Assert(client.CreateDatabase("delete1"), IsNil) c.Assert(client.DeleteDatabase("delete1"), IsNil) c.Assert(client.Ping(), IsNil) } }
func Influxdb(r metrics.Registry, d time.Duration, config *Config) { client, err := influxClient.NewClient(&influxClient.ClientConfig{ Host: config.Host, Database: config.Database, Username: config.Username, Password: config.Password, }) if err != nil { log.Println(err) return } for _ = range time.Tick(d) { if err := send(r, client); err != nil { log.Println(err) } } }
func (self *SingleServerSuite) TestSingleServerHostnameChange(c *C) { self.server.Stop() // TODO: don't hard code the path here c.Assert(os.RemoveAll("/tmp/influxdb/development"), IsNil) server := NewServerWithArgs("src/integration/test_config_single.toml", c, "-hostname", "foo") server.WaitForServerToStart() server.Stop() server = NewServerWithArgs("src/integration/test_config_single.toml", c, "-hostname", "bar") server.WaitForServerToStart() defer func() { server.Stop() self.server = NewServer("src/integration/test_config_single.toml", c) }() client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.Ping(), IsNil) }
// test for issue #41 func (self *SingleServerSuite) TestDbDelete(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) self.server.WriteData(` [ { "name": "test_deletetions", "columns": ["val1", "val2"], "points":[["v1", 2]] } ]`, c, "s") data := self.server.RunQuery("select val1 from test_deletetions", "m", c) c.Assert(data, HasLen, 1) c.Assert(client.DeleteDatabase("db1"), IsNil) self.createUser(c) // this shouldn't return any data data = self.server.RunQuery("select val1 from test_deletetions", "m", c) c.Assert(data, HasLen, 0) }
func (self *SingleServerSuite) TestUserPermissions(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabaseUser("db1", "limited_user", "pass", "test_should_read", ".*"), IsNil) data := ` [ { "points": [ [1] ], "name": "test_should_read", "columns": ["value"] }, { "points": [ [2] ], "name": "test_should_not_read", "columns": ["value"] } ]` self.server.WriteData(data, c) series := self.server.RunQueryAsUser("select value from test_should_read", "s", "limited_user", "pass", true, c) c.Assert(series[0].Points, HasLen, 1) c.Assert(series[0].Points[0][2], Equals, float64(1)) _ = self.server.RunQueryAsUser("select value from test_should_not_read", "s", "limited_user", "pass", false, c) series = self.server.RunQueryAsUser("select value from /.*/", "s", "limited_user", "pass", true, c) c.Assert(series, HasLen, 1) c.Assert(series[0].Name, Equals, "test_should_read") client.UpdateDatabaseUserPermissions("db1", "limited_user", ".*", ".*") self.server.WaitForServerToSync() series = self.server.RunQueryAsUser("select value from /.*/", "s", "limited_user", "pass", true, c) c.Assert(series, HasLen, 2) }
func (self *Server) reportStats() { client, err := influxdb.NewClient(&influxdb.ClientConfig{ Database: "reporting", Host: "m.influxdb.com:8086", Username: "******", Password: "******", }) if err != nil { log.Error("Couldn't create client for reporting: %s", err) } else { series := &influxdb.Series{ Name: "reports", Columns: []string{"os", "arch", "id", "version"}, Points: [][]interface{}{ {runtime.GOOS, runtime.GOARCH, self.RaftServer.GetRaftName(), self.Config.InfluxDBVersion}, }, } log.Info("Reporting stats: %#v", series) client.WriteSeries([]*influxdb.Series{series}) } }
func main() { flag.Parse() // Scan the arguments list if versionFlag { fmt.Println("Version:", APP_VERSION) } else { // Fill InfluxDB connection settings var client *influxClient.Client = nil if databaseFlag != "" { config := new(influxClient.ClientConfig) config.Host = hostFlag config.Username = usernameFlag config.Password = passwordFlag config.Database = databaseFlag var err error client, err = influxClient.NewClient(config) if err != nil { panic(err) } if err := createDatabase(client); err != nil { panic(err) } } // Build collect list var collectList []GatherFunc for _, c := range strings.Split(collectFlag, ",") { switch strings.Trim(c, " ") { case "cpus": collectList = append(collectList, cpus) case "mem": collectList = append(collectList, mem) case "swap": collectList = append(collectList, swap) case "uptime": collectList = append(collectList, uptime) case "load": collectList = append(collectList, load) case "network": collectList = append(collectList, network) case "disks": collectList = append(collectList, disks) default: fmt.Fprintf(os.Stderr, "Unknown collect option `%s'\n", c) return } } if prefixFlag != "" && prefixFlag[len(prefixFlag)-1] != '.' { prefixFlag += "." } ch := make(chan *influxClient.Series, len(collectList)) // Without daemon mode, do at least one lap first := true for first || daemonFlag { first = false // Collect data var data []*influxClient.Series for _, cl := range collectList { go cl(prefixFlag, ch) } for i := len(collectList); i > 0; i-- { res := <-ch if res != nil { data = append(data, res) } else if !daemonFlag { // Loop if we haven't all data: // Since diffed data didn't respond the // first time they are collected, loop // one more time to have it first = true } } // Show data if !first && (databaseFlag == "" || verboseFlag != "") { if strings.ToLower(verboseFlag) == "text" || verboseFlag == "" { prettyPrinter(data) } else { b, _ := json.Marshal(data) fmt.Printf("%s\n", b) } } // Send data if client != nil && !first { if err := send(client, data); err != nil { if daemonFlag { fmt.Printf("error:", err) } else { panic(err) } } } if daemonFlag || first { time.Sleep(daemonIntervalFlag) } } } }
func (self *SingleServerSuite) TestInvalidDbUserCreation(c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabaseUser("db999", "user", "pass"), NotNil) }
func (self *DataTestClient) CreateDatabase(db string, c *C) { client, err := influxdb.NewClient(&influxdb.ClientConfig{}) c.Assert(err, IsNil) c.Assert(client.CreateDatabase(db), IsNil) }