Skip to content

Instantly share code, notes, and snippets.

@juanhuttemann
Last active February 20, 2019 14:53
Show Gist options
  • Select an option

  • Save juanhuttemann/c193832b835878f97764d4d55f493046 to your computer and use it in GitHub Desktop.

Select an option

Save juanhuttemann/c193832b835878f97764d4d55f493046 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
)
//Address for NetworkDevices
type Address struct {
IP string `json:"ip"`
}
//NetworkDevice properties
type NetworkDevice struct {
Name string `json:"name"`
Flags net.Flags `json:"flags"`
MAC net.HardwareAddr `json:"mac"`
Addresses []Address `json:"addresses"`
}
//RAM of the host machine
type RAM struct {
Free uint64 `json:"free"`
Total uint64 `json:"total"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"usedPercent"`
}
//CPU of the host machine
type CPU struct {
UsagePerCore []float64 `json:"UsagePerCore"`
}
//Host machine data
type Host struct {
Hostname string `json:"hostname"`
OS string `json:"os"`
Platform string `json:"platform"`
PlatformVersion string `json:"platformVersion"`
Uptime uint64 `json:"uptime"`
RAM `json:"ram"`
CPU []float64 `json:"cpu"`
NetworkDevices []NetworkDevice `json:"networkDevices"`
}
var timeTotal, _ = time.ParseDuration("500ms")
func checkCPU() {
cpuName, _ := cpu.Info()
other := cpuName[0]
fmt.Println(other.CPU)
fmt.Println(other.Cores)
fmt.Println(other.Family)
fmt.Println(other.Model)
fmt.Println(other.ModelName)
fmt.Println(other.VendorID)
fmt.Println(other.CacheSize)
}
func checkNetwork() []NetworkDevice {
var n []NetworkDevice
interfaces, err := net.Interfaces()
if err != nil {
fmt.Print(err)
}
for _, iface := range interfaces {
var a []Address
byNameInterface, err := net.InterfaceByName(iface.Name)
if err != nil {
fmt.Println(err)
}
addresses, err := byNameInterface.Addrs()
for _, v := range addresses {
address := Address{
IP: v.String(),
}
a = append(a, address)
}
if err != nil {
fmt.Println(err)
}
networkDevice := NetworkDevice{
Name: iface.Name,
Flags: iface.Flags,
MAC: iface.HardwareAddr,
Addresses: a,
}
n = append(n, networkDevice)
}
return n
}
func checkServer(w http.ResponseWriter, r *http.Request) {
hostInfo, err := host.Info()
if err != nil {
fmt.Print(err)
return
}
m, err := mem.VirtualMemory()
if err != nil {
fmt.Print(err)
return
}
cpuUsagePerCore, err := cpu.Percent(timeTotal, true)
if err != nil {
fmt.Print(err)
return
}
networkDevices := checkNetwork()
checkCPU()
host := Host{
Hostname: hostInfo.Hostname,
OS: hostInfo.OS,
Platform: hostInfo.Platform,
PlatformVersion: hostInfo.PlatformVersion,
Uptime: hostInfo.Uptime,
RAM: RAM{
Total: m.Total,
Free: m.Free,
Used: m.Used,
UsedPercent: m.UsedPercent,
},
CPU: cpuUsagePerCore,
NetworkDevices: networkDevices,
}
js, err := json.Marshal(host)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func main() {
http.HandleFunc("/", checkServer)
if err := http.ListenAndServe(":3000", nil); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment