publish / publish (push) Successful in 2m53s
The host connector answers filesystem_usage, directory_sizes and old_files on configured hosts over SSH with pinned host keys, dialling through the egress tunnel when confined. The sops engine decrypts with the sops library instead of the CLI, takes its file from settings and an age identity from its secrets.
103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type filesystemUsage struct {
|
|
Filesystem string `json:"filesystem"`
|
|
Mount string `json:"mount"`
|
|
SizeBytes int64 `json:"size_bytes"`
|
|
UsedBytes int64 `json:"used_bytes"`
|
|
AvailableBytes int64 `json:"available_bytes"`
|
|
UsedPercent int `json:"used_percent"`
|
|
InodesUsedPercent int `json:"inodes_used_percent"`
|
|
}
|
|
|
|
// parseDF reads `df -P -B1` followed by `df -P -i`: a header and one data line
|
|
// each.
|
|
func parseDF(out []byte) (any, error) {
|
|
var rows [][]string
|
|
for line := range strings.Lines(string(out)) {
|
|
f := strings.Fields(line)
|
|
if len(f) >= 6 && f[0] != "Filesystem" {
|
|
rows = append(rows, f)
|
|
}
|
|
}
|
|
if len(rows) != 2 {
|
|
return nil, fmt.Errorf("unexpected df output: %d data lines", len(rows))
|
|
}
|
|
bytes, inodes := rows[0], rows[1]
|
|
u := filesystemUsage{Filesystem: bytes[0], Mount: bytes[5]}
|
|
var errs []error
|
|
u.SizeBytes, errs = num(bytes[1], errs)
|
|
u.UsedBytes, errs = num(bytes[2], errs)
|
|
u.AvailableBytes, errs = num(bytes[3], errs)
|
|
used, errs := num(strings.TrimSuffix(bytes[4], "%"), errs)
|
|
iused, errs := num(strings.TrimSuffix(inodes[4], "%"), errs)
|
|
u.UsedPercent, u.InodesUsedPercent = int(used), int(iused)
|
|
return u, errors.Join(errs...)
|
|
}
|
|
|
|
type entrySize struct {
|
|
Path string `json:"path"`
|
|
Bytes int64 `json:"bytes"`
|
|
}
|
|
|
|
// parseDU reads "<bytes>\t<path>" lines.
|
|
func parseDU(out []byte) (any, error) {
|
|
entries := []entrySize{}
|
|
for line := range strings.Lines(string(out)) {
|
|
size, p, ok := strings.Cut(strings.TrimRight(line, "\n"), "\t")
|
|
if !ok {
|
|
continue
|
|
}
|
|
n, err := strconv.ParseInt(size, 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
entries = append(entries, entrySize{Path: p, Bytes: n})
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
type oldFile struct {
|
|
Path string `json:"path"`
|
|
Bytes int64 `json:"bytes"`
|
|
Modified time.Time `json:"modified"`
|
|
Owner string `json:"owner"`
|
|
}
|
|
|
|
// parseFind reads "<bytes>\t<mtime>\t<owner>\t<path>" lines.
|
|
func parseFind(out []byte) (any, error) {
|
|
files := []oldFile{}
|
|
for line := range strings.Lines(string(out)) {
|
|
f := strings.SplitN(strings.TrimRight(line, "\n"), "\t", 4)
|
|
if len(f) != 4 {
|
|
continue
|
|
}
|
|
size, err := strconv.ParseInt(f[0], 10, 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
mtime, err := strconv.ParseFloat(f[1], 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
files = append(files, oldFile{Path: f[3], Bytes: size, Modified: time.Unix(int64(mtime), 0).UTC(), Owner: f[2]})
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
func num(s string, errs []error) (int64, []error) {
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
if err != nil {
|
|
errs = append(errs, fmt.Errorf("number %q: %w", s, err))
|
|
}
|
|
return n, errs
|
|
}
|