-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_funcs.go
More file actions
89 lines (72 loc) · 1.94 KB
/
main_funcs.go
File metadata and controls
89 lines (72 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"github.com/shirou/gopsutil/v3/disk"
"github.com/valyala/fasthttp"
"encoding/json"
"syscall"
"time"
"fmt"
)
// Json converts any data or []string to JSON format
func Json(data interface{}) (string, error) {
// Marshal the data to JSON with indentation for better readability
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}
return string(jsonData), nil
}
func GetDiskUsage() (map[string]float64, error) {
usageMap := make(map[string]float64)
// Get a list of disk partitions
partitions, err := disk.Partitions(true) // true to include all devices
if err != nil {
usageMap["error"] = -1
return usageMap, err // handle error
}
// Iterate over partitions to get disk usage
for _, partition := range partitions {
usage, err := disk.Usage(partition.Mountpoint)
if err != nil {
// Skip or handle error
continue
}
// Store usage percentage in the map with mountpoint as the key
usageMap[partition.Mountpoint] = usage.UsedPercent
}
return usageMap, nil
}
func formatUptime(uptimeSecs int64) string {
uptime := time.Duration(uptimeSecs) * time.Second
days := uptime / (24 * time.Hour)
uptime -= days * 24 * time.Hour
hours := uptime / time.Hour
uptime -= hours * time.Hour
minutes := uptime / time.Minute
return fmt.Sprintf("%d day(s) %d hour(s) %d min(s)", days, hours, minutes)
}
func GetUptime() (string,int64) {
var sysinfo syscall.Sysinfo_t
err := syscall.Sysinfo(&sysinfo)
if err != nil {
fmt.Println("Error:", err)
return "",0
}
return formatUptime(sysinfo.Uptime),sysinfo.Uptime
}
func ctx_error_handler(ctx *fasthttp.RequestCtx, err error) {
if err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
fmt.Fprintf(ctx, "An error occurred: Check terminal errors.")
}
}
func Contains(slice []string, toCheck string) bool {
found := false
for _, value := range slice {
if value == toCheck {
found = true
break
}
}
return found
}