forked from Azure/azure-container-networking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetrymain.go
147 lines (125 loc) · 3.74 KB
/
telemetrymain.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
package main
// Entry point of the telemetry service if started by CNI
import (
"fmt"
"os"
"runtime"
"time"
acn "github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/telemetry"
)
const (
reportToHostIntervalInSeconds = 30
azureVnetTelemetry = "azure-vnet-telemetry"
configExtension = ".config"
)
var version string
var args = acn.ArgumentList{
{
Name: acn.OptLogLevel,
Shorthand: acn.OptLogLevelAlias,
Description: "Set the logging level",
Type: "int",
DefaultValue: acn.OptLogLevelInfo,
ValueMap: map[string]interface{}{
acn.OptLogLevelInfo: log.LevelInfo,
acn.OptLogLevelDebug: log.LevelDebug,
},
},
{
Name: acn.OptLogTarget,
Shorthand: acn.OptLogTargetAlias,
Description: "Set the logging target",
Type: "int",
DefaultValue: acn.OptLogTargetFile,
ValueMap: map[string]interface{}{
acn.OptLogTargetSyslog: log.TargetSyslog,
acn.OptLogTargetStderr: log.TargetStderr,
acn.OptLogTargetFile: log.TargetLogfile,
acn.OptLogStdout: log.TargetStdout,
acn.OptLogMultiWrite: log.TargetStdOutAndLogFile,
},
},
{
Name: acn.OptLogLocation,
Shorthand: acn.OptLogLocationAlias,
Description: "Set the directory location where logs will be saved",
Type: "string",
DefaultValue: "",
},
{
Name: acn.OptVersion,
Shorthand: acn.OptVersionAlias,
Description: "Print version information",
Type: "bool",
DefaultValue: false,
},
{
Name: acn.OptTelemetryConfigDir,
Shorthand: acn.OptTelemetryConfigDirAlias,
Description: "Set the telmetry config directory",
Type: "string",
DefaultValue: telemetry.CniInstallDir,
},
}
// Prints description and version information.
func printVersion() {
fmt.Printf("Azure Container Telemetry Service\n")
fmt.Printf("Version %v\n", version)
}
func main() {
var tb *telemetry.TelemetryBuffer
var config telemetry.TelemetryConfig
var configPath string
var err error
acn.ParseArgs(&args, printVersion)
logTarget := acn.GetArg(acn.OptLogTarget).(int)
logDirectory := acn.GetArg(acn.OptLogLocation).(string)
logLevel := acn.GetArg(acn.OptLogLevel).(int)
configDirectory := acn.GetArg(acn.OptTelemetryConfigDir).(string)
vers := acn.GetArg(acn.OptVersion).(bool)
if vers {
printVersion()
os.Exit(0)
}
log.SetName(azureVnetTelemetry)
log.SetLevel(logLevel)
if logDirectory != "" {
log.SetLogDirectory(logDirectory)
}
err = log.SetTarget(logTarget)
if err != nil {
fmt.Printf("Failed to configure logging: %v\n", err)
return
}
log.Logf("args %+v", os.Args)
if runtime.GOOS == "linux" {
configPath = fmt.Sprintf("%s/%s%s", configDirectory, azureVnetTelemetry, configExtension)
} else {
configPath = fmt.Sprintf("%s\\%s%s", configDirectory, azureVnetTelemetry, configExtension)
}
log.Logf("[Telemetry] Config path: %s", configPath)
config, err = telemetry.ReadConfigFile(configPath)
if err != nil {
log.Logf("[Telemetry] Error reading telemetry config: %v", err)
}
log.Logf("read config returned %+v", config)
for {
tb = telemetry.NewTelemetryBuffer("")
log.Logf("[Telemetry] Starting telemetry server")
err = tb.StartServer()
if err == nil || tb.FdExists {
break
}
log.Logf("[Telemetry] Telemetry service starting failed: %v", err)
tb.Cleanup(telemetry.FdName)
time.Sleep(time.Millisecond * 200)
}
if config.ReportToHostIntervalInSeconds == 0 {
config.ReportToHostIntervalInSeconds = reportToHostIntervalInSeconds
}
log.Logf("[Telemetry] Report to host for an interval of %d seconds", config.ReportToHostIntervalInSeconds)
tb.BufferAndPushData(config.ReportToHostIntervalInSeconds * time.Second)
log.Close()
}