-
Notifications
You must be signed in to change notification settings - Fork 112
/
sysinfo.go
82 lines (71 loc) · 2.64 KB
/
sysinfo.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
// Copyright 2018-2024 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package sysinfo
import (
"encoding/json"
"fmt"
"reflect"
"github.com/pkg/errors"
)
// SystemInformation stores general information about Reva and the system it's running on.
type SystemInformation struct {
// Reva holds the main Reva information
Reva *RevaVersion `json:"reva"`
}
var (
// SysInfo provides global system information.
SysInfo = &SystemInformation{}
)
// ToJSON converts the system information to JSON.
func (sysInfo *SystemInformation) ToJSON() (string, error) {
data, err := json.MarshalIndent(sysInfo, "", "\t")
if err != nil {
return "", fmt.Errorf("unable to marshal the system information: %v", err)
}
return string(data), nil
}
func replaceEmptyInfoValues(i interface{}) {
// Iterate over each field of the given interface and search for "empty" values
v := reflect.ValueOf(i).Elem()
for i := 0; i < v.NumField(); i++ {
// Check if the field is either a struct or a pointer to a struct; in that case, process the field recursively
f := v.Field(i)
if f.Kind() == reflect.Struct || (f.Kind() == reflect.Ptr && f.Elem().Kind() == reflect.Struct) {
replaceEmptyInfoValues(f.Interface())
} else if f.CanSet() { // Replace empty values with something more meaningful
if f.Kind() == reflect.String {
if len(f.String()) == 0 {
f.SetString("(Unknown)")
}
}
}
}
}
// InitSystemInfo initializes the global system information object and also registers the corresponding metrics.
func InitSystemInfo(revaVersion *RevaVersion) error {
SysInfo = &SystemInformation{
Reva: revaVersion,
}
// Replace any empty values in the system information by more meaningful ones
replaceEmptyInfoValues(SysInfo)
// Register the system information metrics, as the necessary system info object has been filled out
if err := registerSystemInfoMetrics(); err != nil {
return errors.Wrap(err, "unable to register the system info metrics")
}
return nil
}