Skip to content

Commit

Permalink
Merge pull request #87 from inexio/pre-release
Browse files Browse the repository at this point in the history
normalized options of read and check interfaces
  • Loading branch information
TheFireMike authored Oct 21, 2021
2 parents e370fbe + 95c211b commit 3cdf79a
Show file tree
Hide file tree
Showing 8 changed files with 448 additions and 149 deletions.
44 changes: 4 additions & 40 deletions cmd/check_interface_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ package cmd

import (
"github.com/inexio/thola/internal/request"
"github.com/inexio/thola/internal/utility"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkInterfaceMetricsCMD)
addInterfaceOptionsFlags(checkInterfaceMetricsCMD)
checkCMD.AddCommand(checkInterfaceMetricsCMD)

checkInterfaceMetricsCMD.Flags().Bool("print-interfaces", false, "Print interfaces to plugin output")
checkInterfaceMetricsCMD.Flags().Bool("snmp-gets-instead-of-walk", false, "Use SNMP Gets instead of Walks")
checkInterfaceMetricsCMD.Flags().String("ifDescr-regex", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex-replace' flag")
checkInterfaceMetricsCMD.Flags().String("ifDescr-regex-replace", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex' flag")
checkInterfaceMetricsCMD.Flags().StringSlice("ifType-filter", []string{}, "Filter out interfaces which ifType equals the given types")
checkInterfaceMetricsCMD.Flags().StringSlice("ifName-filter", []string{}, "Filter out interfaces which ifName matches the given regex")
checkInterfaceMetricsCMD.Flags().StringSlice("ifDescr-filter", []string{}, "Filter out interfaces which ifDescription matches the given regex")
}

var checkInterfaceMetricsCMD = &cobra.Command{
Expand All @@ -29,41 +23,11 @@ var checkInterfaceMetricsCMD = &cobra.Command{
if err != nil {
log.Fatal().Err(err).Msg("print-interfaces needs to be a boolean")
}
snmpGetsInsteadOfWalk, err := cmd.Flags().GetBool("snmp-gets-instead-of-walk")
if err != nil {
log.Fatal().Err(err).Msg("snmp-gets-instead-of-walk needs to be a boolean")
}
ifDescrRegex, err := cmd.Flags().GetString("ifDescr-regex")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-regex needs to be a string")
}
ifDescrRegexReplace, err := cmd.Flags().GetString("ifDescr-regex-replace")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-regex-replace needs to be a string")
}
ifTypeFilter, err := cmd.Flags().GetStringSlice("ifType-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifType-filter needs to be a string")
}
ifNameFilter, err := cmd.Flags().GetStringSlice("ifName-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifName-filter needs to be a string")
}
ifDescrFilter, err := cmd.Flags().GetStringSlice("ifDescr-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-filter needs to be a string")
}

var nullString *string
r := request.CheckInterfaceMetricsRequest{
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
PrintInterfaces: printInterfaces,
IfDescrRegex: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex"), &ifDescrRegex, nullString).(*string),
IfDescrRegexReplace: utility.IfThenElse(cmd.Flags().Changed("ifDescr-regex-replace"), &ifDescrRegexReplace, nullString).(*string),
IfTypeFilter: ifTypeFilter,
IfNameFilter: ifNameFilter,
IfDescrFilter: ifDescrFilter,
SNMPGetsInsteadOfWalk: snmpGetsInsteadOfWalk,
PrintInterfaces: printInterfaces,
InterfaceOptions: getInterfaceOptions(),
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
}

handleRequest(&r)
Expand Down
69 changes: 69 additions & 0 deletions cmd/interfaceOptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cmd

import (
"github.com/inexio/thola/internal/request"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)

var interfaceOptionsFlagSet = buildInterfaceOptionsFlagSet()

func buildInterfaceOptionsFlagSet() *flag.FlagSet {
fs := flag.NewFlagSet("interface_options", flag.ContinueOnError)

fs.StringSlice("value", []string{}, "If set only the specified values will be read from the interfaces (e.g. 'ifDescr')")
fs.Bool("snmp-gets-instead-of-walk", false, "Use SNMP Gets instead of Walks")
fs.String("ifDescr-regex", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex-replace' flag")
fs.String("ifDescr-regex-replace", "", "Apply a regex on the ifDescr of the interfaces. Use it together with the 'ifDescr-regex' flag")
fs.StringSlice("ifType-filter", []string{}, "Filter out interfaces which ifType equals the given types")
fs.StringSlice("ifName-filter", []string{}, "Filter out interfaces which ifName matches the given regex")
fs.StringSlice("ifDescr-filter", []string{}, "Filter out interfaces which ifDescription matches the given regex")

return fs
}

func addInterfaceOptionsFlags(cmd *cobra.Command) {
cmd.Flags().AddFlagSet(interfaceOptionsFlagSet)
}

func getInterfaceOptions() request.InterfaceOptions {
values, err := interfaceOptionsFlagSet.GetStringSlice("value")
if err != nil {
log.Fatal().Err(err).Msg("value needs to be a string")
}
snmpGetsInsteadOfWalk, err := interfaceOptionsFlagSet.GetBool("snmp-gets-instead-of-walk")
if err != nil {
log.Fatal().Err(err).Msg("snmp-gets-instead-of-walk needs to be a boolean")
}
ifDescrRegex, err := interfaceOptionsFlagSet.GetString("ifDescr-regex")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-regex needs to be a string")
}
ifDescrRegexReplace, err := interfaceOptionsFlagSet.GetString("ifDescr-regex-replace")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-regex-replace needs to be a string")
}
ifTypeFilter, err := interfaceOptionsFlagSet.GetStringSlice("ifType-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifType-filter needs to be a string")
}
ifNameFilter, err := interfaceOptionsFlagSet.GetStringSlice("ifName-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifName-filter needs to be a string")
}
ifDescrFilter, err := interfaceOptionsFlagSet.GetStringSlice("ifDescr-filter")
if err != nil {
log.Fatal().Err(err).Msg("ifDescr-filter needs to be a string")
}

return request.InterfaceOptions{
Values: values,
IfDescrRegex: ifDescrRegex,
IfDescrRegexReplace: ifDescrRegexReplace,
IfTypeFilter: ifTypeFilter,
IfNameFilter: ifNameFilter,
IfDescrFilter: ifDescrFilter,
SNMPGetsInsteadOfWalk: snmpGetsInsteadOfWalk,
}
}
13 changes: 3 additions & 10 deletions cmd/read_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package cmd

import (
"github.com/inexio/thola/internal/request"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(readInterfacesCMD)
addInterfaceOptionsFlags(readInterfacesCMD)
readCMD.AddCommand(readInterfacesCMD)

readInterfacesCMD.Flags().StringSlice("value", []string{}, "Only read out these values of the interfaces (e.g. 'ifDescr')")
}

var readInterfacesCMD = &cobra.Command{
Expand All @@ -19,14 +17,9 @@ var readInterfacesCMD = &cobra.Command{
Long: "Read out interface information of a device.\n\n" +
"Also reads special values based on the interface type.",
Run: func(cmd *cobra.Command, args []string) {
values, err := cmd.Flags().GetStringSlice("value")
if err != nil {
log.Fatal().Err(err).Msg("value needs to be a string")
}

request := request.ReadInterfacesRequest{
Values: values,
ReadRequest: getReadRequest(args[0]),
InterfaceOptions: getInterfaceOptions(),
ReadRequest: getReadRequest(args[0]),
}
handleRequest(&request)
},
Expand Down
Loading

0 comments on commit 3cdf79a

Please sign in to comment.