diff --git a/agent/go.mod b/agent/go.mod new file mode 100644 index 0000000..95b8ce3 --- /dev/null +++ b/agent/go.mod @@ -0,0 +1,16 @@ +module agent + +go 1.24.5 + +require github.com/shirou/gopsutil/v4 v4.25.6 + +require ( + github.com/ebitengine/purego v0.8.4 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect + github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect + github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect + github.com/tklauser/go-sysconf v0.3.15 // indirect + github.com/tklauser/numcpus v0.10.0 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect + golang.org/x/sys v0.34.0 // indirect +) diff --git a/agent/go.sum b/agent/go.sum new file mode 100644 index 0000000..88631e4 --- /dev/null +++ b/agent/go.sum @@ -0,0 +1,32 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/ebitengine/purego v0.8.4 h1:CF7LEKg5FFOsASUj0+QwaXf8Ht6TlFxg09+S9wz0omw= +github.com/ebitengine/purego v0.8.4/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc= +github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/shirou/gopsutil/v4 v4.25.6 h1:kLysI2JsKorfaFPcYmcJqbzROzsBWEOAtw6A7dIfqXs= +github.com/shirou/gopsutil/v4 v4.25.6/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4= +github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4= +github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso= +github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= +golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/agent/main.go b/agent/main.go new file mode 100644 index 0000000..dcd2476 --- /dev/null +++ b/agent/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + + "github.com/shirou/gopsutil/v4/cpu" + "github.com/shirou/gopsutil/v4/host" + "github.com/shirou/gopsutil/v4/mem" + + "time" + + "net/http" +) + +type signal struct { + HostID string `json:"host_id"` + Hostname string `json:"host_name"` + TotalMem uint64 `json:"total_memory"` + UsedMem uint64 `json:"used_memory"` + UsedMemPercentage float64 `json:"used_memory_percentage"` + CPUPercentage float64 `json:"cpu_used_percentage"` + Timestamp time.Time `json:"timestamp"` +} + +func main() { + url := "http://localhost:8080/report" + + hid, err := host.HostID() + errorCheck(err, "[HostID] Object creation Issue!") + info, err := host.Info() + errorCheck(err, "[HostInfo] Object creation Issue!") + for { + // infinite loop + + vmem, err := mem.VirtualMemory() + errorCheck(err, "[MemoryObject] Object creation Issue!") + + timestamp := time.Now() + + cpupr, err := cpu.Percent(time.Second, false) + errorCheck(err, "[CPUPercentage] Object creation Issue!") + + signal := signal{ + HostID: hid, + Hostname: info.Hostname, + TotalMem: vmem.Total, + UsedMem: vmem.Used, + UsedMemPercentage: vmem.UsedPercent, + CPUPercentage: cpupr[0], + Timestamp: timestamp, + } + + jsonSignal, err := json.Marshal(signal) + errorCheck(err, "[JSON-Encoding] Issue") + + // displaySignal(jsonSignal) + + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonSignal)) + errorCheck(err, "[POST] signal issue") + resp.Body.Close() + // fmt.Println(resp.StatusCode) + time.Sleep(1 * time.Second) // 1s sleep (at least) + } +} + +// func displaySignal(sig signal) { +// fmt.Println("Signal {") +// fmt.Printf("\tHostID:\t\t%v\n", sig.HostID) +// fmt.Printf("\tHostName:\t%v\n", sig.Hostname) +// fmt.Printf("\tMemory Total:\t%v\n", sig.TotalMem) +// fmt.Printf("\tMemory Used:\t%v\n", sig.UsedMem) +// fmt.Printf("\tMemory Used:\t%.2f%%\n", sig.UsedMemPercentage) +// fmt.Printf("\tCPU Used:\t%.2f%%\n", sig.CPUPercentage) +// fmt.Printf("\tTimestamp:\t%v\n", sig.Timestamp) +// fmt.Println("}") +// fmt.Println() +// } + +// func displaySignal(jsonSig []byte) { +// fmt.Println("Signal =>") +// fmt.Println(string(jsonSig)) +// fmt.Println() +// } + +func errorCheck(err error, msg string) { + if err != nil { + fmt.Println(msg) + fmt.Println(err) + // time.Sleep(5 * time.Second) + } +} diff --git a/master/go.mod b/master/go.mod new file mode 100644 index 0000000..0efaf63 --- /dev/null +++ b/master/go.mod @@ -0,0 +1,3 @@ +module master + +go 1.24.5 diff --git a/master/main.go b/master/main.go new file mode 100644 index 0000000..eae757c --- /dev/null +++ b/master/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" + "time" +) + +type signal struct { + HostID string `json:"host_id"` + Hostname string `json:"host_name"` + TotalMem uint64 `json:"total_memory"` + UsedMem uint64 `json:"used_memory"` + UsedMemPercentage float64 `json:"used_memory_percentage"` + CPUPercentage float64 `json:"cpu_used_percentage"` + Timestamp time.Time `json:"timestamp"` +} + +func main() { + fmt.Println("Server Listing on Port :8080") + http.HandleFunc("/report", displayReport) + log.Fatal(http.ListenAndServe(":8080", nil)) +} + +func displayReport(respw http.ResponseWriter, req *http.Request) { + if req.Method == "POST" { + // fmt.Println("Got Signal!") + var sig signal + if err := json.NewDecoder(req.Body).Decode(&sig); err != nil { + http.Error(respw, "Invalid JSON", http.StatusBadRequest) + return + } + fmt.Printf("[RECEIVED] Host: %s | CPU: %.2f%% | RAM: %.2f%% | Time: %v\n", + sig.Hostname, sig.CPUPercentage, sig.UsedMemPercentage, sig.Timestamp) + respw.WriteHeader(http.StatusOK) + } +}