| 1 |
// Package nodeproblemdetector provides configurations for node-problem-detector.service. |
| 2 |
package nodeproblemdetector |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/google/go-tpm-tools/launcher/internal/logging" |
| 11 |
"github.com/google/go-tpm-tools/launcher/internal/systemctl" |
| 12 |
) |
| 13 |
|
| 14 |
var systemStatsFilePath = "/etc/node_problem_detector/system-stats-monitor.json" |
| 15 |
|
| 16 |
var defaultInvokeIntervalString = (60 * time.Second).String() |
| 17 |
|
| 18 |
type metricConfig struct { |
| 19 |
DisplayName string `json:"displayName"` |
| 20 |
} |
| 21 |
|
| 22 |
type statsConfig struct { |
| 23 |
MetricsConfigs map[string]metricConfig `json:"metricsConfigs"` |
| 24 |
} |
| 25 |
|
| 26 |
type diskConfig struct { |
| 27 |
IncludeAllAttachedBlk bool `json:"includeAllAttachedBlk"` |
| 28 |
IncludeRootBlk bool `json:"includeRootBlk"` |
| 29 |
LsblkTimeout string `json:"lsblkTimeout"` |
| 30 |
MetricsConfigs *statsConfig `json:"metricsConfigs"` |
| 31 |
} |
| 32 |
|
| 33 |
// SystemStatsConfig contains configurations for `System Stats Monitor`, |
| 34 |
// a problem daemon in node-problem-detector that collects pre-defined health-related metrics from different system components. |
| 35 |
// View the comprehensive configuration details on https://github.com/kubernetes/node-problem-detector/tree/master/pkg/systemstatsmonitor#detailed-configuration-options |
| 36 |
type SystemStatsConfig struct { |
| 37 |
CPU *statsConfig `json:"cpu,omitempty"` |
| 38 |
Disk *diskConfig `json:"disk,omitempty"` |
| 39 |
Host *statsConfig `json:"host,omitempty"` |
| 40 |
Memory *statsConfig `json:"memory,omitempty"` |
| 41 |
InvokeInterval string `json:"invokeInterval,omitempty"` |
| 42 |
} |
| 43 |
|
| 44 |
// NewSystemStatsConfig returns a new SystemStatsConfig struct with default configurations. |
| 45 |
func NewSystemStatsConfig() SystemStatsConfig { |
| 46 |
return SystemStatsConfig{ |
| 47 |
Memory: &statsConfig{MetricsConfigs: map[string]metricConfig{}}, |
| 48 |
InvokeInterval: defaultInvokeIntervalString, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
var allConfig = &SystemStatsConfig{ |
| 53 |
CPU: &statsConfig{map[string]metricConfig{ |
| 54 |
"cpu/usage_time": {"cpu/usage_time"}, |
| 55 |
"cpu/load_1m": {"cpu/load_1m"}, |
| 56 |
}}, |
| 57 |
Disk: &diskConfig{ |
| 58 |
true, true, "5s", |
| 59 |
&statsConfig{map[string]metricConfig{ |
| 60 |
"disk/avg_queue_len": {"disk/avg_queue_len"}, |
| 61 |
"disk/bytes_used": {"disk/bytes_used"}, |
| 62 |
"disk/percent_used": {"disk/percent_used"}, |
| 63 |
"disk/io_time": {"disk/io_time"}, |
| 64 |
"disk/merged_operation_count": {"disk/merged_operation_count"}, |
| 65 |
"disk/operation_bytes_count": {"disk/operation_bytes_count"}, |
| 66 |
"disk/operation_count": {"disk/operation_count"}, |
| 67 |
"disk/operation_time": {"disk/operation_time"}, |
| 68 |
"disk/weighted_io": {"disk/weighted_io"}, |
| 69 |
}}, |
| 70 |
}, |
| 71 |
Host: &statsConfig{map[string]metricConfig{ |
| 72 |
"host/uptime": {"host/uptime"}, |
| 73 |
}}, |
| 74 |
Memory: &statsConfig{map[string]metricConfig{ |
| 75 |
"memory/anonymous_used": {"memory/anonymous_used"}, |
| 76 |
"memory/bytes_used": {"memory/bytes_used"}, |
| 77 |
"memory/dirty_used": {"memory/dirty_used"}, |
| 78 |
"memory/page_cache_used": {"memory/page_cache_used"}, |
| 79 |
"memory/percent_used": {"memory/percent_used"}, |
| 80 |
}}, |
| 81 |
InvokeInterval: defaultInvokeIntervalString, |
| 82 |
} |
| 83 |
|
| 84 |
// EnableAllConfig overwrites system stats config with health monitoring config. |
| 85 |
func EnableAllConfig() error { |
| 86 |
return allConfig.WriteFile(systemStatsFilePath) |
| 87 |
} |
| 88 |
|
| 89 |
// EnableMemoryBytesUsed enables "memory/bytes_used" for memory monitoring. |
| 90 |
func (ssc *SystemStatsConfig) EnableMemoryBytesUsed() { |
| 91 |
ssc.Memory.MetricsConfigs["memory/bytes_used"] = metricConfig{DisplayName: "memory/bytes_used"} |
| 92 |
} |
| 93 |
|
| 94 |
// WithInvokeInterval overrides the default invokeInterval. |
| 95 |
func (ssc *SystemStatsConfig) WithInvokeInterval(interval time.Duration) { |
| 96 |
ssc.InvokeInterval = interval.String() |
| 97 |
} |
| 98 |
|
| 99 |
// WriteFile writes systemStatsConfig data to the named file, creating it if necessary. |
| 100 |
func (ssc *SystemStatsConfig) WriteFile(path string) error { |
| 101 |
bytes, err := json.Marshal(ssc) |
| 102 |
if err != nil { |
| 103 |
return fmt.Errorf("failed to marshal struct [%v]: %w", ssc, err) |
| 104 |
} |
| 105 |
return os.WriteFile(path, bytes, 0644) |
| 106 |
} |
| 107 |
|
| 108 |
// StartService starts Node Problem Detector. |
| 109 |
func StartService(logger logging.Logger) error { |
| 110 |
s, err := systemctl.New() |
| 111 |
if err != nil { |
| 112 |
return fmt.Errorf("failed to create systemctl client: %v", err) |
| 113 |
} |
| 114 |
defer s.Close() |
| 115 |
|
| 116 |
logger.Info("Starting node-problem-detector.service") |
| 117 |
if err := s.Start("node-problem-detector.service"); err != nil { |
| 118 |
return fmt.Errorf("failed to start node-problem-detector.service") |
| 119 |
} |
| 120 |
|
| 121 |
logger.Info("node-problem-detector.service successfully started") |
| 122 |
return nil |
| 123 |
} |