Skip to content

Commit

Permalink
Merge branch 'release/v0.4.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandaoc-io committed Oct 21, 2021
2 parents 01fefc8 + 30a610b commit d65a237
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

project.txt
output.html

.vscode/
5 changes: 3 additions & 2 deletions cmd/netintcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ func networkInterfaceCheck(snmpVersion string, cmd *cobra.Command, args []string
sknchk.Unknown(fmt.Sprint(err), "")
}

//Check if interface is admin down, in that no need to process other information.
//Check if interface is admin down, in this case no need to process other information.
if intNewData.IfAdminStatus != nil && *intNewData.IfAdminStatus == netint.DOWN {
sknchk.Critical(fmt.Sprintf("The interface is administratively %v",
//quit with Ok return Code, because the action have been made consciously
sknchk.Ok(fmt.Sprintf("The interface is administratively %v",
sknchk.FmtCritical("DOWN")), "")
}
if intNewData.IfOperStatus != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/version3.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func init() {
rootCmd.AddCommand(version3Cmd)

version3Cmd.Flags().StringP("username", "u", "admin", "Username used for SNMP v3 authentication.")
version3Cmd.Flags().StringP("auth-protocol", "a", "SHA", "Authentication protocol (MD5|SHA|SHA-224|SHA-256|SHA-384|SHA-512).")
version3Cmd.Flags().StringP("auth-protocol", "a", "SHA", "Authentication protocol, can be in Upper or Lower case (MD5|SHA|SHA-224|SHA-256|SHA-384|SHA-512).")
version3Cmd.Flags().StringP("auth-passphrase", "A", "passphrase", "Authentication passphrase.")
version3Cmd.Flags().StringP("sec-level", "l", "authPriv", "Security level (noAuthNoPriv|authNoPriv|authPriv).")
version3Cmd.Flags().StringP("context", "n", "", "Context name.")
version3Cmd.Flags().StringP("priv-protocol", "x", "AES", "Privacy protocol (DES|AES).")
version3Cmd.Flags().StringP("priv-protocol", "x", "AES", "Privacy protocol, can be in Upper or Lower case (DES|AES).")
version3Cmd.Flags().StringP("priv-passphrase", "X", "passphrase", "Privacy passphrase.")
}
40 changes: 9 additions & 31 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import (
"fmt"
"strconv"
"strings"
)

//Global value for B, KB, MB, GB and TB
const (
B = 1
KB = B * 1000
MB = KB * 1000
GB = MB * 1000
TB = GB * 1000
)

//HumanReadable convert float64 value to something more readable in K, M, G, T with a 2 points precision
func HumanReadable(value float64, suffix string) string {
unit := ""

switch {
case value >= TB:
unit = "T"
value = value / TB
case value >= GB:
unit = "G"
value = value / GB
case value >= MB:
unit = "M"
value = value / MB
case value >= KB:
unit = "K"
value = value / KB
func HumanReadable(value float64, base float64, suffix string) string {
if value < base {
return fmt.Sprintf("%.0f %v", value, suffix)
}

result := strconv.FormatFloat(value, 'f', 2, 64)
result = strings.TrimSuffix(result, ".0")
return result + " " + unit + suffix
div, exp := base, 0
for n := value / base; n >= base; n /= base {
div *= base
exp++
}
return fmt.Sprintf("%.2f %c%v", float64(value)/float64(div), "kMGTPE"[exp], suffix)
}

//ToUint convert int and uint types to uint
Expand Down
3 changes: 1 addition & 2 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

//CheckPath is the global path where all the files read/write by the check will be hosted
const CheckPath = "/var/tmp/check_snmp_interface_foreach"
const CheckPath = "/var/tmp/go_check_snmp_interface_foreach"

//DevicePath is the path where is will be stored all the JSON files of the device
var DevicePath string
Expand Down Expand Up @@ -99,7 +99,6 @@ func CreateJSONFile(devicePath string, filename string, datas interface{}) error
return fmt.Errorf("Can't create the file : %v", err)
}
return nil

}

//GenDeviceDirName will string the full path used to read/write the interface information
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/golang/mock v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.3.2
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pandaoc-io/go-shinken-check v0.2.3
github.com/pandaoc-io/go-shinken-check v0.3.1
github.com/sirupsen/logrus v1.6.0
github.com/soniah/gosnmp v1.25.0
github.com/spf13/cobra v1.0.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ github.com/pandaoc-io/go-shinken-check v0.2.2 h1:3e+11xHZZbFsgnc6sMZ26QB0bWCLuSx
github.com/pandaoc-io/go-shinken-check v0.2.2/go.mod h1:3Wn8r+Mixw3Hq+GD4wDMUcct6jwJj4IGxMtmSXRWZf0=
github.com/pandaoc-io/go-shinken-check v0.2.3 h1:IEQI/VNRLQ1A8uF5aPytPoLMQp8ZOr0rz7VYuWlBJtc=
github.com/pandaoc-io/go-shinken-check v0.2.3/go.mod h1:3Wn8r+Mixw3Hq+GD4wDMUcct6jwJj4IGxMtmSXRWZf0=
github.com/pandaoc-io/go-shinken-check v0.3.0 h1:sPYi+b4WyTVx1teX03+SekLAYppDmYvREH3gJbWXl48=
github.com/pandaoc-io/go-shinken-check v0.3.0/go.mod h1:3Wn8r+Mixw3Hq+GD4wDMUcct6jwJj4IGxMtmSXRWZf0=
github.com/pandaoc-io/go-shinken-check v0.3.1 h1:Nqg2JylLYBDOddq86tc3hrQ3Tispywc0IbtempGZmBY=
github.com/pandaoc-io/go-shinken-check v0.3.1/go.mod h1:3Wn8r+Mixw3Hq+GD4wDMUcct6jwJj4IGxMtmSXRWZf0=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
46 changes: 27 additions & 19 deletions netint/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func FetchAllDatas(snmpConnection *g.GoSNMP, index string, version string, cmd *
if err != nil {
return nil, err
}
if elem == "IfAlias" {
if elem == "IfAlias" && networkinterface.IfAlias != nil {
log.Debug("Replace the characters of the alias '|' by '!'")
*networkinterface.IfAlias = strings.ReplaceAll(*networkinterface.IfAlias, "|", "!")
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func Bandwidth(intNewData *InterfaceDetails, intOldData *InterfaceDetails, timeD
}

//Force 0% bandwidth usage for interfaces named vlanxxx
if strings.Contains(strings.ToLower(*intNewData.IfName), "vlan") || strings.Contains(strings.ToLower(*intNewData.IfDescr), "vlan") {
if (intNewData.IfName != nil && strings.Contains(strings.ToLower(*intNewData.IfName), "vlan")) || (intNewData.IfDescr != nil && strings.Contains(strings.ToLower(*intNewData.IfDescr), "vlan")) {
intNewData.IfInPrct = nil
intNewData.IfOutPrct = nil
}
Expand All @@ -229,27 +229,27 @@ func Bandwidth(intNewData *InterfaceDetails, intOldData *InterfaceDetails, timeD

if intNewData.IfInPrct != nil && *intNewData.IfInPrct > bc {
chk.AddShort(fmt.Sprintf(`Very high In Bandwidth : %v - %v (> %v%%)`,
convert.HumanReadable(*intNewData.IfInRate, "bits/sec"),
convert.HumanReadable(*intNewData.IfInRate, 1024, "bits/sec"),
sknchk.FmtCritical(fmt.Sprintf("%.2f%%", *intNewData.IfInPrct)), bc),
true)
chk.AddCritical()
} else if intNewData.IfInPrct != nil && *intNewData.IfInPrct > bw {
chk.AddShort(fmt.Sprintf(`High In Bandwidth : %v - %v (> %v%%)`,
convert.HumanReadable(*intNewData.IfInRate, "bits/sec"),
convert.HumanReadable(*intNewData.IfInRate, 1024, "bits/sec"),
sknchk.FmtWarning(fmt.Sprintf("%.2f%%", *intNewData.IfInPrct)), bw),
true)
chk.AddWarning()
}

if intNewData.IfOutPrct != nil && *intNewData.IfOutPrct > bc {
chk.AddShort(fmt.Sprintf(`Very high Out Bandwidth : %v - %v (> %v%%)`,
convert.HumanReadable(*intNewData.IfOutRate, "bits/sec"),
convert.HumanReadable(*intNewData.IfOutRate, 1024, "bits/sec"),
sknchk.FmtCritical(fmt.Sprintf("%.2f%%", *intNewData.IfOutPrct)), bc),
true)
chk.AddCritical()
} else if intNewData.IfOutPrct != nil && *intNewData.IfOutPrct > bw {
chk.AddShort(fmt.Sprintf(`High Out Bandwidth : %v - %v (> %v%%)`,
convert.HumanReadable(*intNewData.IfOutRate, "bits/sec"),
convert.HumanReadable(*intNewData.IfOutRate, 1024, "bits/sec"),
sknchk.FmtWarning(fmt.Sprintf("%.2f%%", *intNewData.IfOutPrct)), bw),
true)
chk.AddWarning()
Expand Down Expand Up @@ -544,17 +544,17 @@ func Errors(intNewData *InterfaceDetails, intOldData *InterfaceDetails, timeDiff
chk.AddPerfData("in_errors", strconv.FormatFloat(*intNewData.IfInErrorsRate, 'f', 2, 64), "pps", 0, 0, 0, 0)
}

if intNewData.IfInErrorsPrct != nil {
/* if intNewData.IfInErrorsPrct != nil {
chk.AddPerfData("in_errors_prct", strconv.FormatFloat(*intNewData.IfInErrorsPrct, 'f', 2, 64), "%", 0, 0, 0, 0)
}
} */

if intNewData.IfOutErrorsRate != nil {
chk.AddPerfData("out_errors", strconv.FormatFloat(*intNewData.IfOutErrorsRate, 'f', 2, 64), "pps", 0, 0, 0, 0)
}

if intNewData.IfOutErrorsPrct != nil {
/* if intNewData.IfOutErrorsPrct != nil {
chk.AddPerfData("out_errors_prct", strconv.FormatFloat(*intNewData.IfOutErrorsPrct, 'f', 2, 64), "%", 0, 0, 0, 0)
}
} */

//Error thresholds
if ev == "pps" {
Expand Down Expand Up @@ -640,17 +640,17 @@ func Discards(intNewData *InterfaceDetails, intOldData *InterfaceDetails, timeDi
chk.AddPerfData("in_discards", strconv.FormatFloat(*intNewData.IfInDiscardsRate, 'f', 2, 64), "pps", 0, 0, 0, 0)
}

if intNewData.IfInDiscardsPrct != nil {
/* if intNewData.IfInDiscardsPrct != nil {
chk.AddPerfData("in_discards_prct", strconv.FormatFloat(*intNewData.IfInDiscardsPrct, 'f', 2, 64), "%", 0, 0, 0, 0)
}
} */

if intNewData.IfOutDiscardsRate != nil {
chk.AddPerfData("out_discards", strconv.FormatFloat(*intNewData.IfOutDiscardsRate, 'f', 2, 64), "pps", 0, 0, 0, 0)
}

if intNewData.IfOutDiscardsPrct != nil {
/* if intNewData.IfOutDiscardsPrct != nil {
chk.AddPerfData("out_discards_prct", strconv.FormatFloat(*intNewData.IfOutDiscardsPrct, 'f', 2, 64), "%", 0, 0, 0, 0)
}
} */

if dv == "pps" {
if intNewData.IfInDiscardsRate != nil && *intNewData.IfInDiscardsRate > dc {
Expand Down Expand Up @@ -715,7 +715,15 @@ func Speed(intNewData *InterfaceDetails, chk *sknchk.Check) {
var speed uint
if intNewData.IfHighSpeed != nil {
log.Debug("ifHighSpeed found")
speed = *intNewData.IfHighSpeed * 1000000
if *intNewData.IfHighSpeed == 1 {
log.Debug("ifHighSpeed is equal to 1Mb, we check if the ifSpeed is lower than 1Mb.")
if intNewData.IfSpeed != nil && *intNewData.IfSpeed < 1000000 {
log.Debugf("ifSpeed value is inferior to 1Mb, keep ifSpeed as speed value : %v bps", *intNewData.IfSpeed)
speed = *intNewData.IfSpeed
}
} else {
speed = *intNewData.IfHighSpeed * 1000000
}
} else if intNewData.IfSpeed != nil {
log.Debug("No ifHighSpeed found, switch to ifSpeed")
if *intNewData.IfSpeed == math.MaxUint32 {
Expand Down Expand Up @@ -797,7 +805,7 @@ func bwStats(newData interface{}, oldData interface{}, speed interface{}, elapse
rate = diffConverted * 8 / elapseTime.Seconds()
}

log.Debugf("Rate : %v\n", convert.HumanReadable(rate, "bits/sec"))
log.Debugf("Rate : %v\n", convert.HumanReadable(rate, 1024, "bits/sec"))

speedConverted, err := convert.ToFloat(speed)
if err != nil {
Expand All @@ -807,7 +815,7 @@ func bwStats(newData interface{}, oldData interface{}, speed interface{}, elapse
if speedConverted > 0 {
prct = (rate / speedConverted) * 100
}
log.Debugf("Max value : %v\n", convert.HumanReadable(speedConverted, "bps"))
log.Debugf("Max value : %v\n", convert.HumanReadable(speedConverted, 1000, "bps"))
log.Debugf("Percent : %.2f %%\n", prct)

//Now we test is the values are relevant
Expand All @@ -823,7 +831,7 @@ func bwStats(newData interface{}, oldData interface{}, speed interface{}, elapse
BwInconsistency = true
rate = 0
prct = 0
log.Debugf("New Rate : %v\n", convert.HumanReadable(rate, "bits/sec"))
log.Debugf("New Rate : %v\n", convert.HumanReadable(rate, 1024, "bits/sec"))
log.Debugf("New Percent : %.2f %%\n", prct)
}
//For some link like bond we don't have any speed, generaly set to 0.
Expand All @@ -837,7 +845,7 @@ func bwStats(newData interface{}, oldData interface{}, speed interface{}, elapse
BwInconsistency = true
rate = 0
prct = 0
log.Debugf("New Rate : %.2f\n", convert.HumanReadable(rate, "bits/sec"))
log.Debugf("New Rate : %.2f\n", convert.HumanReadable(rate, 1024, "bits/sec"))
log.Debugf("New Percent : %.2f %%\n", prct)
}

Expand Down
8 changes: 4 additions & 4 deletions snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ func CreateConnection(version string, cmd *cobra.Command) (*g.GoSNMP, error) {
var secLevel g.SnmpV3MsgFlags = g.AuthPriv

switch cmd.Flag("auth-protocol").Value.String() {
case "MD5":
case "MD5", "md5":
authProto = g.MD5
case "SHA":
case "SHA", "sha":
authProto = g.SHA
default:
return nil, fmt.Errorf("%v is not a valid authentication protocol, check usage", cmd.Flag("auth-protocol").Value.String())
}

switch cmd.Flag("priv-protocol").Value.String() {
case "DES":
case "DES", "des":
privProto = g.DES
case "AES":
case "AES", "aes":
privProto = g.AES
default:
return nil, fmt.Errorf("%v is not a valid privacy protocol, check usage", cmd.Flag("priv-protocol").Value.String())
Expand Down
76 changes: 57 additions & 19 deletions ui/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func CliSummary(intNewData *netint.InterfaceDetails, chk *sknchk.Check) {
if intNewData.IfAlias != nil {
chk.AddShort(fmt.Sprintf("Alias : %v", *intNewData.IfAlias), true)
}
chk.AddShort(fmt.Sprintf("Speed : %v", convert.HumanReadable(float64(*intNewData.SpeedInbit), "bps")), true)
chk.AddShort(fmt.Sprintf("Speed : %v", convert.HumanReadable(float64(*intNewData.SpeedInbit), 1000, "bps")), true)
if intNewData.IfOperStatus != nil {
if *intNewData.IfOperStatus == netint.UP {
chk.AddShort("Oper Status : UP", true)
Expand All @@ -49,34 +49,72 @@ func CliSummary(intNewData *netint.InterfaceDetails, chk *sknchk.Check) {
chk.AddShort(fmt.Sprintf("Total pkts In : %.2f pps", *intNewData.IfInTotalPktsRate), true)
chk.AddShort(fmt.Sprintf("Total pkts Out : %.2f pps", *intNewData.IfOutTotalPktsRate), true)

if intNewData.InUniPcktRate != nil || intNewData.InMultiPcktRate != nil {
if intNewData.InBroadPcktRate != nil {
chk.AddShort(fmt.Sprintf("In : Uni : %.2f pps, Multi : %.2f pps, Broad :%.2f pps", *intNewData.InUniPcktRate, *intNewData.InMultiPcktRate, *intNewData.InBroadPcktRate), true)
} else {
chk.AddShort(fmt.Sprintf("In : Uni : %.2f pps, Multi/Broad : %.2f pps", *intNewData.InUniPcktRate, *intNewData.InMultiPcktRate), true)
}
if intNewData.InUniPcktRate != nil {
chk.AddShort(fmt.Sprintf("In Uni : %.2f pps", *intNewData.InUniPcktRate), true)
} else {
chk.AddShort("In Uni/Multi/Broadcast packets : Can't be determined", true)
chk.AddShort("In Uni : Can't be determined", true)
}
if intNewData.OutUniPcktRate != nil || intNewData.OutMultiPcktRate != nil {
if intNewData.OutBroadPcktRate != nil {
chk.AddShort(fmt.Sprintf("Out : Uni : %.2f pps, Multi : %.2f pps, Broad :%.2f pps", *intNewData.OutUniPcktRate, *intNewData.OutMultiPcktRate, *intNewData.OutBroadPcktRate), true)
} else {
chk.AddShort(fmt.Sprintf("Out : Uni : %.2f pps, Multi/Broad : %.2f pps", *intNewData.OutUniPcktRate, *intNewData.OutMultiPcktRate), true)
}

if intNewData.InMultiPcktRate != nil {
chk.AddShort(fmt.Sprintf("In : Multi : %.2f pps", *intNewData.InMultiPcktRate), true)
} else {
chk.AddShort("In Multi : Can't be determined", true)
}

if intNewData.InBroadPcktRate != nil {
chk.AddShort(fmt.Sprintf("In Broad : %.2f pps", *intNewData.InBroadPcktRate), true)
} else {
chk.AddShort("In Broad : Can't be determined", true)
}

if intNewData.OutUniPcktRate != nil {
chk.AddShort(fmt.Sprintf("Out Uni : %.2f pps", *intNewData.OutUniPcktRate), true)
} else {
chk.AddShort("Out Uni : Can't be determined", true)
}

if intNewData.OutMultiPcktRate != nil {
chk.AddShort(fmt.Sprintf("Out Multi : %.2f pps", *intNewData.OutMultiPcktRate), true)
} else {
chk.AddShort("Out Multi : Can't be determined", true)
}

if intNewData.OutBroadPcktRate != nil {
chk.AddShort(fmt.Sprintf("Out Broad : %.2f pps", *intNewData.OutBroadPcktRate), true)
} else {
chk.AddShort("Out Uni/Multi/Broadcast packets : Can't be determined", true)
chk.AddShort("Out Broad : Can't be determined", true)
}

var inRateStr string
if intNewData.IfInRate != nil {
chk.AddShort(fmt.Sprintf("In BW : %v (%.2f%%)", convert.HumanReadable(*intNewData.IfInRate, "bits/sec"), *intNewData.IfInPrct), true)
inRateStr = fmt.Sprintf("In BW : %v", convert.HumanReadable(*intNewData.IfInRate, 1024, "bits/sec"))
} else {
inRateStr = "In BW : Rate can't be determined"
}
var inPrctStr string
if intNewData.IfInPrct != nil {
inPrctStr = fmt.Sprintf(" (%.2f%%)", *intNewData.IfInPrct)
} else {
chk.AddShort("In BW : Can't be determined", true)
inPrctStr = ", Percentage can't be determined"
}

chk.AddShort(inRateStr+inPrctStr, true)

var outRateStr string
if intNewData.IfOutRate != nil {
chk.AddShort(fmt.Sprintf("Out BW : %v (%.2f%%)", convert.HumanReadable(*intNewData.IfOutRate, "bits/sec"), *intNewData.IfOutPrct), true)
outRateStr = fmt.Sprintf("Out BW : %v", convert.HumanReadable(*intNewData.IfOutRate, 1024, "bits/sec"))
} else {
outRateStr = "Out BW : Rate can't be determined"
}
var outPrctStr string
if intNewData.IfOutPrct != nil {
outPrctStr = fmt.Sprintf(" (%.2f%%)", *intNewData.IfOutPrct)
} else {
chk.AddShort("Out BW : Can't be determined", true)
outPrctStr = ", Percentage can't be determined"
}

chk.AddShort(outRateStr+outPrctStr, true)

if intNewData.IfInErrorsRate != nil {
if intNewData.LocIfInCRCRate != nil {
chk.AddShort(fmt.Sprintf("In Errors : %.2f pps (%.2f%%) with %.2f pps CRC Errors", *intNewData.IfInErrorsRate, *intNewData.IfInErrorsPrct, *intNewData.LocIfInCRCRate), true)
Expand Down
Loading

0 comments on commit d65a237

Please sign in to comment.