forked from google/cloudprober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudprober.go
188 lines (166 loc) · 4.98 KB
/
cloudprober.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Binary cloudprober is a tool for running a set of probes and metric surfacers
on a GCE VM. Cloudprober takes in a config proto which dictates what probes
and surfacers should be created with what configuration, and then manages the
asynchronous fan-in/fan-out of the data between the probes and the surfacers.
*/
package main
import (
"context"
"fmt"
"io/ioutil"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/pprof"
"cloud.google.com/go/compute/metadata"
"flag"
"github.com/golang/glog"
"github.com/google/cloudprober"
"github.com/google/cloudprober/config"
"github.com/google/cloudprober/config/runconfig"
"github.com/google/cloudprober/sysvars"
"github.com/google/cloudprober/web"
)
var (
configFile = flag.String("config_file", "", "Config file")
versionFlag = flag.Bool("version", false, "Print version and exit")
cpuprofile = flag.String("cpuprof", "", "Write cpu profile to file")
memprofile = flag.String("memprof", "", "Write heap profile to file")
configTest = flag.Bool("configtest", false, "Dry run to test config file")
dumpConfig = flag.Bool("dumpconfig", false, "Dump processed config to stdout")
testInstanceName = flag.String("test_instance_name", "ig-us-central1-a-01-0000", "Instance name example to be used in tests")
// configTestVars provides a sane set of sysvars for config testing.
configTestVars = map[string]string(nil)
)
// This gets overwritten by using -ldflags="-X main.version=${VERSION}" at
// the build time.
var version = "undefined"
func setupConfigTestVars() {
configTestVars = map[string]string{
"zone": "us-central1-a",
"project": "fake-domain.com:fake-project",
"project_id": "12345678",
"instance": *testInstanceName,
"internal_ip": "192.168.0.10",
"external_ip": "10.10.10.10",
"instance_template": "ig-us-central1-a-01",
}
}
const (
configMetadataKeyName = "cloudprober_config"
defaultConfigFile = "/etc/cloudprober.cfg"
)
func setupProfiling() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
var f *os.File
if *cpuprofile != "" {
var err error
f, err = os.Create(*cpuprofile)
if err != nil {
glog.Exit(err)
}
if err = pprof.StartCPUProfile(f); err != nil {
glog.Errorf("Could not start CPU profiling: %v", err)
}
}
go func(file *os.File) {
<-sigChan
pprof.StopCPUProfile()
if *cpuprofile != "" {
if err := file.Close(); err != nil {
glog.Exit(err)
}
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
glog.Exit(err)
}
if err = pprof.WriteHeapProfile(f); err != nil {
glog.Exit(err)
}
if err := f.Close(); err != nil {
glog.Exit(err)
}
}
os.Exit(1)
}(f)
}
func configFileToString(fileName string) string {
b, err := ioutil.ReadFile(fileName)
if err != nil {
glog.Exitf("Failed to read the config file: %v", err)
}
return string(b)
}
func getConfig() string {
if *configFile != "" {
return configFileToString(*configFile)
}
// On GCE first check if there is a config in custom metadata
// attributes.
if metadata.OnGCE() {
if config, err := config.ReadFromGCEMetadata(configMetadataKeyName); err != nil {
glog.Infof("Error reading config from metadata. Err: %v", err)
} else {
return config
}
}
// If config not found in metadata, check default config on disk
if _, err := os.Stat(defaultConfigFile); !os.IsNotExist(err) {
return configFileToString(defaultConfigFile)
}
glog.Warningf("Config file %s not found. Using default config.", defaultConfigFile)
return config.DefaultConfig()
}
func main() {
flag.Parse()
runconfig.SetVersion(version)
if *versionFlag {
fmt.Println(version)
return
}
setupConfigTestVars()
if *dumpConfig {
sysvars.Init(nil, configTestVars)
text, err := config.ParseTemplate(getConfig(), sysvars.Vars())
if err != nil {
glog.Exitf("Error parsing config file. Err: %v", err)
}
fmt.Println(text)
return
}
if *configTest {
sysvars.Init(nil, configTestVars)
_, err := config.Parse(configFileToString(*configFile), sysvars.Vars())
if err != nil {
glog.Exitf("Error parsing config file. Err: %v", err)
}
return
}
setupProfiling()
err := cloudprober.InitFromConfig(getConfig())
if err != nil {
glog.Exitf("Error initializing cloudprober. Err: %v", err)
}
// web.Init sets up web UI for cloudprober.
web.Init()
cloudprober.Start(context.Background())
// Wait forever
select {}
}