-
Notifications
You must be signed in to change notification settings - Fork 10
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
6 changed files
with
138 additions
and
13 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
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Package serverstatus generate the server system status | ||
package serverstatus | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/v3/cpu" | ||
"github.com/shirou/gopsutil/v3/disk" | ||
"github.com/shirou/gopsutil/v3/host" | ||
"github.com/shirou/gopsutil/v3/mem" | ||
) | ||
|
||
// GetSystemInfo get the system info of a given periodic | ||
func GetSystemInfo() (Cpu float64, Mem float64, Disk float64, Uptime uint64, err error) { | ||
|
||
errorString := "" | ||
|
||
cpuPercent, err := cpu.Percent(0, false) | ||
// Check if cpuPercent is empty | ||
if len(cpuPercent) > 0 && err == nil { | ||
Cpu = cpuPercent[0] | ||
} else { | ||
Cpu = 0 | ||
errorString += fmt.Sprintf("get cpu usage failed: %s ", err) | ||
} | ||
|
||
memUsage, err := mem.VirtualMemory() | ||
if err != nil { | ||
errorString += fmt.Sprintf("get mem usage failed: %s ", err) | ||
} else { | ||
Mem = memUsage.UsedPercent | ||
} | ||
|
||
diskUsage, err := disk.Usage("/") | ||
if err != nil { | ||
errorString += fmt.Sprintf("get disk usage failed: %s ", err) | ||
} else { | ||
Disk = diskUsage.UsedPercent | ||
} | ||
|
||
uptime, err := host.Uptime() | ||
if err != nil { | ||
errorString += fmt.Sprintf("get uptime failed: %s ", err) | ||
} else { | ||
Uptime = uptime | ||
} | ||
|
||
if errorString != "" { | ||
err = fmt.Errorf(errorString) | ||
} | ||
|
||
return Cpu, Mem, Disk, Uptime, err | ||
} |
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
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
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