예제 #1
0
파일: main.go 프로젝트: hanshenu/influxdb
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))
}
예제 #2
0
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)
}
예제 #3
0
파일: server.go 프로젝트: 9cat/influxdb
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()
}
예제 #4
0
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
}
예제 #5
0
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)
}
예제 #6
0
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)

}
예제 #7
0
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
}
예제 #8
0
파일: server.go 프로젝트: 9cat/influxdb
// 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
}
예제 #9
0
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)
	}
}
예제 #10
0
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)
}
예제 #11
0
파일: server.go 프로젝트: hyc/influxdb
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
}
예제 #12
0
파일: counter.go 프로젝트: speedland/lib
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
}
예제 #13
0
파일: server.go 프로젝트: 9cat/influxdb
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
}
예제 #14
0
// 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)
	}
}
예제 #15
0
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)
		}
	}
}
예제 #16
0
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)
}
예제 #17
0
// 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)
}
예제 #18
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)
}
예제 #19
0
파일: server.go 프로젝트: jhermann/influxdb
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})
	}
}
예제 #20
0
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)
			}
		}
	}
}
예제 #21
0
func (self *SingleServerSuite) TestInvalidDbUserCreation(c *C) {
	client, err := influxdb.NewClient(&influxdb.ClientConfig{})
	c.Assert(err, IsNil)

	c.Assert(client.CreateDatabaseUser("db999", "user", "pass"), NotNil)
}
예제 #22
0
func (self *DataTestClient) CreateDatabase(db string, c *C) {
	client, err := influxdb.NewClient(&influxdb.ClientConfig{})
	c.Assert(err, IsNil)
	c.Assert(client.CreateDatabase(db), IsNil)
}