Debian Codegraph @

cmd/influx_inspect/verify/seriesfile/command.go

raw symbols SCIP
1 // Package seriesfile verifies integrity of series files.
2 package seriesfile
3
4 import (
5 "flag"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "os"
10 "path/filepath"
11 "runtime"
12
13 "github.com/influxdata/influxdb/logger"
14 "go.uber.org/zap/zapcore"
15 )
16
17 // Command represents the program execution for "influx_inspect verify-seriesfile".
18 type Command struct {
19 Stdout io.Writer
20 Stderr io.Writer
21
22 dir string
23 db string
24 seriesFile string
25 verbose bool
26 concurrent int
27 }
28
29 // NewCommand returns a new instance of Command.
30 func NewCommand() *Command {
31 return &Command{
32 Stdout: os.Stdout,
33 Stderr: os.Stderr,
34 }
35 }
36
37 // Run executes the command.
38 func (cmd *Command) Run(args ...string) error {
39 fs := flag.NewFlagSet("verify-seriesfile", flag.ExitOnError)
40 fs.StringVar(&cmd.dir, "dir", filepath.Join(os.Getenv("HOME"), ".influxdb", "data"),
41 "Data directory.")
42 fs.StringVar(&cmd.db, "db", "",
43 "Only use this database inside of the data directory.")
44 fs.StringVar(&cmd.seriesFile, "series-file", "",
45 "Path to a series file. This overrides -db and -dir.")
46 fs.BoolVar(&cmd.verbose, "v", false,
47 "Verbose output.")
48 fs.IntVar(&cmd.concurrent, "c", runtime.GOMAXPROCS(0),
49 "How many concurrent workers to run.")
50
51 fs.SetOutput(cmd.Stdout)
52 fs.Usage = cmd.printUsage
53
54 if err := fs.Parse(args); err != nil {
55 return err
56 }
57
58 config := logger.NewConfig()
59 config.Level = zapcore.WarnLevel
60 if cmd.verbose {
61 config.Level = zapcore.InfoLevel
62 }
63 logger, err := config.New(cmd.Stderr)
64 if err != nil {
65 return err
66 }
67
68 v := NewVerify()
69 v.Logger = logger
70 v.Concurrent = cmd.concurrent
71
72 if cmd.seriesFile != "" {
73 _, err := v.VerifySeriesFile(cmd.seriesFile)
74 return err
75 }
76
77 if cmd.db != "" {
78 _, err := v.VerifySeriesFile(filepath.Join(cmd.dir, cmd.db, "_series"))
79 return err
80 }
81
82 dbs, err := ioutil.ReadDir(cmd.dir)
83 if err != nil {
84 return err
85 }
86
87 for _, db := range dbs {
88 if !db.IsDir() {
89 continue
90 }
91 _, err := v.VerifySeriesFile(filepath.Join(cmd.dir, db.Name(), "_series"))
92 if err != nil {
93 return err
94 }
95 }
96
97 return nil
98 }
99
100 func (cmd *Command) printUsage() {
101 usage := `Verifies the integrity of Series files.
102
103 Usage: influx_inspect verify-seriesfile [flags]
104
105 -dir <path>
106 Root data path.
107 Defaults to "%[1]s/.influxdb/data".
108 -db <name>
109 Only verify this database inside of the data directory.
110 -series-file <path>
111 Path to a series file. This overrides -db and -dir.
112 -v
113 Enable verbose logging.
114 -c
115 How many concurrent workers to run.
116 Defaults to "%[2]d" on this machine.
117 `
118
119 fmt.Printf(usage, os.Getenv("HOME"), runtime.GOMAXPROCS(0))
120 }