2013-12-03 22:19:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-11-11 05:25:03 +00:00
|
|
|
"log"
|
2015-03-22 20:58:40 +00:00
|
|
|
"math/rand"
|
2013-12-03 22:19:42 +00:00
|
|
|
"os"
|
2015-02-23 20:27:51 +00:00
|
|
|
"os/signal"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2014-11-21 03:31:04 +00:00
|
|
|
"strings"
|
2015-03-22 20:58:40 +00:00
|
|
|
"time"
|
2013-12-03 22:19:42 +00:00
|
|
|
)
|
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
const logo = `
|
2015-01-24 20:11:03 +00:00
|
|
|
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"
|
|
|
|
|
2014-10-22 05:32:19 +00:00
|
|
|
`
|
|
|
|
|
2014-11-18 00:34:47 +00:00
|
|
|
// These variables are populated via the Go linker.
|
|
|
|
var (
|
2014-12-06 01:02:30 +00:00
|
|
|
version string = "0.9"
|
2014-11-18 00:34:47 +00:00
|
|
|
commit string
|
|
|
|
)
|
|
|
|
|
2014-12-11 21:49:42 +00:00
|
|
|
// Various constants used by the main package.
|
|
|
|
const (
|
2015-04-03 21:35:44 +00:00
|
|
|
messagingClientFile string = "messaging"
|
|
|
|
monitoringDatabase string = "_influxdb"
|
|
|
|
monitoringRetentionPolicy string = "default"
|
2014-12-11 21:49:42 +00:00
|
|
|
)
|
|
|
|
|
2013-12-03 22:19:42 +00:00
|
|
|
func main() {
|
2014-12-06 01:02:30 +00:00
|
|
|
log.SetFlags(0)
|
2015-03-29 22:49:32 +00:00
|
|
|
log.SetPrefix(`[srvr] `)
|
|
|
|
log.SetFlags(log.LstdFlags)
|
2015-03-22 20:58:40 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
2014-12-06 01:02:30 +00:00
|
|
|
|
2015-02-24 17:47:07 +00:00
|
|
|
// If commit not set, make that clear.
|
|
|
|
if commit == "" {
|
|
|
|
commit = "unknown"
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:02:30 +00:00
|
|
|
// Shift binary name off argument list.
|
|
|
|
args := os.Args[1:]
|
|
|
|
|
|
|
|
// Retrieve command name as first argument.
|
|
|
|
var cmd string
|
|
|
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
|
|
cmd = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case -h immediately following binary name
|
2014-12-12 00:04:53 +00:00
|
|
|
if len(args) > 0 && args[0] == "-h" {
|
2014-12-06 01:02:30 +00:00
|
|
|
cmd = "help"
|
|
|
|
}
|
|
|
|
|
|
|
|
// If command is "help" and has an argument then rewrite args to use "-h".
|
|
|
|
if cmd == "help" && len(args) > 1 {
|
|
|
|
args[0], args[1] = args[1], "-h"
|
|
|
|
cmd = args[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract name from args.
|
|
|
|
switch cmd {
|
|
|
|
case "run":
|
2015-03-25 16:49:05 +00:00
|
|
|
cmd := NewRunCommand()
|
|
|
|
if err := cmd.Run(args[1:]...); err != nil {
|
|
|
|
log.Fatalf("run: %s", err)
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
case "":
|
2015-03-25 16:49:05 +00:00
|
|
|
cmd := NewRunCommand()
|
|
|
|
if err := cmd.Run(args...); err != nil {
|
|
|
|
log.Fatalf("run: %s", err)
|
|
|
|
}
|
2015-03-20 04:23:52 +00:00
|
|
|
case "backup":
|
2015-03-22 16:28:04 +00:00
|
|
|
cmd := NewBackupCommand()
|
|
|
|
if err := cmd.Run(args[1:]...); err != nil {
|
|
|
|
log.Fatalf("backup: %s", err)
|
|
|
|
}
|
2015-03-20 04:23:52 +00:00
|
|
|
case "restore":
|
2015-03-22 20:58:40 +00:00
|
|
|
cmd := NewRestoreCommand()
|
|
|
|
if err := cmd.Run(args[1:]...); err != nil {
|
|
|
|
log.Fatalf("restore: %s", err)
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
case "version":
|
|
|
|
execVersion(args[1:])
|
2015-03-16 15:23:53 +00:00
|
|
|
case "config":
|
|
|
|
execConfig(args[1:])
|
2014-12-06 01:02:30 +00:00
|
|
|
case "help":
|
2015-03-25 16:27:48 +00:00
|
|
|
cmd := NewHelpCommand()
|
|
|
|
if err := cmd.Run(args[1:]...); err != nil {
|
|
|
|
log.Fatalf("help: %s", err)
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
default:
|
|
|
|
log.Fatalf(`influxd: unknown command "%s"`+"\n"+`Run 'influxd help' for usage`+"\n\n", cmd)
|
2014-11-04 20:36:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:02:30 +00:00
|
|
|
// execVersion runs the "version" command.
|
|
|
|
// Prints the commit SHA1 if set by the build process.
|
|
|
|
func execVersion(args []string) {
|
|
|
|
fs := flag.NewFlagSet("", flag.ExitOnError)
|
|
|
|
fs.Usage = func() {
|
2015-04-06 20:12:00 +00:00
|
|
|
fmt.Println(`usage: version
|
2014-10-22 00:20:43 +00:00
|
|
|
|
2014-12-06 01:02:30 +00:00
|
|
|
version displays the InfluxDB version and build git commit hash
|
|
|
|
`)
|
2013-12-03 22:19:42 +00:00
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
fs.Parse(args)
|
2014-11-11 05:25:03 +00:00
|
|
|
|
2014-12-06 01:02:30 +00:00
|
|
|
s := fmt.Sprintf("InfluxDB v%s", version)
|
|
|
|
if commit != "" {
|
|
|
|
s += fmt.Sprintf(" (git: %s)", commit)
|
2014-03-27 23:09:08 +00:00
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
log.Print(s)
|
|
|
|
}
|
2014-03-27 16:24:40 +00:00
|
|
|
|
2015-03-16 15:23:53 +00:00
|
|
|
// execConfig parses and prints the current config loaded.
|
|
|
|
func execConfig(args []string) {
|
|
|
|
// Parse command flags.
|
|
|
|
fs := flag.NewFlagSet("", flag.ExitOnError)
|
2015-03-20 22:55:09 +00:00
|
|
|
fs.Usage = func() {
|
2015-04-06 20:12:00 +00:00
|
|
|
fmt.Println(`usage: config
|
2015-03-20 22:55:09 +00:00
|
|
|
|
2015-04-06 20:12:00 +00:00
|
|
|
config displays the default configuration
|
2015-03-20 22:55:09 +00:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2015-03-16 15:23:53 +00:00
|
|
|
var (
|
2015-04-13 16:45:39 +00:00
|
|
|
configPath string
|
|
|
|
hostname string
|
2015-03-16 15:23:53 +00:00
|
|
|
)
|
2015-04-13 16:45:39 +00:00
|
|
|
fs.StringVar(&configPath, "config", "", "")
|
|
|
|
fs.StringVar(&hostname, "hostname", "", "")
|
2015-03-16 15:23:53 +00:00
|
|
|
fs.Parse(args)
|
|
|
|
|
2015-04-13 16:45:39 +00:00
|
|
|
var config *Config
|
|
|
|
var err error
|
|
|
|
if configPath == "" {
|
|
|
|
config, err = NewTestConfig()
|
|
|
|
} else {
|
|
|
|
config, err = ParseConfigFile(configPath)
|
|
|
|
}
|
2015-03-22 21:38:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("parse config: %s", err)
|
|
|
|
}
|
2015-04-13 16:45:39 +00:00
|
|
|
// Override config properties.
|
|
|
|
if hostname != "" {
|
|
|
|
config.Hostname = hostname
|
|
|
|
}
|
2015-03-16 15:23:53 +00:00
|
|
|
|
2015-03-16 19:31:15 +00:00
|
|
|
config.Write(os.Stdout)
|
2015-03-16 15:23:53 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 05:32:47 +00:00
|
|
|
type Stopper interface {
|
|
|
|
Stop()
|
|
|
|
}
|
2014-12-06 01:02:30 +00:00
|
|
|
|
|
|
|
type State struct {
|
|
|
|
Mode string `json:"mode"`
|
|
|
|
}
|
2015-02-23 20:27:51 +00:00
|
|
|
|
|
|
|
var prof struct {
|
|
|
|
cpu *os.File
|
|
|
|
mem *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func startProfiling(cpuprofile, memprofile string) {
|
|
|
|
if cpuprofile != "" {
|
|
|
|
f, err := os.Create(cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cpuprofile: %v", err)
|
|
|
|
}
|
|
|
|
prof.cpu = f
|
|
|
|
pprof.StartCPUProfile(prof.cpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
if memprofile != "" {
|
|
|
|
f, err := os.Create(memprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("memprofile: %v", err)
|
|
|
|
}
|
|
|
|
prof.mem = f
|
|
|
|
runtime.MemProfileRate = 4096
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
<-c
|
|
|
|
stopProfiling()
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopProfiling() {
|
|
|
|
if prof.cpu != nil {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
prof.cpu.Close()
|
|
|
|
}
|
|
|
|
if prof.mem != nil {
|
|
|
|
pprof.Lookup("heap").WriteTo(prof.mem, 0)
|
|
|
|
prof.mem.Close()
|
|
|
|
}
|
|
|
|
}
|
2015-03-22 20:58:40 +00:00
|
|
|
|
|
|
|
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
|
|
|
|
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }
|