<commit_before>package collectors

import (
	"fmt"
	"time"

	"github.com/StackExchange/scollector/opentsdb"
	"github.com/StackExchange/slog"
)

const (
	IfDescr         = ".1.3.6.1.2.1.2.2.1.2"
	ifInOctets      = ".1.3.6.1.2.1.2.2.1.10"
	ifInUcastPkts   = ".1.3.6.1.2.1.2.2.1.11"
	ifInNUcastPkts  = ".1.3.6.1.2.1.2.2.1.12"
	ifInDiscards    = ".1.3.6.1.2.1.2.2.1.13"
	ifInErrors      = ".1.3.6.1.2.1.2.2.1.14"
	ifOutOctets     = ".1.3.6.1.2.1.2.2.1.16"
	ifOutUcastPkts  = ".1.3.6.1.2.1.2.2.1.17"
	ifOutNUcastPkts = ".1.3.6.1.2.1.2.2.1.18"
	ifOutDiscards   = ".1.3.6.1.2.1.2.2.1.19"
	ifOutErrors     = ".1.3.6.1.2.1.2.2.1.20"
)

//IFXTable Constants
const (
	ifName               = ".1.3.6.1.2.1.31.1.1.1.1"
	ifAlias              = ".1.3.6.1.2.1.31.1.1.1.18"
	ifHCinOctets         = ".1.3.6.1.2.1.31.1.1.1.6"
	ifHCInUcastPkts      = ".1.3.6.1.2.1.31.1.1.1.7"
	ifHCInMulticastPkts  = ".1.3.6.1.2.1.31.1.1.1.8"
	ifHCInBroadcastPkts  = ".1.3.6.1.2.1.31.1.1.1.9"
	ifHCOutOctets        = ".1.3.6.1.2.1.31.1.1.1.10"
	ifHCOutUcastPkts     = ".1.3.6.1.2.1.31.1.1.1.11"
	ifHCOutMulticastPkts = ".1.3.6.1.2.1.31.1.1.1.12"
	ifHCOutBroadcastPkts = ".1.3.6.1.2.1.31.1.1.1.13"
)

// SNMPIfaces registers a SNMP Interfaces collector for the given community and host.
func SNMPIfaces(community, host string) {
	collectors = append(collectors, &IntervalCollector{
		F: func() opentsdb.MultiDataPoint {
			return c_snmp_ifaces(community, host)
		},
		Interval: time.Minute * 5,
	})
}

func c_snmp_ifaces(community, host string) opentsdb.MultiDataPoint {
	n, err := snmp_subtree(host, community, IfDescr)
	if err != nil {
		slog.Errorln("snmp ifaces1 :", err)
		return nil
	}
	names := make(map[interface{}]string, len(n))
	for k, v := range n {
		names[k] = fmt.Sprintf("%s", v)
	}
	var md opentsdb.MultiDataPoint
	add := func(oid, metric, dir string) error {
		m, err := snmp_subtree(host, community, oid)
		if err != nil {
			return err
		}
		for k, v := range m {
			Add(&md, metric, v, opentsdb.TagSet{
				"host":      host,
				"direction": dir,
				"iface":     fmt.Sprint(k),
				"iname":     names[k],
			})
		}
		return nil
	}
	oids := []snmpAdd{
		{ifHCinOctets, osNetBytes, "in"},
		{ifHCInUcastPkts, osNetUnicast, "in"},
		{ifHCInBroadcastPkts, osNetBroadcast, "in"},
		{ifHCInMulticastPkts, osNetMulticast, "in"},
		{ifInDiscards, osNetDropped, "in"},
		{ifInErrors, osNetErrors, "in"},
		{ifHCOutOctets, osNetBytes, "out"},
		{ifHCOutUcastPkts, osNetUnicast, "out"},
		{ifHCOutBroadcastPkts, osNetBroadcast, "out"},
		{ifHCOutMulticastPkts, osNetMulticast, "out"},
		{ifOutDiscards, osNetDropped, "out"},
		{ifOutErrors, osNetErrors, "out"},
	}
	for _, o := range oids {
		if err := add(o.oid, o.metric, o.dir); err != nil {
			slog.Errorln("snmp:", err)
			return nil
		}
	}<commit_msg>cmd/scollector: Also include aliases as a tag<commit_after>package collectors

import (
	"fmt"
	"time"

	"github.com/StackExchange/scollector/opentsdb"
	"github.com/StackExchange/slog"
)

const (
	IfDescr         = ".1.3.6.1.2.1.2.2.1.2"
	ifInOctets      = ".1.3.6.1.2.1.2.2.1.10"
	ifInUcastPkts   = ".1.3.6.1.2.1.2.2.1.11"
	ifInNUcastPkts  = ".1.3.6.1.2.1.2.2.1.12"
	ifInDiscards    = ".1.3.6.1.2.1.2.2.1.13"
	ifInErrors      = ".1.3.6.1.2.1.2.2.1.14"
	ifOutOctets     = ".1.3.6.1.2.1.2.2.1.16"
	ifOutUcastPkts  = ".1.3.6.1.2.1.2.2.1.17"
	ifOutNUcastPkts = ".1.3.6.1.2.1.2.2.1.18"
	ifOutDiscards   = ".1.3.6.1.2.1.2.2.1.19"
	ifOutErrors     = ".1.3.6.1.2.1.2.2.1.20"
)

//IFXTable Constants
const (
	ifName               = ".1.3.6.1.2.1.31.1.1.1.1"
	ifAlias              = ".1.3.6.1.2.1.31.1.1.1.18"
	ifHCinOctets         = ".1.3.6.1.2.1.31.1.1.1.6"
	ifHCInUcastPkts      = ".1.3.6.1.2.1.31.1.1.1.7"
	ifHCInMulticastPkts  = ".1.3.6.1.2.1.31.1.1.1.8"
	ifHCInBroadcastPkts  = ".1.3.6.1.2.1.31.1.1.1.9"
	ifHCOutOctets        = ".1.3.6.1.2.1.31.1.1.1.10"
	ifHCOutUcastPkts     = ".1.3.6.1.2.1.31.1.1.1.11"
	ifHCOutMulticastPkts = ".1.3.6.1.2.1.31.1.1.1.12"
	ifHCOutBroadcastPkts = ".1.3.6.1.2.1.31.1.1.1.13"
)

// SNMPIfaces registers a SNMP Interfaces collector for the given community and host.
func SNMPIfaces(community, host string) {
	collectors = append(collectors, &IntervalCollector{
		F: func() opentsdb.MultiDataPoint {
			return c_snmp_ifaces(community, host)
		},
		Interval: time.Minute * 5,
	})
}

func c_snmp_ifaces(community, host string) opentsdb.MultiDataPoint {
	n, err := snmp_subtree(host, community, IfDescr)
	if err != nil {
		slog.Errorln("snmp ifaces1 :", err)
		return nil
	}
	a, err := snmp_subtree(host, community, ifAlias)
	if err != nil {
		slog.Errorln("snmp ifaces1 :", err)
		return nil
	}
	names := make(map[interface{}]string, len(n))
	aliases := make(map[interface{}]string, len(a))
	for k, v := range n {
		names[k] = fmt.Sprintf("%s", v)
	}
	for k, v := range a {
		aliases[k] = fmt.Sprintf("%s", v)
	}
	var md opentsdb.MultiDataPoint
	add := func(oid, metric, dir string) error {
		m, err := snmp_subtree(host, community, oid)
		if err != nil {
			return err
		}
		for k, v := range m {
			Add(&md, metric, v, opentsdb.TagSet{
				"host":      host,
				"direction": dir,
				"iface":     fmt.Sprint(k),
				"iname":     names[k],
				"alias":     aliases[k],
			})
		}
		return nil
	}
	oids := []snmpAdd{
		{ifHCinOctets, osNetBytes, "in"},
		{ifHCInUcastPkts, osNetUnicast, "in"},
		{ifHCInBroadcastPkts, osNetBroadcast, "in"},
		{ifHCInMulticastPkts, osNetMulticast, "in"},
		{ifInDiscards, osNetDropped, "in"},
		{ifInErrors, osNetErrors, "in"},
		{ifHCOutOctets, osNetBytes, "out"},
		{ifHCOutUcastPkts, osNetUnicast, "out"},
		{ifHCOutBroadcastPkts, osNetBroadcast, "out"},
		{ifHCOutMulticastPkts, osNetMulticast, "out"},
		{ifOutDiscards, osNetDropped, "out"},
		{ifOutErrors, osNetErrors, "out"},
	}
	for _, o := range oids {
		if err := add(o.oid, o.metric, o.dir); err != nil {
			slog.Errorln("snmp:", err)
			return nil
		}
	}
{'fraction_non_alphanumeric': 0.19941949803653747, 'fraction_numerical': 0.08451425644527916, 'mean_word_length': 2.7696267696267696, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 84, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4541326', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2880, 'n_words': 434}