github.com/elfadel/cilium@v1.6.12/pkg/option/monitor.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package option
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  // MonitorAggregationLevel represents a level of aggregation for monitor events
    24  // from the datapath. Low values represent no aggregation, that is, to increase
    25  // the number of events emitted from the datapath; Higher values represent more
    26  // aggregation, to minimize the number of events emitted from the datapath.
    27  //
    28  // The MonitorAggregationLevel does not affect the Debug option in the daemon
    29  // or endpoint, so debug notifications will continue uninhibited by this
    30  // setting.
    31  type MonitorAggregationLevel OptionSetting
    32  
    33  const (
    34  	// MonitorAggregationLevelNone represents no aggregation in the
    35  	// datapath; all packets will be monitored.
    36  	MonitorAggregationLevelNone OptionSetting = 0
    37  
    38  	// MonitorAggregationLevelLow represents aggregation of monitor events
    39  	// to emit a maximum of one trace event per packet. Trace events when
    40  	// packets are received are disabled.
    41  	MonitorAggregationLevelLowest OptionSetting = 1
    42  
    43  	// MonitorAggregationLevelLow is the same as
    44  	// MonitorAggregationLevelLowest, but may aggregate additional traffic
    45  	// in future.
    46  	MonitorAggregationLevelLow OptionSetting = 2
    47  
    48  	// MonitorAggregationLevelMedium represents aggregation of monitor
    49  	// events to only emit notifications periodically for each connection
    50  	// unless there is new information (eg, a TCP connection is closed).
    51  	MonitorAggregationLevelMedium OptionSetting = 3
    52  
    53  	// MonitorAggregationLevelMax is the maximum level of aggregation
    54  	// currently supported.
    55  	MonitorAggregationLevelMax = MonitorAggregationLevelMedium
    56  )
    57  
    58  // monitorAggregationOption maps a user-specified string to a monitor
    59  // aggregation level.
    60  var monitorAggregationOption = map[string]OptionSetting{
    61  	"":         MonitorAggregationLevelNone,
    62  	"none":     MonitorAggregationLevelNone,
    63  	"disabled": MonitorAggregationLevelNone,
    64  	"lowest":   MonitorAggregationLevelLowest,
    65  	"low":      MonitorAggregationLevelLow,
    66  	"medium":   MonitorAggregationLevelMedium,
    67  	"max":      MonitorAggregationLevelMax,
    68  	"maximum":  MonitorAggregationLevelMax,
    69  }
    70  
    71  func init() {
    72  	for i := MonitorAggregationLevelNone; i <= MonitorAggregationLevelMax; i++ {
    73  		number := strconv.Itoa(int(i))
    74  		monitorAggregationOption[number] = OptionSetting(i)
    75  	}
    76  }
    77  
    78  // monitorAggregationFormat maps an aggregation level to a formatted string.
    79  var monitorAggregationFormat = map[OptionSetting]string{
    80  	MonitorAggregationLevelNone:   "None",
    81  	MonitorAggregationLevelLowest: "Lowest",
    82  	MonitorAggregationLevelLow:    "Low",
    83  	MonitorAggregationLevelMedium: "Medium",
    84  }
    85  
    86  // VerifyMonitorAggregationLevel validates the specified key/value for a
    87  // monitor aggregation level.
    88  func VerifyMonitorAggregationLevel(key, value string) error {
    89  	_, err := ParseMonitorAggregationLevel(value)
    90  	return err
    91  }
    92  
    93  // ParseMonitorAggregationLevel turns a string into a monitor aggregation
    94  // level. The string may contain an integer value or a string representation of
    95  // a particular monitor aggregation level.
    96  func ParseMonitorAggregationLevel(value string) (OptionSetting, error) {
    97  	// First, attempt the string representation.
    98  	if level, ok := monitorAggregationOption[strings.ToLower(value)]; ok {
    99  		return level, nil
   100  	}
   101  
   102  	// If it's not a valid string option, attempt to parse an integer.
   103  	valueParsed, err := strconv.Atoi(value)
   104  	if err != nil {
   105  		err = fmt.Errorf("invalid monitor aggregation level %q", value)
   106  		return MonitorAggregationLevelNone, err
   107  	}
   108  	parsed := OptionSetting(valueParsed)
   109  	if parsed < MonitorAggregationLevelNone || parsed > MonitorAggregationLevelMax {
   110  		err = fmt.Errorf("monitor aggregation level must be between %d and %d",
   111  			MonitorAggregationLevelNone, MonitorAggregationLevelMax)
   112  		return MonitorAggregationLevelNone, err
   113  	}
   114  	return parsed, nil
   115  }
   116  
   117  // FormatMonitorAggregationLevel maps a MonitorAggregationLevel to a string.
   118  func FormatMonitorAggregationLevel(level OptionSetting) string {
   119  	return monitorAggregationFormat[level]
   120  }