forked from velp/netns-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (56 loc) · 1.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/sirupsen/logrus"
)
const defaultConfigFile = "/etc/netns-exporter/config.yaml"
var (
cfgPath string
logFilePath string
logLevel string
threads int
)
func init() { //nolint:gochecknoinits
flag.StringVar(&cfgPath, "config", defaultConfigFile, fmt.Sprintf("Path to config file (default: %s)", defaultConfigFile))
flag.StringVar(&logFilePath, "log-file", "", "Write logs to file (default: send logs to stdout)")
flag.StringVar(&logLevel, "log-level", "info", "Logging level (default: info)")
flag.IntVar(&threads, "threads", 0, "Number of threads for collecting data (default: number of CPU cors)")
}
func main() {
flag.Parse()
// Init logger
logger := logrus.New()
level, err := logrus.ParseLevel(logLevel)
if err != nil {
log.Fatalf("Parsing log level failed: %s", err)
}
logger.SetLevel(level)
if logFilePath != "" {
logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o755)
if err != nil {
log.Fatalf("Failed to initialize log file %s", err)
}
logger.SetOutput(logFile)
} else {
logger.SetOutput(os.Stdout)
}
// Load config
config, err := LoadConfig(cfgPath)
if err != nil {
logger.Fatalf("Loading config failed: %s", err)
}
if threads > 0 {
config.Threads = threads
}
// Run exporter
apiServer, err := NewAPIServer(config, logger)
if err != nil {
logger.Fatalf("Creating API server failed: %s", err)
}
if err := apiServer.Start(); err != nil {
logger.Fatalf("Starting API server failed: %s", err)
}
}