SPB Git

Repository Public

parsers.go 12.8 KB 442 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/internal/agent/collect/parsers.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142package collect4344import (45	"errors"46	"fmt"47	"net/url"48	"regexp"49	"strconv"50	"strings"51)5253// parseDecimal accepts "1234.56" and "1234,56" (decimal comma, as printed54// under a French locale). Thousands separators are not expected.55func parseDecimal(s string) (float64, error) {56	return strconv.ParseFloat(strings.Replace(strings.TrimSpace(s), ",", ".", 1), 64)57}5859var swapRE = regexp.MustCompile(`(total|used|free)\s*=\s*([0-9]+(?:[.,][0-9]+)?)\s*([KMGT]?)`)6061// ParseSwapUsage parses the text of `sysctl -n vm.swapusage`, e.g.62// "total = 8192,00M  used = 7238,19M  free = 953,81M  (encrypted)".63func ParseSwapUsage(s string) (total, used uint64, err error) {64	found := map[string]uint64{}65	for _, m := range swapRE.FindAllStringSubmatch(s, -1) {66		v, perr := parseDecimal(m[2])67		if perr != nil {68			return 0, 0, perr69		}70		mult := 1.071		switch m[3] {72		case "K":73			mult = 1 << 1074		case "M":75			mult = 1 << 2076		case "G":77			mult = 1 << 3078		case "T":79			mult = 1 << 4080		}81		found[m[1]] = uint64(v*mult + 0.5)82	}83	t, okT := found["total"]84	u, okU := found["used"]85	if !okT || !okU {86		return 0, 0, fmt.Errorf("vm.swapusage: unexpected format %q", s)87	}88	return t, u, nil89}9091// GPUStats is what IOAccelerator PerformanceStatistics exposes.92type GPUStats struct {93	UtilizationPct float6494	HasUtilization bool95	MemUsed        uint6496	HasMem         bool97	Model          string98	Cores          int99}100101var (102	gpuPerfRE  = regexp.MustCompile(`"PerformanceStatistics"\s*=\s*\{([^}]*)\}`)103	gpuUtilRE  = regexp.MustCompile(`"Device Utilization %"\s*=\s*([0-9]+)`)104	gpuMemRE   = regexp.MustCompile(`"In use system memory"\s*=\s*([0-9]+)`)105	gpuModelRE = regexp.MustCompile(`"model"\s*=\s*"([^"]*)"`)106	gpuCoresRE = regexp.MustCompile(`"gpu-core-count"\s*=\s*([0-9]+)`)107)108109// ErrNoGPUStats means ioreg listed no accelerator statistics.110var ErrNoGPUStats = errors.New("no IOAccelerator PerformanceStatistics")111112// ParseIORegGPU parses `ioreg -r -d 1 -w 0 -c IOAccelerator`. With several113// accelerators, utilization is the highest and memory the sum.114func ParseIORegGPU(out string) (GPUStats, error) {115	var g GPUStats116	if m := gpuModelRE.FindStringSubmatch(out); m != nil {117		g.Model = m[1]118	}119	if m := gpuCoresRE.FindStringSubmatch(out); m != nil {120		g.Cores, _ = strconv.Atoi(m[1])121	}122	blocks := gpuPerfRE.FindAllStringSubmatch(out, -1)123	if len(blocks) == 0 {124		return g, ErrNoGPUStats125	}126	for _, b := range blocks {127		if m := gpuUtilRE.FindStringSubmatch(b[1]); m != nil {128			if v, err := strconv.ParseFloat(m[1], 64); err == nil && v >= 0 && v <= 100 {129				if !g.HasUtilization || v > g.UtilizationPct {130					g.UtilizationPct = v131				}132				g.HasUtilization = true133			}134		}135		if m := gpuMemRE.FindStringSubmatch(b[1]); m != nil {136			if v, err := strconv.ParseUint(m[1], 10, 64); err == nil {137				g.MemUsed += v138				g.HasMem = true139			}140		}141	}142	if !g.HasUtilization && !g.HasMem {143		return g, ErrNoGPUStats144	}145	return g, nil146}147148// PingResult is the summary of one ping run.149type PingResult struct {150	Sent, Received int151	LossPct        float64152	AvgMS          float64153	HasRTT         bool // false when no reply came back (100 % loss)154}155156var (157	pingCountRE = regexp.MustCompile(`(\d+) packets transmitted, (\d+) (?:packets )?received`)158	pingRTTRE   = regexp.MustCompile(`(?:round-trip|rtt) min/avg/max/(?:stddev|mdev) = ([0-9.,]+)/([0-9.,]+)/([0-9.,]+)`)159)160161// ParsePing parses the summary printed by ping on macOS and Linux. Loss is162// computed from the packet counts. With 100 % loss there is no round-trip163// line: HasRTT is false and no latency must be reported.164func ParsePing(out string) (PingResult, error) {165	var r PingResult166	m := pingCountRE.FindStringSubmatch(out)167	if m == nil {168		return r, errors.New("ping: no summary line")169	}170	r.Sent, _ = strconv.Atoi(m[1])171	r.Received, _ = strconv.Atoi(m[2])172	if r.Sent <= 0 {173		return r, errors.New("ping: no packet sent")174	}175	if r.Received > r.Sent {176		r.Received = r.Sent // duplicates177	}178	r.LossPct = 100 * float64(r.Sent-r.Received) / float64(r.Sent)179	if r.Received > 0 {180		if t := pingRTTRE.FindStringSubmatch(out); t != nil {181			if avg, err := parseDecimal(t[2]); err == nil {182				r.AvgMS, r.HasRTT = avg, true183			}184		}185	}186	return r, nil187}188189var batteryRE = regexp.MustCompile(`InternalBattery[^\n]*?\s(\d{1,3})%`)190191// ParsePmsetBatt parses `pmset -g batt`. present is false on a machine192// without a battery.193func ParsePmsetBatt(out string) (pct float64, present bool) {194	m := batteryRE.FindStringSubmatch(out)195	if m == nil {196		return 0, false197	}198	v, err := strconv.ParseFloat(m[1], 64)199	if err != nil || v < 0 || v > 100 {200		return 0, false201	}202	return v, true203}204205// ParseRouteGet parses `route -n get default` (macOS).206func ParseRouteGet(out string) (gateway, iface string) {207	for _, line := range strings.Split(out, "\n") {208		k, v, ok := strings.Cut(strings.TrimSpace(line), ":")209		if !ok {210			continue211		}212		switch strings.TrimSpace(k) {213		case "gateway":214			gateway = strings.TrimSpace(v)215		case "interface":216			iface = strings.TrimSpace(v)217		}218	}219	return220}221222// ParseProcNetRoute parses /proc/net/route (Linux) for the default route.223func ParseProcNetRoute(content string) (gateway, iface string) {224	for i, line := range strings.Split(content, "\n") {225		f := strings.Fields(line)226		if i == 0 || len(f) < 4 || f[1] != "00000000" {227			continue228		}229		flags, err := strconv.ParseUint(f[3], 16, 32)230		if err != nil || flags&0x2 == 0 { // RTF_GATEWAY231			continue232		}233		gw, err := strconv.ParseUint(f[2], 16, 32)234		if err != nil {235			continue236		}237		// Little-endian hex.238		return fmt.Sprintf("%d.%d.%d.%d", gw&0xff, gw>>8&0xff, gw>>16&0xff, gw>>24&0xff), f[0]239	}240	return "", ""241}242243// StripRemoteCredentials turns the source of a network mount into244// //host/share (or host:/export), without user name or password.245func StripRemoteCredentials(from string) string {246	if strings.HasPrefix(from, "//") {247		rest := from[2:]248		hostpart, pathpart, _ := strings.Cut(rest, "/")249		if i := strings.LastIndex(hostpart, "@"); i >= 0 {250			hostpart = hostpart[i+1:]251		}252		if pathpart != "" {253			return "//" + hostpart + "/" + pathpart254		}255		return "//" + hostpart256	}257	if u, err := url.Parse(from); err == nil && u.Scheme != "" && u.Host != "" {258		u.User = nil259		return u.String()260	}261	if i := strings.Index(from, "@"); i >= 0 && !strings.Contains(from[:i], "/") {262		return from[i+1:] // user@host:/export263	}264	return from265}266267// RemoteHost extracts the host of a credential-free remote string.268func RemoteHost(remote string) string {269	if strings.HasPrefix(remote, "//") {270		h, _, _ := strings.Cut(remote[2:], "/")271		return strings.ToLower(h)272	}273	if u, err := url.Parse(remote); err == nil && u.Host != "" {274		return strings.ToLower(u.Hostname())275	}276	if h, _, ok := strings.Cut(remote, ":"); ok {277		return strings.ToLower(h)278	}279	return ""280}281282// PSRow is one process from `ps`.283type PSRow struct {284	PID     int32285	User    string286	RSS     int64   // bytes287	CPUTime float64 // cumulative seconds288	Name    string  // executable base name; never the command line289	Path    string  // executable path as printed by ps (comm), used only locally290}291292// ParsePS parses `ps -axo pid=,user=,rss=,time=,comm=`. comm is the293// executable (a path on macOS, a short name on Linux), not the arguments.294func ParsePS(out string) []PSRow {295	var rows []PSRow296	for _, line := range strings.Split(out, "\n") {297		f := strings.Fields(line)298		if len(f) < 5 {299			continue300		}301		pid, err := strconv.ParseInt(f[0], 10, 32)302		if err != nil {303			continue304		}305		rssKB, err := strconv.ParseInt(f[2], 10, 64)306		if err != nil {307			continue308		}309		cpu, ok := parseCPUTime(f[3])310		if !ok {311			continue312		}313		// comm may contain spaces ("/Applications/Some App.app/…"): it is the rest of the line.314		idx := fieldOffset(line, 4)315		comm := strings.TrimSpace(line[idx:])316		name := comm317		if i := strings.LastIndex(comm, "/"); i >= 0 && i < len(comm)-1 {318			name = comm[i+1:]319		}320		rows = append(rows, PSRow{PID: int32(pid), User: f[1], RSS: rssKB * 1024, CPUTime: cpu, Name: name, Path: comm})321	}322	return rows323}324325// fieldOffset returns the byte offset where the n-th (0-based) whitespace326// separated field of line starts.327func fieldOffset(line string, n int) int {328	i, field := 0, 0329	for {330		for i < len(line) && (line[i] == ' ' || line[i] == '\t') {331			i++332		}333		if field == n || i >= len(line) {334			return i335		}336		for i < len(line) && line[i] != ' ' && line[i] != '\t' {337			i++338		}339		field++340	}341}342343// parseCPUTime parses ps cumulative CPU time: "M:SS.cc" (macOS),344// "[DD-]HH:MM:SS" (Linux). A decimal comma is accepted.345func parseCPUTime(s string) (float64, bool) {346	days := 0.0347	if d, rest, ok := strings.Cut(s, "-"); ok {348		v, err := strconv.ParseFloat(d, 64)349		if err != nil {350			return 0, false351		}352		days, s = v, rest353	}354	parts := strings.Split(s, ":")355	if len(parts) < 2 || len(parts) > 3 {356		return 0, false357	}358	total := 0.0359	for _, p := range parts {360		v, err := parseDecimal(p)361		if err != nil {362			return 0, false363		}364		total = total*60 + v365	}366	return days*86400 + total, true367}368369var ioregUUIDRE = regexp.MustCompile(`"IOPlatformUUID"\s*=\s*"([0-9A-Fa-f-]{36})"`)370371// ParseIOPlatformUUID extracts the hardware UUID from `ioreg -rd1 -c IOPlatformExpertDevice`.372func ParseIOPlatformUUID(out string) string {373	if m := ioregUUIDRE.FindStringSubmatch(out); m != nil {374		return strings.ToUpper(m[1])375	}376	return ""377}378379// ParseHardwarePorts parses `networksetup -listallhardwareports` into380// device -> port name ("Wi-Fi", "Ethernet", "Thunderbolt Bridge"…).381func ParseHardwarePorts(out string) map[string]string {382	res := map[string]string{}383	port := ""384	for _, line := range strings.Split(out, "\n") {385		if v, ok := strings.CutPrefix(line, "Hardware Port: "); ok {386			port = strings.TrimSpace(v)387		} else if v, ok := strings.CutPrefix(line, "Device: "); ok && port != "" {388			res[strings.TrimSpace(v)] = port389			port = ""390		}391	}392	return res393}394395// InterfaceKind classifies an interface from its name and, on macOS, its396// hardware port name.397func InterfaceKind(name, hardwarePort string, wireless bool) string {398	hp := strings.ToLower(hardwarePort)399	switch {400	case wireless || strings.Contains(hp, "wi-fi") || strings.Contains(hp, "airport") || strings.HasPrefix(name, "wl"):401		return "wifi"402	case strings.HasPrefix(name, "utun"), strings.HasPrefix(name, "wg"), strings.HasPrefix(name, "tun"),403		strings.HasPrefix(name, "tap"), strings.HasPrefix(name, "tailscale"), strings.HasPrefix(name, "ipsec"), strings.HasPrefix(name, "ppp"):404		return "tunnel"405	case strings.Contains(hp, "ethernet"), strings.Contains(hp, "lan"), strings.HasPrefix(name, "eth"),406		strings.HasPrefix(name, "eno"), strings.HasPrefix(name, "enp"), strings.HasPrefix(name, "ens"):407		return "ethernet"408	case hp == "" && strings.HasPrefix(name, "en"):409		return "ethernet"410	}411	return "other"412}413414// ParseDiskutilInfo extracts the SMART status and location from `diskutil info <mount>`.415func ParseDiskutilInfo(out string) (smart string, internal, known bool) {416	for _, line := range strings.Split(out, "\n") {417		k, v, ok := strings.Cut(line, ":")418		if !ok {419			continue420		}421		k, v = strings.TrimSpace(k), strings.TrimSpace(v)422		switch k {423		case "SMART Status":424			switch strings.ToLower(v) {425			case "verified":426				smart = "verified"427			case "failing":428				smart = "failing"429			}430		case "Device Location":431			known = true432			internal = strings.EqualFold(v, "Internal")433		case "Internal":434			if !known {435				known = true436				internal = strings.EqualFold(v, "Yes")437			}438		}439	}440	return441}442
Files main
Menu

Theme