1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
6
+ "github.com/NETWAYS/support-collector/internal/metrics"
5
7
"os"
6
8
"path/filepath"
7
9
"strings"
@@ -111,16 +113,31 @@ var (
111
113
outputFile string
112
114
commandTimeout = 60 * time .Second
113
115
noDetailedCollection bool
116
+ startTime = time .Now ()
117
+ metric * metrics.Metrics
114
118
)
115
119
116
120
func main () {
117
121
handleArguments ()
118
122
119
- // set locale to C, to avoid translations in command output
123
+ // Set locale to C, to avoid translations in command output
120
124
_ = os .Setenv ("LANG" , "C" )
121
125
122
- c , cleanup := NewCollection (outputFile )
123
- defer cleanup ()
126
+ c , closeCollection := NewCollection (outputFile )
127
+ // Close collection
128
+ defer closeCollection ()
129
+
130
+ // Initialize new metrics and defer function to save it to json
131
+ metric = metrics .New (getVersion ())
132
+ defer func () {
133
+ // Save metrics to file
134
+ body , err := json .Marshal (metric )
135
+ if err != nil {
136
+ c .Log .Warn ("cant unmarshal metrics: %w" , err )
137
+ }
138
+
139
+ c .AddFileJSON ("metrics.json" , body )
140
+ }()
124
141
125
142
if noDetailedCollection {
126
143
c .Detailed = false
@@ -133,37 +150,16 @@ func main() {
133
150
c .Log .Warn ("This tool should be run as a privileged user (root) to collect all necessary information" )
134
151
}
135
152
136
- var (
137
- startTime = time .Now ()
138
- timings = map [string ]time.Duration {}
139
- )
140
-
141
153
// Set command Timeout from argument
142
154
c .ExecTimeout = commandTimeout
143
155
144
- // Call all enabled modules
145
- for _ , name := range moduleOrder {
146
- switch {
147
- case util .StringInSlice (name , disabledModules ):
148
- c .Log .Debugf ("Module %s is disabled" , name )
149
- case ! util .StringInSlice (name , enabledModules ):
150
- c .Log .Debugf ("Module %s is not enabled" , name )
151
- default :
152
- moduleStart := time .Now ()
156
+ // Collect modules
157
+ collectModules (c )
153
158
154
- c .Log .Debugf ("Start collecting data for module %s" , name )
159
+ // Save overall timing
160
+ metric .Timings ["total" ] = time .Since (startTime )
155
161
156
- for _ , o := range extraObfuscators {
157
- c .Log .Debugf ("Adding custom obfuscator for '%s' to module %s" , o , name )
158
- c .RegisterObfuscator (obfuscate .NewAny (o ))
159
- }
160
-
161
- modules [name ](c )
162
-
163
- timings [name ] = time .Since (moduleStart )
164
- c .Log .Debugf ("Finished with module %s in %.3f seconds" , name , timings [name ].Seconds ())
165
- }
166
- }
162
+ c .Log .Infof ("Collection complete, took us %.3f seconds" , metric .Timings ["total" ].Seconds ())
167
163
168
164
// Collect obfuscation info
169
165
var files , count uint
@@ -178,12 +174,7 @@ func main() {
178
174
c .Log .Infof ("Obfuscation replaced %d token in %d files (%d definitions)" , count , files , len (c .Obfuscators ))
179
175
}
180
176
181
- // Save timings
182
- timings ["total" ] = time .Since (startTime )
183
- c .Log .Infof ("Collection complete, took us %.3f seconds" , timings ["total" ].Seconds ())
184
-
185
- c .AddFileYAML ("timing.yml" , timings )
186
-
177
+ // get absolute path of outputFile
187
178
path , err := filepath .Abs (outputFile )
188
179
if err != nil {
189
180
c .Log .Debug (err )
@@ -224,7 +215,7 @@ func handleArguments() {
224
215
flag .Parse ()
225
216
226
217
if printVersion {
227
- fmt .Println (Product , "version" , buildVersion ()) //nolint:forbidigo
218
+ fmt .Println (Product , "version" , getBuildInfo ()) //nolint:forbidigo
228
219
os .Exit (0 )
229
220
}
230
221
@@ -241,6 +232,9 @@ func buildFileName() string {
241
232
return util .GetHostnameWithoutDomain () + "-" + FilePrefix + "-" + time .Now ().Format ("20060102-1504" ) + ".zip"
242
233
}
243
234
235
+ // NewCollection starts a new collection. outputFile will be created.
236
+ //
237
+ // Collection and cleanup function to defer are returned
244
238
func NewCollection (outputFile string ) (* collection.Collection , func ()) {
245
239
file , err := os .Create (outputFile )
246
240
if err != nil {
@@ -263,9 +257,8 @@ func NewCollection(outputFile string) (*collection.Collection, func()) {
263
257
Level : consoleLevel ,
264
258
})
265
259
266
- versionString := buildVersion ()
260
+ versionString := getBuildInfo ()
267
261
c .Log .Infof ("Starting %s version %s" , Product , versionString )
268
- c .AddFileDataRaw ("version" , []byte (versionString + "\n " ))
269
262
270
263
return c , func () {
271
264
// Close all open outputs in order, but only log errors
@@ -281,3 +274,34 @@ func NewCollection(outputFile string) (*collection.Collection, func()) {
281
274
}
282
275
}
283
276
}
277
+
278
+ func collectModules (c * collection.Collection ) {
279
+ // Check if module is enabled / disabled and call it
280
+ for _ , name := range moduleOrder {
281
+ switch {
282
+ case util .StringInSlice (name , disabledModules ):
283
+ c .Log .Debugf ("Module %s is disabled" , name )
284
+ case ! util .StringInSlice (name , enabledModules ):
285
+ c .Log .Debugf ("Module %s is not enabled" , name )
286
+ default :
287
+ // Save current time
288
+ moduleStart := time .Now ()
289
+
290
+ c .Log .Debugf ("Start collecting data for module %s" , name )
291
+
292
+ // Register custom obfuscators
293
+ for _ , o := range extraObfuscators {
294
+ c .Log .Debugf ("Adding custom obfuscator for '%s' to module %s" , o , name )
295
+ c .RegisterObfuscator (obfuscate .NewAny (o ))
296
+ }
297
+
298
+ // Call collection function for module
299
+ modules [name ](c )
300
+
301
+ // Save runtime of module
302
+ metric .Timings [name ] = time .Since (moduleStart )
303
+
304
+ c .Log .Debugf ("Finished with module %s in %.3f seconds" , name , metric .Timings [name ].Seconds ())
305
+ }
306
+ }
307
+ }
0 commit comments