Files
troubleshoot/pkg/collect/host_memory.go
2025-02-20 16:04:03 -08:00

55 lines
1.3 KiB
Go

package collect
import (
"bytes"
"encoding/json"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/shirou/gopsutil/v4/mem"
)
type MemoryInfo struct {
Total uint64 `json:"total"`
}
const HostMemoryPath = `host-collectors/system/memory.json`
const HostMemoryFileName = `memory.json`
type CollectHostMemory struct {
hostCollector *troubleshootv1beta2.Memory
BundlePath string
}
func (c *CollectHostMemory) Title() string {
return hostCollectorTitleOrDefault(c.hostCollector.HostCollectorMeta, "Amount of Memory")
}
func (c *CollectHostMemory) IsExcluded() (bool, error) {
return isExcluded(c.hostCollector.Exclude)
}
func (c *CollectHostMemory) Collect(progressChan chan<- interface{}) (map[string][]byte, error) {
memoryInfo := MemoryInfo{}
vmstat, err := mem.VirtualMemory()
if err != nil {
return nil, errors.Wrap(err, "failed to read virtual memory")
}
memoryInfo.Total = vmstat.Total
b, err := json.Marshal(memoryInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal memory info")
}
output := NewResult()
output.SaveResult(c.BundlePath, HostMemoryPath, bytes.NewBuffer(b))
return output, nil
}
func (c *CollectHostMemory) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
return nil, ErrRemoteCollectorNotImplemented
}