func()

in internal/mysqlmetrics/mysqlmetrics.go [249:284]


func (m *MySQLMetrics) totalRAM(ctx context.Context, isWindowsOS bool) (int, error) {
	cmd := commandlineexecutor.Params{
		Executable: "grep",
		Args:       []string{"MemTotal", "/proc/meminfo"},
	}
	if isWindowsOS {
		cmd = commandlineexecutor.Params{
			Executable: "cmd",
			Args:       []string{"/C", "wmic", "computersystem", "get", "totalphysicalmemory"},
		}
	}
	log.CtxLogger(ctx).Debugw("getTotalRAM command", "command", cmd)
	res := m.execute(ctx, cmd)
	log.CtxLogger(ctx).Debugw("getTotalRAM result", "result", res)
	if res.Error != nil {
		return 0, fmt.Errorf("failed to execute command: %v", res.Error)
	}
	if isWindowsOS {
		return windowsTotalRAM(ctx, res.StdOut)
	}
	// Expected to be something like "MemTotal:       1348760 kB"
	lines := strings.Split(res.StdOut, "\n")
	fields := strings.Fields(lines[0])
	if len(fields) != 3 {
		return 0, fmt.Errorf("found wrong number of fields in total RAM: %d", len(fields))
	}
	ram, err := strconv.Atoi(fields[1])
	if err != nil {
		return 0, fmt.Errorf("failed to convert total RAM to integer: %v", err)
	}
	units := fields[2]
	if strings.ToUpper(units) == "KB" {
		ram = ram * 1024
	}
	return ram, nil
}