Created
June 15, 2024 11:53
-
-
Save habibiefaried/485daf4393fdc295ccbcfd8cbc28e4e6 to your computer and use it in GitHub Desktop.
using infinite loop for sending the stats, what could go wrong?
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
package main | |
import ( | |
"fmt" | |
"net" | |
"sort" | |
"time" | |
"github.com/shirou/gopsutil/disk" | |
"github.com/shirou/gopsutil/host" | |
"github.com/shirou/gopsutil/process" | |
) | |
func GetStatsD() (string, error) { | |
ret := "" | |
// Uptime | |
uptime, err := host.Uptime() | |
if err != nil { | |
return ret, err | |
} | |
ret = ret + fmt.Sprintf("System Uptime: %v seconds\n", uptime) | |
// Time | |
ret = ret + fmt.Sprintf("Current Date and Time: %v\n", time.Now().Format("2006-01-02 15:04:05")) | |
// Disk usage | |
usage, err := disk.Usage("/") | |
if err != nil { | |
return ret, err | |
} | |
ret = ret + fmt.Sprintf("Total Disk: %v bytes\n", usage.Total) | |
ret = ret + fmt.Sprintf("Used Disk: %v bytes\n", usage.Used) | |
ret = ret + fmt.Sprintf("Free Disk: %v bytes\n", usage.Free) | |
// Top 10 heavy-ram processes | |
processes, err := process.Processes() | |
if err != nil { | |
return ret, err | |
} | |
type ProcessRAM struct { | |
Pid int32 | |
Name string | |
RAM uint64 | |
} | |
var procRAMs []ProcessRAM | |
for _, proc := range processes { | |
memInfo, err := proc.MemoryInfo() | |
if err != nil { | |
continue | |
} | |
name, err := proc.Name() | |
if err != nil { | |
continue | |
} | |
procRAMs = append(procRAMs, ProcessRAM{ | |
Pid: proc.Pid, | |
Name: name, | |
RAM: memInfo.RSS, | |
}) | |
} | |
sort.Slice(procRAMs, func(i, j int) bool { | |
return procRAMs[i].RAM > procRAMs[j].RAM | |
}) | |
ret = ret + fmt.Sprintf("Top 10 RAM Consuming Processes:\n") | |
for i, proc := range procRAMs { | |
if i >= 10 { | |
break | |
} | |
ret = ret + fmt.Sprintf("%d. PID: %d, Name: %s, RAM: %d bytes\n", i+1, proc.Pid, proc.Name, proc.RAM) | |
} | |
return ret, nil | |
} | |
func main() { | |
addr := net.UDPAddr{ | |
Port: 8080, | |
IP: net.ParseIP("0.0.0.0"), | |
} | |
conn, err := net.ListenUDP("udp", &addr) | |
if err != nil { | |
fmt.Println("Error starting server:", err) | |
return | |
} | |
defer conn.Close() | |
fmt.Println("Server is listening on port 8080") | |
buffer := make([]byte, 1024) | |
for { | |
n, clientAddr, err := conn.ReadFromUDP(buffer) | |
if err != nil { | |
fmt.Println("Error reading from connection:", err) | |
} | |
message := string(buffer[:n]) | |
fmt.Println("Received from client:", message) | |
go func() { | |
for { | |
fmt.Println("Kirim stats monitoring") | |
response, _ := GetStatsD() | |
_, err = conn.WriteToUDP([]byte(response), clientAddr) | |
if err != nil { | |
fmt.Println("Error writing to connection:", err) | |
} | |
time.Sleep(3 * time.Second) | |
} | |
}() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment