mirror of
				https://github.com/ClusterCockpit/cc-metric-collector.git
				synced 2025-11-04 02:35:07 +01:00 
			
		
		
		
	Refactor: Consolidate data structures
This commit is contained in:
		@@ -24,14 +24,15 @@ type NetstatCollectorConfig struct {
 | 
				
			|||||||
type NetstatCollectorMetric struct {
 | 
					type NetstatCollectorMetric struct {
 | 
				
			||||||
	name      string
 | 
						name      string
 | 
				
			||||||
	index     int
 | 
						index     int
 | 
				
			||||||
	devtags   map[string]string
 | 
						tags      map[string]string
 | 
				
			||||||
 | 
						rate_tags map[string]string
 | 
				
			||||||
	lastValue float64
 | 
						lastValue float64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type NetstatCollector struct {
 | 
					type NetstatCollector struct {
 | 
				
			||||||
	metricCollector
 | 
						metricCollector
 | 
				
			||||||
	config        NetstatCollectorConfig
 | 
						config        NetstatCollectorConfig
 | 
				
			||||||
	matches       map[string]map[string]NetstatCollectorMetric
 | 
						matches       map[string][]NetstatCollectorMetric
 | 
				
			||||||
	lastTimestamp time.Time
 | 
						lastTimestamp time.Time
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -64,14 +65,7 @@ func (m *NetstatCollector) Init(config json.RawMessage) error {
 | 
				
			|||||||
		fieldTransmitCompressed = iota
 | 
							fieldTransmitCompressed = iota
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	nameIndexMap := map[string]int{
 | 
						m.matches = make(map[string][]NetstatCollectorMetric)
 | 
				
			||||||
		"net_bytes_in":  fieldReceiveBytes,
 | 
					 | 
				
			||||||
		"net_pkts_in":   fieldReceivePackets,
 | 
					 | 
				
			||||||
		"net_bytes_out": fieldTransmitBytes,
 | 
					 | 
				
			||||||
		"net_pkts_out":  fieldTransmitPackets,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	m.matches = make(map[string]map[string]NetstatCollectorMetric)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Set default configuration,
 | 
						// Set default configuration,
 | 
				
			||||||
	m.config.SendAbsoluteValues = true
 | 
						m.config.SendAbsoluteValues = true
 | 
				
			||||||
@@ -110,20 +104,40 @@ func (m *NetstatCollector) Init(config json.RawMessage) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// Check if device is a included device
 | 
							// Check if device is a included device
 | 
				
			||||||
		if _, ok := stringArrayContains(m.config.IncludeDevices, dev); ok {
 | 
							if _, ok := stringArrayContains(m.config.IncludeDevices, dev); ok {
 | 
				
			||||||
			m.matches[dev] = make(map[string]NetstatCollectorMetric)
 | 
								m.matches[dev] = []NetstatCollectorMetric{
 | 
				
			||||||
			for name, idx := range nameIndexMap {
 | 
									{
 | 
				
			||||||
				m.matches[dev][name] = NetstatCollectorMetric{
 | 
										name:      "net_bytes_in",
 | 
				
			||||||
					name:      name,
 | 
										index:     fieldReceiveBytes,
 | 
				
			||||||
					index:     idx,
 | 
					 | 
				
			||||||
					lastValue: 0,
 | 
										lastValue: 0,
 | 
				
			||||||
					devtags: map[string]string{
 | 
										tags:      map[string]string{"device": dev, "type": "node", "unit": "bytes"},
 | 
				
			||||||
						"device": dev,
 | 
										rate_tags: map[string]string{"device": dev, "type": "node", "unit": "bytes/sec"},
 | 
				
			||||||
						"type":   "node",
 | 
									},
 | 
				
			||||||
					},
 | 
									{
 | 
				
			||||||
				}
 | 
										name:      "net_pkts_in",
 | 
				
			||||||
 | 
										index:     fieldReceivePackets,
 | 
				
			||||||
 | 
										lastValue: 0,
 | 
				
			||||||
 | 
										tags:      map[string]string{"device": dev, "type": "node", "unit": "packets"},
 | 
				
			||||||
 | 
										rate_tags: map[string]string{"device": dev, "type": "node", "unit": "packets/sec"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										name:      "net_bytes_out",
 | 
				
			||||||
 | 
										index:     fieldTransmitBytes,
 | 
				
			||||||
 | 
										lastValue: 0,
 | 
				
			||||||
 | 
										tags:      map[string]string{"device": dev, "type": "node", "unit": "bytes"},
 | 
				
			||||||
 | 
										rate_tags: map[string]string{"device": dev, "type": "node", "unit": "bytes/sec"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										name:      "net_pkts_out",
 | 
				
			||||||
 | 
										index:     fieldTransmitPackets,
 | 
				
			||||||
 | 
										lastValue: 0,
 | 
				
			||||||
 | 
										tags:      map[string]string{"device": dev, "type": "node", "unit": "packets"},
 | 
				
			||||||
 | 
										rate_tags: map[string]string{"device": dev, "type": "node", "unit": "packets/sec"},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(m.matches) == 0 {
 | 
						if len(m.matches) == 0 {
 | 
				
			||||||
		return errors.New("no devices to collector metrics found")
 | 
							return errors.New("no devices to collector metrics found")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -166,17 +180,11 @@ func (m *NetstatCollector) Read(interval time.Duration, output chan lp.CCMetric)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// Check if device is a included device
 | 
							// Check if device is a included device
 | 
				
			||||||
		if devmetrics, ok := m.matches[dev]; ok {
 | 
							if devmetrics, ok := m.matches[dev]; ok {
 | 
				
			||||||
			for name, data := range devmetrics {
 | 
								for _, data := range devmetrics {
 | 
				
			||||||
				v, err := strconv.ParseFloat(f[data.index], 64)
 | 
									v, err := strconv.ParseFloat(f[data.index], 64)
 | 
				
			||||||
				if err == nil {
 | 
									if err == nil {
 | 
				
			||||||
					if m.config.SendAbsoluteValues {
 | 
										if m.config.SendAbsoluteValues {
 | 
				
			||||||
						if y, err := lp.New(name, data.devtags, m.meta, map[string]interface{}{"value": v}, now); err == nil {
 | 
											if y, err := lp.New(data.name, data.tags, m.meta, map[string]interface{}{"value": v}, now); err == nil {
 | 
				
			||||||
							switch {
 | 
					 | 
				
			||||||
							case strings.Contains(name, "byte"):
 | 
					 | 
				
			||||||
								y.AddMeta("unit", "bytes")
 | 
					 | 
				
			||||||
							case strings.Contains(name, "pkt"):
 | 
					 | 
				
			||||||
								y.AddMeta("unit", "packets")
 | 
					 | 
				
			||||||
							}
 | 
					 | 
				
			||||||
							output <- y
 | 
												output <- y
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
@@ -188,17 +196,10 @@ func (m *NetstatCollector) Read(interval time.Duration, output chan lp.CCMetric)
 | 
				
			|||||||
							value = 0
 | 
												value = 0
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
						data.lastValue = v
 | 
											data.lastValue = v
 | 
				
			||||||
						if y, err := lp.New(name+"_bw", data.devtags, m.meta, map[string]interface{}{"value": value}, now); err == nil {
 | 
											if y, err := lp.New(data.name+"_bw", data.rate_tags, m.meta, map[string]interface{}{"value": value}, now); err == nil {
 | 
				
			||||||
							switch {
 | 
					 | 
				
			||||||
							case strings.Contains(name, "byte"):
 | 
					 | 
				
			||||||
								y.AddMeta("unit", "bytes/sec")
 | 
					 | 
				
			||||||
							case strings.Contains(name, "pkt"):
 | 
					 | 
				
			||||||
								y.AddMeta("unit", "packets/sec")
 | 
					 | 
				
			||||||
							}
 | 
					 | 
				
			||||||
							output <- y
 | 
												output <- y
 | 
				
			||||||
						}
 | 
											}
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					devmetrics[name] = data
 | 
					 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user