Skip to content

Commit fd6e8bd

Browse files
committed
motd: add gpu info
1 parent 21ff126 commit fd6e8bd

File tree

3 files changed

+112
-17
lines changed

3 files changed

+112
-17
lines changed

cmd/motd.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
showDistribution bool
2121
showDocker bool
2222
showEmby bool
23+
showGPU bool
2324
showJellyfin bool
2425
showKernel bool
2526
showLastLogin bool
@@ -60,6 +61,7 @@ last login, user sessions, process information, and system update status based o
6061
showDistribution = true
6162
showDocker = true
6263
showEmby = true
64+
showGPU = true
6365
showJellyfin = true
6466
showKernel = true
6567
showLastLogin = true
@@ -79,7 +81,7 @@ last login, user sessions, process information, and system update status based o
7981

8082
// Check if at least one flag is enabled
8183
if !showAptStatus && !showCPU && !showCpuAverages && !showDisk && !showDistribution &&
82-
!showDocker && !showEmby && !showJellyfin && !showKernel && !showLastLogin &&
84+
!showDocker && !showEmby && !showGPU && !showJellyfin && !showKernel && !showLastLogin &&
8385
!showMemory && !showNzbget && !showPlex && !showProcesses && !showQbittorrent &&
8486
!showQueues && !showRebootRequired && !showRtorrent && !showSabnzbd && !showSessions &&
8587
!showTraefik && !showUptime {
@@ -93,6 +95,7 @@ last login, user sessions, process information, and system update status based o
9395
fmt.Println(" --distro Show distribution information")
9496
fmt.Println(" --docker Show Docker container information")
9597
fmt.Println(" --emby Show Emby streaming information")
98+
fmt.Println(" --gpu Show GPU information")
9699
fmt.Println(" --jellyfin Show Jellyfin streaming information")
97100
fmt.Println(" --kernel Show kernel information")
98101
fmt.Println(" --login Show last login information")
@@ -199,22 +202,23 @@ func displayMotd() {
199202
{Key: "Load Averages:", Provider: motd.GetCpuAveragesWithContext, Timeout: 1 * time.Second, Order: 4},
200203
{Key: "Processes:", Provider: motd.GetProcessCountWithContext, Timeout: 2 * time.Second, Order: 5},
201204
{Key: "CPU:", Provider: motd.GetCpuInfoWithContext, Timeout: 2 * time.Second, Order: 6},
202-
{Key: "Memory Usage:", Provider: motd.GetMemoryInfoWithContext, Timeout: 2 * time.Second, Order: 7},
203-
{Key: "Package Status:", Provider: motd.GetAptStatusWithContext, Timeout: 5 * time.Second, Order: 8},
204-
{Key: "Reboot Status:", Provider: motd.GetRebootRequiredWithContext, Timeout: 2 * time.Second, Order: 9},
205-
{Key: "User Sessions:", Provider: motd.GetUserSessionsWithContext, Timeout: 1 * time.Second, Order: 10},
206-
{Key: "Last login:", Provider: motd.GetLastLoginWithContext, Timeout: 3 * time.Second, Order: 11},
207-
{Key: "Disk Usage:", Provider: motd.GetDiskInfoWithContext, Timeout: 3 * time.Second, Order: 12},
208-
{Key: "Docker:", Provider: motd.GetDockerInfoWithContext, Timeout: 5 * time.Second, Order: 13},
209-
{Key: "Traefik:", Provider: motd.GetTraefikInfoWithContext, Timeout: 10 * time.Second, Order: 14},
210-
{Key: "Download Queues:", Provider: motd.GetQueueInfoWithContext, Timeout: 10 * time.Second, Order: 15},
211-
{Key: "SABnzbd:", Provider: motd.GetSabnzbdInfoWithContext, Timeout: 10 * time.Second, Order: 16},
212-
{Key: "NZBGet:", Provider: motd.GetNzbgetInfoWithContext, Timeout: 10 * time.Second, Order: 17},
213-
{Key: "qBittorrent:", Provider: motd.GetQbittorrentInfoWithContext, Timeout: 10 * time.Second, Order: 18},
214-
{Key: "rTorrent:", Provider: motd.GetRtorrentInfoWithContext, Timeout: 10 * time.Second, Order: 19},
215-
{Key: "Plex:", Provider: motd.GetPlexInfoWithContext, Timeout: 10 * time.Second, Order: 20},
216-
{Key: "Emby:", Provider: motd.GetEmbyInfoWithContext, Timeout: 10 * time.Second, Order: 21},
217-
{Key: "Jellyfin:", Provider: motd.GetJellyfinInfoWithContext, Timeout: 10 * time.Second, Order: 22},
205+
{Key: "GPU:", Provider: motd.GetGpuInfoWithContext, Timeout: 3 * time.Second, Order: 7},
206+
{Key: "Memory Usage:", Provider: motd.GetMemoryInfoWithContext, Timeout: 2 * time.Second, Order: 8},
207+
{Key: "Package Status:", Provider: motd.GetAptStatusWithContext, Timeout: 5 * time.Second, Order: 9},
208+
{Key: "Reboot Status:", Provider: motd.GetRebootRequiredWithContext, Timeout: 2 * time.Second, Order: 10},
209+
{Key: "User Sessions:", Provider: motd.GetUserSessionsWithContext, Timeout: 1 * time.Second, Order: 11},
210+
{Key: "Last login:", Provider: motd.GetLastLoginWithContext, Timeout: 3 * time.Second, Order: 12},
211+
{Key: "Disk Usage:", Provider: motd.GetDiskInfoWithContext, Timeout: 3 * time.Second, Order: 13},
212+
{Key: "Docker:", Provider: motd.GetDockerInfoWithContext, Timeout: 5 * time.Second, Order: 14},
213+
{Key: "Traefik:", Provider: motd.GetTraefikInfoWithContext, Timeout: 10 * time.Second, Order: 15},
214+
{Key: "Download Queues:", Provider: motd.GetQueueInfoWithContext, Timeout: 10 * time.Second, Order: 16},
215+
{Key: "SABnzbd:", Provider: motd.GetSabnzbdInfoWithContext, Timeout: 10 * time.Second, Order: 17},
216+
{Key: "NZBGet:", Provider: motd.GetNzbgetInfoWithContext, Timeout: 10 * time.Second, Order: 18},
217+
{Key: "qBittorrent:", Provider: motd.GetQbittorrentInfoWithContext, Timeout: 10 * time.Second, Order: 19},
218+
{Key: "rTorrent:", Provider: motd.GetRtorrentInfoWithContext, Timeout: 10 * time.Second, Order: 20},
219+
{Key: "Plex:", Provider: motd.GetPlexInfoWithContext, Timeout: 10 * time.Second, Order: 21},
220+
{Key: "Emby:", Provider: motd.GetEmbyInfoWithContext, Timeout: 10 * time.Second, Order: 22},
221+
{Key: "Jellyfin:", Provider: motd.GetJellyfinInfoWithContext, Timeout: 10 * time.Second, Order: 23},
218222
}
219223

220224
// Filter sources based on enabled flags
@@ -226,6 +230,7 @@ func displayMotd() {
226230
"Load Averages:": showCpuAverages,
227231
"Processes:": showProcesses,
228232
"CPU:": showCPU,
233+
"GPU:": showGPU,
229234
"Memory Usage:": showMemory,
230235
"Package Status:": showAptStatus,
231236
"Reboot Status:": showRebootRequired,
@@ -310,6 +315,7 @@ func init() {
310315
motdCmd.Flags().BoolVar(&showDistribution, "distro", false, "Show distribution information")
311316
motdCmd.Flags().BoolVar(&showDocker, "docker", false, "Show Docker container information")
312317
motdCmd.Flags().BoolVar(&showEmby, "emby", false, "Show Emby streaming information")
318+
motdCmd.Flags().BoolVar(&showGPU, "gpu", false, "Show GPU information")
313319
motdCmd.Flags().BoolVar(&showJellyfin, "jellyfin", false, "Show Jellyfin streaming information")
314320
motdCmd.Flags().BoolVar(&showKernel, "kernel", false, "Show kernel information")
315321
motdCmd.Flags().BoolVar(&showLastLogin, "login", false, "Show last login information")

motd/context_providers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ func GetCpuInfoWithContext(ctx context.Context) string {
164164
}
165165
}
166166

167+
// GetGpuInfoWithContext provides GPU info with context/timeout support
168+
func GetGpuInfoWithContext(ctx context.Context) string {
169+
ch := make(chan string, 1)
170+
171+
go func() {
172+
ch <- GetGpuInfo()
173+
}()
174+
175+
select {
176+
case result := <-ch:
177+
return result
178+
case <-ctx.Done():
179+
return DefaultStyle.Render("GPU info timed out")
180+
}
181+
}
182+
167183
// GetMemoryInfoWithContext provides memory usage info with context/timeout support
168184
func GetMemoryInfoWithContext(ctx context.Context) string {
169185
ch := make(chan string, 1)

motd/info.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,79 @@ func GetCpuInfo() string {
561561
return DefaultStyle.Render("Not available")
562562
}
563563

564+
// GetGpuInfo returns information about the GPU(s) in the system
565+
func GetGpuInfo() string {
566+
var gpus []string
567+
568+
// List of GPU vendors/models to exclude (IPMI, server management, etc.)
569+
excludedGPUs := []string{
570+
"ASPEED", // ASPEED BMC/IPMI controllers
571+
"Matrox MGA", // Matrox G200/G400 series (server management)
572+
"Cirrus Logic", // Cirrus Logic CL-GD series (legacy/server)
573+
"XGI", // XGI Volari series (legacy)
574+
"Silicon Motion", // SM750/SM712 (embedded/server)
575+
"Hisilicon", // HiSilicon Hi171x series (server BMC)
576+
"ServerEngines", // ServerEngines Pilot series
577+
"Nuvoton", // Nuvoton WPCM450 (server management)
578+
"Pilot", // Pilot series BMC controllers
579+
}
580+
581+
// Use lspci to detect GPUs (works for NVIDIA, AMD, Intel, etc.)
582+
lspciOutput := ExecCommand("lspci")
583+
if lspciOutput != "Not available" {
584+
lines := strings.Split(lspciOutput, "\n")
585+
for _, line := range lines {
586+
// Look for VGA compatible controller or 3D controller
587+
if strings.Contains(line, "VGA compatible controller") ||
588+
strings.Contains(line, "3D controller") ||
589+
strings.Contains(line, "Display controller") {
590+
// Extract GPU name after the colon, skipping the PCI address
591+
parts := strings.SplitN(line, ":", 3)
592+
var gpuInfo string
593+
594+
if len(parts) >= 3 {
595+
// Skip the PCI address (first part) and device type (second part)
596+
gpuInfo = strings.TrimSpace(parts[2])
597+
} else if len(parts) >= 2 {
598+
// Fallback: clean up device type descriptors from the second part
599+
gpuInfo = strings.TrimSpace(parts[1])
600+
gpuInfo = strings.ReplaceAll(gpuInfo, "VGA compatible controller:", "")
601+
gpuInfo = strings.ReplaceAll(gpuInfo, "3D controller:", "")
602+
gpuInfo = strings.ReplaceAll(gpuInfo, "Display controller:", "")
603+
gpuInfo = strings.TrimSpace(gpuInfo)
604+
}
605+
606+
if gpuInfo != "" {
607+
// Check if this GPU should be excluded
608+
shouldExclude := false
609+
for _, excluded := range excludedGPUs {
610+
if strings.Contains(strings.ToUpper(gpuInfo), strings.ToUpper(excluded)) {
611+
shouldExclude = true
612+
break
613+
}
614+
}
615+
616+
if !shouldExclude {
617+
gpus = append(gpus, DefaultStyle.Render(gpuInfo))
618+
}
619+
}
620+
}
621+
}
622+
}
623+
624+
// Return empty string if no GPUs found to hide the section
625+
if len(gpus) == 0 {
626+
return ""
627+
}
628+
629+
if len(gpus) == 1 {
630+
return gpus[0]
631+
}
632+
633+
// Multiple GPUs - show each on a clean line
634+
return strings.Join(gpus, "\n")
635+
}
636+
564637
// GetMemoryInfo returns the system memory usage in a simple text format
565638
func GetMemoryInfo() string {
566639
// Try to read from /proc/meminfo first

0 commit comments

Comments
 (0)