-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
448 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,158 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/shirou/gopsutil/disk" | ||
"github.com/shirou/gopsutil/load" | ||
"github.com/shirou/gopsutil/mem" | ||
"net" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"runtime" | ||
"strings" | ||
"time" | ||
"fmt" | ||
"github.com/shirou/gopsutil/disk" | ||
"github.com/shirou/gopsutil/load" | ||
"github.com/shirou/gopsutil/mem" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"runtime" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func displaySystemInfoInInterval(interval time.Duration) { | ||
for { | ||
clearScreen() | ||
displaySystemInfo() | ||
time.Sleep(interval) | ||
} | ||
// displaySystemInfoAtInterval displays system information at specified intervals. | ||
func displaySystemInfoAtInterval(interval time.Duration) { | ||
for { | ||
clearScreen() | ||
displaySystemInfo() | ||
time.Sleep(interval) | ||
} | ||
} | ||
|
||
// displaySystemInfo collects and displays system information. | ||
func displaySystemInfo() { | ||
currentUser, err := user.Current() | ||
if err != nil { | ||
fmt.Printf("Failed to get current user: %s\n", err) | ||
return | ||
} | ||
userName := currentUser.Username | ||
|
||
currentPTS, err := getCurrentPTS() | ||
if err != nil { | ||
fmt.Printf("Failed to get current PTS: %s\n", err) | ||
return | ||
} | ||
|
||
activeUsers, err := getActiveUsers() | ||
if err != nil { | ||
fmt.Printf("Error getting active users: %s\n", err) | ||
return | ||
} | ||
|
||
hostName, err := os.Hostname() | ||
if err != nil { | ||
fmt.Printf("Failed to get hostname: %s\n", err) | ||
return | ||
} | ||
|
||
conn, err := net.Dial("udp", "8.8.8.8:80") | ||
if err != nil { | ||
fmt.Printf("Failed to dial: %s\n", err) | ||
return | ||
} | ||
localAddr := conn.LocalAddr().(*net.UDPAddr) | ||
ip := localAddr.IP.String() | ||
conn.Close() | ||
|
||
uptime, err := getUptime() | ||
if err != nil { | ||
fmt.Printf("Failed to get uptime: %s\n", err) | ||
return | ||
} | ||
upDays := uptime / (24 * time.Hour) | ||
upHours := (uptime % (24 * time.Hour)) / time.Hour | ||
upMins := (uptime % time.Hour) / time.Minute | ||
upSecs := (uptime % time.Minute) / time.Second | ||
|
||
vmStat, err := mem.VirtualMemory() | ||
if err != nil { | ||
fmt.Printf("Failed to get virtual memory info: %s\n", err) | ||
return | ||
} | ||
memoryUsed := vmStat.Used / 1024 / 1024 | ||
memoryTotal := vmStat.Total / 1024 / 1024 | ||
memoryAvailable := vmStat.Available / 1024 / 1024 | ||
usedMemoryPercent := (float64(memoryUsed) / float64(memoryTotal)) * 100 | ||
freeMemoryPercent := 100 - usedMemoryPercent | ||
|
||
cpuInfo, err := getCPUInfo() | ||
if err != nil { | ||
fmt.Printf("Failed to get CPU info: %s\n", err) | ||
return | ||
} | ||
|
||
cpuCores, err := getCPUCores() | ||
if err != nil { | ||
fmt.Printf("Failed to get CPU cores: %s\n", err) | ||
return | ||
} | ||
|
||
loadAvg, err := load.Avg() | ||
if err != nil { | ||
fmt.Printf("Failed to get load average: %s\n", err) | ||
return | ||
} | ||
|
||
diskStat, err := disk.Usage("/") | ||
if err != nil { | ||
fmt.Printf("Failed to get disk usage: %s\n", err) | ||
return | ||
} | ||
|
||
memoryUsedGB := float64(memoryUsed) / 1024 | ||
memoryTotalGB := float64(memoryTotal) / 1024 | ||
memoryAvailableGB := float64(memoryAvailable) / 1024 | ||
|
||
diskFree := float64(diskStat.Free) / 1024 / 1024 / 1024 | ||
diskTotal := float64(diskStat.Total) / 1024 / 1024 / 1024 | ||
diskUsed := diskTotal - diskFree | ||
freeDiskPercent := (diskFree / diskTotal) * 100 | ||
|
||
const ( | ||
resetColor = "\033[0m" | ||
bold = "\033[1m" | ||
cyan = "\033[36m" | ||
) | ||
fmt.Println(cyan + "======================================OSMON======================================" + resetColor) | ||
fmt.Printf("%s%s - CPU.................:%s %s (%d cores)%s\n", bold, cyan, resetColor, cpuInfo, cpuCores, resetColor) | ||
fmt.Printf("%s%s - Load................:%s %.2f %.2f %.2f%s\n", bold, cyan, resetColor, loadAvg.Load1, loadAvg.Load5, loadAvg.Load15, resetColor) | ||
fmt.Printf("%s%s - Memory..............:%s %.2f GB / %.2f GB (%.2f GB remaining, %.2f%% free)%s\n", | ||
bold, cyan, resetColor, memoryUsedGB, memoryTotalGB, memoryAvailableGB, freeMemoryPercent, resetColor) | ||
fmt.Printf("%s%s - Disk space /........:%s %.2f GB / %.2f GB (%.2f GB remaining, %.2f%% free)%s\n", bold, cyan, resetColor, diskUsed, diskTotal, diskFree, freeDiskPercent, resetColor) | ||
fmt.Printf("%s%s - Processes...........:%s %d running%s\n", bold, cyan, resetColor, getProcessCount(), resetColor) | ||
fmt.Printf("%s%s - System uptime.......:%s %d days %d hours %d minutes %d seconds%s\n", bold, cyan, resetColor, upDays, upHours, upMins, upSecs, resetColor) | ||
fmt.Printf("%s%s - Hostname / IP.......:%s %s / %s%s\n", bold, cyan, resetColor, hostName, ip, resetColor) | ||
fmt.Printf("%s%s - Release.............:%s %s%s\n", bold, cyan, resetColor, getOSRelease(), resetColor) | ||
fmt.Printf("%s%s - Current user........:%s [%s: %s]%s\n", bold, cyan, resetColor, currentPTS, userName, resetColor) | ||
if runtime.GOOS == "linux" { | ||
fmt.Printf("%s%s - Active users........:%s ", bold, cyan, resetColor) | ||
for terminal, users := range activeUsers { | ||
fmt.Printf("[%s: %s] ", terminal, strings.Join(users, ", ")) | ||
} | ||
fmt.Println() | ||
} | ||
fmt.Println(cyan + "=================================================================================" + resetColor) | ||
currentUser, err := user.Current() | ||
if err != nil { | ||
fmt.Printf("Failed to get current user: %s\n", err) | ||
return | ||
} | ||
userName := currentUser.Username | ||
|
||
currentPTS, err := getCurrentPTS() | ||
if err != nil { | ||
fmt.Printf("Failed to get current PTS: %s\n", err) | ||
currentPTS = "Unknown" | ||
} | ||
|
||
activeUsers, err := getActiveUsers() | ||
if err != nil { | ||
fmt.Printf("Error getting active users: %s\n", err) | ||
activeUsers = make(map[string][]string) | ||
} | ||
|
||
hostName, err := os.Hostname() | ||
if err != nil { | ||
fmt.Printf("Failed to get hostname: %s\n", err) | ||
hostName = "Unknown" | ||
} | ||
|
||
ip, err := getLocalIP() | ||
if err != nil { | ||
fmt.Println("No network access") | ||
ip = "No connection" | ||
} | ||
|
||
uptime, err := getUptime() | ||
if err != nil { | ||
fmt.Printf("Failed to get uptime: %s\n", err) | ||
uptime = 0 | ||
} | ||
upDays := uptime / (24 * time.Hour) | ||
upHours := (uptime % (24 * time.Hour)) / time.Hour | ||
upMins := (uptime % time.Hour) / time.Minute | ||
upSecs := (uptime % time.Minute) / time.Second | ||
|
||
vmStat, err := mem.VirtualMemory() | ||
if err != nil { | ||
fmt.Printf("Failed to get virtual memory info: %s\n", err) | ||
vmStat = &mem.VirtualMemoryStat{} | ||
} | ||
memoryUsedGB := float64(vmStat.Used) / 1024 / 1024 / 1024 | ||
memoryTotalGB := float64(vmStat.Total) / 1024 / 1024 / 1024 | ||
memoryAvailableGB := float64(vmStat.Available) / 1024 / 1024 / 1024 | ||
usedMemoryPercent := (float64(vmStat.Used) / float64(vmStat.Total)) * 100 | ||
freeMemoryPercent := 100 - usedMemoryPercent | ||
|
||
cpuInfo, err := getCPUInfo() | ||
if err != nil { | ||
fmt.Printf("Failed to get CPU info: %s\n", err) | ||
cpuInfo = "Unknown" | ||
} | ||
|
||
cpuCores, err := getCPUCores() | ||
if err != nil { | ||
fmt.Printf("Failed to get CPU cores: %s\n", err) | ||
cpuCores = 0 | ||
} | ||
|
||
loadAvg, err := load.Avg() | ||
if err != nil { | ||
fmt.Printf("Failed to get load average: %s\n", err) | ||
loadAvg = &load.AvgStat{} | ||
} | ||
|
||
diskStat, err := disk.Usage("/") | ||
if err != nil { | ||
fmt.Printf("Failed to get disk usage: %s\n", err) | ||
diskStat = &disk.UsageStat{} | ||
} | ||
diskFreeGB := float64(diskStat.Free) / 1024 / 1024 / 1024 | ||
diskTotalGB := float64(diskStat.Total) / 1024 / 1024 / 1024 | ||
diskUsedGB := diskTotalGB - diskFreeGB | ||
freeDiskPercent := (diskFreeGB / diskTotalGB) * 100 | ||
|
||
const ( | ||
resetColor = "\033[0m" | ||
bold = "\033[1m" | ||
cyan = "\033[36m" | ||
) | ||
|
||
fmt.Println(cyan + "======================================OSMON======================================" + resetColor) | ||
fmt.Printf("%s%s - CPU.................:%s %s (%d cores)%s\n", bold, cyan, resetColor, cpuInfo, cpuCores, resetColor) | ||
fmt.Printf("%s%s - Load................:%s %.2f %.2f %.2f%s\n", bold, cyan, resetColor, loadAvg.Load1, loadAvg.Load5, loadAvg.Load15, resetColor) | ||
fmt.Printf("%s%s - Memory..............:%s %.2f GB / %.2f GB (%.2f GB remaining, %.2f%% free)%s\n", | ||
bold, cyan, resetColor, memoryUsedGB, memoryTotalGB, memoryAvailableGB, freeMemoryPercent, resetColor) | ||
fmt.Printf("%s%s - Disk space /........:%s %.2f GB / %.2f GB (%.2f GB remaining, %.2f%% free)%s\n", | ||
bold, cyan, resetColor, diskUsedGB, diskTotalGB, diskFreeGB, freeDiskPercent, resetColor) | ||
fmt.Printf("%s%s - Processes...........:%s %d running%s\n", bold, cyan, resetColor, getProcessCount(), resetColor) | ||
fmt.Printf("%s%s - System uptime.......:%s %d days %d hours %d minutes %d seconds%s\n", bold, cyan, resetColor, upDays, upHours, upMins, upSecs, resetColor) | ||
fmt.Printf("%s%s - Hostname / IP.......:%s %s / %s%s\n", bold, cyan, resetColor, hostName, ip, resetColor) | ||
fmt.Printf("%s%s - Release.............:%s %s%s\n", bold, cyan, resetColor, getOSRelease(), resetColor) | ||
fmt.Printf("%s%s - Current user........:%s [%s: %s]%s\n", bold, cyan, resetColor, currentPTS, userName, resetColor) | ||
if runtime.GOOS == "linux" { | ||
fmt.Printf("%s%s - Active users........:%s ", bold, cyan, resetColor) | ||
for terminal, users := range activeUsers { | ||
fmt.Printf("[%s: %s] ", terminal, strings.Join(users, ", ")) | ||
} | ||
fmt.Println() | ||
} | ||
fmt.Println(cyan + "=================================================================================" + resetColor) | ||
} | ||
|
||
// clearScreen clears the terminal screen. | ||
func clearScreen() { | ||
if runtime.GOOS == "windows" { | ||
cmd := exec.Command("cmd", "/c", "cls") | ||
cmd.Stdout = os.Stdout | ||
cmd.Run() | ||
} else { | ||
fmt.Print("\033[H\033[2J") | ||
} | ||
if runtime.GOOS == "windows" { | ||
cmd := exec.Command("cmd", "/c", "cls") | ||
cmd.Stdout = os.Stdout | ||
cmd.Run() | ||
} else { | ||
fmt.Print("\033[H\033[2J") | ||
} | ||
} | ||
|
||
// displayHelp displays the help information. | ||
func displayHelp() { | ||
fmt.Println("OSInfo - Display system information with refresh interval") | ||
fmt.Println("\nUsage:") | ||
fmt.Println(" ./osmon Display system information once") | ||
fmt.Println(" ./osmon -i <interval> Refresh system information every <interval> seconds") | ||
fmt.Println(" ./osmon -h/--help Display this help information") | ||
fmt.Println(" ./osmon -v/--version Display the version of the application") | ||
fmt.Println("\nFlags:") | ||
fmt.Println(" -i, --interval <interval> Set interval for refreshing the display in seconds") | ||
fmt.Println(" -h, --help Display help information") | ||
fmt.Println(" -v, --version Display the version of the application") | ||
fmt.Println("OSMON - Display system information with refresh interval") | ||
fmt.Println("\nUsage:") | ||
fmt.Println(" ./osmon Display system information once") | ||
fmt.Println(" ./osmon -i <interval> Refresh system information every <interval> seconds") | ||
fmt.Println(" ./osmon -h/--help Display this help information") | ||
fmt.Println(" ./osmon -v/--version Display the version of the application") | ||
fmt.Println("\nFlags:") | ||
fmt.Println(" -i, --interval <interval> Set interval for refreshing the display in seconds") | ||
fmt.Println(" -h, --help Display help information") | ||
fmt.Println(" -v, --version Display the version of the application") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
"flag" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type intervalFlag struct { | ||
set bool | ||
value time.Duration | ||
set bool | ||
value time.Duration | ||
} | ||
|
||
// Set is a method called by the flag package when the flag is set. | ||
// Set is called by the flag package when the flag is set. | ||
func (f *intervalFlag) Set(s string) error { | ||
f.set = true | ||
var seconds int | ||
var err error | ||
seconds, err = strconv.Atoi(s) | ||
if err != nil { | ||
return fmt.Errorf("invalid format for interval: %v", err) | ||
} | ||
f.value = time.Duration(seconds) * time.Second | ||
return nil | ||
f.set = true | ||
var seconds int | ||
var err error | ||
seconds, err = strconv.Atoi(s) | ||
if err != nil { | ||
return fmt.Errorf("invalid format for interval: %v", err) | ||
} | ||
f.value = time.Duration(seconds) * time.Second | ||
return nil | ||
} | ||
|
||
// String returns the string representation of the flag | ||
// String returns the string representation of the flag. | ||
func (f *intervalFlag) String() string { | ||
if !f.set { | ||
return "not set" | ||
} | ||
return fmt.Sprintf("%d", f.value) | ||
if !f.set { | ||
return "not set" | ||
} | ||
return fmt.Sprintf("%d", f.value) | ||
} | ||
|
||
func DefineFlags() (intervalFlag, *bool, *bool) { | ||
var interval intervalFlag | ||
var helpFlag = flag.Bool("h", false, "Display help information (shorthand)") | ||
var versionFlag = flag.Bool("v", false, "Display the version of the application (shorthand)") | ||
var interval intervalFlag | ||
var helpFlag = flag.Bool("h", false, "Display help information (shorthand)") | ||
var versionFlag = flag.Bool("v", false, "Display the version of the application (shorthand)") | ||
|
||
flag.Var(&interval, "i", "Set interval for refreshing the display in seconds (shorthand)") | ||
flag.Var(&interval, "interval", "Set interval for refreshing the display in seconds") | ||
flag.Var(&interval, "i", "Set interval for refreshing the display in seconds (shorthand)") | ||
flag.Var(&interval, "interval", "Set interval for refreshing the display in seconds") | ||
|
||
return interval, helpFlag, versionFlag | ||
return interval, helpFlag, versionFlag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
module github.com/debek/osmon | ||
module osmon | ||
|
||
go 1.16 | ||
go 1.17 | ||
|
||
require github.com/shirou/gopsutil v3.21.11+incompatible | ||
|
||
require ( | ||
github.com/stretchr/testify v1.8.4 // indirect | ||
github.com/tklauser/go-sysconf v0.3.13 // indirect | ||
github.com/yusufpapurcu/wmi v1.2.3 // indirect | ||
github.com/tklauser/go-sysconf v0.3.14 // indirect | ||
github.com/tklauser/numcpus v0.8.0 // indirect | ||
golang.org/x/sys v0.26.0 // indirect | ||
) |
Oops, something went wrong.