From 4cad514e1630bc9ba108ab42162eecfacfdb59ea Mon Sep 17 00:00:00 2001 From: Samuel COLLADO Date: Thu, 15 Dec 2022 20:33:51 +0100 Subject: [PATCH] =?UTF-8?q?=C3=89ischt=20Import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpu.go | 48 +++++++++++++++++++++ disk.go | 79 ++++++++++++++++++++++++++++++++++ go.mod | 15 +++++++ go.sum | 38 ++++++++++++++++ ip.go | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ load.go | 25 +++++++++++ main.go | 86 +++++++++++++++++++++++++++++++++++++ memoy.go | 27 ++++++++++++ net.go | 79 ++++++++++++++++++++++++++++++++++ processus.go | 62 +++++++++++++++++++++++++++ 10 files changed, 578 insertions(+) create mode 100644 cpu.go create mode 100644 disk.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 ip.go create mode 100644 load.go create mode 100644 main.go create mode 100644 memoy.go create mode 100644 net.go create mode 100644 processus.go diff --git a/cpu.go b/cpu.go new file mode 100644 index 0000000..e0504bd --- /dev/null +++ b/cpu.go @@ -0,0 +1,48 @@ +package system + +import ( + "time" + + "github.com/shirou/gopsutil/v3/cpu" +) + +type Processeur struct { + ID int32 `json:"id"` + VendorID string `json:"vendoid"` + Family string `json:"family"` + Model string `json:"model"` + Mhz float64 `json:"freq"` + PerCent float64 `json:"pcent"` +} + +// GetProcesseur : Récupération des processeurs +func GetProcesseur() ([]Processeur, error) { + + var cpus []Processeur + + cpuInfos, err := cpu.Info() + if err != nil { + return []Processeur{}, err + } + + percents, err := cpu.Percent(time.Second, true) + if err != nil { + return []Processeur{}, err + } + + i := 0 + for _, ci := range cpuInfos { + cpu := Processeur{} + cpu.ID = ci.CPU + cpu.VendorID = ci.VendorID + cpu.Family = ci.Family + cpu.Model = ci.Model + cpu.Mhz = ci.Mhz + cpu.PerCent = percents[i] + cpus = append(cpus, cpu) + i++ + cpus = append(cpus, cpu) + } + + return cpus, nil +} diff --git a/disk.go b/disk.go new file mode 100644 index 0000000..2bad8c7 --- /dev/null +++ b/disk.go @@ -0,0 +1,79 @@ +package system + +import ( + "strconv" + "strings" + + "github.com/shirou/gopsutil/v3/disk" +) + +type Disque struct { + ID int `json:"id"` + Device string `json:"device"` + MountPoint string `json:"mountpoint"` + FSType string `json:"fstype"` + UsedPerCent float64 `json:"usedpcent"` + Total uint64 `json:"total"` + Used uint64 `json:"used"` + Free uint64 `json:"free"` +} + +// GetDisque : Récupération des disques +func GetDisque() ([]Disque, error) { + + parts, err := disk.Partitions(true) + if err != nil { + return []Disque{}, err + } + + var disks []Disque + for i, part := range parts { + var d Disque + t := strings.Split(part.Device, "/") + if t[0] == "" { + diskInfo, _ := disk.Usage(part.Mountpoint) + d.ID = i + d.Device = part.Device + d.MountPoint = part.Mountpoint + d.FSType = part.Fstype + d.Total = diskInfo.Total + d.Used = diskInfo.Used + d.Free = diskInfo.Free + d.UsedPerCent = diskInfo.UsedPercent + disks = append(disks, d) + } + } + return disks, nil +} + +// Get1Disque : Récupération d'un disque +func Get1Disque(id string) (Disque, error) { + n, err := strconv.ParseInt(id, 10, 32) + if err != nil { + return Disque{}, err + } + num := int((n)) + + parts, err := disk.Partitions(true) + if err != nil { + return Disque{}, err + } + + var d Disque + for i, part := range parts { + t := strings.Split(part.Device, "/") + if t[0] == "" && i == num { + diskInfo, _ := disk.Usage(part.Mountpoint) + d.ID = i + d.Device = part.Device + d.MountPoint = part.Mountpoint + d.FSType = part.Fstype + d.Total = diskInfo.Total + d.Used = diskInfo.Used + d.Free = diskInfo.Free + d.UsedPerCent = diskInfo.UsedPercent + break + } + } + return d, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..be58924 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module git.evoliatis.fr/scollado/system + +go 1.19 + +require github.com/shirou/gopsutil/v3 v3.22.11 + +require ( + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/tklauser/go-sysconf v0.3.11 // indirect + github.com/tklauser/numcpus v0.6.0 // indirect + github.com/yusufpapurcu/wmi v1.2.2 // indirect + golang.org/x/sys v0.2.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d522133 --- /dev/null +++ b/go.sum @@ -0,0 +1,38 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= +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-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= +github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= +github.com/shirou/gopsutil/v3 v3.22.11 h1:kxsPKS+Eeo+VnEQ2XCaGJepeP6KY53QoRTETx3+1ndM= +github.com/shirou/gopsutil/v3 v3.22.11/go.mod h1:xl0EeL4vXJ+hQMAGN8B9VFpxukEMA0XdevQOe5MZ1oY= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM= +github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI= +github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms= +github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4= +github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= +github.com/yusufpapurcu/wmi v1.2.2/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.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +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/ip.go b/ip.go new file mode 100644 index 0000000..747446a --- /dev/null +++ b/ip.go @@ -0,0 +1,119 @@ +package system + +import ( + "fmt" + "io" + "net" + "net/http" + "strconv" +) + +type AdresseIP struct { + ID int `json:"id"` + Type string `json:"type"` + Adresse string `json:"ip"` + Netmask string `json:"netmask"` +} + +// netmask : transformation du masque ivp4 +func netmask(val string) string { + var tab [4]int64 + tab[0], _ = strconv.ParseInt(val[0:2], 16, 64) + tab[1], _ = strconv.ParseInt(val[2:4], 16, 64) + tab[2], _ = strconv.ParseInt(val[4:6], 16, 64) + tab[3], _ = strconv.ParseInt(val[6:8], 16, 64) + + return fmt.Sprint(tab[0], ".", tab[1], ".", tab[2], ".", tab[3]) +} + +// netmask6 : transformation du masque ivp6 +func netmask6(val string) string { + var r string + + for i := 0; i < 16; i++ { + r = r + val[i:i+2] + ":" + } + + return r[:len(r)-1] +} + +// Get1AdresseIP : Récupération d'une adresse IP +func Get1AdresseIP(id string) (AdresseIP, error) { + n, err := strconv.ParseInt(id, 10, 32) + if err != nil { + return AdresseIP{}, err + } + num := int((n)) + + addrs, err := net.InterfaceAddrs() + if err != nil { + return AdresseIP{}, err + } + ip := AdresseIP{} + for i, address := range addrs { + if i != num { + continue + } + var mask, itype string + ipnet, _ := address.(*net.IPNet) + if ipnet.IP.To4() != nil { + mask = netmask(ipnet.Mask.String()) + itype = "ipv4" + + } else { + mask = netmask6(ipnet.Mask.String()) + itype = "ipv6" + } + ip = AdresseIP{ + ID: i, + Netmask: mask, + Adresse: ipnet.IP.String(), + Type: itype, + } + break + } + return ip, nil +} + +// GetAdresseIP : Récupération de l'adresse IP +func GetAdresseIP() ([]AdresseIP, error) { + ips := []AdresseIP{} + addrs, err := net.InterfaceAddrs() + if err != nil { + return []AdresseIP{}, err + } + for i, address := range addrs { + var mask string + ipnet, _ := address.(*net.IPNet) + if ipnet.IP.To4() != nil { + mask = netmask(ipnet.Mask.String()) + + } else { + mask = netmask6(ipnet.Mask.String()) + } + ip := AdresseIP{ + ID: i, + Netmask: mask, + Adresse: ipnet.IP.String(), + Type: "local", + } + ips = append(ips, ip) + } + return ips, nil +} + +// GetPasserelle : IP Externe +func GetPasserelle() (AdresseIP, error) { + resp, err := http.Get("http://ifconfig.me") + if err != nil { + return AdresseIP{}, err + } + defer resp.Body.Close() + content, _ := io.ReadAll(resp.Body) + ip := AdresseIP{ + Type: "passerelle", + Adresse: string(content), + Netmask: "0.0.0.0", + } + return ip, nil +} diff --git a/load.go b/load.go new file mode 100644 index 0000000..6b668b0 --- /dev/null +++ b/load.go @@ -0,0 +1,25 @@ +package system + +import ( + "github.com/shirou/gopsutil/v3/load" +) + +type Charge struct { + Load1 float64 `json:"load1"` + Load5 float64 `json:"load5"` + Load15 float64 `json:"load15"` +} + +// GetCharge : Récupération de la charge +func GetCharge() (Charge, error) { + info, err := load.Avg() + if err != nil { + return Charge{}, err + } + l := Charge{ + Load1: info.Load1, + Load5: info.Load5, + Load15: info.Load15, + } + return l, nil +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..9488241 --- /dev/null +++ b/main.go @@ -0,0 +1,86 @@ +package system + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +type API struct { + URL string `json:"url"` + Methode string `json:"methode"` +} + +// GetAPI : Liste des méthodes par API +func GetAPI() []API { + return []API{ + { + URL: "/api", + Methode: "GetAPI", + }, + { + URL: "/processeur", + Methode: "GetProcesseur", + }, + { + URL: "/disque", + Methode: "GetDisque", + }, + { + URL: "/disque/{id}", + Methode: "Get1Disque", + }, + { + URL: "/ip", + Methode: "GetAdresseIP", + }, + { + URL: "/ip/{id}", + Methode: "Get1AdresseIP", + }, + { + URL: "/ip/passerelle", + Methode: "GetPasserelle", + }, + { + URL: "/charge", + Methode: "GetCharge", + }, + { + URL: "/memoire", + Methode: "GetMemoire", + }, + { + URL: "/carte", + Methode: "GetCarteReseau", + }, + { + URL: "/carte/{name}", + Methode: "Get1CarteReseau", + }, + { + URL: "/processus", + Methode: "GetProcessus", + }, + { + URL: "/processus/{id}", + Methode: "Get1Processus", + }, + { + URL: "/processus/kill/{id}", + Methode: "Kill1Processus", + }, + } +} + +// EnvoiJSON : envoie le JSON +func EnvoiJSON(ps any, url string, w http.ResponseWriter) { + // Transformation JSON + j, err := json.Marshal(ps) + if err != nil { + log.Printf("ERREUR %s : %v", url, err) + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, string(j)) +} diff --git a/memoy.go b/memoy.go new file mode 100644 index 0000000..eadc518 --- /dev/null +++ b/memoy.go @@ -0,0 +1,27 @@ +package system + +import ( + "github.com/shirou/gopsutil/v3/mem" +) + +type Memoire struct { + Total uint64 `json:"total"` + Available uint64 `json:"available"` + Used uint64 `json:"used"` + Free uint64 `json:"free"` +} + +// GetMemoire : Récupération de la mémoire +func GetMemoire() (Memoire, error) { + memInfo, err := mem.VirtualMemory() + if err != nil { + return Memoire{}, err + } + var mem Memoire + mem.Available = memInfo.Available + mem.Free = memInfo.Free + mem.Total = memInfo.Total + mem.Used = memInfo.Used + + return mem, nil +} diff --git a/net.go b/net.go new file mode 100644 index 0000000..2e07e4e --- /dev/null +++ b/net.go @@ -0,0 +1,79 @@ +package system + +import ( + "github.com/shirou/gopsutil/v3/net" +) + +type CarteReseau struct { + Name string + MTU int + MacAddress string + BytesSent uint64 + BytesRecv uint64 + PacketsSent uint64 + PacketsRecv uint64 + ErrorsIn uint64 + ErrorsOut uint64 + Addrs []net.InterfaceAddr +} + +// Get1CarteReseau : Carte réseau +func Get1CarteReseau(name string) (CarteReseau, error) { + var c CarteReseau + + cards, err := net.Interfaces() + if err != nil { + return CarteReseau{}, err + } + infos, err := net.IOCounters(true) + if err != nil { + return CarteReseau{}, err + } + + for i, card := range cards { + if infos[i].Name == name { + c.Name = infos[i].Name + c.MTU = card.MTU + c.MacAddress = card.HardwareAddr + c.BytesSent = infos[i].BytesSent + c.BytesRecv = infos[i].BytesRecv + c.PacketsSent = infos[i].PacketsSent + c.PacketsRecv = infos[i].PacketsRecv + c.ErrorsIn = infos[i].Errin + c.ErrorsOut = infos[i].Errout + c.Addrs = card.Addrs + break + } + } + return c, nil +} + +// GetCarteReseau : Cartes réseaux +func GetCarteReseau() ([]CarteReseau, error) { + var result []CarteReseau + + cards, err := net.Interfaces() + if err != nil { + return []CarteReseau{}, err + } + infos, err := net.IOCounters(true) + if err != nil { + return []CarteReseau{}, err + } + + for i, card := range cards { + var c CarteReseau + c.Name = infos[i].Name + c.MTU = card.MTU + c.MacAddress = card.HardwareAddr + c.BytesSent = infos[i].BytesSent + c.BytesRecv = infos[i].BytesRecv + c.PacketsSent = infos[i].PacketsSent + c.PacketsRecv = infos[i].PacketsRecv + c.ErrorsIn = infos[i].Errin + c.ErrorsOut = infos[i].Errout + c.Addrs = card.Addrs + result = append(result, c) + } + return result, nil +} diff --git a/processus.go b/processus.go new file mode 100644 index 0000000..17644cf --- /dev/null +++ b/processus.go @@ -0,0 +1,62 @@ +package system + +import ( + "strconv" + + "github.com/shirou/gopsutil/v3/process" +) + +type Processus struct { + User string `json:"user"` + Pid int32 `json:"pid"` + Cpu float64 `json:"cpu"` + Mem float32 `json:"mem"` + Name string `json:"name"` + Status []string `json:"status"` +} + +// Get1Processus : récupération d'un processus +func Get1Processus(id string) (Processus, error) { + i, err := strconv.ParseInt(id, 10, 32) + if err != nil { + return Processus{}, err + } + + processes, err := process.Processes() + if err != nil { + return Processus{}, err + } + var ps Processus + for _, process := range processes { + if int32(i) == process.Pid { + ps.User, _ = process.Username() + ps.Pid = process.Pid + ps.Cpu, _ = process.CPUPercent() + ps.Mem, _ = process.MemoryPercent() + ps.Name, _ = process.Name() + ps.Status, _ = process.Status() + break + } + } + return ps, nil +} + +// GetProcessus : récupération des processus +func GetProcessus() ([]Processus, error) { + processes, err := process.Processes() + if err != nil { + return []Processus{}, err + } + var pslist []Processus + for _, process := range processes { + var ps Processus + ps.User, _ = process.Username() + ps.Pid = process.Pid + ps.Cpu, _ = process.CPUPercent() + ps.Mem, _ = process.MemoryPercent() + ps.Name, _ = process.Name() + ps.Status, _ = process.Status() + pslist = append(pslist, ps) + } + return pslist, nil +}