diff --git a/CHANGELOG.md b/CHANGELOG.md index bf26c58..2504c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ================== diff --git a/internal/config/config.go b/internal/config/config.go index 54db608..60bfaa6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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) } diff --git a/internal/status/network.go b/internal/status/network.go index b9a25b1..7fc22cb 100644 --- a/internal/status/network.go +++ b/internal/status/network.go @@ -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"` @@ -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) @@ -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 @@ -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 } @@ -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 +} diff --git a/internal/status/processes.go b/internal/status/processes.go index 0f5e527..6048d89 100644 --- a/internal/status/processes.go +++ b/internal/status/processes.go @@ -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, }) }