From 95c211bb9aa5e5549ebb437e8258f1a6f8ac412c Mon Sep 17 00:00:00 2001 From: mikameyer Date: Thu, 21 Oct 2021 14:28:44 +0200 Subject: [PATCH] normalized options of read and check interfaces --- cmd/check_interface_metrics.go | 44 +-- cmd/interfaceOptions.go | 69 ++++ cmd/read_interfaces.go | 13 +- doc/api_doc.json | 341 +++++++++++++++--- .../check_interface_metrics_request.go | 26 +- ...check_interface_metrics_request_process.go | 18 +- internal/request/read_interfaces_request.go | 73 +++- .../read_interfaces_request_process.go | 13 +- 8 files changed, 448 insertions(+), 149 deletions(-) create mode 100644 cmd/interfaceOptions.go diff --git a/cmd/check_interface_metrics.go b/cmd/check_interface_metrics.go index 3eb5e82..3f245f4 100644 --- a/cmd/check_interface_metrics.go +++ b/cmd/check_interface_metrics.go @@ -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{ @@ -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) diff --git a/cmd/interfaceOptions.go b/cmd/interfaceOptions.go new file mode 100644 index 0000000..77c23e0 --- /dev/null +++ b/cmd/interfaceOptions.go @@ -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, + } +} diff --git a/cmd/read_interfaces.go b/cmd/read_interfaces.go index b46f74a..5f9cf2d 100644 --- a/cmd/read_interfaces.go +++ b/cmd/read_interfaces.go @@ -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{ @@ -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) }, diff --git a/doc/api_doc.json b/doc/api_doc.json index fac22b6..3064b9f 100644 --- a/doc/api_doc.json +++ b/doc/api_doc.json @@ -971,26 +971,34 @@ "title": "BaseResponse", "x-go-package": "github.com/inexio/thola/internal/request" }, + "CPU": { + "description": "CPU contains information per CPU.", + "type": "object", + "title": "CPU", + "properties": { + "label": { + "type": "string", + "x-go-name": "Label" + }, + "load": { + "type": "number", + "format": "double", + "x-go-name": "Load" + } + }, + "x-go-package": "github.com/inexio/thola/internal/device" + }, "CPUComponent": { "description": "CPUComponent represents a CPU component.", "type": "object", "title": "CPUComponent", "properties": { - "load": { + "cpus": { "type": "array", "items": { - "type": "number", - "format": "double" + "$ref": "#/definitions/CPU" }, - "x-go-name": "Load" - }, - "temperature": { - "type": "array", - "items": { - "type": "number", - "format": "double" - }, - "x-go-name": "Temperature" + "x-go-name": "CPUs" } }, "x-go-package": "github.com/inexio/thola/internal/device" @@ -1024,7 +1032,7 @@ "x-go-package": "github.com/inexio/thola/internal/request" }, "CheckDeviceRequest": { - "description": "CheckDeviceRequest is a the request struct for the check device request.", + "description": "CheckDeviceRequest is the request struct for the check device request.", "type": "object", "title": "CheckDeviceRequest", "properties": { @@ -1202,6 +1210,21 @@ "device_data": { "$ref": "#/definitions/DeviceData" }, + "ifDescr_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfDescrFilter" + }, + "ifDescr_regex": { + "type": "string", + "x-go-name": "IfDescrRegex" + }, + "ifDescr_regex_replace": { + "type": "string", + "x-go-name": "IfDescrRegexReplace" + }, "ifName_filter": { "type": "array", "items": { @@ -1228,11 +1251,23 @@ "type": "boolean", "x-go-name": "PrintPerformanceData" }, + "snmp_gets_instead_of_walk": { + "type": "boolean", + "x-go-name": "SNMPGetsInsteadOfWalk" + }, "timeout": { "description": "Timeout for the request (0 =\u003e no timeout)", "type": "integer", "format": "int64", "x-go-name": "Timeout" + }, + "values": { + "description": "If you only want specific values of the interfaces you can specify them here.", + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "Values" } }, "x-go-package": "github.com/inexio/thola/internal/request" @@ -1624,11 +1659,31 @@ "type": "object", "title": "EthernetLikeInterface", "properties": { + "dot3HCStatsAlignmentErrors": { + "type": "integer", + "format": "uint64", + "x-go-name": "Dot3HCStatsAlignmentErrors" + }, "dot3HCStatsFCSErrors": { "type": "integer", "format": "uint64", "x-go-name": "Dot3HCStatsFCSErrors" }, + "dot3HCStatsFrameTooLongs": { + "type": "integer", + "format": "uint64", + "x-go-name": "Dot3HCStatsFrameTooLongs" + }, + "dot3HCStatsInternalMacReceiveErrors": { + "type": "integer", + "format": "uint64", + "x-go-name": "Dot3HCStatsInternalMacReceiveErrors" + }, + "dot3HCStatsInternalMacTransmitErrors": { + "type": "integer", + "format": "uint64", + "x-go-name": "Dot3HCStatsInternalMacTransmitErrors" + }, "dot3StatsAlignmentErrors": { "type": "integer", "format": "uint64", @@ -1698,7 +1753,7 @@ "x-go-package": "github.com/inexio/thola/internal/device" }, "HTTPConnectionData": { - "description": "HTTPConnectionData includes all http connection data for a device.", + "description": "HTTPConnectionData includes all HTTP connection data for a device.", "type": "object", "title": "HTTPConnectionData", "properties": { @@ -1715,7 +1770,7 @@ "example": "username" }, "http_ports": { - "description": "The http port(s) of the device.", + "description": "The HTTP port(s) of the device.", "type": "array", "items": { "type": "integer", @@ -1727,7 +1782,7 @@ ] }, "https_ports": { - "description": "The https port(s) of the device.", + "description": "The HTTPS port(s) of the device.", "type": "array", "items": { "type": "integer", @@ -1747,9 +1802,7 @@ "title": "HardwareHealthComponent", "properties": { "environment_monitor_state": { - "type": "integer", - "format": "int64", - "x-go-name": "EnvironmentMonitorState" + "$ref": "#/definitions/HardwareHealthComponentState" }, "fans": { "type": "array", @@ -1764,6 +1817,20 @@ "$ref": "#/definitions/HardwareHealthComponentPowerSupply" }, "x-go-name": "PowerSupply" + }, + "temperature": { + "type": "array", + "items": { + "$ref": "#/definitions/HardwareHealthComponentTemperature" + }, + "x-go-name": "Temperature" + }, + "voltage": { + "type": "array", + "items": { + "$ref": "#/definitions/HardwareHealthComponentVoltage" + }, + "x-go-name": "Voltage" } }, "x-go-package": "github.com/inexio/thola/internal/device" @@ -1778,9 +1845,7 @@ "x-go-name": "Description" }, "state": { - "type": "integer", - "format": "int64", - "x-go-name": "State" + "$ref": "#/definitions/HardwareHealthComponentState" } }, "x-go-package": "github.com/inexio/thola/internal/device" @@ -1795,9 +1860,51 @@ "x-go-name": "Description" }, "state": { - "type": "integer", - "format": "int64", - "x-go-name": "State" + "$ref": "#/definitions/HardwareHealthComponentState" + } + }, + "x-go-package": "github.com/inexio/thola/internal/device" + }, + "HardwareHealthComponentState": { + "type": "string", + "x-go-package": "github.com/inexio/thola/internal/device" + }, + "HardwareHealthComponentTemperature": { + "description": "HardwareHealthComponentTemperature represents one fan of a device.", + "type": "object", + "title": "HardwareHealthComponentTemperature", + "properties": { + "description": { + "type": "string", + "x-go-name": "Description" + }, + "state": { + "$ref": "#/definitions/HardwareHealthComponentState" + }, + "temperature": { + "type": "number", + "format": "double", + "x-go-name": "Temperature" + } + }, + "x-go-package": "github.com/inexio/thola/internal/device" + }, + "HardwareHealthComponentVoltage": { + "description": "HardwareHealthComponentVoltage represents the voltage of a device.", + "type": "object", + "title": "HardwareHealthComponentVoltage", + "properties": { + "description": { + "type": "string", + "x-go-name": "Description" + }, + "state": { + "$ref": "#/definitions/HardwareHealthComponentState" + }, + "voltage": { + "type": "number", + "format": "double", + "x-go-name": "Voltage" } }, "x-go-package": "github.com/inexio/thola/internal/device" @@ -2069,6 +2176,87 @@ }, "x-go-package": "github.com/inexio/thola/internal/device" }, + "InterfaceOptions": { + "description": "InterfaceOptions is the request struct for the options of an interface request.", + "type": "object", + "title": "InterfaceOptions", + "properties": { + "ifDescr_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfDescrFilter" + }, + "ifDescr_regex": { + "type": "string", + "x-go-name": "IfDescrRegex" + }, + "ifDescr_regex_replace": { + "type": "string", + "x-go-name": "IfDescrRegexReplace" + }, + "ifName_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfNameFilter" + }, + "ifType_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfTypeFilter" + }, + "snmp_gets_instead_of_walk": { + "type": "boolean", + "x-go-name": "SNMPGetsInsteadOfWalk" + }, + "values": { + "description": "If you only want specific values of the interfaces you can specify them here.", + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "Values" + } + }, + "x-go-package": "github.com/inexio/thola/internal/request" + }, + "MemoryComponent": { + "description": "MemoryComponent represents a Memory component", + "type": "object", + "title": "MemoryComponent", + "properties": { + "pools": { + "type": "array", + "items": { + "$ref": "#/definitions/MemoryPool" + }, + "x-go-name": "Pools" + } + }, + "x-go-package": "github.com/inexio/thola/internal/device" + }, + "MemoryPool": { + "description": "MemoryPool contains information per memory pool.", + "type": "object", + "title": "MemoryPool", + "properties": { + "label": { + "type": "string", + "x-go-name": "Label" + }, + "usage": { + "type": "number", + "format": "double", + "x-go-name": "Usage" + } + }, + "x-go-package": "github.com/inexio/thola/internal/device" + }, "OpticalAmplifierInterface": { "description": "OpticalAmplifierInterface represents an optical amplifier interface.", "type": "object", @@ -2364,7 +2552,7 @@ "x-go-package": "github.com/inexio/thola/internal/request" }, "ReadCPULoadRequest": { - "description": "ReadCPULoadRequest is a the request struct for the read cpu request.", + "description": "ReadCPULoadRequest is the request struct for the read cpu request.", "type": "object", "title": "ReadCPULoadRequest", "properties": { @@ -2381,17 +2569,16 @@ "x-go-package": "github.com/inexio/thola/internal/request" }, "ReadCPULoadResponse": { - "description": "ReadCPULoadResponse is a the response struct for the read cpu response.", + "description": "ReadCPULoadResponse is the response struct for the read cpu response.", "type": "object", "title": "ReadCPULoadResponse", "properties": { - "cpu_load": { + "cpus": { "type": "array", "items": { - "type": "number", - "format": "double" + "$ref": "#/definitions/CPU" }, - "x-go-name": "CPULoad" + "x-go-name": "CPUs" } }, "x-go-package": "github.com/inexio/thola/internal/request" @@ -2477,9 +2664,7 @@ "title": "ReadHardwareHealthResponse", "properties": { "environment_monitor_state": { - "type": "integer", - "format": "int64", - "x-go-name": "EnvironmentMonitorState" + "$ref": "#/definitions/HardwareHealthComponentState" }, "fans": { "type": "array", @@ -2494,29 +2679,84 @@ "$ref": "#/definitions/HardwareHealthComponentPowerSupply" }, "x-go-name": "PowerSupply" + }, + "temperature": { + "type": "array", + "items": { + "$ref": "#/definitions/HardwareHealthComponentTemperature" + }, + "x-go-name": "Temperature" + }, + "voltage": { + "type": "array", + "items": { + "$ref": "#/definitions/HardwareHealthComponentVoltage" + }, + "x-go-name": "Voltage" } }, "x-go-package": "github.com/inexio/thola/internal/request" }, "ReadInterfacesRequest": { - "description": "ReadInterfacesRequest is a the request struct for the read interfaces request.", + "description": "ReadInterfacesRequest is the request struct for the read interfaces request.", "type": "object", "title": "ReadInterfacesRequest", "properties": { "device_data": { "$ref": "#/definitions/DeviceData" }, + "ifDescr_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfDescrFilter" + }, + "ifDescr_regex": { + "type": "string", + "x-go-name": "IfDescrRegex" + }, + "ifDescr_regex_replace": { + "type": "string", + "x-go-name": "IfDescrRegexReplace" + }, + "ifName_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfNameFilter" + }, + "ifType_filter": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "IfTypeFilter" + }, + "snmp_gets_instead_of_walk": { + "type": "boolean", + "x-go-name": "SNMPGetsInsteadOfWalk" + }, "timeout": { "description": "Timeout for the request (0 =\u003e no timeout)", "type": "integer", "format": "int64", "x-go-name": "Timeout" + }, + "values": { + "description": "If you only want specific values of the interfaces you can specify them here.", + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "Values" } }, "x-go-package": "github.com/inexio/thola/internal/request" }, "ReadInterfacesResponse": { - "description": "ReadInterfacesResponse is a the request struct for the read interfaces response.", + "description": "ReadInterfacesResponse is the request struct for the read interfaces response.", "type": "object", "title": "ReadInterfacesResponse", "properties": { @@ -2552,10 +2792,12 @@ "type": "object", "title": "ReadMemoryUsageResponse", "properties": { - "memory_usage": { - "type": "number", - "format": "double", - "x-go-name": "MemoryUsage" + "memory_pools": { + "type": "array", + "items": { + "$ref": "#/definitions/MemoryPool" + }, + "x-go-name": "MemoryPools" } }, "x-go-package": "github.com/inexio/thola/internal/request" @@ -2862,12 +3104,12 @@ "x-go-package": "github.com/inexio/thola/internal/device" }, "SNMPConnectionData": { - "description": "SNMPConnectionData includes all snmp connection data for a device.", + "description": "SNMPConnectionData includes all SNMP connection information for a device.", "type": "object", "title": "SNMPConnectionData", "properties": { "communities": { - "description": "The snmp community string(s) for the device.", + "description": "The SNMP community string(s) for the device.", "type": "array", "items": { "type": "string" @@ -2898,8 +3140,15 @@ "x-go-name": "DiscoverTimeout", "example": 2 }, + "maxRepetitions": { + "description": "The Max Repetitions of the SNMP connection. Overrides the device class settings if set.", + "type": "integer", + "format": "uint32", + "x-go-name": "MaxRepetitions", + "example": 20 + }, "ports": { - "description": "The snmp port(s) of the device.", + "description": "The SNMP port(s) of the device.", "type": "array", "items": { "type": "integer", @@ -2914,7 +3163,7 @@ "$ref": "#/definitions/SNMPv3ConnectionData" }, "versions": { - "description": "The snmp version(s) of the device.", + "description": "The SNMP version(s) of the device.", "type": "array", "items": { "type": "string" @@ -2929,7 +3178,7 @@ }, "SNMPCredentials": { "type": "object", - "title": "SNMPCredentials includes all credential information of the snmp connection.", + "title": "SNMPCredentials includes all credential information of the SNMP connection.", "properties": { "community": { "type": "string", @@ -2956,7 +3205,7 @@ "x-go-package": "github.com/inexio/thola/internal/network" }, "SNMPv3ConnectionData": { - "description": "SNMPv3ConnectionData includes all snmp v3 specific connection data.", + "description": "SNMPv3ConnectionData includes all SNMP v3 specific connection data.", "type": "object", "title": "SNMPv3ConnectionData", "properties": { diff --git a/internal/request/check_interface_metrics_request.go b/internal/request/check_interface_metrics_request.go index 8ddcf1d..5698d47 100644 --- a/internal/request/check_interface_metrics_request.go +++ b/internal/request/check_interface_metrics_request.go @@ -2,8 +2,6 @@ package request import ( "context" - "github.com/pkg/errors" - "regexp" ) // CheckInterfaceMetricsRequest @@ -12,30 +10,14 @@ import ( // // swagger:model type CheckInterfaceMetricsRequest struct { - PrintInterfaces bool `yaml:"print_interfaces" json:"print_interfaces" xml:"print_interfaces"` - IfDescrRegex *string `yaml:"ifDescr_regex" json:"ifDescr_regex" xml:"ifDescr_regex"` - ifDescrRegex *regexp.Regexp - IfDescrRegexReplace *string `yaml:"ifDescr_regex_replace" json:"ifDescr_regex_replace" xml:"ifDescr_regex_replace"` - IfTypeFilter []string `yaml:"ifType_filter" json:"ifType_filter" xml:"ifType_filter"` - IfNameFilter []string `yaml:"ifName_filter" json:"ifName_filter" xml:"ifName_filter"` - IfDescrFilter []string `yaml:"ifDescr_filter" json:"ifDescr_filter" xml:"ifDescr_filter"` - SNMPGetsInsteadOfWalk bool `yaml:"snmp_gets_instead_of_walk" json:"snmp_gets_instead_of_walk" xml:"snmp_gets_instead_of_walk"` + PrintInterfaces bool `yaml:"print_interfaces" json:"print_interfaces" xml:"print_interfaces"` + InterfaceOptions CheckDeviceRequest } func (r *CheckInterfaceMetricsRequest) validate(ctx context.Context) error { - if r.IfDescrRegex != nil && r.IfDescrRegexReplace == nil || - r.IfDescrRegex == nil && r.IfDescrRegexReplace != nil { - return errors.New("'ifDescr-regex' and 'ifDescr-regex-replace' must be set together") + if err := r.InterfaceOptions.validate(); err != nil { + return err } - - if r.IfDescrRegex != nil { - regex, err := regexp.Compile(*r.IfDescrRegex) - if err != nil { - return errors.Wrap(err, "compiling ifDescrRegex failed") - } - r.ifDescrRegex = regex - } - return r.CheckDeviceRequest.validate(ctx) } diff --git a/internal/request/check_interface_metrics_request_process.go b/internal/request/check_interface_metrics_request_process.go index b08216d..8e60c7a 100644 --- a/internal/request/check_interface_metrics_request_process.go +++ b/internal/request/check_interface_metrics_request_process.go @@ -104,21 +104,7 @@ func (r *CheckInterfaceMetricsRequest) process(ctx context.Context) (Response, e } func (r *CheckInterfaceMetricsRequest) getFilter() []groupproperty.Filter { - var res []groupproperty.Filter - - for _, f := range r.IfTypeFilter { - res = append(res, groupproperty.GetGroupFilter([]string{"ifType"}, f)) - } - for _, f := range r.IfNameFilter { - res = append(res, groupproperty.GetGroupFilter([]string{"ifName"}, f)) - } - for _, f := range r.IfDescrFilter { - res = append(res, groupproperty.GetGroupFilter([]string{"ifDescr"}, f)) - } - - res = append(res, groupproperty.GetValueFilter([]string{"vlan"})) - - return res + return append(r.InterfaceOptions.getFilter(), groupproperty.GetValueFilter([]string{"vlan"})) } func (r *CheckInterfaceMetricsRequest) normalizeInterfaces(interfaces []device.Interface) error { @@ -133,7 +119,7 @@ func (r *CheckInterfaceMetricsRequest) normalizeInterfaces(interfaces []device.I } if r.ifDescrRegex != nil { - normalizedIfDescr := r.ifDescrRegex.ReplaceAllString(*interfaces[i].IfDescr, *r.IfDescrRegexReplace) + normalizedIfDescr := r.ifDescrRegex.ReplaceAllString(*interfaces[i].IfDescr, r.IfDescrRegexReplace) interfaces[i].IfDescr = &normalizedIfDescr } } diff --git a/internal/request/read_interfaces_request.go b/internal/request/read_interfaces_request.go index 7c670d7..ef9a37f 100644 --- a/internal/request/read_interfaces_request.go +++ b/internal/request/read_interfaces_request.go @@ -1,7 +1,12 @@ package request import ( + "context" "github.com/inexio/thola/internal/device" + "github.com/inexio/thola/internal/deviceclass/groupproperty" + "github.com/pkg/errors" + "regexp" + "strings" ) // ReadInterfacesRequest @@ -10,17 +15,79 @@ import ( // // swagger:model type ReadInterfacesRequest struct { - // If you only want specific values of the interfaces you can specify them here. - Values []string `yaml:"values" json:"values" xml:"values"` + InterfaceOptions ReadRequest } +func (r *ReadInterfacesRequest) validate(ctx context.Context) error { + if err := r.InterfaceOptions.validate(); err != nil { + return err + } + return r.ReadRequest.validate(ctx) +} + // ReadInterfacesResponse // -// ReadInterfacesResponse is a the request struct for the read interfaces response. +// ReadInterfacesResponse is the request struct for the read interfaces response. // // swagger:model type ReadInterfacesResponse struct { Interfaces []device.Interface `yaml:"interfaces" json:"interfaces" xml:"interfaces"` ReadResponse } + +// InterfaceOptions +// +// InterfaceOptions is the request struct for the options of an interface request. +// +// swagger:model +type InterfaceOptions struct { + // If you only want specific values of the interfaces you can specify them here. + Values []string `yaml:"values" json:"values" xml:"values"` + IfDescrRegex string `yaml:"ifDescr_regex" json:"ifDescr_regex" xml:"ifDescr_regex"` + ifDescrRegex *regexp.Regexp + IfDescrRegexReplace string `yaml:"ifDescr_regex_replace" json:"ifDescr_regex_replace" xml:"ifDescr_regex_replace"` + IfTypeFilter []string `yaml:"ifType_filter" json:"ifType_filter" xml:"ifType_filter"` + IfNameFilter []string `yaml:"ifName_filter" json:"ifName_filter" xml:"ifName_filter"` + IfDescrFilter []string `yaml:"ifDescr_filter" json:"ifDescr_filter" xml:"ifDescr_filter"` + SNMPGetsInsteadOfWalk bool `yaml:"snmp_gets_instead_of_walk" json:"snmp_gets_instead_of_walk" xml:"snmp_gets_instead_of_walk"` +} + +func (r *InterfaceOptions) validate() error { + if r.IfDescrRegex != "" && r.IfDescrRegexReplace == "" || + r.IfDescrRegex == "" && r.IfDescrRegexReplace != "" { + return errors.New("'ifDescr-regex' and 'ifDescr-regex-replace' must be set together") + } + if r.IfDescrRegex != "" { + regex, err := regexp.Compile(r.IfDescrRegex) + if err != nil { + return errors.Wrap(err, "compiling ifDescrRegex failed") + } + r.ifDescrRegex = regex + } + return nil +} + +func (r *InterfaceOptions) getFilter() []groupproperty.Filter { + var res []groupproperty.Filter + + for _, f := range r.IfTypeFilter { + res = append(res, groupproperty.GetGroupFilter([]string{"ifType"}, f)) + } + for _, f := range r.IfNameFilter { + res = append(res, groupproperty.GetGroupFilter([]string{"ifName"}, f)) + } + for _, f := range r.IfDescrFilter { + res = append(res, groupproperty.GetGroupFilter([]string{"ifDescr"}, f)) + } + + if len(r.Values) > 0 { + var values [][]string + for _, fil := range r.Values { + values = append(values, strings.Split(fil, "/")) + } + res = append(res, groupproperty.GetExclusiveValueFilter(values)) + } + + return res +} diff --git a/internal/request/read_interfaces_request_process.go b/internal/request/read_interfaces_request_process.go index 67f6ad6..99c61c9 100644 --- a/internal/request/read_interfaces_request_process.go +++ b/internal/request/read_interfaces_request_process.go @@ -5,9 +5,7 @@ package request import ( "context" - "github.com/inexio/thola/internal/deviceclass/groupproperty" "github.com/pkg/errors" - "strings" ) func (r *ReadInterfacesRequest) process(ctx context.Context) (Response, error) { @@ -16,16 +14,7 @@ func (r *ReadInterfacesRequest) process(ctx context.Context) (Response, error) { return nil, errors.Wrap(err, "failed to get communicator") } - var filter []groupproperty.Filter - if len(r.Values) > 0 { - var values [][]string - for _, fil := range r.Values { - values = append(values, strings.Split(fil, "/")) - } - filter = append(filter, groupproperty.GetExclusiveValueFilter(values)) - } - - result, err := com.GetInterfaces(ctx, filter...) + result, err := com.GetInterfaces(ctx, r.getFilter()...) if err != nil { return nil, errors.Wrap(err, "failed to get interfaces") }