2013-12-03 22:19:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-11-11 05:25:03 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2013-12-03 22:19:42 +00:00
|
|
|
"os"
|
2014-01-02 23:06:20 +00:00
|
|
|
"path/filepath"
|
2013-12-03 22:19:42 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2014-03-27 16:24:40 +00:00
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
"code.google.com/p/log4go"
|
2014-10-22 05:32:19 +00:00
|
|
|
"github.com/influxdb/influxdb"
|
2014-11-11 05:25:03 +00:00
|
|
|
"github.com/influxdb/influxdb/messaging"
|
2013-12-03 22:19:42 +00:00
|
|
|
)
|
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
const logo = `
|
|
|
|
+---------------------------------------------+
|
|
|
|
| _____ __ _ _____ ____ |
|
|
|
|
| |_ _| / _| | | __ \| _ \ |
|
|
|
|
| | | _ __ | |_| |_ ___ _| | | | |_) | |
|
|
|
|
| | | | '_ \| _| | | | \ \/ / | | | _ < |
|
|
|
|
| _| |_| | | | | | | |_| |> <| |__| | |_) | |
|
|
|
|
| |_____|_| |_|_| |_|\__,_/_/\_\_____/|____/ |
|
|
|
|
+---------------------------------------------+
|
|
|
|
`
|
|
|
|
|
2014-11-18 00:34:47 +00:00
|
|
|
// These variables are populated via the Go linker.
|
|
|
|
var (
|
|
|
|
version string
|
|
|
|
commit string
|
|
|
|
)
|
|
|
|
|
2013-12-03 22:19:42 +00:00
|
|
|
func main() {
|
2014-11-06 06:20:36 +00:00
|
|
|
if err := start(); err != nil {
|
2014-11-24 23:55:15 +00:00
|
|
|
fmt.Println(err.Error())
|
2014-11-04 20:36:15 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func start() error {
|
2014-10-21 05:32:47 +00:00
|
|
|
var (
|
2014-11-11 05:25:03 +00:00
|
|
|
fileName = flag.String("config", "config.sample.toml", "Config file")
|
|
|
|
showVersion = flag.Bool("v", false, "Get version number")
|
|
|
|
hostname = flag.String("hostname", "", "Override the hostname, the `hostname` config option will be overridden")
|
|
|
|
protobufPort = flag.Int("protobuf-port", 0, "Override the protobuf port, the `protobuf_port` config option will be overridden")
|
|
|
|
pidFile = flag.String("pidfile", "", "the pid file")
|
|
|
|
stdout = flag.Bool("stdout", false, "Log to stdout overriding the configuration")
|
|
|
|
syslog = flag.String("syslog", "", "Log to syslog facility overriding the configuration")
|
2014-10-21 05:32:47 +00:00
|
|
|
)
|
2013-12-03 22:19:42 +00:00
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
flag.Parse()
|
|
|
|
|
2014-11-18 00:34:47 +00:00
|
|
|
v := fmt.Sprintf("InfluxDB v%s (git: %s)", version, commit)
|
2014-10-22 05:32:19 +00:00
|
|
|
if *showVersion {
|
2014-05-29 23:53:11 +00:00
|
|
|
fmt.Println(v)
|
2014-11-04 20:36:15 +00:00
|
|
|
return nil
|
2013-12-03 22:19:42 +00:00
|
|
|
}
|
2014-04-28 19:10:08 +00:00
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
// Parse configuration.
|
|
|
|
config, err := ParseConfigFile(*fileName)
|
2014-08-02 13:58:57 +00:00
|
|
|
if err != nil {
|
2014-11-04 20:36:15 +00:00
|
|
|
return err
|
2014-08-02 13:58:57 +00:00
|
|
|
}
|
2014-10-22 05:32:19 +00:00
|
|
|
config.Version = v
|
|
|
|
config.InfluxDBVersion = version
|
2014-09-03 17:22:53 +00:00
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
// Override config properties.
|
|
|
|
if *hostname != "" {
|
2014-04-28 19:10:08 +00:00
|
|
|
config.Hostname = *hostname
|
|
|
|
}
|
2014-10-22 05:32:19 +00:00
|
|
|
if *protobufPort != 0 {
|
2014-11-11 05:25:03 +00:00
|
|
|
config.Cluster.ProtobufPort = *protobufPort
|
2014-04-29 19:15:17 +00:00
|
|
|
}
|
2014-09-03 20:22:51 +00:00
|
|
|
if *syslog != "" {
|
2014-10-22 05:32:19 +00:00
|
|
|
config.Logging.File = *syslog
|
|
|
|
} else if *stdout {
|
|
|
|
config.Logging.File = "stdout"
|
2014-09-03 19:41:56 +00:00
|
|
|
}
|
2014-10-22 05:32:19 +00:00
|
|
|
setupLogging(config.Logging.Level, config.Logging.File)
|
2014-09-03 19:41:56 +00:00
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
// Write pid file.
|
|
|
|
if *pidFile != "" {
|
2013-12-03 22:19:42 +00:00
|
|
|
pid := strconv.Itoa(os.Getpid())
|
|
|
|
if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 17:16:23 +00:00
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
// Initialize directories.
|
|
|
|
if err := os.MkdirAll(config.Storage.Dir, 0744); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-10-22 00:20:43 +00:00
|
|
|
// TODO(benbjohnson): Start admin server.
|
|
|
|
|
2014-01-22 18:35:42 +00:00
|
|
|
if config.BindAddress == "" {
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.Info("Starting Influx Server %s...", version)
|
2014-02-04 17:16:23 +00:00
|
|
|
} else {
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.Info("Starting Influx Server %s bound to %s...", version, config.BindAddress)
|
2014-02-04 17:16:23 +00:00
|
|
|
}
|
2014-10-22 05:32:19 +00:00
|
|
|
fmt.Printf(logo)
|
2013-12-09 15:37:13 +00:00
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
// Parse broker URLs from seed servers.
|
|
|
|
var brokerURLs []*url.URL
|
|
|
|
for _, s := range config.Cluster.SeedServers {
|
|
|
|
u, err := url.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
brokerURLs = append(brokerURLs, u)
|
2013-12-03 22:19:42 +00:00
|
|
|
}
|
2014-11-11 05:25:03 +00:00
|
|
|
|
|
|
|
// Create messaging client for broker.
|
|
|
|
client := messaging.NewClient("XXX-CHANGEME-XXX")
|
|
|
|
if err := client.Open(brokerURLs); err != nil {
|
2014-11-19 05:26:58 +00:00
|
|
|
log4go.Error("Error opening Messaging Client: %s", err.Error())
|
2014-03-27 23:09:08 +00:00
|
|
|
}
|
2014-03-27 16:24:40 +00:00
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
// Start server.
|
|
|
|
s := influxdb.NewServer(client)
|
|
|
|
|
|
|
|
// TODO: startProfiler()
|
|
|
|
// TODO: -reset-root
|
|
|
|
|
|
|
|
// Initialize HTTP handler.
|
|
|
|
h := influxdb.NewHandler(s)
|
|
|
|
|
|
|
|
// Start HTTP server.
|
|
|
|
func() { log.Fatal(http.ListenAndServe(":8086", h)) }() // TODO: Change HTTP port.
|
|
|
|
// TODO: Start HTTPS server.
|
|
|
|
|
|
|
|
// Wait indefinitely.
|
|
|
|
<-(chan struct{})(nil)
|
|
|
|
return nil
|
2013-12-03 22:19:42 +00:00
|
|
|
}
|
2014-10-21 05:32:47 +00:00
|
|
|
|
|
|
|
func setupLogging(loggingLevel, logFile string) {
|
2014-11-11 05:25:03 +00:00
|
|
|
level := log4go.DEBUG
|
2014-10-21 05:32:47 +00:00
|
|
|
switch loggingLevel {
|
2014-11-06 06:20:36 +00:00
|
|
|
case "trace":
|
2014-11-11 05:25:03 +00:00
|
|
|
level = log4go.TRACE
|
2014-11-06 06:20:36 +00:00
|
|
|
case "fine":
|
2014-11-11 05:25:03 +00:00
|
|
|
level = log4go.FINE
|
2014-10-21 05:32:47 +00:00
|
|
|
case "info":
|
2014-11-11 05:25:03 +00:00
|
|
|
level = log4go.INFO
|
2014-10-21 05:32:47 +00:00
|
|
|
case "warn":
|
2014-11-11 05:25:03 +00:00
|
|
|
level = log4go.WARNING
|
2014-10-21 05:32:47 +00:00
|
|
|
case "error":
|
2014-11-11 05:25:03 +00:00
|
|
|
level = log4go.ERROR
|
2014-11-06 06:20:36 +00:00
|
|
|
default:
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.Error("Unknown log level %s. Defaulting to DEBUG", loggingLevel)
|
2014-10-21 05:32:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.Global = make(map[string]*log4go.Filter)
|
2014-10-21 05:32:47 +00:00
|
|
|
|
|
|
|
facility, ok := GetSysLogFacility(logFile)
|
|
|
|
if ok {
|
|
|
|
flw, err := NewSysLogWriter(facility)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "NewSysLogWriter: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.AddFilter("syslog", level, flw)
|
2014-10-21 05:32:47 +00:00
|
|
|
} else if logFile == "stdout" {
|
2014-11-11 05:25:03 +00:00
|
|
|
flw := log4go.NewConsoleLogWriter()
|
|
|
|
log4go.AddFilter("stdout", level, flw)
|
2014-10-21 05:32:47 +00:00
|
|
|
} else {
|
|
|
|
logFileDir := filepath.Dir(logFile)
|
|
|
|
os.MkdirAll(logFileDir, 0744)
|
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
flw := log4go.NewFileLogWriter(logFile, false)
|
|
|
|
log4go.AddFilter("file", level, flw)
|
2014-10-21 05:32:47 +00:00
|
|
|
|
|
|
|
flw.SetFormat("[%D %T] [%L] (%S) %M")
|
|
|
|
flw.SetRotate(true)
|
|
|
|
flw.SetRotateSize(0)
|
|
|
|
flw.SetRotateLines(0)
|
|
|
|
flw.SetRotateDaily(true)
|
|
|
|
}
|
|
|
|
|
2014-11-11 05:25:03 +00:00
|
|
|
log4go.Info("Redirectoring logging to %s", logFile)
|
2014-10-21 05:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Stopper interface {
|
|
|
|
Stop()
|
|
|
|
}
|