Skip to content

Commit

Permalink
Add meraki logs (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
i3149 authored Mar 6, 2024
1 parent 11f1b37 commit b2e76d4
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pkg/cat/kkc.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func (kc *KTranslate) Run(ctx context.Context) error {
}
assureInput()
kc.metrics.SnmpDeviceData = kt.NewSnmpMetricSet(kc.registry)
err := snmp.StartSNMPPolls(ctx, kc.inputChan, kc.metrics.SnmpDeviceData, kc.registry, kc.apic, kc.log, kc.config.SNMPInput, kc.resolver, kc.confMgr)
err := snmp.StartSNMPPolls(ctx, kc.inputChan, kc.metrics.SnmpDeviceData, kc.registry, kc.apic, kc.log, kc.config.SNMPInput, kc.resolver, kc.confMgr, kc.logTee)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/inputs/snmp/metrics/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Poller struct {
pingSec int
}

func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL) *Poller {
func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) *Poller {
// Default poll rate is 5 min. This is what a lot of SNMP billing is on.
counterTimeSec := 5 * 60
if conf != nil && conf.PollTimeSec > 0 {
Expand Down Expand Up @@ -84,7 +84,7 @@ func NewPoller(server *gosnmp.GoSNMP, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpD
}

// If we are extending the metrics for this device in any way, set it up now.
ext, err := extension.NewExtension(jchfChan, gconf, conf, metrics, log)
ext, err := extension.NewExtension(jchfChan, gconf, conf, metrics, log, logchan)
if err != nil {
log.Errorf("Cannot setup extension for %s -> %s: %v", err, conf.DeviceIP, conf.DeviceName)
} else if ext != nil {
Expand Down Expand Up @@ -144,7 +144,7 @@ func NewPollerForPing(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jch
return &poller
}

func NewPollerForExtention(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL) *Poller {
func NewPollerForExtention(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) *Poller {
// Default poll rate is 5 min. This is what a lot of SNMP billing is on.
counterTimeSec := 5 * 60
if conf != nil && conf.PollTimeSec > 0 {
Expand Down Expand Up @@ -174,7 +174,7 @@ func NewPollerForExtention(gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig
}

// If we are extending the metrics for this device in any way, set it up now.
ext, err := extension.NewExtension(jchfChan, gconf, conf, metrics, log)
ext, err := extension.NewExtension(jchfChan, gconf, conf, metrics, log, logchan)
if err != nil {
log.Errorf("Cannot setup extension for %s -> %s: %v", err, conf.DeviceIP, conf.DeviceName)
} else if ext != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/inputs/snmp/pollOnce.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/kentik/ktranslate/pkg/kt"
)

func pollOnce(ctx context.Context, tdevice string, conf *kt.SnmpConfig, connectTimeout time.Duration, retries int, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, log logger.ContextL) error {
func pollOnce(ctx context.Context, tdevice string, conf *kt.SnmpConfig, connectTimeout time.Duration, retries int, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, log logger.ContextL, logchan chan string) error {
device := conf.Devices[tdevice]
if device == nil {
for _, dev := range conf.Devices {
Expand Down Expand Up @@ -50,7 +50,7 @@ func pollOnce(ctx context.Context, tdevice string, conf *kt.SnmpConfig, connectT

nm := kt.NewSnmpDeviceMetric(registry, device.DeviceName)
metadataPoller := metadata.NewPoller(metadataServer, conf.Global, device, jchfChan, nm, profile, log)
metricPoller := snmp_metrics.NewPoller(metricsServer, conf.Global, device, jchfChan, nm, profile, log)
metricPoller := snmp_metrics.NewPoller(metricsServer, conf.Global, device, jchfChan, nm, profile, log, logchan)

metadataPoller.StartLoop(ctx)
// Give a little time to get this done.
Expand Down
26 changes: 13 additions & 13 deletions pkg/inputs/snmp/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func init() {
flag.BoolVar(&validateMib, "snmp_validate", false, "If true, validate mib profiles and exit.")
}

func StartSNMPPolls(ctx context.Context, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, cfg *ktranslate.SNMPInputConfig, resolv *resolv.Resolver, confMgr config.ConfigManager) error {
func StartSNMPPolls(ctx context.Context, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, cfg *ktranslate.SNMPInputConfig, resolv *resolv.Resolver, confMgr config.ConfigManager, logchan chan string) error {
snmpFile := cfg.SNMPFile
// Do this once here just to see if we need to exit right away.
conf, connectTimeout, retries, err := initSnmp(ctx, snmpFile, log)
Expand Down Expand Up @@ -92,12 +92,12 @@ func StartSNMPPolls(ctx context.Context, jchfChan chan []*kt.JCHF, metrics *kt.S

// If we just want to poll one device and exit, do this here.
if v := cfg.PollNowTarget; v != "" {
return pollOnce(ctx, v, conf, connectTimeout, retries, jchfChan, metrics, registry, log)
return pollOnce(ctx, v, conf, connectTimeout, retries, jchfChan, metrics, registry, log, logchan)
}

// Now, launch a metadata and metrics server for each configured or discovered device.
if conf.Trap == nil || !conf.Trap.TrapOnly { // Unless we are turning off everything but snmp traps.
go wrapSnmpPolling(ctx, snmpFile, jchfChan, metrics, registry, apic, log, 0, cfg, confMgr)
go wrapSnmpPolling(ctx, snmpFile, jchfChan, metrics, registry, apic, log, 0, cfg, confMgr, logchan)
}

// Run a trap listener?
Expand Down Expand Up @@ -147,9 +147,9 @@ func initSnmp(ctx context.Context, snmpFile string, log logger.ContextL) (*kt.Sn
return conf, connectTimeout, retries, nil
}

func wrapSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, restartCount int, cfg *ktranslate.SNMPInputConfig, confMgr config.ConfigManager) {
func wrapSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, restartCount int, cfg *ktranslate.SNMPInputConfig, confMgr config.ConfigManager, logchan chan string) {
ctxSnmp, cancel := context.WithCancel(ctx)
err := runSnmpPolling(ctxSnmp, snmpFile, jchfChan, metrics, registry, apic, log, restartCount, cfg)
err := runSnmpPolling(ctxSnmp, snmpFile, jchfChan, metrics, registry, apic, log, restartCount, cfg, logchan)
if err != nil {
log.Errorf("There was an error when polling for SNMP devices: %v.", err)
}
Expand All @@ -173,10 +173,10 @@ func wrapSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.J
// If we got this signal, redo the snmp system.
cancel()

go wrapSnmpPolling(ctx, snmpFile, jchfChan, metrics, registry, apic, log, restartCount+1, cfg, confMgr) // Track how many times through here we've been.
go wrapSnmpPolling(ctx, snmpFile, jchfChan, metrics, registry, apic, log, restartCount+1, cfg, confMgr, logchan) // Track how many times through here we've been.
}

func runSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, restartCount int, cfg *ktranslate.SNMPInputConfig) error {
func runSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.JCHF, metrics *kt.SnmpMetricSet, registry go_metrics.Registry, apic *api.KentikApi, log logger.ContextL, restartCount int, cfg *ktranslate.SNMPInputConfig, logchan chan string) error {
// Parse again to make sure nothing's changed.
conf, connectTimeout, retries, err := initSnmp(ctx, snmpFile, log)
if err != nil || conf == nil || conf.Global == nil {
Expand Down Expand Up @@ -238,7 +238,7 @@ func runSnmpPolling(ctx context.Context, snmpFile string, jchfChan chan []*kt.JC
return err
}

err = launchSnmp(ctx, conf.Global, device, jchfChan, connectTimeout, retries, nm, profile, cl)
err = launchSnmp(ctx, conf.Global, device, jchfChan, connectTimeout, retries, nm, profile, cl, logchan)
if err != nil {
return err
}
Expand All @@ -261,7 +261,7 @@ func launchSnmpTrap(ctx context.Context, conf *kt.SnmpConfig, jchfChan chan []*k
return nil
}

func launchSnmp(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, connectTimeout time.Duration, retries int, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL) error {
func launchSnmp(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, connectTimeout time.Duration, retries int, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) error {
// Sometimes this device is pinging only. In this case, start the ping loop and return.
if device.PingOnly {
return launchPingOnly(ctx, conf, device, jchfChan, connectTimeout, retries, metrics, profile, log)
Expand All @@ -273,7 +273,7 @@ func launchSnmp(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpD

// Sometimes a device is only going to be running its extention.
if device.Ext != nil && device.Ext.ExtOnly {
return launchExtOnly(ctx, conf, device, jchfChan, connectTimeout, retries, metrics, profile, log)
return launchExtOnly(ctx, conf, device, jchfChan, connectTimeout, retries, metrics, profile, log, logchan)
}

// We need two of these, to avoid concurrent access by the two pollers.
Expand All @@ -293,7 +293,7 @@ func launchSnmp(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpD
}

metadataPoller := metadata.NewPoller(metadataServer, conf, device, jchfChan, metrics, profile, log)
metricPoller := snmp_metrics.NewPoller(metricsServer, conf, device, jchfChan, metrics, profile, log)
metricPoller := snmp_metrics.NewPoller(metricsServer, conf, device, jchfChan, metrics, profile, log, logchan)

// We've now done everything we can do synchronously -- return to the client initialization
// code, and do everything else in the background
Expand Down Expand Up @@ -469,8 +469,8 @@ func launchPingOnly(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.S
*
Handle the case where we're only doing a extention loop of a device.
*/
func launchExtOnly(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, connectTimeout time.Duration, retries int, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL) error {
metricPoller := snmp_metrics.NewPollerForExtention(conf, device, jchfChan, metrics, profile, log)
func launchExtOnly(ctx context.Context, conf *kt.SnmpGlobalConfig, device *kt.SnmpDeviceConfig, jchfChan chan []*kt.JCHF, connectTimeout time.Duration, retries int, metrics *kt.SnmpDeviceMetric, profile *mibs.Profile, log logger.ContextL, logchan chan string) error {
metricPoller := snmp_metrics.NewPollerForExtention(conf, device, jchfChan, metrics, profile, log, logchan)

// We've now done everything we can do synchronously -- return to the client initialization
// code, and do everything else in the background
Expand Down
4 changes: 2 additions & 2 deletions pkg/inputs/snmp/x/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type Extension interface {
GetName() string
}

func NewExtension(jchfChan chan []*kt.JCHF, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, metrics *kt.SnmpDeviceMetric, log logger.ContextL) (Extension, error) {
func NewExtension(jchfChan chan []*kt.JCHF, gconf *kt.SnmpGlobalConfig, conf *kt.SnmpDeviceConfig, metrics *kt.SnmpDeviceMetric, log logger.ContextL, logchan chan string) (Extension, error) {
if conf.Ext == nil { // No extensions set.
return nil, nil
}

if conf.Ext.EAPIConfig != nil {
return arista.NewEAPIClient(jchfChan, gconf, conf, metrics, log)
} else if conf.Ext.MerakiConfig != nil {
return meraki.NewMerakiClient(jchfChan, gconf, conf, metrics, log)
return meraki.NewMerakiClient(jchfChan, gconf, conf, metrics, log, logchan)
}

return nil, nil
Expand Down
Loading

0 comments on commit b2e76d4

Please sign in to comment.