Add a new service to InfluxDB in CentOS7.3
Operating System: CentOS7.3.1611 _ x64
Go language version: 1.8.3 linux/amd64
InfluxDB version: 1.1.0
Here, we use the syncd service as an example to record the process of adding a new service to InfluxDB.
Add master Service Code
Create a syncd folder in the influxdata/influxdb/services Directory to store the syncd service code.
1. Add service configurations
Add the config. go file. The example content is as follows:
package syncd
// E-Mail : Mike_Zhang@live.com
type Config struct {
Enabled bool `toml:"enabled"`
LogEnabled bool `toml:"log-enabled"`
RemoteTSDBHost string `toml:"remote-host"`
DefaultDB string `toml:"defaultDB"`
}
func NewConfig() Config { return Config{
Enabled: true,
LogEnabled: true,
RemoteTSDBHost: "http://127.0.0.1:8086",
DefaultDB: "Monitor",
}
}
Explanation:
- Define Config to store specific configurations;
- Add the NewConfig function to create a Config object;
2. Add the specific content of the syncd Service
Add the syncd. go file. The example content is as follows:
package syncd
// E-Mail : Mike_Zhang@live.com
import ( "log" "os" "time" )
type Service struct {
Logger *log.Logger
remote_host string DefaultDB string username string password string }
func NewService(c Config) *Service { return &Service{
remote_host: c.RemoteTSDBHost,
DefaultDB: c.DefaultDB,
username: "root",
password: "root",
Logger: log.New(os.Stderr, "[syncd] ", log.LstdFlags),
}
}
func (s *Service) Run() { for {
cur_time := time.Now().Unix()
s.Logger.Printf("current timestamp : %d\n", cur_time)
time.Sleep(1 * time.Second)
}
Explanation: * defines the Service structure;
- Add the NewService function to create a specific service;
- Add the Run function to implement specific services.
This function is used as an entry to implement specific services. Here, the timestamp is printed regularly in the log as the specific service content.
Start the service in the server master program
1. Add configuration code
Go to the influxdata/influxdb/cmd/influxd directory and modify the run/config. go file.
- Introduce the syncd Service
Add the following code to import:
"github.com/influxdata/influxdb/services/syncd"
Add the following variables to the Config structure:
SyncdConfig syncd.Config `toml:"syncd"`
Add the following code to the NewConfig function:
c.SyncdConfig = syncd.NewConfig()
Run the git diff command to view the following information:
[root@localhost run]# git diff config.go
diff --git a/cmd/influxd/run/config.go b/cmd/influxd/run/config.go
index 36e4f14..01df0cc 100644
--- a/cmd/influxd/run/config.go
+++ b/cmd/influxd/run/config.go
@@ -27,6 +27,7 @@ import (
"github.com/influxdata/influxdb/services/precreator"
"github.com/influxdata/influxdb/services/retention"
"github.com/influxdata/influxdb/services/subscriber"
+ "github.com/influxdata/influxdb/services/syncd"
"github.com/influxdata/influxdb/services/udp"
"github.com/influxdata/influxdb/tsdb"
)
@@ -48,6 +49,7 @@ type Config struct {
Monitor monitor.Config `toml:"monitor"`
Subscriber subscriber.Config `toml:"subscriber"`
HTTPD httpd.Config `toml:"http"`
+ SyncdConfig syncd.Config `toml:"syncd"`
GraphiteInputs []graphite.Config `toml:"graphite"`
CollectdInputs []collectd.Config `toml:"collectd"`
OpenTSDBInputs []opentsdb.Config `toml:"opentsdb"`
@@ -84,6 +86,7 @@ func NewConfig() *Config {
c.Retention = retention.NewConfig()
c.BindAddress = DefaultBindAddress
+ c.SyncdConfig = syncd.NewConfig()
return c
}
[root@localhost run]#
2. Add the startup Service Code
Go to the influxdata/influxdb/cmd/influxd directory and modify the run/command. go file.
- Introduce the syncd Service
Add the following code to import:
"github.com/influxdata/influxdb/services/syncd"
Add the following code to the Command-> Run function (before go cmd. monitorServerErrors ):
// start syncdsyncdInstance := syncd.NewService(config.SyncdConfig)go syncdInstance.Run()
Add the following variables to the Config structure:
Run the git diff command to view the following information:
[root@localhost run]# git diff command.go
diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go
index 51036f1..8743f04 100644
--- a/cmd/influxd/run/command.go
+++ b/cmd/influxd/run/command.go
@@ -1,6 +1,7 @@
package run
import (
+ "github.com/influxdata/influxdb/services/syncd"
"flag"
"fmt"
"io"
@@ -120,6 +121,11 @@ func (cmd *Command) Run(args ...string) error {
}
cmd.Server = s
+ // start syncd
+ syncdInstance := syncd.NewService(config.SyncdConfig)
+ go syncdInstance.Run()
+
+
// Begin monitoring the server's error channel.
go cmd.monitorServerErrors()
[root@localhost run]#
Test Service
Go to the influxdata/influxdb/cmd/influxd directory, execute the go build command, and copy the compiled binary file to the bin directory, as shown below:
[root@localhost influxd]# go build
[root@localhost influxd]# cp influxd /usr/bin/ cp: overwrite ‘/usr/bin/influxd’? y
[root@localhost influxd]#
Start the InfluxDB server and you can see the following in the console:
[root@localhost influxdb]# influxd
8888888 .d888 888 8888888b. 888888b.
888 d88P" 888 888 "Y88b 888 "88b
888 888 888 888 888 888 .88P
888 88888b. 888888 888 888 888 888 888 888 888 8888888K.
888 888 "88b 888 888 888 888 Y8bd8P' 888 888 888 "Y88b
888 888 888 888 888 888 888 X88K 888 888 888 888
888 888 888 888 888 Y88b 888 .d8""8b. 888 .d88P 888 d88P
8888888 888 888 888 888 "Y88888 888 888 8888888P" 8888888P"
[run] 2018/02/07 04:24:28 InfluxDB starting, version unknown, branch unknown, commit unknown
[run] 2018/02/07 04:24:28 Go version go1.8.3, GOMAXPROCS set to 2
[run] 2018/02/07 04:24:28 Using configuration at: /etc/influxdb/influxdb.conf
[store] 2018/02/07 04:24:29 Using data dir: /var/lib/influxdb/data
...
[syncd] 2018/02/07 21:56:11 current timestamp : 1518058571
[syncd] 2018/02/07 21:56:12 current timestamp : 1518058572
[syncd] 2018/02/07 21:56:13 current timestamp : 1518058573
Generate a new configuration file:
influxd config > new.conf
The default configuration of the syncd service is as follows:
[syncd]
enabled = true
log-enabled = true
remote-host = "http://127.0.0.1:8086"
defaultDB = "Monitor"
Okay, that's all. I hope it will help you.