-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more information about the system
- Loading branch information
Showing
7 changed files
with
259 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= | ||
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= | ||
github.com/ervitis/goprocinfo v1.0.0 h1:QKty3U/By/lWz+yJu1zLQw9DpEmF/1cJVmr9wxyeVo0= | ||
github.com/ervitis/goprocinfo v1.0.0/go.mod h1:uY2dRmcKAzEjtgvQ+NBj7RK4EBIK/oDtp2ydUNRa0Ds= | ||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= | ||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package gohealthchecker | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ervitis/goprocinfo/linux" | ||
"net" | ||
"os" | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
Running string = "R" | ||
Sleeping = "S" | ||
Waiting = "D" | ||
Zombie = "Z" | ||
Stopped = "T" | ||
Traced = "t" | ||
Dead = "X" | ||
) | ||
|
||
type ( | ||
ipAddressInfo struct { | ||
ipAddress string | ||
} | ||
|
||
mappedProcessesStatus map[string]bool | ||
|
||
memoryData struct { | ||
total uint64 | ||
free uint64 | ||
available uint64 | ||
} | ||
|
||
SystemInformation struct { | ||
mappedProcessStatus mappedProcessesStatus | ||
processStatus string | ||
processActive bool | ||
pid uint64 | ||
startTime time.Time | ||
memory *memoryData | ||
ipAddress *ipAddressInfo | ||
runtimeVersion string | ||
canAcceptWork bool | ||
|
||
mtx sync.Mutex | ||
} | ||
) | ||
|
||
// GetIpAddress returns the ip address of the machine | ||
// it returns an IpAddressInfo object which stores the IpAddress | ||
// we are using the net.Dial function to get the origin of the connection to the host we want to connect | ||
func (info *SystemInformation) getIpAddress() *ipAddressInfo { | ||
conn, _ := net.Dial("udp", "1.1.1.1:80") | ||
defer conn.Close() | ||
|
||
return &ipAddressInfo{ipAddress: conn.LocalAddr().(*net.UDPAddr).IP.String()} | ||
} | ||
|
||
// GetRuntimeVersion returns the Golang version which was compiled | ||
func (info *SystemInformation) getRuntimeVersion() string { | ||
return runtime.Version() | ||
} | ||
|
||
func (info *SystemInformation) initMappedStatus() { | ||
if info.mappedProcessStatus != nil { | ||
return | ||
} | ||
|
||
info.mtx.Lock() | ||
defer info.mtx.Unlock() | ||
info.mappedProcessStatus = make(mappedProcessesStatus) | ||
info.mappedProcessStatus[Running] = true | ||
info.mappedProcessStatus[Sleeping] = true | ||
info.mappedProcessStatus[Traced] = false | ||
info.mappedProcessStatus[Waiting] = false | ||
info.mappedProcessStatus[Zombie] = false | ||
info.mappedProcessStatus[Stopped] = false | ||
info.mappedProcessStatus[Dead] = false | ||
} | ||
|
||
// GetSystemInfo initialize the data of the current process running | ||
// it returns an error if it cannot locate the *nix files to get the data | ||
func (info *SystemInformation) GetSystemInfo() error { | ||
info.initMappedStatus() | ||
|
||
info.mtx.Lock() | ||
defer info.mtx.Unlock() | ||
|
||
processStatus, err := linux.ReadProcessStat(fmt.Sprintf("/proc/%d/stat", os.Getpid())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
memInfo, err := linux.ReadMemInfo("/proc/meminfo") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
info.pid = processStatus.Pid | ||
info.processActive = info.mappedProcessStatus[processStatus.State] | ||
info.processStatus = processStatus.State | ||
info.memory = &memoryData{ | ||
total: memInfo.MemTotal, | ||
free: memInfo.MemFree, | ||
available: memInfo.MemAvailable, | ||
} | ||
info.runtimeVersion = info.getRuntimeVersion() | ||
info.ipAddress = info.getIpAddress() | ||
info.canAcceptWork = info.processActive && (info.memory.available > 0) | ||
return nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package gohealthchecker | ||
|
||
import "testing" | ||
|
||
func TestHelperGetHostIpAddress(t *testing.T) { | ||
info := &SystemInformation{} | ||
myIp := info.getIpAddress() | ||
|
||
if myIp.ipAddress == "" { | ||
t.Errorf("ip address should not be empty") | ||
} | ||
|
||
if myIp.ipAddress == "127.0.0.1" || myIp.ipAddress == "0.0.0.0" { | ||
t.Errorf("ip address should not be localhost") | ||
} | ||
} | ||
|
||
func TestHelperGetRuntimeVersion(t *testing.T) { | ||
info := &SystemInformation{} | ||
version := info.getRuntimeVersion() | ||
|
||
if version == "" { | ||
t.Errorf("version should not be empty") | ||
} | ||
} | ||
|
||
func TestGetStatusSystem(t *testing.T) { | ||
info := &SystemInformation{} | ||
if err := info.GetSystemInfo(); err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
} |