Skip to content

Commit

Permalink
Added delta values to all networks when delta=1 passed
Browse files Browse the repository at this point in the history
  • Loading branch information
jomann09 committed Apr 29, 2023
1 parent 1f90576 commit 5527c9a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 46 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
04/23/2023 - 1.0.3
04/30/2023 - 1.0.3
==================
- Added cpuPercent and memPercent to processes output (#14)
- Added delta values to the network output when delta=1 parameter passed

03/29/2023 - 1.0.2
==================
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func ParseFile(file string, defaultFile embed.FS) error {
// Set the version to the version in the VERSION file if it doesn't
// already exist during build time (this is mostly for DEV)
func ParseVersion(versionFile embed.FS) {
v, _ := versionFile.ReadFile("VERSION");
v, _ := versionFile.ReadFile("VERSION")
if Version == "" {
Version = string(v)
}
Expand Down
116 changes: 78 additions & 38 deletions internal/status/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ type Interface struct {
}

type InterfaceDelta struct {
HardwareAddr string
Addrs net.InterfaceAddrList `json:"addrs"`
net.IOCountersStat
InterfaceDeltaStat
}

type InterfaceDeltaStat struct {
Name string `json:"name"`
OutTotal float64 `json:"outTotal"`
OutPerSec float64 `json:"outPerSec"`
Expand All @@ -30,7 +37,7 @@ type InterfaceDelta struct {
checkValue float64
}

func (i InterfaceDelta) String() string {
func (i InterfaceDeltaStat) String() string {
if i.checkType == "in" {
return fmt.Sprintf("Current inbound traffic %0.2f %s/s",
i.checkValue, i.Units)
Expand All @@ -43,11 +50,11 @@ func (i InterfaceDelta) String() string {
i.checkValue, i.Units)
}

func (i InterfaceDelta) CheckValue() float64 {
func (i InterfaceDeltaStat) CheckValue() float64 {
return i.checkValue
}

func (i InterfaceDelta) PerfData(warn, crit string) string {
func (i InterfaceDeltaStat) PerfData(warn, crit string) string {
var perfdata []string
var data string

Expand Down Expand Up @@ -97,47 +104,41 @@ func HandleNetworks(cv config.Values) interface{} {
// If delta is passed, get the old value and adjust it
// based on amount of seconds passed
if cv.Delta > 0 {
tmpItr := getInterfaceStats(cv.Name)
timeSince := itr.stored.Sub(tmpItr.stored)

dOut := itr.BytesRecv - tmpItr.BytesRecv
in := float64(dOut) / timeSince.Seconds()
inPs := ConvertToUnit(uint64(in), cv.Units())
dIn := itr.BytesSent - tmpItr.BytesSent
out := float64(dIn) / timeSince.Seconds()
outPs := ConvertToUnit(uint64(out), cv.Units())

// Get check value
var cVal float64
if cv.Against == "in" {
cVal = inPs
} else if cv.Against == "out" {
cVal = outPs
} else {
cv.Against = "total"
cVal = inPs + outPs
}

deltaItr := InterfaceDelta{
Name: itr.Name,
OutTotal: ConvertToUnit(dOut, cv.Units()),
OutPerSec: outPs,
InTotal: ConvertToUnit(dIn, cv.Units()),
InPerSec: inPs,
Units: cv.Units(),
checkType: cv.Against,
checkValue: cVal,
}

// Save the current itr for later
setInterfaceStats(itr)

deltaItr := getInterfaceDeltaStat(itr, cv)
return deltaItr
}

return itr
}

// If delta, we need the InterfaceDeltaStat added to the interfaces
if cv.Delta > 0 {
var ifDeltaList []InterfaceDelta
for _, i := range ifs {
deltaItr := getInterfaceDeltaStat(i, cv)
ifDeltaList = append(ifDeltaList, InterfaceDelta{
i.HardwareAddr,
i.Addrs,
net.IOCountersStat{
Name: i.Name,
BytesSent: i.BytesSent,
BytesRecv: i.BytesRecv,
PacketsSent: i.PacketsSent,
PacketsRecv: i.PacketsRecv,
Errin: i.Errin,
Errout: i.Errout,
Dropin: i.Dropin,
Dropout: i.Dropout,
Fifoin: i.Fifoin,
Fifoout: i.Fifoout,
},
deltaItr,
})
}

return ifDeltaList
}

return ifs
}

Expand All @@ -164,3 +165,42 @@ func getNetworkIfs() ([]Interface, error) {

return ifList, nil
}

func getInterfaceDeltaStat(itr Interface, cv config.Values) InterfaceDeltaStat {
tmpItr := getInterfaceStats(itr.Name)
timeSince := itr.stored.Sub(tmpItr.stored)

dOut := itr.BytesRecv - tmpItr.BytesRecv
in := float64(dOut) / timeSince.Seconds()
inPs := ConvertToUnit(uint64(in), cv.Units())
dIn := itr.BytesSent - tmpItr.BytesSent
out := float64(dIn) / timeSince.Seconds()
outPs := ConvertToUnit(uint64(out), cv.Units())

// Get check value
var cVal float64
if cv.Against == "in" {
cVal = inPs
} else if cv.Against == "out" {
cVal = outPs
} else {
cv.Against = "total"
cVal = inPs + outPs
}

deltaItr := InterfaceDeltaStat{
Name: itr.Name,
OutTotal: ConvertToUnit(dOut, cv.Units()),
OutPerSec: outPs,
InTotal: ConvertToUnit(dIn, cv.Units()),
InPerSec: inPs,
Units: cv.Units(),
checkType: cv.Against,
checkValue: cVal,
}

// Save the current itr for later
setInterfaceStats(itr)

return deltaItr
}
12 changes: 6 additions & 6 deletions internal/status/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func HandleProcesses(cv config.Values) interface{} {
}
}
procs = append(procs, Process{
Name: name,
Exe: exe,
Cmdline: cmdline,
PID: p.Pid,
Username: username,
Name: name,
Exe: exe,
Cmdline: cmdline,
PID: p.Pid,
Username: username,
CPUPercent: cpuPercent,
MemPercent: float64(memPercent),
Status: status,
Status: status,
})
}

Expand Down

0 comments on commit 5527c9a

Please sign in to comment.