Éischt Import

This commit is contained in:
Samuel COLLADO 2022-12-15 20:33:51 +01:00
parent c1ef28ee06
commit 4cad514e16
10 changed files with 578 additions and 0 deletions

48
cpu.go Normal file
View File

@ -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
}

79
disk.go Normal file
View File

@ -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
}

15
go.mod Normal file
View File

@ -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
)

38
go.sum Normal file
View File

@ -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=

119
ip.go Normal file
View File

@ -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
}

25
load.go Normal file
View File

@ -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
}

86
main.go Normal file
View File

@ -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))
}

27
memoy.go Normal file
View File

@ -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
}

79
net.go Normal file
View File

@ -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
}

62
processus.go Normal file
View File

@ -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
}