-
Notifications
You must be signed in to change notification settings - Fork 21
/
amonagent.go
137 lines (109 loc) · 3.01 KB
/
amonagent.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
log "github.com/Sirupsen/logrus"
"github.com/amonapp/amonagent"
"github.com/amonapp/amonagent/collectors"
"github.com/amonapp/amonagent/plugins"
"github.com/amonapp/amonagent/internal/settings"
_ "github.com/amonapp/amonagent/plugins/all"
)
var fTest = flag.Bool("test", false, "gather all metrics, print them out, and exit")
var fDebug = flag.Bool("debug", false, "Starts the agent and displays the metrics sent in the terminal")
var fListPlugins = flag.Bool("list-plugins", false, "lists all available plugins and exit")
var fTestPlugin = flag.String("test-plugin", "", "gather plugin metrics, print them out, and exit")
var fPluginConfig = flag.String("plugin-config", "", "Shows the example config for a plugin")
var fVersion = flag.Bool("version", false, "display the version")
var fPidfile = flag.String("pidfile", "", "file to write our pid to")
var fMachineID = flag.Bool("machineid", false, "Get or Create unique machine id, this value is used to identify hosts")
// Amonagent version
// -ldflags "-X main.Version=`git describe --always --tags`"
// Version - XXX
var Version string
// ListPlugins -- XXX
func ListPlugins() {
allPlugins := plugins.Plugins
fmt.Println("\033[92m \nAvailable plugins: \033[0m")
for r := range allPlugins {
fmt.Println(r)
}
}
// Debug - XXX
func Debug() {
}
func main() {
flag.Parse()
machineID := collectors.GetOrCreateMachineID()
if *fListPlugins {
ListPlugins()
return
}
if len(*fPluginConfig) > 0 {
pluginConfig, _ := plugins.GetConfigPath(*fPluginConfig)
creator, ok := plugins.Plugins[pluginConfig.Name]
if ok {
plugin := creator()
conf := plugin.SampleConfig()
fmt.Println(conf)
} else {
fmt.Printf("Non existing plugin: %s", pluginConfig.Name)
ListPlugins()
}
return
}
if *fVersion {
v := fmt.Sprintf("Amon - Version %s", Version)
fmt.Println(v)
return
}
config := settings.Settings()
serverKey := config.ServerKey
ag, err := amonagent.NewAgent(config)
if err != nil {
log.Fatal(err)
}
if *fTest {
err = ag.Test(config)
if err != nil {
log.Fatal(err)
}
return
}
if *fMachineID {
fmt.Print(machineID)
return
}
if len(*fTestPlugin) > 0 {
ag.TestPlugin(*fTestPlugin)
return
}
if len(machineID) == 0 && len(serverKey) == 0 {
log.Fatal("Can't detect Machine ID. Please define `server_key` in " + settings.ConfigPath + "/amonagent.conf ")
}
shutdown := make(chan struct{})
signals := make(chan os.Signal)
signal.Notify(signals, os.Interrupt)
go func() {
<-signals
close(shutdown)
}()
log.Infof("Starting Amon Agent (Version: %s)\n", Version)
if *fPidfile != "" {
// Ensure the required directory structure exists.
err := os.MkdirAll(filepath.Dir(*fPidfile), 0700)
if err != nil {
log.Fatalf("Failed to verify pid directory %v", err)
}
f, err := os.Create(*fPidfile)
if err != nil {
log.Fatalf("Unable to create pidfile %v", err)
}
fmt.Fprintf(f, "%d\n", os.Getpid())
f.Close()
}
ag.Run(shutdown, *fDebug)
}