|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package systemstatsmonitor |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "io/ioutil" |
| 19 | + "strconv" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/golang/glog" |
| 23 | + ssmtypes "k8s.io/node-problem-detector/pkg/systemstatsmonitor/types" |
| 24 | + "k8s.io/node-problem-detector/pkg/util/metrics" |
| 25 | + "k8s.io/node-problem-detector/pkg/util/metrics/system" |
| 26 | +) |
| 27 | + |
| 28 | +type osFeatureCollector struct { |
| 29 | + config *ssmtypes.OSFeatureStatsConfig |
| 30 | + osFeature *metrics.Int64Metric |
| 31 | +} |
| 32 | + |
| 33 | +func NewOsFeatureCollectorOrDie(osFeatureConfig *ssmtypes.OSFeatureStatsConfig) *osFeatureCollector { |
| 34 | + oc := osFeatureCollector{config: osFeatureConfig} |
| 35 | + var err error |
| 36 | + // Use metrics.Last aggregation method to ensure the metric is a guage metric. |
| 37 | + if osFeatureConfig.MetricsConfigs["system/os_feature"].DisplayName != "" { |
| 38 | + oc.osFeature, err = metrics.NewInt64Metric( |
| 39 | + metrics.OSFeatureID, |
| 40 | + osFeatureConfig.MetricsConfigs[string(metrics.OSFeatureID)].DisplayName, |
| 41 | + "OS Features like GPU support, KTD kernel, third party modules as unknown modules. 1 if the feature is enabled and 0, if disabled.", |
| 42 | + "1", |
| 43 | + metrics.LastValue, |
| 44 | + []string{featureLabel, valueLabel}) |
| 45 | + if err != nil { |
| 46 | + glog.Fatalf("Error initializing metric for system/os_feature: %v", err) |
| 47 | + } |
| 48 | + } |
| 49 | + return &oc |
| 50 | +} |
| 51 | + |
| 52 | +// recordFeaturesFromCmdline records the guest OS features that can be derived |
| 53 | +// from the /proc/cmdline |
| 54 | +// The following features are recorded: |
| 55 | +// 1. KTD kernel based on csm.enabled value |
| 56 | +// 2. UnifiedCgroupHierarchy based on systemd.unified_cgroup_hierarchy |
| 57 | +// 3. KernelModuleIntegrity based on the loadpin enabled and a module signed. |
| 58 | +func (ofc *osFeatureCollector) recordFeaturesFromCmdline(cmdlineArgs []system.CmdlineArg) { |
| 59 | + var featuresMap = map[string]int64{ |
| 60 | + "KTD": 0, |
| 61 | + "UnifiedCgroupHierarchy": 0, |
| 62 | + "ModuleSigned": 0, |
| 63 | + "LoadPinEnabled": 0, |
| 64 | + } |
| 65 | + for _, cmdlineArg := range cmdlineArgs { |
| 66 | + // record KTD feature. |
| 67 | + if cmdlineArg.Key == "csm.enabled" { |
| 68 | + featuresMap["KTD"], _ = strconv.ParseInt(cmdlineArg.Value, 10, 64) |
| 69 | + } |
| 70 | + // record UnifiedCgroupHierarchy feature. |
| 71 | + if cmdlineArg.Key == "systemd.unified_cgroup_hierarchy" { |
| 72 | + featuresMap["UnifiedCgroupHierarchy"], _ = strconv.ParseInt(cmdlineArg.Value, 10, 64) |
| 73 | + } |
| 74 | + // record KernelModuleIntegrity feature. |
| 75 | + if cmdlineArg.Key == "module.sig_enforce" { |
| 76 | + featuresMap["ModuleSigned"], _ = strconv.ParseInt(cmdlineArg.Value, 10, 64) |
| 77 | + } |
| 78 | + if cmdlineArg.Key == "loadpin.enabled" { |
| 79 | + featuresMap["LoadPinEnabled"], _ = strconv.ParseInt(cmdlineArg.Value, 10, 64) |
| 80 | + } |
| 81 | + } |
| 82 | + // Record the feature values. |
| 83 | + ofc.osFeature.Record(map[string]string{featureLabel: "KTD"}, featuresMap["KTD"]) |
| 84 | + ofc.osFeature.Record(map[string]string{featureLabel: "UnifiedCgroupHierarchy"}, featuresMap["UnifiedCgroupHierarchy"]) |
| 85 | + if featuresMap["ModuleSigned"] == 1 && featuresMap["LoadPinEnabled"] == 1 { |
| 86 | + ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 1) |
| 87 | + } else { |
| 88 | + ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 0) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// recordFeaturesFromModules records the guest OS features that can be derived |
| 93 | +// from the /proc/modules |
| 94 | +// The following features are recorded: |
| 95 | +// 1. GPUSupport based on the presence of nvidia module |
| 96 | +// 2. UnknownModules are tracked based on the presence of thirdparty kernel modules. |
| 97 | +func (ofc *osFeatureCollector) recordFeaturesFromModules(modules []system.Module) { |
| 98 | + // Collect known modules (default modules based on guest OS present in known-modules.json) |
| 99 | + var knownModules []system.Module |
| 100 | + f, err := ioutil.ReadFile(ofc.config.KnownModulesConfigPath) |
| 101 | + if err != nil { |
| 102 | + glog.Warningf("Failed to read configuration file %s: %v", |
| 103 | + ofc.config.KnownModulesConfigPath, err) |
| 104 | + } |
| 105 | + // When the knownModulesConfigPath is not set |
| 106 | + // it should assume all the metrics are assumed to be default modules. |
| 107 | + if f != nil { |
| 108 | + err = json.Unmarshal(f, &knownModules) |
| 109 | + if err != nil { |
| 110 | + glog.Warningf("Failed to retrieve known modules %v", err) |
| 111 | + } |
| 112 | + } else { |
| 113 | + knownModules = []system.Module{} |
| 114 | + } |
| 115 | + |
| 116 | + var hasGPUSupport = 0 |
| 117 | + unknownModules := []string{} |
| 118 | + |
| 119 | + // Collect UnknownModules and check GPUSupport |
| 120 | + for _, module := range modules { |
| 121 | + // if the module has nvidia modules, then the hasGPUSupport is set. |
| 122 | + if strings.Contains(module.ModuleName, "nvidia") { |
| 123 | + hasGPUSupport = 1 |
| 124 | + } else { |
| 125 | + if module.OutOfTree || module.Proprietary { |
| 126 | + if !system.ContainsModule(module.ModuleName, knownModules) { |
| 127 | + unknownModules = append(unknownModules, module.ModuleName) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + // record the UnknownModules and GPUSupport |
| 133 | + if len(unknownModules) > 0 { |
| 134 | + ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules", |
| 135 | + valueLabel: strings.Join(unknownModules, ",")}, 1) |
| 136 | + } else { |
| 137 | + ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules"}, |
| 138 | + 0) |
| 139 | + } |
| 140 | + ofc.osFeature.Record(map[string]string{featureLabel: "GPUSupport"}, |
| 141 | + int64(hasGPUSupport)) |
| 142 | +} |
| 143 | + |
| 144 | +func (ofc *osFeatureCollector) collect() { |
| 145 | + cmdlineArgs, err := system.CmdlineArgs() |
| 146 | + if err != nil { |
| 147 | + glog.Fatalf("Error retrieving cmdline args: %v", err) |
| 148 | + } |
| 149 | + ofc.recordFeaturesFromCmdline(cmdlineArgs) |
| 150 | + modules, err := system.Modules() |
| 151 | + if err != nil { |
| 152 | + glog.Fatalf("Error retrieving kernel modules: %v", err) |
| 153 | + } |
| 154 | + ofc.recordFeaturesFromModules(modules) |
| 155 | +} |
0 commit comments