modbot/lib/readers/memory.go

96 lines
2.3 KiB
Go
Raw Permalink Normal View History

// modbot is a system information agregator
2024-07-30 04:02:05 -04:00
// Copyright (C) 2024 frosty <inthishouseofcards@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
2024-08-25 02:27:55 -04:00
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
2024-07-30 04:02:05 -04:00
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2024-08-25 02:27:55 -04:00
// GNU General Public License for more details.
2024-07-30 04:02:05 -04:00
//
2024-08-25 02:27:55 -04:00
// You should have received a copy of the GNU General Public License
2024-07-30 04:02:05 -04:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package readers
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"
"codeberg.org/frosty/modbot/lib/ui"
2024-07-30 04:02:05 -04:00
)
type MemoryInfo struct {
Total uint64
Available uint64
Used uint64
TotalPretty string
AvailablePretty string
UsedPretty string
2024-07-30 04:02:05 -04:00
}
func ReadMemory() func() (interface{}, error) {
return func() (interface{}, error) {
2024-08-25 02:27:55 -04:00
const memoryFile = "/proc/meminfo"
file, err := os.Open(memoryFile)
if err != nil {
return MemoryInfo{}, err
}
defer file.Close()
2024-07-30 04:02:05 -04:00
var memTotal, memAvailable, memUsed uint64
scanner := bufio.NewScanner(file)
2024-07-30 04:02:05 -04:00
for scanner.Scan() {
line := scanner.Text()
columns := strings.Fields(line)
2024-07-30 04:02:05 -04:00
if len(columns) < 2 {
continue
}
2024-07-30 04:02:05 -04:00
value, err := strconv.ParseUint(columns[1], 10, 64)
if err != nil {
return MemoryInfo{}, fmt.Errorf("failed to parse memory value: %w", err)
}
2024-07-30 04:02:05 -04:00
switch {
case strings.HasPrefix(line, "MemTotal:"):
memTotal = value
case strings.HasPrefix(line, "MemAvailable:"):
memAvailable = value
}
2024-07-30 04:02:05 -04:00
}
if err := scanner.Err(); err != nil {
return MemoryInfo{}, err
}
if memTotal == 0 {
return MemoryInfo{}, errors.New("missing MemTotal")
}
if memAvailable == 0 {
return MemoryInfo{}, errors.New("missing MemAvailable")
}
memUsed = memTotal - memAvailable
return MemoryInfo{
Total: memTotal,
Available: memAvailable,
Used: memUsed,
2024-08-25 02:27:55 -04:00
TotalPretty: ui.PrettifyKib(memTotal, 1),
AvailablePretty: ui.PrettifyKib(memAvailable, 1),
UsedPretty: ui.PrettifyKib(memUsed, 1),
}, nil
}
}