-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add memory, CPU, and disk usage endpoints to API
- Loading branch information
1 parent
16777f9
commit 3bb95f4
Showing
7 changed files
with
244 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/cpu" | ||
) | ||
|
||
type CpuUsage struct { | ||
Cpu string `json:"cpu"` | ||
Usage float64 `json:"usage"` | ||
Idle float64 `json:"idle"` | ||
System float64 `json:"system"` | ||
User float64 `json:"user"` | ||
Percent string `json:"percent"` | ||
} | ||
|
||
func getCpuUsage() (string, error) { | ||
times, err := cpu.Times(true) | ||
if err != nil { | ||
fmt.Println("Failed to get CPU times:", err) | ||
return "", err | ||
} | ||
percentage, err := cpu.Percent(0, true) | ||
if err != nil { | ||
fmt.Println("Failed to get CPU percentage:", err) | ||
return "", err | ||
} | ||
|
||
usages := make([]CpuUsage, len(times)) | ||
for i, time := range times { | ||
usages[i] = CpuUsage{ | ||
Cpu: fmt.Sprintf("%d", i), | ||
Usage: time.Total(), | ||
Idle: time.Idle, | ||
System: time.System, | ||
User: time.User, | ||
Percent: fmt.Sprintf("%.2f%%", percentage[i]), | ||
} | ||
} | ||
overallPercentage, err := cpu.Percent(0, false) | ||
if err != nil { | ||
fmt.Println("Failed to get overall CPU percentage:", err) | ||
return "", err | ||
} | ||
usages = append(usages, CpuUsage{ | ||
Cpu: "Overall", | ||
Percent: fmt.Sprintf("%.2f%%", overallPercentage[0]), | ||
}) | ||
|
||
jsonData, err := json.MarshalIndent(usages, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(jsonData), nil | ||
|
||
} |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/disk" | ||
) | ||
|
||
type DiskUsage struct { | ||
Disk string `json:"disk"` | ||
MountPoint string `json:"mount_point"` | ||
Total uint64 `json:"total"` | ||
Free uint64 `json:"free"` | ||
Used uint64 `json:"used"` | ||
Usage float64 `json:"usage"` | ||
} | ||
|
||
func getDiskUsage() (string, error) { | ||
partitions, err := disk.Partitions(true) | ||
if err != nil { | ||
fmt.Println("Failed to get disk partitions:", err) | ||
return "", err | ||
} | ||
var usages []DiskUsage | ||
for _, partition := range partitions { | ||
if partition.Mountpoint == "" { | ||
continue | ||
} | ||
if partition.Fstype != "ext4" && partition.Fstype != "xfs" && partition.Fstype != "btrfs" && partition.Fstype != "zfs" && partition.Fstype != "ext3" && partition.Fstype != "ext2" && partition.Fstype != "ntfs" && partition.Fstype != "fat32" && partition.Fstype != "exfat" { | ||
continue | ||
} | ||
|
||
usage, err := disk.Usage(partition.Mountpoint) | ||
if err != nil { | ||
fmt.Printf("Failed to get usage for partition %s: %s\n", partition.Mountpoint, err) | ||
continue | ||
} | ||
usages = append(usages, DiskUsage{ | ||
Disk: partition.Device, | ||
MountPoint: partition.Mountpoint, | ||
Total: usage.Total, | ||
Free: usage.Free, | ||
Used: usage.Used, | ||
Usage: usage.UsedPercent, | ||
}) | ||
|
||
} | ||
jsonData, err := json.MarshalIndent(usages, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(jsonData), nil | ||
} |
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,39 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/mem" | ||
) | ||
|
||
type MemUsage struct { | ||
Total uint64 `json:"total"` | ||
Available uint64 `json:"available"` | ||
Used uint64 `json:"used"` | ||
UsedPercent float64 `json:"usedPercent"` | ||
Free uint64 `json:"free"` | ||
} | ||
|
||
func getMemUsage() (string, error) { | ||
memory, err := mem.VirtualMemory() | ||
if err != nil { | ||
fmt.Println("Failed to get memory usage:", err) | ||
return "", err | ||
} | ||
|
||
usages := MemUsage{ | ||
Total: memory.Total, | ||
Available: memory.Available, | ||
Used: memory.Used, | ||
UsedPercent: memory.UsedPercent, | ||
Free: memory.Free, | ||
} | ||
jsonData, err := json.MarshalIndent(usages, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(jsonData), nil | ||
|
||
} |
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