influxdb/cmd/influxd/main.go

189 lines
4.8 KiB
Go
Raw Normal View History

package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"time"
log "code.google.com/p/log4go"
2014-10-22 05:32:19 +00:00
"github.com/influxdb/influxdb"
"github.com/influxdb/influxdb/configuration"
"github.com/influxdb/influxdb/coordinator"
"github.com/jmhodges/levigo"
)
2014-10-22 05:32:19 +00:00
const logo = `
+---------------------------------------------+
| _____ __ _ _____ ____ |
| |_ _| / _| | | __ \| _ \ |
| | | _ __ | |_| |_ ___ _| | | | |_) | |
| | | | '_ \| _| | | | \ \/ / | | | _ < |
| _| |_| | | | | | | |_| |> <| |__| | |_) | |
| |_____|_| |_|_| |_|\__,_/_/\_\_____/|____/ |
+---------------------------------------------+
`
func main() {
if err := start(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
func start() error {
2014-10-21 05:32:47 +00:00
var (
fileName = flag.String("config", "config.sample.toml", "Config file")
2014-10-22 05:32:19 +00:00
showVersion = flag.Bool("v", false, "Get version number")
2014-10-21 05:32:47 +00:00
resetRootPassword = flag.Bool("reset-root", false, "Reset root password")
hostname = flag.String("hostname", "", "Override the hostname, the `hostname` config option will be overridden")
raftPort = flag.Int("raft-port", 0, "Override the raft port, the `raft.port` 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")
repairLeveldb = flag.Bool("repair-ldb", false, "set to true to repair the leveldb files")
stdout = flag.Bool("stdout", false, "Log to stdout overriding the configuration")
syslog = flag.String("syslog", "", "Log to syslog facility overriding the configuration")
)
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
2014-10-22 05:32:19 +00:00
c := fmt.Sprintf("InfluxDB v%s (git: %s) (leveldb: %d.%d)", version, gitSha, levigo.GetLevelDBMajorVersion(), levigo.GetLevelDBMinorVersion())
if *showVersion {
2014-05-29 23:53:11 +00:00
fmt.Println(v)
return nil
}
2014-10-22 05:32:19 +00:00
// Parse configuration.
config, err := ParseConfigFile(*fileName)
if err != nil {
return err
}
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 != "" {
config.Hostname = *hostname
}
2014-10-22 05:32:19 +00:00
if *raftPort != 0 {
config.RaftServerPort = *raftPort
}
2014-10-22 05:32:19 +00:00
if *protobufPort != 0 {
config.ProtobufPort = *protobufPort
}
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 != "" {
pid := strconv.Itoa(os.Getpid())
if err := ioutil.WriteFile(*pidFile, []byte(pid), 0644); err != nil {
panic(err)
}
}
2014-10-22 05:32:19 +00:00
// Initialize directories.
if err := os.MkdirAll(config.Raft.Dir, 0744); err != nil {
panic(err)
}
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.
if config.BindAddress == "" {
log.Info("Starting Influx Server %s...", version)
} else {
log.Info("Starting Influx Server %s bound to %s...", version, config.BindAddress)
}
2014-10-22 05:32:19 +00:00
fmt.Printf(logo)
2013-12-09 15:37:13 +00:00
2014-10-22 05:32:19 +00:00
// Start server.
s, err := influxdb.NewServer(config)
if err != nil {
// sleep for the log to flush
time.Sleep(time.Second)
panic(err)
}
2014-10-22 05:32:19 +00:00
if err := startProfiler(s); err != nil {
2014-03-27 23:09:08 +00:00
panic(err)
}
if *resetRootPassword {
// TODO: make this not suck
// This is ghetto as hell, but it'll work for now.
go func() {
time.Sleep(2 * time.Second) // wait for the raft server to join the cluster
2014-10-22 05:32:19 +00:00
log.Warn("Resetting root's password to %s", influxdb.DefaultRootPassword)
if err := server.RaftServer.CreateRootUser(); err != nil {
panic(err)
}
}()
}
2014-10-22 05:32:19 +00:00
if err := server.ListenAndServe(); err != nil {
log.Error("ListenAndServe failed: ", err)
}
return err
}
2014-10-21 05:32:47 +00:00
func setupLogging(loggingLevel, logFile string) {
level := log.DEBUG
switch loggingLevel {
case "trace":
level = log.TRACE
case "fine":
level = log.FINE
2014-10-21 05:32:47 +00:00
case "info":
level = log.INFO
case "warn":
level = log.WARNING
case "error":
level = log.ERROR
default:
log.Error("Unknown log level %s. Defaulting to DEBUG", loggingLevel)
2014-10-21 05:32:47 +00:00
}
log.Global = make(map[string]*log.Filter)
facility, ok := GetSysLogFacility(logFile)
if ok {
flw, err := NewSysLogWriter(facility)
if err != nil {
fmt.Fprintf(os.Stderr, "NewSysLogWriter: %s\n", err.Error())
return
}
log.AddFilter("syslog", level, flw)
} else if logFile == "stdout" {
flw := log.NewConsoleLogWriter()
log.AddFilter("stdout", level, flw)
} else {
logFileDir := filepath.Dir(logFile)
os.MkdirAll(logFileDir, 0744)
flw := log.NewFileLogWriter(logFile, false)
log.AddFilter("file", level, flw)
flw.SetFormat("[%D %T] [%L] (%S) %M")
flw.SetRotate(true)
flw.SetRotateSize(0)
flw.SetRotateLines(0)
flw.SetRotateDaily(true)
}
log.Info("Redirectoring logging to %s", logFile)
}
type Stopper interface {
Stop()
}