Document and add support to input plugins for logging alias (#6357)

This commit is contained in:
Greg 2019-09-23 16:39:50 -06:00 committed by Daniel Nelson
parent e42d2e39c6
commit 817c9a69a9
111 changed files with 961 additions and 659 deletions

View File

@ -188,6 +188,7 @@ driven operation.
Parameters that can be used with any input plugin: Parameters that can be used with any input plugin:
- **alias**: Name an instance of a plugin.
- **interval**: How often to gather this metric. Normal plugins use a single - **interval**: How often to gather this metric. Normal plugins use a single
global interval, but if one particular input should be run less or more global interval, but if one particular input should be run less or more
often, you can configure that here. often, you can configure that here.

View File

@ -144,7 +144,7 @@ func (r *RunningAggregator) Add(m telegraf.Metric) bool {
defer r.Unlock() defer r.Unlock()
if m.Time().Before(r.periodStart.Add(-r.Config.Grace)) || m.Time().After(r.periodEnd.Add(r.Config.Delay)) { if m.Time().Before(r.periodStart.Add(-r.Config.Grace)) || m.Time().After(r.periodEnd.Add(r.Config.Delay)) {
r.log.Debugf("metric is outside aggregation window; discarding. %s: m: %s e: %s g: %s", r.log.Debugf("Metric is outside aggregation window; discarding. %s: m: %s e: %s g: %s",
m.Time(), r.periodStart, r.periodEnd, r.Config.Grace) m.Time(), r.periodStart, r.periodEnd, r.Config.Grace)
r.MetricsDropped.Incr(1) r.MetricsDropped.Incr(1)
return r.Config.DropOriginal return r.Config.DropOriginal

View File

@ -10,6 +10,7 @@ emitting the aggregate every `period` seconds.
[[aggregators.basicstats]] [[aggregators.basicstats]]
## The period on which to flush & clear the aggregator. ## The period on which to flush & clear the aggregator.
period = "30s" period = "30s"
## If true, the original metric will be dropped by the ## If true, the original metric will be dropped by the
## aggregator and will not get sent to the output plugins. ## aggregator and will not get sent to the output plugins.
drop_original = false drop_original = false

View File

@ -1,7 +1,6 @@
package basicstats package basicstats
import ( import (
"log"
"math" "math"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -10,6 +9,7 @@ import (
type BasicStats struct { type BasicStats struct {
Stats []string `toml:"stats"` Stats []string `toml:"stats"`
Log telegraf.Logger
cache map[uint64]aggregate cache map[uint64]aggregate
statsConfig *configuredStats statsConfig *configuredStats
@ -28,9 +28,9 @@ type configuredStats struct {
} }
func NewBasicStats() *BasicStats { func NewBasicStats() *BasicStats {
mm := &BasicStats{} return &BasicStats{
mm.Reset() cache: make(map[uint64]aggregate),
return mm }
} }
type aggregate struct { type aggregate struct {
@ -53,6 +53,7 @@ type basicstats struct {
var sampleConfig = ` var sampleConfig = `
## The period on which to flush & clear the aggregator. ## The period on which to flush & clear the aggregator.
period = "30s" period = "30s"
## If true, the original metric will be dropped by the ## If true, the original metric will be dropped by the
## aggregator and will not get sent to the output plugins. ## aggregator and will not get sent to the output plugins.
drop_original = false drop_original = false
@ -61,17 +62,17 @@ var sampleConfig = `
# stats = ["count", "min", "max", "mean", "stdev", "s2", "sum"] # stats = ["count", "min", "max", "mean", "stdev", "s2", "sum"]
` `
func (m *BasicStats) SampleConfig() string { func (*BasicStats) SampleConfig() string {
return sampleConfig return sampleConfig
} }
func (m *BasicStats) Description() string { func (*BasicStats) Description() string {
return "Keep the aggregate basicstats of each metric passing through." return "Keep the aggregate basicstats of each metric passing through."
} }
func (m *BasicStats) Add(in telegraf.Metric) { func (b *BasicStats) Add(in telegraf.Metric) {
id := in.HashID() id := in.HashID()
if _, ok := m.cache[id]; !ok { if _, ok := b.cache[id]; !ok {
// hit an uncached metric, create caches for first time: // hit an uncached metric, create caches for first time:
a := aggregate{ a := aggregate{
name: in.Name(), name: in.Name(),
@ -92,13 +93,13 @@ func (m *BasicStats) Add(in telegraf.Metric) {
} }
} }
} }
m.cache[id] = a b.cache[id] = a
} else { } else {
for _, field := range in.FieldList() { for _, field := range in.FieldList() {
if fv, ok := convert(field.Value); ok { if fv, ok := convert(field.Value); ok {
if _, ok := m.cache[id].fields[field.Key]; !ok { if _, ok := b.cache[id].fields[field.Key]; !ok {
// hit an uncached field of a cached metric // hit an uncached field of a cached metric
m.cache[id].fields[field.Key] = basicstats{ b.cache[id].fields[field.Key] = basicstats{
count: 1, count: 1,
min: fv, min: fv,
max: fv, max: fv,
@ -111,7 +112,7 @@ func (m *BasicStats) Add(in telegraf.Metric) {
continue continue
} }
tmp := m.cache[id].fields[field.Key] tmp := b.cache[id].fields[field.Key]
//https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance //https://en.m.wikipedia.org/wiki/Algorithms_for_calculating_variance
//variable initialization //variable initialization
x := fv x := fv
@ -138,32 +139,30 @@ func (m *BasicStats) Add(in telegraf.Metric) {
//diff compute //diff compute
tmp.diff = fv - tmp.LAST tmp.diff = fv - tmp.LAST
//store final data //store final data
m.cache[id].fields[field.Key] = tmp b.cache[id].fields[field.Key] = tmp
} }
} }
} }
} }
func (m *BasicStats) Push(acc telegraf.Accumulator) { func (b *BasicStats) Push(acc telegraf.Accumulator) {
config := getConfiguredStats(m) for _, aggregate := range b.cache {
for _, aggregate := range m.cache {
fields := map[string]interface{}{} fields := map[string]interface{}{}
for k, v := range aggregate.fields { for k, v := range aggregate.fields {
if config.count { if b.statsConfig.count {
fields[k+"_count"] = v.count fields[k+"_count"] = v.count
} }
if config.min { if b.statsConfig.min {
fields[k+"_min"] = v.min fields[k+"_min"] = v.min
} }
if config.max { if b.statsConfig.max {
fields[k+"_max"] = v.max fields[k+"_max"] = v.max
} }
if config.mean { if b.statsConfig.mean {
fields[k+"_mean"] = v.mean fields[k+"_mean"] = v.mean
} }
if config.sum { if b.statsConfig.sum {
fields[k+"_sum"] = v.sum fields[k+"_sum"] = v.sum
} }
@ -171,16 +170,16 @@ func (m *BasicStats) Push(acc telegraf.Accumulator) {
if v.count > 1 { if v.count > 1 {
variance := v.M2 / (v.count - 1) variance := v.M2 / (v.count - 1)
if config.variance { if b.statsConfig.variance {
fields[k+"_s2"] = variance fields[k+"_s2"] = variance
} }
if config.stdev { if b.statsConfig.stdev {
fields[k+"_stdev"] = math.Sqrt(variance) fields[k+"_stdev"] = math.Sqrt(variance)
} }
if config.diff { if b.statsConfig.diff {
fields[k+"_diff"] = v.diff fields[k+"_diff"] = v.diff
} }
if config.non_negative_diff && v.diff >= 0 { if b.statsConfig.non_negative_diff && v.diff >= 0 {
fields[k+"_non_negative_diff"] = v.diff fields[k+"_non_negative_diff"] = v.diff
} }
@ -194,14 +193,12 @@ func (m *BasicStats) Push(acc telegraf.Accumulator) {
} }
} }
func parseStats(names []string) *configuredStats { // member function for logging.
func (b *BasicStats) parseStats() *configuredStats {
parsed := &configuredStats{} parsed := &configuredStats{}
for _, name := range names { for _, name := range b.Stats {
switch name { switch name {
case "count": case "count":
parsed.count = true parsed.count = true
case "min": case "min":
@ -222,45 +219,32 @@ func parseStats(names []string) *configuredStats {
parsed.non_negative_diff = true parsed.non_negative_diff = true
default: default:
log.Printf("W! Unrecognized basic stat '%s', ignoring", name) b.Log.Warnf("Unrecognized basic stat %q, ignoring", name)
} }
} }
return parsed return parsed
} }
func defaultStats() *configuredStats { func (b *BasicStats) getConfiguredStats() {
if b.Stats == nil {
defaults := &configuredStats{} b.statsConfig = &configuredStats{
count: true,
defaults.count = true min: true,
defaults.min = true max: true,
defaults.max = true mean: true,
defaults.mean = true variance: true,
defaults.variance = true stdev: true,
defaults.stdev = true sum: false,
defaults.sum = false non_negative_diff: false,
defaults.non_negative_diff = false
return defaults
}
func getConfiguredStats(m *BasicStats) *configuredStats {
if m.statsConfig == nil {
if m.Stats == nil {
m.statsConfig = defaultStats()
} else {
m.statsConfig = parseStats(m.Stats)
} }
} else {
b.statsConfig = b.parseStats()
} }
return m.statsConfig
} }
func (m *BasicStats) Reset() { func (b *BasicStats) Reset() {
m.cache = make(map[uint64]aggregate) b.cache = make(map[uint64]aggregate)
} }
func convert(in interface{}) (float64, bool) { func convert(in interface{}) (float64, bool) {
@ -276,6 +260,12 @@ func convert(in interface{}) (float64, bool) {
} }
} }
func (b *BasicStats) Init() error {
b.getConfiguredStats()
return nil
}
func init() { func init() {
aggregators.Add("basicstats", func() telegraf.Aggregator { aggregators.Add("basicstats", func() telegraf.Aggregator {
return NewBasicStats() return NewBasicStats()

View File

@ -39,6 +39,8 @@ var m2, _ = metric.New("m1",
func BenchmarkApply(b *testing.B) { func BenchmarkApply(b *testing.B) {
minmax := NewBasicStats() minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
minmax.Add(m1) minmax.Add(m1)
@ -50,6 +52,8 @@ func BenchmarkApply(b *testing.B) {
func TestBasicStatsWithPeriod(t *testing.T) { func TestBasicStatsWithPeriod(t *testing.T) {
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
minmax := NewBasicStats() minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
minmax.Add(m1) minmax.Add(m1)
minmax.Add(m2) minmax.Add(m2)
@ -106,6 +110,8 @@ func TestBasicStatsWithPeriod(t *testing.T) {
func TestBasicStatsDifferentPeriods(t *testing.T) { func TestBasicStatsDifferentPeriods(t *testing.T) {
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
minmax := NewBasicStats() minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.getConfiguredStats()
minmax.Add(m1) minmax.Add(m1)
minmax.Push(&acc) minmax.Push(&acc)
@ -181,6 +187,8 @@ func TestBasicStatsWithOnlyCount(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"count"} aggregator.Stats = []string{"count"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -208,6 +216,8 @@ func TestBasicStatsWithOnlyMin(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"min"} aggregator.Stats = []string{"min"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -235,6 +245,8 @@ func TestBasicStatsWithOnlyMax(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"max"} aggregator.Stats = []string{"max"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -262,6 +274,8 @@ func TestBasicStatsWithOnlyMean(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"mean"} aggregator.Stats = []string{"mean"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -289,6 +303,8 @@ func TestBasicStatsWithOnlySum(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"sum"} aggregator.Stats = []string{"sum"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -347,6 +363,8 @@ func TestBasicStatsWithOnlySumFloatingPointErrata(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"sum"} aggregator.Stats = []string{"sum"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(sum1) aggregator.Add(sum1)
aggregator.Add(sum2) aggregator.Add(sum2)
@ -368,6 +386,8 @@ func TestBasicStatsWithOnlyVariance(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"s2"} aggregator.Stats = []string{"s2"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -393,6 +413,8 @@ func TestBasicStatsWithOnlyStandardDeviation(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"stdev"} aggregator.Stats = []string{"stdev"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -418,6 +440,8 @@ func TestBasicStatsWithMinAndMax(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"min", "max"} aggregator.Stats = []string{"min", "max"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -452,6 +476,8 @@ func TestBasicStatsWithDiff(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"diff"} aggregator.Stats = []string{"diff"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -477,6 +503,8 @@ func TestBasicStatsWithNonNegativeDiff(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"non_negative_diff"} aggregator.Stats = []string{"non_negative_diff"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -500,7 +528,9 @@ func TestBasicStatsWithNonNegativeDiff(t *testing.T) {
func TestBasicStatsWithAllStats(t *testing.T) { func TestBasicStatsWithAllStats(t *testing.T) {
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
minmax := NewBasicStats() minmax := NewBasicStats()
minmax.Log = testutil.Logger{}
minmax.Stats = []string{"count", "min", "max", "mean", "stdev", "s2", "sum"} minmax.Stats = []string{"count", "min", "max", "mean", "stdev", "s2", "sum"}
minmax.getConfiguredStats()
minmax.Add(m1) minmax.Add(m1)
minmax.Add(m2) minmax.Add(m2)
@ -564,6 +594,8 @@ func TestBasicStatsWithNoStats(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{} aggregator.Stats = []string{}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -579,6 +611,8 @@ func TestBasicStatsWithUnknownStat(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Stats = []string{"crazy"} aggregator.Stats = []string{"crazy"}
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)
@ -596,6 +630,8 @@ func TestBasicStatsWithUnknownStat(t *testing.T) {
func TestBasicStatsWithDefaultStats(t *testing.T) { func TestBasicStatsWithDefaultStats(t *testing.T) {
aggregator := NewBasicStats() aggregator := NewBasicStats()
aggregator.Log = testutil.Logger{}
aggregator.getConfiguredStats()
aggregator.Add(m1) aggregator.Add(m1)
aggregator.Add(m2) aggregator.Add(m2)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"strings" "strings"
"sync" "sync"
@ -55,6 +54,7 @@ type AMQPConsumer struct {
tls.ClientConfig tls.ClientConfig
ContentEncoding string `toml:"content_encoding"` ContentEncoding string `toml:"content_encoding"`
Log telegraf.Logger
deliveries map[telegraf.TrackingID]amqp.Delivery deliveries map[telegraf.TrackingID]amqp.Delivery
@ -241,11 +241,11 @@ func (a *AMQPConsumer) Start(acc telegraf.Accumulator) error {
break break
} }
log.Printf("I! [inputs.amqp_consumer] connection closed: %s; trying to reconnect", err) a.Log.Infof("Connection closed: %s; trying to reconnect", err)
for { for {
msgs, err := a.connect(amqpConf) msgs, err := a.connect(amqpConf)
if err != nil { if err != nil {
log.Printf("E! AMQP connection failed: %s", err) a.Log.Errorf("AMQP connection failed: %s", err)
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
continue continue
} }
@ -272,14 +272,14 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err
p := rand.Perm(len(brokers)) p := rand.Perm(len(brokers))
for _, n := range p { for _, n := range p {
broker := brokers[n] broker := brokers[n]
log.Printf("D! [inputs.amqp_consumer] connecting to %q", broker) a.Log.Debugf("Connecting to %q", broker)
conn, err := amqp.DialConfig(broker, *amqpConf) conn, err := amqp.DialConfig(broker, *amqpConf)
if err == nil { if err == nil {
a.conn = conn a.conn = conn
log.Printf("D! [inputs.amqp_consumer] connected to %q", broker) a.Log.Debugf("Connected to %q", broker)
break break
} }
log.Printf("D! [inputs.amqp_consumer] error connecting to %q", broker) a.Log.Debugf("Error connecting to %q", broker)
} }
if a.conn == nil { if a.conn == nil {
@ -288,7 +288,7 @@ func (a *AMQPConsumer) connect(amqpConf *amqp.Config) (<-chan amqp.Delivery, err
ch, err := a.conn.Channel() ch, err := a.conn.Channel()
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to open a channel: %s", err) return nil, fmt.Errorf("Failed to open a channel: %s", err.Error())
} }
if a.Exchange != "" { if a.Exchange != "" {
@ -395,7 +395,7 @@ func declareExchange(
) )
} }
if err != nil { if err != nil {
return fmt.Errorf("error declaring exchange: %v", err) return fmt.Errorf("Error declaring exchange: %v", err)
} }
return nil return nil
} }
@ -437,7 +437,7 @@ func declareQueue(
) )
} }
if err != nil { if err != nil {
return nil, fmt.Errorf("error declaring queue: %v", err) return nil, fmt.Errorf("Error declaring queue: %v", err)
} }
return &queue, nil return &queue, nil
} }
@ -486,8 +486,7 @@ func (a *AMQPConsumer) onMessage(acc telegraf.TrackingAccumulator, d amqp.Delive
// this message. // this message.
rejErr := d.Ack(false) rejErr := d.Ack(false)
if rejErr != nil { if rejErr != nil {
log.Printf("E! [inputs.amqp_consumer] Unable to reject message: %d: %v", a.Log.Errorf("Unable to reject message: %d: %v", d.DeliveryTag, rejErr)
d.DeliveryTag, rejErr)
a.conn.Close() a.conn.Close()
} }
} }
@ -519,15 +518,13 @@ func (a *AMQPConsumer) onDelivery(track telegraf.DeliveryInfo) bool {
if track.Delivered() { if track.Delivered() {
err := delivery.Ack(false) err := delivery.Ack(false)
if err != nil { if err != nil {
log.Printf("E! [inputs.amqp_consumer] Unable to ack written delivery: %d: %v", a.Log.Errorf("Unable to ack written delivery: %d: %v", delivery.DeliveryTag, err)
delivery.DeliveryTag, err)
a.conn.Close() a.conn.Close()
} }
} else { } else {
err := delivery.Reject(false) err := delivery.Reject(false)
if err != nil { if err != nil {
log.Printf("E! [inputs.amqp_consumer] Unable to reject failed delivery: %d: %v", a.Log.Errorf("Unable to reject failed delivery: %d: %v", delivery.DeliveryTag, err)
delivery.DeliveryTag, err)
a.conn.Close() a.conn.Close()
} }
} }
@ -541,7 +538,7 @@ func (a *AMQPConsumer) Stop() {
a.wg.Wait() a.wg.Wait()
err := a.conn.Close() err := a.conn.Close()
if err != nil && err != amqp.ErrClosed { if err != nil && err != amqp.ErrClosed {
log.Printf("E! [inputs.amqp_consumer] Error closing AMQP connection: %s", err) a.Log.Errorf("Error closing AMQP connection: %s", err)
return return
} }
} }

View File

@ -1,9 +1,8 @@
package activemq package azure_storage_queue
import ( import (
"context" "context"
"errors" "errors"
"log"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -17,6 +16,7 @@ type AzureStorageQueue struct {
StorageAccountName string `toml:"account_name"` StorageAccountName string `toml:"account_name"`
StorageAccountKey string `toml:"account_key"` StorageAccountKey string `toml:"account_key"`
PeekOldestMessageAge bool `toml:"peek_oldest_message_age"` PeekOldestMessageAge bool `toml:"peek_oldest_message_age"`
Log telegraf.Logger
serviceURL *azqueue.ServiceURL serviceURL *azqueue.ServiceURL
} }
@ -92,7 +92,7 @@ func (a *AzureStorageQueue) Gather(acc telegraf.Accumulator) error {
ctx := context.TODO() ctx := context.TODO()
for marker := (azqueue.Marker{}); marker.NotDone(); { for marker := (azqueue.Marker{}); marker.NotDone(); {
log.Printf("D! [inputs.azure_storage_queue] Listing queues of storage account '%s'", a.StorageAccountName) a.Log.Debugf("Listing queues of storage account '%s'", a.StorageAccountName)
queuesSegment, err := serviceURL.ListQueuesSegment(ctx, marker, queuesSegment, err := serviceURL.ListQueuesSegment(ctx, marker,
azqueue.ListQueuesSegmentOptions{ azqueue.ListQueuesSegmentOptions{
Detail: azqueue.ListQueuesSegmentDetails{Metadata: false}, Detail: azqueue.ListQueuesSegmentDetails{Metadata: false},
@ -103,11 +103,11 @@ func (a *AzureStorageQueue) Gather(acc telegraf.Accumulator) error {
marker = queuesSegment.NextMarker marker = queuesSegment.NextMarker
for _, queueItem := range queuesSegment.QueueItems { for _, queueItem := range queuesSegment.QueueItems {
log.Printf("D! [inputs.azure_storage_queue] Processing queue '%s' of storage account '%s'", queueItem.Name, a.StorageAccountName) a.Log.Debugf("Processing queue '%s' of storage account '%s'", queueItem.Name, a.StorageAccountName)
queueURL := serviceURL.NewQueueURL(queueItem.Name) queueURL := serviceURL.NewQueueURL(queueItem.Name)
properties, err := queueURL.GetProperties(ctx) properties, err := queueURL.GetProperties(ctx)
if err != nil { if err != nil {
log.Printf("E! [inputs.azure_storage_queue] Error getting properties for queue %s: %s", queueItem.Name, err.Error()) a.Log.Errorf("Error getting properties for queue %s: %s", queueItem.Name, err.Error())
continue continue
} }
var peekedMessage *azqueue.PeekedMessage var peekedMessage *azqueue.PeekedMessage
@ -115,7 +115,7 @@ func (a *AzureStorageQueue) Gather(acc telegraf.Accumulator) error {
messagesURL := queueURL.NewMessagesURL() messagesURL := queueURL.NewMessagesURL()
messagesResponse, err := messagesURL.Peek(ctx, 1) messagesResponse, err := messagesURL.Peek(ctx, 1)
if err != nil { if err != nil {
log.Printf("E! [inputs.azure_storage_queue] Error peeking queue %s: %s", queueItem.Name, err.Error()) a.Log.Errorf("Error peeking queue %s: %s", queueItem.Name, err.Error())
} else if messagesResponse.NumMessages() > 0 { } else if messagesResponse.NumMessages() > 0 {
peekedMessage = messagesResponse.Message(0) peekedMessage = messagesResponse.Message(0)
} }

View File

@ -101,12 +101,12 @@ func (c *Ceph) gatherAdminSocketStats(acc telegraf.Accumulator) error {
for _, s := range sockets { for _, s := range sockets {
dump, err := perfDump(c.CephBinary, s) dump, err := perfDump(c.CephBinary, s)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("E! error reading from socket '%s': %v", s.socket, err)) acc.AddError(fmt.Errorf("error reading from socket '%s': %v", s.socket, err))
continue continue
} }
data, err := parseDump(dump) data, err := parseDump(dump)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("E! error parsing dump from socket '%s': %v", s.socket, err)) acc.AddError(fmt.Errorf("error parsing dump from socket '%s': %v", s.socket, err))
continue continue
} }
for tag, metrics := range data { for tag, metrics := range data {
@ -287,7 +287,7 @@ func flatten(data interface{}) []*metric {
} }
} }
default: default:
log.Printf("I! Ignoring unexpected type '%T' for value %v", val, val) log.Printf("I! [inputs.ceph] ignoring unexpected type '%T' for value %v", val, val)
} }
return metrics return metrics

View File

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"strings" "strings"
"sync" "sync"
@ -54,6 +53,8 @@ type CiscoTelemetryGNMI struct {
acc telegraf.Accumulator acc telegraf.Accumulator
cancel context.CancelFunc cancel context.CancelFunc
wg sync.WaitGroup wg sync.WaitGroup
Log telegraf.Logger
} }
// Subscription for a GNMI client // Subscription for a GNMI client
@ -211,8 +212,8 @@ func (c *CiscoTelemetryGNMI) subscribeGNMI(ctx context.Context, address string,
return fmt.Errorf("failed to send subscription request: %v", err) return fmt.Errorf("failed to send subscription request: %v", err)
} }
log.Printf("D! [inputs.cisco_telemetry_gnmi]: Connection to GNMI device %s established", address) c.Log.Debugf("Connection to GNMI device %s established", address)
defer log.Printf("D! [inputs.cisco_telemetry_gnmi]: Connection to GNMI device %s closed", address) defer c.Log.Debugf("Connection to GNMI device %s closed", address)
for ctx.Err() == nil { for ctx.Err() == nil {
var reply *gnmi.SubscribeResponse var reply *gnmi.SubscribeResponse
if reply, err = subscribeClient.Recv(); err != nil { if reply, err = subscribeClient.Recv(); err != nil {
@ -267,7 +268,7 @@ func (c *CiscoTelemetryGNMI) handleSubscribeResponse(address string, reply *gnmi
if alias, ok := c.aliases[aliasPath]; ok { if alias, ok := c.aliases[aliasPath]; ok {
name = alias name = alias
} else { } else {
log.Printf("D! [inputs.cisco_telemetry_gnmi]: No measurement alias for GNMI path: %s", name) c.Log.Debugf("No measurement alias for GNMI path: %s", name)
} }
} }

View File

@ -104,8 +104,10 @@ func TestGNMIError(t *testing.T) {
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 0, server: server, acc: acc}) gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 0, server: server, acc: acc})
c := &CiscoTelemetryGNMI{Addresses: []string{listener.Addr().String()}, c := &CiscoTelemetryGNMI{
Username: "theuser", Password: "thepassword", Encoding: "proto", Log: testutil.Logger{},
Addresses: []string{listener.Addr().String()},
Username: "theuser", Password: "thepassword", Encoding: "proto",
Redial: internal.Duration{Duration: 1 * time.Second}} Redial: internal.Duration{Duration: 1 * time.Second}}
require.NoError(t, c.Start(acc)) require.NoError(t, c.Start(acc))
@ -174,8 +176,10 @@ func TestGNMIMultiple(t *testing.T) {
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 1, server: server, acc: acc}) gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 1, server: server, acc: acc})
c := &CiscoTelemetryGNMI{Addresses: []string{listener.Addr().String()}, c := &CiscoTelemetryGNMI{
Username: "theuser", Password: "thepassword", Encoding: "proto", Log: testutil.Logger{},
Addresses: []string{listener.Addr().String()},
Username: "theuser", Password: "thepassword", Encoding: "proto",
Redial: internal.Duration{Duration: 1 * time.Second}, Redial: internal.Duration{Duration: 1 * time.Second},
Subscriptions: []Subscription{{Name: "alias", Origin: "type", Path: "/model", SubscriptionMode: "sample"}}, Subscriptions: []Subscription{{Name: "alias", Origin: "type", Path: "/model", SubscriptionMode: "sample"}},
} }
@ -215,8 +219,10 @@ func TestGNMIMultipleRedial(t *testing.T) {
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 2, server: server, acc: acc}) gnmi.RegisterGNMIServer(server, &mockGNMIServer{t: t, scenario: 2, server: server, acc: acc})
c := &CiscoTelemetryGNMI{Addresses: []string{listener.Addr().String()}, c := &CiscoTelemetryGNMI{
Username: "theuser", Password: "thepassword", Encoding: "proto", Log: testutil.Logger{},
Addresses: []string{listener.Addr().String()},
Username: "theuser", Password: "thepassword", Encoding: "proto",
Redial: internal.Duration{Duration: 10 * time.Millisecond}, Redial: internal.Duration{Duration: 10 * time.Millisecond},
Subscriptions: []Subscription{{Name: "alias", Origin: "type", Path: "/model", SubscriptionMode: "sample"}}, Subscriptions: []Subscription{{Name: "alias", Origin: "type", Path: "/model", SubscriptionMode: "sample"}},
} }

View File

@ -5,7 +5,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"path" "path"
"strconv" "strconv"
@ -43,6 +42,8 @@ type CiscoTelemetryMDT struct {
Aliases map[string]string `toml:"aliases"` Aliases map[string]string `toml:"aliases"`
EmbeddedTags []string `toml:"embedded_tags"` EmbeddedTags []string `toml:"embedded_tags"`
Log telegraf.Logger
// GRPC TLS settings // GRPC TLS settings
internaltls.ServerConfig internaltls.ServerConfig
@ -146,11 +147,11 @@ func (c *CiscoTelemetryMDT) acceptTCPClients() {
// Individual client connection routine // Individual client connection routine
c.wg.Add(1) c.wg.Add(1)
go func() { go func() {
log.Printf("D! [inputs.cisco_telemetry_mdt]: Accepted Cisco MDT TCP dialout connection from %s", conn.RemoteAddr()) c.Log.Debugf("Accepted Cisco MDT TCP dialout connection from %s", conn.RemoteAddr())
if err := c.handleTCPClient(conn); err != nil { if err := c.handleTCPClient(conn); err != nil {
c.acc.AddError(err) c.acc.AddError(err)
} }
log.Printf("D! [inputs.cisco_telemetry_mdt]: Closed Cisco MDT TCP dialout connection from %s", conn.RemoteAddr()) c.Log.Debugf("Closed Cisco MDT TCP dialout connection from %s", conn.RemoteAddr())
mutex.Lock() mutex.Lock()
delete(clients, conn) delete(clients, conn)
@ -165,7 +166,7 @@ func (c *CiscoTelemetryMDT) acceptTCPClients() {
mutex.Lock() mutex.Lock()
for client := range clients { for client := range clients {
if err := client.Close(); err != nil { if err := client.Close(); err != nil {
log.Printf("E! [inputs.cisco_telemetry_mdt]: Failed to close TCP dialout client: %v", err) c.Log.Errorf("Failed to close TCP dialout client: %v", err)
} }
} }
mutex.Unlock() mutex.Unlock()
@ -218,7 +219,7 @@ func (c *CiscoTelemetryMDT) handleTCPClient(conn net.Conn) error {
func (c *CiscoTelemetryMDT) MdtDialout(stream dialout.GRPCMdtDialout_MdtDialoutServer) error { func (c *CiscoTelemetryMDT) MdtDialout(stream dialout.GRPCMdtDialout_MdtDialoutServer) error {
peer, peerOK := peer.FromContext(stream.Context()) peer, peerOK := peer.FromContext(stream.Context())
if peerOK { if peerOK {
log.Printf("D! [inputs.cisco_telemetry_mdt]: Accepted Cisco MDT GRPC dialout connection from %s", peer.Addr) c.Log.Debugf("Accepted Cisco MDT GRPC dialout connection from %s", peer.Addr)
} }
var chunkBuffer bytes.Buffer var chunkBuffer bytes.Buffer
@ -252,7 +253,7 @@ func (c *CiscoTelemetryMDT) MdtDialout(stream dialout.GRPCMdtDialout_MdtDialoutS
} }
if peerOK { if peerOK {
log.Printf("D! [inputs.cisco_telemetry_mdt]: Closed Cisco MDT GRPC dialout connection from %s", peer.Addr) c.Log.Debugf("Closed Cisco MDT GRPC dialout connection from %s", peer.Addr)
} }
return nil return nil
@ -291,7 +292,7 @@ func (c *CiscoTelemetryMDT) handleTelemetry(data []byte) {
} }
if keys == nil || content == nil { if keys == nil || content == nil {
log.Printf("I! [inputs.cisco_telemetry_mdt]: Message from %s missing keys or content", msg.GetNodeIdStr()) c.Log.Infof("Message from %s missing keys or content", msg.GetNodeIdStr())
continue continue
} }
@ -412,7 +413,7 @@ func (c *CiscoTelemetryMDT) parseContentField(grouper *metric.SeriesGrouper, fie
} else { } else {
c.mutex.Lock() c.mutex.Lock()
if _, haveWarned := c.warned[path]; !haveWarned { if _, haveWarned := c.warned[path]; !haveWarned {
log.Printf("D! [inputs.cisco_telemetry_mdt]: No measurement alias for encoding path: %s", path) c.Log.Debugf("No measurement alias for encoding path: %s", path)
c.warned[path] = struct{}{} c.warned[path] = struct{}{}
} }
c.mutex.Unlock() c.mutex.Unlock()

View File

@ -18,7 +18,7 @@ import (
) )
func TestHandleTelemetryTwoSimple(t *testing.T) { func TestHandleTelemetryTwoSimple(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "dummy", Aliases: map[string]string{"alias": "type:model/some/path"}} c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "dummy", Aliases: map[string]string{"alias": "type:model/some/path"}}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
c.Start(acc) c.Start(acc)
@ -93,7 +93,7 @@ func TestHandleTelemetryTwoSimple(t *testing.T) {
} }
func TestHandleTelemetrySingleNested(t *testing.T) { func TestHandleTelemetrySingleNested(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "dummy", Aliases: map[string]string{"nested": "type:model/nested/path"}} c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "dummy", Aliases: map[string]string{"nested": "type:model/nested/path"}}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
c.Start(acc) c.Start(acc)
@ -385,7 +385,7 @@ func TestHandleNXDME(t *testing.T) {
} }
func TestTCPDialoutOverflow(t *testing.T) { func TestTCPDialoutOverflow(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "tcp", ServiceAddress: "127.0.0.1:57000"} c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "tcp", ServiceAddress: "127.0.0.1:57000"}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
assert.Nil(t, c.Start(acc)) assert.Nil(t, c.Start(acc))
@ -441,7 +441,7 @@ func mockTelemetryMessage() *telemetry.Telemetry {
} }
func TestTCPDialoutMultiple(t *testing.T) { func TestTCPDialoutMultiple(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "tcp", ServiceAddress: "127.0.0.1:57000", Aliases: map[string]string{ c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "tcp", ServiceAddress: "127.0.0.1:57000", Aliases: map[string]string{
"some": "type:model/some/path", "parallel": "type:model/parallel/path", "other": "type:model/other/path"}} "some": "type:model/some/path", "parallel": "type:model/parallel/path", "other": "type:model/other/path"}}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
assert.Nil(t, c.Start(acc)) assert.Nil(t, c.Start(acc))
@ -500,7 +500,7 @@ func TestTCPDialoutMultiple(t *testing.T) {
} }
func TestGRPCDialoutError(t *testing.T) { func TestGRPCDialoutError(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "grpc", ServiceAddress: "127.0.0.1:57001"} c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "grpc", ServiceAddress: "127.0.0.1:57001"}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
assert.Nil(t, c.Start(acc)) assert.Nil(t, c.Start(acc))
@ -519,7 +519,7 @@ func TestGRPCDialoutError(t *testing.T) {
} }
func TestGRPCDialoutMultiple(t *testing.T) { func TestGRPCDialoutMultiple(t *testing.T) {
c := &CiscoTelemetryMDT{Transport: "grpc", ServiceAddress: "127.0.0.1:57001", Aliases: map[string]string{ c := &CiscoTelemetryMDT{Log: testutil.Logger{}, Transport: "grpc", ServiceAddress: "127.0.0.1:57001", Aliases: map[string]string{
"some": "type:model/some/path", "parallel": "type:model/parallel/path", "other": "type:model/other/path"}} "some": "type:model/some/path", "parallel": "type:model/parallel/path", "other": "type:model/other/path"}}
acc := &testutil.Accumulator{} acc := &testutil.Accumulator{}
assert.Nil(t, c.Start(acc)) assert.Nil(t, c.Start(acc))

View File

@ -5,16 +5,16 @@ import (
"fmt" "fmt"
"sync" "sync"
"cloud.google.com/go/pubsub"
"encoding/base64" "encoding/base64"
"time"
"cloud.google.com/go/pubsub"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
"google.golang.org/api/option" "google.golang.org/api/option"
"log"
"time"
) )
type empty struct{} type empty struct{}
@ -43,6 +43,8 @@ type PubSub struct {
Base64Data bool `toml:"base64_data"` Base64Data bool `toml:"base64_data"`
Log telegraf.Logger
sub subscription sub subscription
stubSub func() subscription stubSub func() subscription
@ -134,14 +136,14 @@ func (ps *PubSub) receiveWithRetry(parentCtx context.Context) {
err := ps.startReceiver(parentCtx) err := ps.startReceiver(parentCtx)
for err != nil && parentCtx.Err() == nil { for err != nil && parentCtx.Err() == nil {
log.Printf("E! [inputs.cloud_pubsub] Receiver for subscription %s exited with error: %v", ps.sub.ID(), err) ps.Log.Errorf("Receiver for subscription %s exited with error: %v", ps.sub.ID(), err)
delay := defaultRetryDelaySeconds delay := defaultRetryDelaySeconds
if ps.RetryReceiveDelaySeconds > 0 { if ps.RetryReceiveDelaySeconds > 0 {
delay = ps.RetryReceiveDelaySeconds delay = ps.RetryReceiveDelaySeconds
} }
log.Printf("I! [inputs.cloud_pubsub] Waiting %d seconds before attempting to restart receiver...", delay) ps.Log.Infof("Waiting %d seconds before attempting to restart receiver...", delay)
time.Sleep(time.Duration(delay) * time.Second) time.Sleep(time.Duration(delay) * time.Second)
err = ps.startReceiver(parentCtx) err = ps.startReceiver(parentCtx)
@ -149,7 +151,7 @@ func (ps *PubSub) receiveWithRetry(parentCtx context.Context) {
} }
func (ps *PubSub) startReceiver(parentCtx context.Context) error { func (ps *PubSub) startReceiver(parentCtx context.Context) error {
log.Printf("I! [inputs.cloud_pubsub] Starting receiver for subscription %s...", ps.sub.ID()) ps.Log.Infof("Starting receiver for subscription %s...", ps.sub.ID())
cctx, ccancel := context.WithCancel(parentCtx) cctx, ccancel := context.WithCancel(parentCtx)
err := ps.sub.Receive(cctx, func(ctx context.Context, msg message) { err := ps.sub.Receive(cctx, func(ctx context.Context, msg message) {
if err := ps.onMessage(ctx, msg); err != nil { if err := ps.onMessage(ctx, msg); err != nil {
@ -159,7 +161,7 @@ func (ps *PubSub) startReceiver(parentCtx context.Context) error {
if err != nil { if err != nil {
ps.acc.AddError(fmt.Errorf("receiver for subscription %s exited: %v", ps.sub.ID(), err)) ps.acc.AddError(fmt.Errorf("receiver for subscription %s exited: %v", ps.sub.ID(), err))
} else { } else {
log.Printf("I! [inputs.cloud_pubsub] subscription pull ended (no error, most likely stopped)") ps.Log.Info("Subscription pull ended (no error, most likely stopped)")
} }
ccancel() ccancel()
return err return err

View File

@ -3,10 +3,11 @@ package cloud_pubsub
import ( import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"testing"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"testing"
) )
const ( const (
@ -26,6 +27,7 @@ func TestRunParse(t *testing.T) {
sub.receiver = testMessagesReceive(sub) sub.receiver = testMessagesReceive(sub)
ps := &PubSub{ ps := &PubSub{
Log: testutil.Logger{},
parser: testParser, parser: testParser,
stubSub: func() subscription { return sub }, stubSub: func() subscription { return sub },
Project: "projectIDontMatterForTests", Project: "projectIDontMatterForTests",
@ -69,6 +71,7 @@ func TestRunBase64(t *testing.T) {
sub.receiver = testMessagesReceive(sub) sub.receiver = testMessagesReceive(sub)
ps := &PubSub{ ps := &PubSub{
Log: testutil.Logger{},
parser: testParser, parser: testParser,
stubSub: func() subscription { return sub }, stubSub: func() subscription { return sub },
Project: "projectIDontMatterForTests", Project: "projectIDontMatterForTests",
@ -112,6 +115,7 @@ func TestRunInvalidMessages(t *testing.T) {
sub.receiver = testMessagesReceive(sub) sub.receiver = testMessagesReceive(sub)
ps := &PubSub{ ps := &PubSub{
Log: testutil.Logger{},
parser: testParser, parser: testParser,
stubSub: func() subscription { return sub }, stubSub: func() subscription { return sub },
Project: "projectIDontMatterForTests", Project: "projectIDontMatterForTests",
@ -158,6 +162,7 @@ func TestRunOverlongMessages(t *testing.T) {
sub.receiver = testMessagesReceive(sub) sub.receiver = testMessagesReceive(sub)
ps := &PubSub{ ps := &PubSub{
Log: testutil.Logger{},
parser: testParser, parser: testParser,
stubSub: func() subscription { return sub }, stubSub: func() subscription { return sub },
Project: "projectIDontMatterForTests", Project: "projectIDontMatterForTests",
@ -205,6 +210,7 @@ func TestRunErrorInSubscriber(t *testing.T) {
sub.receiver = testMessagesError(sub, errors.New("a fake error")) sub.receiver = testMessagesError(sub, errors.New("a fake error"))
ps := &PubSub{ ps := &PubSub{
Log: testutil.Logger{},
parser: testParser, parser: testParser,
stubSub: func() subscription { return sub }, stubSub: func() subscription { return sub },
Project: "projectIDontMatterForTests", Project: "projectIDontMatterForTests",

View File

@ -6,7 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"sync" "sync"
@ -33,6 +32,7 @@ type PubSubPush struct {
WriteTimeout internal.Duration WriteTimeout internal.Duration
MaxBodySize internal.Size MaxBodySize internal.Size
AddMeta bool AddMeta bool
Log telegraf.Logger
MaxUndeliveredMessages int `toml:"max_undelivered_messages"` MaxUndeliveredMessages int `toml:"max_undelivered_messages"`
@ -227,21 +227,21 @@ func (p *PubSubPush) serveWrite(res http.ResponseWriter, req *http.Request) {
var payload Payload var payload Payload
if err = json.Unmarshal(bytes, &payload); err != nil { if err = json.Unmarshal(bytes, &payload); err != nil {
log.Printf("E! [inputs.cloud_pubsub_push] Error decoding payload %s", err.Error()) p.Log.Errorf("Error decoding payload %s", err.Error())
res.WriteHeader(http.StatusBadRequest) res.WriteHeader(http.StatusBadRequest)
return return
} }
sDec, err := base64.StdEncoding.DecodeString(payload.Msg.Data) sDec, err := base64.StdEncoding.DecodeString(payload.Msg.Data)
if err != nil { if err != nil {
log.Printf("E! [inputs.cloud_pubsub_push] Base64-Decode Failed %s", err.Error()) p.Log.Errorf("Base64-decode failed %s", err.Error())
res.WriteHeader(http.StatusBadRequest) res.WriteHeader(http.StatusBadRequest)
return return
} }
metrics, err := p.Parse(sDec) metrics, err := p.Parse(sDec)
if err != nil { if err != nil {
log.Println("D! [inputs.cloud_pubsub_push] " + err.Error()) p.Log.Debug(err.Error())
res.WriteHeader(http.StatusBadRequest) res.WriteHeader(http.StatusBadRequest)
return return
} }
@ -295,7 +295,7 @@ func (p *PubSubPush) receiveDelivered() {
ch <- true ch <- true
} else { } else {
ch <- false ch <- false
log.Println("D! [inputs.cloud_pubsub_push] Metric group failed to process") p.Log.Debug("Metric group failed to process")
} }
} }
} }

View File

@ -18,6 +18,7 @@ import (
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/models" "github.com/influxdata/telegraf/internal/models"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
"github.com/influxdata/telegraf/testutil"
) )
func TestServeHTTP(t *testing.T) { func TestServeHTTP(t *testing.T) {
@ -118,6 +119,7 @@ func TestServeHTTP(t *testing.T) {
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
pubPush := &PubSubPush{ pubPush := &PubSubPush{
Log: testutil.Logger{},
Path: "/", Path: "/",
MaxBodySize: internal.Size{ MaxBodySize: internal.Size{
Size: test.maxsize, Size: test.maxsize,

View File

@ -2,7 +2,6 @@ package diskio
import ( import (
"fmt" "fmt"
"log"
"regexp" "regexp"
"strings" "strings"
@ -24,6 +23,8 @@ type DiskIO struct {
NameTemplates []string NameTemplates []string
SkipSerialNumber bool SkipSerialNumber bool
Log telegraf.Logger
infoCache map[string]diskInfoCache infoCache map[string]diskInfoCache
deviceFilter filter.Filter deviceFilter filter.Filter
initialized bool initialized bool
@ -75,7 +76,7 @@ func (s *DiskIO) init() error {
if hasMeta(device) { if hasMeta(device) {
filter, err := filter.Compile(s.Devices) filter, err := filter.Compile(s.Devices)
if err != nil { if err != nil {
return fmt.Errorf("error compiling device pattern: %v", err) return fmt.Errorf("error compiling device pattern: %s", err.Error())
} }
s.deviceFilter = filter s.deviceFilter = filter
} }
@ -99,7 +100,7 @@ func (s *DiskIO) Gather(acc telegraf.Accumulator) error {
diskio, err := s.ps.DiskIO(devices) diskio, err := s.ps.DiskIO(devices)
if err != nil { if err != nil {
return fmt.Errorf("error getting disk io info: %s", err) return fmt.Errorf("error getting disk io info: %s", err.Error())
} }
for _, io := range diskio { for _, io := range diskio {
@ -166,7 +167,7 @@ func (s *DiskIO) diskName(devName string) (string, []string) {
} }
if err != nil { if err != nil {
log.Printf("W! Error gathering disk info: %s", err) s.Log.Warnf("Error gathering disk info: %s", err)
return devName, devLinks return devName, devLinks
} }
@ -199,7 +200,7 @@ func (s *DiskIO) diskTags(devName string) map[string]string {
di, err := s.diskInfo(devName) di, err := s.diskInfo(devName)
if err != nil { if err != nil {
log.Printf("W! Error gathering disk info: %s", err) s.Log.Warnf("Error gathering disk info: %s", err)
return nil return nil
} }

View File

@ -103,6 +103,7 @@ func TestDiskIO(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
diskio := &DiskIO{ diskio := &DiskIO{
Log: testutil.Logger{},
ps: &mps, ps: &mps,
Devices: tt.devices, Devices: tt.devices,
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strconv"
@ -45,6 +44,8 @@ type Docker struct {
ContainerStateInclude []string `toml:"container_state_include"` ContainerStateInclude []string `toml:"container_state_include"`
ContainerStateExclude []string `toml:"container_state_exclude"` ContainerStateExclude []string `toml:"container_state_exclude"`
Log telegraf.Logger
tlsint.ClientConfig tlsint.ClientConfig
newEnvClient func() (Client, error) newEnvClient func() (Client, error)
@ -107,8 +108,10 @@ var sampleConfig = `
## Whether to report for each container per-device blkio (8:0, 8:1...) and ## Whether to report for each container per-device blkio (8:0, 8:1...) and
## network (eth0, eth1, ...) stats or not ## network (eth0, eth1, ...) stats or not
perdevice = true perdevice = true
## Whether to report for each container total blkio and network stats or not ## Whether to report for each container total blkio and network stats or not
total = false total = false
## Which environment variables should we use as a tag ## Which environment variables should we use as a tag
##tag_env = ["JAVA_HOME", "HEAP_SIZE"] ##tag_env = ["JAVA_HOME", "HEAP_SIZE"]
@ -274,7 +277,7 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error {
fields["tasks_running"] = running[service.ID] fields["tasks_running"] = running[service.ID]
fields["tasks_desired"] = tasksNoShutdown[service.ID] fields["tasks_desired"] = tasksNoShutdown[service.ID]
} else { } else {
log.Printf("E! Unknow Replicas Mode") d.Log.Error("Unknown replica mode")
} }
// Add metrics // Add metrics
acc.AddFields("docker_swarm", acc.AddFields("docker_swarm",

View File

@ -252,6 +252,7 @@ func TestDocker_WindowsMemoryContainerStats(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: func(string, *tls.Config) (Client, error) { newClient: func(string, *tls.Config) (Client, error) {
return &MockClient{ return &MockClient{
InfoF: func(ctx context.Context) (types.Info, error) { InfoF: func(ctx context.Context) (types.Info, error) {
@ -390,6 +391,7 @@ func TestContainerLabels(t *testing.T) {
} }
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: newClientFunc, newClient: newClientFunc,
LabelInclude: tt.include, LabelInclude: tt.include,
LabelExclude: tt.exclude, LabelExclude: tt.exclude,
@ -511,6 +513,7 @@ func TestContainerNames(t *testing.T) {
} }
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: newClientFunc, newClient: newClientFunc,
ContainerInclude: tt.include, ContainerInclude: tt.include,
ContainerExclude: tt.exclude, ContainerExclude: tt.exclude,
@ -625,7 +628,10 @@ func TestContainerStatus(t *testing.T) {
return &client, nil return &client, nil
} }
d = Docker{newClient: newClientFunc} d = Docker{
Log: testutil.Logger{},
newClient: newClientFunc,
}
) )
// mock time // mock time
@ -675,6 +681,7 @@ func TestContainerStatus(t *testing.T) {
func TestDockerGatherInfo(t *testing.T) { func TestDockerGatherInfo(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: newClient, newClient: newClient,
TagEnvironment: []string{"ENVVAR1", "ENVVAR2", "ENVVAR3", "ENVVAR5", TagEnvironment: []string{"ENVVAR1", "ENVVAR2", "ENVVAR3", "ENVVAR5",
"ENVVAR6", "ENVVAR7", "ENVVAR8", "ENVVAR9"}, "ENVVAR6", "ENVVAR7", "ENVVAR8", "ENVVAR9"},
@ -824,6 +831,7 @@ func TestDockerGatherInfo(t *testing.T) {
func TestDockerGatherSwarmInfo(t *testing.T) { func TestDockerGatherSwarmInfo(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: newClient, newClient: newClient,
} }
@ -931,6 +939,7 @@ func TestContainerStateFilter(t *testing.T) {
} }
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: newClientFunc, newClient: newClientFunc,
ContainerStateInclude: tt.include, ContainerStateInclude: tt.include,
ContainerStateExclude: tt.exclude, ContainerStateExclude: tt.exclude,
@ -992,6 +1001,7 @@ func TestContainerName(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
d := Docker{ d := Docker{
Log: testutil.Logger{},
newClient: tt.clientFunc, newClient: tt.clientFunc,
} }
var acc testutil.Accumulator var acc testutil.Accumulator

View File

@ -17,8 +17,10 @@ the [upgrading steps][upgrading].
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.
servers = ["localhost:24242"] servers = ["localhost:24242"]
## Type is one of "user", "domain", "ip", or "global" ## Type is one of "user", "domain", "ip", or "global"
type = "global" type = "global"
## Wildcard matches like "*.com". An empty string "" is same as "*" ## Wildcard matches like "*.com". An empty string "" is same as "*"
## If type = "ip" filters should be <IP/network> ## If type = "ip" filters should be <IP/network>
filters = [""] filters = [""]

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
// "log"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@ -32,8 +31,10 @@ var sampleConfig = `
## ##
## If no servers are specified, then localhost is used as the host. ## If no servers are specified, then localhost is used as the host.
servers = ["localhost:24242"] servers = ["localhost:24242"]
## Type is one of "user", "domain", "ip", or "global" ## Type is one of "user", "domain", "ip", or "global"
type = "global" type = "global"
## Wildcard matches like "*.com". An empty string "" is same as "*" ## Wildcard matches like "*.com". An empty string "" is same as "*"
## If type = "ip" filters should be <IP/network> ## If type = "ip" filters should be <IP/network>
filters = [""] filters = [""]
@ -82,12 +83,12 @@ func (d *Dovecot) Gather(acc telegraf.Accumulator) error {
func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype string, filter string) error { func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype string, filter string) error {
_, _, err := net.SplitHostPort(addr) _, _, err := net.SplitHostPort(addr)
if err != nil { if err != nil {
return fmt.Errorf("Error: %s on url %s\n", err, addr) return fmt.Errorf("%q on url %s", err.Error(), addr)
} }
c, err := net.DialTimeout("tcp", addr, defaultTimeout) c, err := net.DialTimeout("tcp", addr, defaultTimeout)
if err != nil { if err != nil {
return fmt.Errorf("Unable to connect to dovecot server '%s': %s", addr, err) return fmt.Errorf("enable to connect to dovecot server '%s': %s", addr, err)
} }
defer c.Close() defer c.Close()

View File

@ -161,7 +161,7 @@ func (e *Exec) ProcessCommand(command string, acc telegraf.Accumulator, wg *sync
if isNagios { if isNagios {
metrics, err = nagios.TryAddState(runErr, metrics) metrics, err = nagios.TryAddState(runErr, metrics)
if err != nil { if err != nil {
e.log.Errorf("failed to add nagios state: %s", err) e.log.Errorf("Failed to add nagios state: %s", err)
} }
} }

View File

@ -1,7 +1,6 @@
package filecount package filecount
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -59,6 +58,7 @@ type FileCount struct {
fileFilters []fileFilterFunc fileFilters []fileFilterFunc
globPaths []globpath.GlobPath globPaths []globpath.GlobPath
Fs fileSystem Fs fileSystem
Log telegraf.Logger
} }
func (_ *FileCount) Description() string { func (_ *FileCount) Description() string {
@ -210,7 +210,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
Unsorted: true, Unsorted: true,
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction { ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
if os.IsPermission(errors.Cause(err)) { if os.IsPermission(errors.Cause(err)) {
log.Println("D! [inputs.filecount]", err) fc.Log.Debug(err)
return godirwalk.SkipNode return godirwalk.SkipNode
} }
return godirwalk.Halt return godirwalk.Halt

View File

@ -152,6 +152,7 @@ func TestDirectoryWithTrailingSlash(t *testing.T) {
func getNoFilterFileCount() FileCount { func getNoFilterFileCount() FileCount {
return FileCount{ return FileCount{
Log: testutil.Logger{},
Directories: []string{getTestdataDir()}, Directories: []string{getTestdataDir()},
Name: "*", Name: "*",
Recursive: true, Recursive: true,

View File

@ -11,6 +11,7 @@ The filestat plugin gathers metrics about file existence, size, and other stats.
## These accept standard unix glob matching rules, but with the addition of ## These accept standard unix glob matching rules, but with the addition of
## ** as a "super asterisk". See https://github.com/gobwas/glob. ## ** as a "super asterisk". See https://github.com/gobwas/glob.
files = ["/etc/telegraf/telegraf.conf", "/var/log/**.log"] files = ["/etc/telegraf/telegraf.conf", "/var/log/**.log"]
## If true, read the entire file and calculate an md5 checksum. ## If true, read the entire file and calculate an md5 checksum.
md5 = false md5 = false
``` ```

View File

@ -4,7 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -23,6 +22,7 @@ const sampleConfig = `
## See https://github.com/gobwas/glob for more examples ## See https://github.com/gobwas/glob for more examples
## ##
files = ["/var/log/**.log"] files = ["/var/log/**.log"]
## If true, read the entire file and calculate an md5 checksum. ## If true, read the entire file and calculate an md5 checksum.
md5 = false md5 = false
` `
@ -31,6 +31,8 @@ type FileStat struct {
Md5 bool Md5 bool
Files []string Files []string
Log telegraf.Logger
// maps full file paths to globmatch obj // maps full file paths to globmatch obj
globs map[string]*globpath.GlobPath globs map[string]*globpath.GlobPath
} }
@ -41,11 +43,11 @@ func NewFileStat() *FileStat {
} }
} }
func (_ *FileStat) Description() string { func (*FileStat) Description() string {
return "Read stats about given file(s)" return "Read stats about given file(s)"
} }
func (_ *FileStat) SampleConfig() string { return sampleConfig } func (*FileStat) SampleConfig() string { return sampleConfig }
func (f *FileStat) Gather(acc telegraf.Accumulator) error { func (f *FileStat) Gather(acc telegraf.Accumulator) error {
var err error var err error
@ -86,7 +88,7 @@ func (f *FileStat) Gather(acc telegraf.Accumulator) error {
} }
if fileInfo == nil { if fileInfo == nil {
log.Printf("E! Unable to get info for file [%s], possible permissions issue", f.Log.Errorf("Unable to get info for file %q, possible permissions issue",
fileName) fileName)
} else { } else {
fields["size_bytes"] = fileInfo.Size() fields["size_bytes"] = fileInfo.Size()

View File

@ -14,6 +14,7 @@ import (
func TestGatherNoMd5(t *testing.T) { func TestGatherNoMd5(t *testing.T) {
dir := getTestdataDir() dir := getTestdataDir()
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{ fs.Files = []string{
dir + "log1.log", dir + "log1.log",
dir + "log2.log", dir + "log2.log",
@ -44,6 +45,7 @@ func TestGatherNoMd5(t *testing.T) {
func TestGatherExplicitFiles(t *testing.T) { func TestGatherExplicitFiles(t *testing.T) {
dir := getTestdataDir() dir := getTestdataDir()
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true fs.Md5 = true
fs.Files = []string{ fs.Files = []string{
dir + "log1.log", dir + "log1.log",
@ -77,6 +79,7 @@ func TestGatherExplicitFiles(t *testing.T) {
func TestGatherGlob(t *testing.T) { func TestGatherGlob(t *testing.T) {
dir := getTestdataDir() dir := getTestdataDir()
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true fs.Md5 = true
fs.Files = []string{ fs.Files = []string{
dir + "*.log", dir + "*.log",
@ -103,6 +106,7 @@ func TestGatherGlob(t *testing.T) {
func TestGatherSuperAsterisk(t *testing.T) { func TestGatherSuperAsterisk(t *testing.T) {
dir := getTestdataDir() dir := getTestdataDir()
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Md5 = true fs.Md5 = true
fs.Files = []string{ fs.Files = []string{
dir + "**", dir + "**",
@ -136,6 +140,7 @@ func TestGatherSuperAsterisk(t *testing.T) {
func TestModificationTime(t *testing.T) { func TestModificationTime(t *testing.T) {
dir := getTestdataDir() dir := getTestdataDir()
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{ fs.Files = []string{
dir + "log1.log", dir + "log1.log",
} }
@ -153,6 +158,7 @@ func TestModificationTime(t *testing.T) {
func TestNoModificationTime(t *testing.T) { func TestNoModificationTime(t *testing.T) {
fs := NewFileStat() fs := NewFileStat()
fs.Log = testutil.Logger{}
fs.Files = []string{ fs.Files = []string{
"/non/existant/file", "/non/existant/file",
} }

View File

@ -5,7 +5,6 @@ import (
"crypto/subtle" "crypto/subtle"
"crypto/tls" "crypto/tls"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -48,6 +47,7 @@ type HTTPListenerV2 struct {
tlsint.ServerConfig tlsint.ServerConfig
TimeFunc TimeFunc
Log telegraf.Logger
wg sync.WaitGroup wg sync.WaitGroup
@ -162,7 +162,7 @@ func (h *HTTPListenerV2) Start(acc telegraf.Accumulator) error {
server.Serve(h.listener) server.Serve(h.listener)
}() }()
log.Printf("I! [inputs.http_listener_v2] Listening on %s", listener.Addr().String()) h.Log.Infof("Listening on %s", listener.Addr().String())
return nil return nil
} }
@ -219,7 +219,7 @@ func (h *HTTPListenerV2) serveWrite(res http.ResponseWriter, req *http.Request)
metrics, err := h.Parse(bytes) metrics, err := h.Parse(bytes)
if err != nil { if err != nil {
log.Printf("D! [inputs.http_listener_v2] Parse error: %v", err) h.Log.Debugf("Parse error: %s", err.Error())
badRequest(res) badRequest(res)
return return
} }
@ -239,7 +239,7 @@ func (h *HTTPListenerV2) collectBody(res http.ResponseWriter, req *http.Request)
var err error var err error
body, err = gzip.NewReader(req.Body) body, err = gzip.NewReader(req.Body)
if err != nil { if err != nil {
log.Println("D! " + err.Error()) h.Log.Debug(err.Error())
badRequest(res) badRequest(res)
return nil, false return nil, false
} }
@ -261,7 +261,7 @@ func (h *HTTPListenerV2) collectQuery(res http.ResponseWriter, req *http.Request
query, err := url.QueryUnescape(rawQuery) query, err := url.QueryUnescape(rawQuery)
if err != nil { if err != nil {
log.Printf("D! [inputs.http_listener_v2] Error parsing query: %v", err) h.Log.Debugf("Error parsing query: %s", err.Error())
badRequest(res) badRequest(res)
return nil, false return nil, false
} }

View File

@ -46,6 +46,7 @@ func newTestHTTPListenerV2() *HTTPListenerV2 {
parser, _ := parsers.NewInfluxParser() parser, _ := parsers.NewInfluxParser()
listener := &HTTPListenerV2{ listener := &HTTPListenerV2{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
Path: "/write", Path: "/write",
Methods: []string{"POST"}, Methods: []string{"POST"},
@ -68,6 +69,7 @@ func newTestHTTPSListenerV2() *HTTPListenerV2 {
parser, _ := parsers.NewInfluxParser() parser, _ := parsers.NewInfluxParser()
listener := &HTTPListenerV2{ listener := &HTTPListenerV2{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
Path: "/write", Path: "/write",
Methods: []string{"POST"}, Methods: []string{"POST"},
@ -231,6 +233,7 @@ func TestWriteHTTPExactMaxBodySize(t *testing.T) {
parser, _ := parsers.NewInfluxParser() parser, _ := parsers.NewInfluxParser()
listener := &HTTPListenerV2{ listener := &HTTPListenerV2{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
Path: "/write", Path: "/write",
Methods: []string{"POST"}, Methods: []string{"POST"},
@ -253,6 +256,7 @@ func TestWriteHTTPVerySmallMaxBody(t *testing.T) {
parser, _ := parsers.NewInfluxParser() parser, _ := parsers.NewInfluxParser()
listener := &HTTPListenerV2{ listener := &HTTPListenerV2{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
Path: "/write", Path: "/write",
Methods: []string{"POST"}, Methods: []string{"POST"},

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -34,6 +33,8 @@ type HTTPResponse struct {
Interface string Interface string
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
compiledStringMatch *regexp.Regexp compiledStringMatch *regexp.Regexp
client *http.Client client *http.Client
} }
@ -242,7 +243,7 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]
// HTTP error codes do not generate errors in the net/http library // HTTP error codes do not generate errors in the net/http library
if err != nil { if err != nil {
// Log error // Log error
log.Printf("D! Network error while polling %s: %s", u, err.Error()) h.Log.Debugf("Network error while polling %s: %s", u, err.Error())
// Get error details // Get error details
netErr := setError(err, fields, tags) netErr := setError(err, fields, tags)
@ -271,7 +272,7 @@ func (h *HTTPResponse) httpGather(u string) (map[string]interface{}, map[string]
bodyBytes, err := ioutil.ReadAll(resp.Body) bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Printf("D! Failed to read body of HTTP Response : %s", err) h.Log.Debugf("Failed to read body of HTTP Response : %s", err.Error())
setResult("body_read_error", fields, tags) setResult("body_read_error", fields, tags)
fields["content_length"] = len(bodyBytes) fields["content_length"] = len(bodyBytes)
if h.ResponseStringMatch != "" { if h.ResponseStringMatch != "" {
@ -322,7 +323,7 @@ func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
if h.Address == "" { if h.Address == "" {
h.URLs = []string{"http://localhost"} h.URLs = []string{"http://localhost"}
} else { } else {
log.Printf("W! [inputs.http_response] 'address' deprecated in telegraf 1.12, please use 'urls'") h.Log.Warn("'address' deprecated in telegraf 1.12, please use 'urls'")
h.URLs = []string{h.Address} h.URLs = []string{h.Address}
} }
} }

View File

@ -150,6 +150,7 @@ func TestHeaders(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL, Address: ts.URL,
Method: "GET", Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 2}, ResponseTimeout: internal.Duration{Duration: time.Second * 2},
@ -185,6 +186,7 @@ func TestFields(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good", Address: ts.URL + "/good",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -246,6 +248,7 @@ func TestInterface(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good", Address: ts.URL + "/good",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -284,6 +287,7 @@ func TestRedirects(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/redirect", Address: ts.URL + "/redirect",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -314,6 +318,7 @@ func TestRedirects(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/badredirect", Address: ts.URL + "/badredirect",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -350,6 +355,7 @@ func TestMethod(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod", Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "POST", Method: "POST",
@ -380,6 +386,7 @@ func TestMethod(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod", Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -411,6 +418,7 @@ func TestMethod(t *testing.T) {
//check that lowercase methods work correctly //check that lowercase methods work correctly
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/mustbepostmethod", Address: ts.URL + "/mustbepostmethod",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "head", Method: "head",
@ -447,6 +455,7 @@ func TestBody(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/musthaveabody", Address: ts.URL + "/musthaveabody",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -477,6 +486,7 @@ func TestBody(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/musthaveabody", Address: ts.URL + "/musthaveabody",
Method: "GET", Method: "GET",
ResponseTimeout: internal.Duration{Duration: time.Second * 20}, ResponseTimeout: internal.Duration{Duration: time.Second * 20},
@ -510,6 +520,7 @@ func TestStringMatch(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good", Address: ts.URL + "/good",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -547,6 +558,7 @@ func TestStringMatchJson(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/jsonresponse", Address: ts.URL + "/jsonresponse",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -584,6 +596,7 @@ func TestStringMatchFail(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good", Address: ts.URL + "/good",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -626,6 +639,7 @@ func TestTimeout(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/twosecondnap", Address: ts.URL + "/twosecondnap",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -659,6 +673,7 @@ func TestBadRegex(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: ts.URL + "/good", Address: ts.URL + "/good",
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -682,6 +697,7 @@ func TestBadRegex(t *testing.T) {
func TestNetworkErrors(t *testing.T) { func TestNetworkErrors(t *testing.T) {
// DNS error // DNS error
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
Address: "https://nonexistent.nonexistent", // Any non-resolvable URL works here Address: "https://nonexistent.nonexistent", // Any non-resolvable URL works here
Body: "", Body: "",
Method: "GET", Method: "GET",
@ -708,6 +724,7 @@ func TestNetworkErrors(t *testing.T) {
// Connecton failed // Connecton failed
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here Address: "https:/nonexistent.nonexistent", // Any non-routable IP works here
Body: "", Body: "",
Method: "GET", Method: "GET",
@ -739,6 +756,7 @@ func TestContentLength(t *testing.T) {
defer ts.Close() defer ts.Close()
h := &HTTPResponse{ h := &HTTPResponse{
Log: testutil.Logger{},
URLs: []string{ts.URL + "/good"}, URLs: []string{ts.URL + "/good"},
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",
@ -769,6 +787,7 @@ func TestContentLength(t *testing.T) {
checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil) checkOutput(t, &acc, expectedFields, expectedTags, absentFields, nil)
h = &HTTPResponse{ h = &HTTPResponse{
Log: testutil.Logger{},
URLs: []string{ts.URL + "/musthaveabody"}, URLs: []string{ts.URL + "/musthaveabody"},
Body: "{ 'test': 'data'}", Body: "{ 'test': 'data'}",
Method: "GET", Method: "GET",

View File

@ -11,10 +11,10 @@ services and hosts. You can read Icinga2's documentation for their remote API
```toml ```toml
# Description # Description
[[inputs.icinga2]] [[inputs.icinga2]]
## Required Icinga2 server address (default: "https://localhost:5665") ## Required Icinga2 server address
# server = "https://localhost:5665" # server = "https://localhost:5665"
## Required Icinga2 object type ("services" or "hosts, default "services") ## Required Icinga2 object type ("services" or "hosts")
# object_type = "services" # object_type = "services"
## Credentials for basic HTTP authentication ## Credentials for basic HTTP authentication

View File

@ -3,7 +3,6 @@ package icinga2
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/url" "net/url"
"time" "time"
@ -22,6 +21,8 @@ type Icinga2 struct {
ResponseTimeout internal.Duration ResponseTimeout internal.Duration
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
client *http.Client client *http.Client
} }
@ -49,10 +50,10 @@ var levels = []string{"ok", "warning", "critical", "unknown"}
type ObjectType string type ObjectType string
var sampleConfig = ` var sampleConfig = `
## Required Icinga2 server address (default: "https://localhost:5665") ## Required Icinga2 server address
# server = "https://localhost:5665" # server = "https://localhost:5665"
## Required Icinga2 object type ("services" or "hosts, default "services") ## Required Icinga2 object type ("services" or "hosts")
# object_type = "services" # object_type = "services"
## Credentials for basic HTTP authentication ## Credentials for basic HTTP authentication
@ -80,25 +81,27 @@ func (i *Icinga2) SampleConfig() string {
func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) { func (i *Icinga2) GatherStatus(acc telegraf.Accumulator, checks []Object) {
for _, check := range checks { for _, check := range checks {
fields := make(map[string]interface{})
tags := make(map[string]string)
url, err := url.Parse(i.Server) url, err := url.Parse(i.Server)
if err != nil { if err != nil {
log.Fatal(err) i.Log.Error(err.Error())
continue
} }
state := int64(check.Attrs.State) state := int64(check.Attrs.State)
fields["name"] = check.Attrs.Name fields := map[string]interface{}{
fields["state_code"] = state "name": check.Attrs.Name,
"state_code": state,
}
tags["display_name"] = check.Attrs.DisplayName tags := map[string]string{
tags["check_command"] = check.Attrs.CheckCommand "display_name": check.Attrs.DisplayName,
tags["state"] = levels[state] "check_command": check.Attrs.CheckCommand,
tags["source"] = url.Hostname() "state": levels[state],
tags["scheme"] = url.Scheme "source": url.Hostname(),
tags["port"] = url.Port() "scheme": url.Scheme,
"port": url.Port(),
}
acc.AddFields(fmt.Sprintf("icinga2_%s", i.ObjectType), fields, tags) acc.AddFields(fmt.Sprintf("icinga2_%s", i.ObjectType), fields, tags)
} }
@ -165,8 +168,9 @@ func (i *Icinga2) Gather(acc telegraf.Accumulator) error {
func init() { func init() {
inputs.Add("icinga2", func() telegraf.Input { inputs.Add("icinga2", func() telegraf.Input {
return &Icinga2{ return &Icinga2{
Server: "https://localhost:5665", Server: "https://localhost:5665",
ObjectType: "services", ObjectType: "services",
ResponseTimeout: internal.Duration{Duration: time.Second * 5},
} }
}) })
} }

View File

@ -32,6 +32,7 @@ func TestGatherServicesStatus(t *testing.T) {
json.Unmarshal([]byte(s), &checks) json.Unmarshal([]byte(s), &checks)
icinga2 := new(Icinga2) icinga2 := new(Icinga2)
icinga2.Log = testutil.Logger{}
icinga2.ObjectType = "services" icinga2.ObjectType = "services"
icinga2.Server = "https://localhost:5665" icinga2.Server = "https://localhost:5665"
@ -86,6 +87,7 @@ func TestGatherHostsStatus(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
icinga2 := new(Icinga2) icinga2 := new(Icinga2)
icinga2.Log = testutil.Logger{}
icinga2.ObjectType = "hosts" icinga2.ObjectType = "hosts"
icinga2.Server = "https://localhost:5665" icinga2.Server = "https://localhost:5665"

View File

@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
"sync" "sync"
@ -75,6 +74,8 @@ type HTTPListener struct {
BuffersCreated selfstat.Stat BuffersCreated selfstat.Stat
AuthFailures selfstat.Stat AuthFailures selfstat.Stat
Log telegraf.Logger
longLines selfstat.Stat longLines selfstat.Stat
} }
@ -202,7 +203,7 @@ func (h *HTTPListener) Start(acc telegraf.Accumulator) error {
server.Serve(h.listener) server.Serve(h.listener)
}() }()
log.Printf("I! Started HTTP listener service on %s\n", h.ServiceAddress) h.Log.Infof("Started HTTP listener service on %s", h.ServiceAddress)
return nil return nil
} }
@ -215,7 +216,7 @@ func (h *HTTPListener) Stop() {
h.listener.Close() h.listener.Close()
h.wg.Wait() h.wg.Wait()
log.Println("I! Stopped HTTP listener service on ", h.ServiceAddress) h.Log.Infof("Stopped HTTP listener service on %s", h.ServiceAddress)
} }
func (h *HTTPListener) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (h *HTTPListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
@ -274,7 +275,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
var err error var err error
body, err = gzip.NewReader(req.Body) body, err = gzip.NewReader(req.Body)
if err != nil { if err != nil {
log.Println("D! " + err.Error()) h.Log.Debug(err.Error())
badRequest(res, err.Error()) badRequest(res, err.Error())
return return
} }
@ -290,7 +291,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
for { for {
n, err := io.ReadFull(body, buf[bufStart:]) n, err := io.ReadFull(body, buf[bufStart:])
if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF { if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
log.Println("D! " + err.Error()) h.Log.Debug(err.Error())
// problem reading the request body // problem reading the request body
badRequest(res, err.Error()) badRequest(res, err.Error())
return return
@ -326,7 +327,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
// finished reading the request body // finished reading the request body
err = h.parse(buf[:n+bufStart], now, precision, db) err = h.parse(buf[:n+bufStart], now, precision, db)
if err != nil { if err != nil {
log.Println("D! "+err.Error(), bufStart+n) h.Log.Debugf("%s: %s", err.Error(), bufStart+n)
return400 = true return400 = true
} }
if return400 { if return400 {
@ -348,7 +349,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
if i == -1 { if i == -1 {
h.longLines.Incr(1) h.longLines.Incr(1)
// drop any line longer than the max buffer size // drop any line longer than the max buffer size
log.Printf("D! http_listener received a single line longer than the maximum of %d bytes", h.Log.Debugf("Http_listener received a single line longer than the maximum of %d bytes",
len(buf)) len(buf))
hangingBytes = true hangingBytes = true
return400 = true return400 = true
@ -356,7 +357,7 @@ func (h *HTTPListener) serveWrite(res http.ResponseWriter, req *http.Request) {
continue continue
} }
if err := h.parse(buf[:i+1], now, precision, db); err != nil { if err := h.parse(buf[:i+1], now, precision, db); err != nil {
log.Println("D! " + err.Error()) h.Log.Debug(err.Error())
return400 = true return400 = true
} }
// rotate the bit remaining after the last newline to the front of the buffer // rotate the bit remaining after the last newline to the front of the buffer

View File

@ -44,6 +44,7 @@ var (
func newTestHTTPListener() *HTTPListener { func newTestHTTPListener() *HTTPListener {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
TimeFunc: time.Now, TimeFunc: time.Now,
} }
@ -59,6 +60,7 @@ func newTestHTTPAuthListener() *HTTPListener {
func newTestHTTPSListener() *HTTPListener { func newTestHTTPSListener() *HTTPListener {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
ServerConfig: *pki.TLSServerConfig(), ServerConfig: *pki.TLSServerConfig(),
TimeFunc: time.Now, TimeFunc: time.Now,
@ -220,6 +222,7 @@ func TestWriteHTTPNoNewline(t *testing.T) {
func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) { func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
MaxLineSize: internal.Size{Size: 128 * 1000}, MaxLineSize: internal.Size{Size: 128 * 1000},
TimeFunc: time.Now, TimeFunc: time.Now,
@ -238,6 +241,7 @@ func TestWriteHTTPMaxLineSizeIncrease(t *testing.T) {
func TestWriteHTTPVerySmallMaxBody(t *testing.T) { func TestWriteHTTPVerySmallMaxBody(t *testing.T) {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
MaxBodySize: internal.Size{Size: 4096}, MaxBodySize: internal.Size{Size: 4096},
TimeFunc: time.Now, TimeFunc: time.Now,
@ -255,6 +259,7 @@ func TestWriteHTTPVerySmallMaxBody(t *testing.T) {
func TestWriteHTTPVerySmallMaxLineSize(t *testing.T) { func TestWriteHTTPVerySmallMaxLineSize(t *testing.T) {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
MaxLineSize: internal.Size{Size: 70}, MaxLineSize: internal.Size{Size: 70},
TimeFunc: time.Now, TimeFunc: time.Now,
@ -282,6 +287,7 @@ func TestWriteHTTPVerySmallMaxLineSize(t *testing.T) {
func TestWriteHTTPLargeLinesSkipped(t *testing.T) { func TestWriteHTTPLargeLinesSkipped(t *testing.T) {
listener := &HTTPListener{ listener := &HTTPListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
MaxLineSize: internal.Size{Size: 100}, MaxLineSize: internal.Size{Size: 100},
TimeFunc: time.Now, TimeFunc: time.Now,

View File

@ -5,7 +5,6 @@ package ipvs
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"math/bits" "math/bits"
"strconv" "strconv"
"syscall" "syscall"
@ -18,6 +17,7 @@ import (
// IPVS holds the state for this input plugin // IPVS holds the state for this input plugin
type IPVS struct { type IPVS struct {
handle *ipvs.Handle handle *ipvs.Handle
Log telegraf.Logger
} }
// Description returns a description string // Description returns a description string
@ -61,7 +61,7 @@ func (i *IPVS) Gather(acc telegraf.Accumulator) error {
destinations, err := i.handle.GetDestinations(s) destinations, err := i.handle.GetDestinations(s)
if err != nil { if err != nil {
log.Println("E! Failed to list destinations for a virtual server") i.Log.Errorf("Failed to list destinations for a virtual server: %s", err.Error())
continue // move on to the next virtual server continue // move on to the next virtual server
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -29,6 +28,8 @@ type Jenkins struct {
tls.ClientConfig tls.ClientConfig
client *client client *client
Log telegraf.Logger
MaxConnections int `toml:"max_connections"` MaxConnections int `toml:"max_connections"`
MaxBuildAge internal.Duration `toml:"max_build_age"` MaxBuildAge internal.Duration `toml:"max_build_age"`
MaxSubJobDepth int `toml:"max_subjob_depth"` MaxSubJobDepth int `toml:"max_subjob_depth"`
@ -304,7 +305,7 @@ func (j *Jenkins) getJobDetail(jr jobRequest, acc telegraf.Accumulator) error {
} }
if build.Building { if build.Building {
log.Printf("D! Ignore running build on %s, build %v", jr.name, number) j.Log.Debugf("Ignore running build on %s, build %v", jr.name, number)
return nil return nil
} }

View File

@ -206,6 +206,7 @@ func TestGatherNodeData(t *testing.T) {
ts := httptest.NewServer(test.input) ts := httptest.NewServer(test.input)
defer ts.Close() defer ts.Close()
j := &Jenkins{ j := &Jenkins{
Log: testutil.Logger{},
URL: ts.URL, URL: ts.URL,
ResponseTimeout: internal.Duration{Duration: time.Microsecond}, ResponseTimeout: internal.Duration{Duration: time.Microsecond},
NodeExclude: []string{"ignore-1", "ignore-2"}, NodeExclude: []string{"ignore-1", "ignore-2"},
@ -258,6 +259,7 @@ func TestInitialize(t *testing.T) {
{ {
name: "bad jenkins config", name: "bad jenkins config",
input: &Jenkins{ input: &Jenkins{
Log: testutil.Logger{},
URL: "http://a bad url", URL: "http://a bad url",
ResponseTimeout: internal.Duration{Duration: time.Microsecond}, ResponseTimeout: internal.Duration{Duration: time.Microsecond},
}, },
@ -266,6 +268,7 @@ func TestInitialize(t *testing.T) {
{ {
name: "has filter", name: "has filter",
input: &Jenkins{ input: &Jenkins{
Log: testutil.Logger{},
URL: ts.URL, URL: ts.URL,
ResponseTimeout: internal.Duration{Duration: time.Microsecond}, ResponseTimeout: internal.Duration{Duration: time.Microsecond},
JobExclude: []string{"job1", "job2"}, JobExclude: []string{"job1", "job2"},
@ -275,10 +278,12 @@ func TestInitialize(t *testing.T) {
{ {
name: "default config", name: "default config",
input: &Jenkins{ input: &Jenkins{
Log: testutil.Logger{},
URL: ts.URL, URL: ts.URL,
ResponseTimeout: internal.Duration{Duration: time.Microsecond}, ResponseTimeout: internal.Duration{Duration: time.Microsecond},
}, },
output: &Jenkins{ output: &Jenkins{
Log: testutil.Logger{},
MaxConnections: 5, MaxConnections: 5,
MaxSubJobPerLayer: 10, MaxSubJobPerLayer: 10,
}, },
@ -570,6 +575,7 @@ func TestGatherJobs(t *testing.T) {
ts := httptest.NewServer(test.input) ts := httptest.NewServer(test.input)
defer ts.Close() defer ts.Close()
j := &Jenkins{ j := &Jenkins{
Log: testutil.Logger{},
URL: ts.URL, URL: ts.URL,
MaxBuildAge: internal.Duration{Duration: time.Hour}, MaxBuildAge: internal.Duration{Duration: time.Hour},
ResponseTimeout: internal.Duration{Duration: time.Microsecond}, ResponseTimeout: internal.Duration{Duration: time.Microsecond},

View File

@ -2,7 +2,6 @@ package jti_openconfig_telemetry
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"regexp" "regexp"
"strings" "strings"
@ -34,6 +33,8 @@ type OpenConfigTelemetry struct {
EnableTLS bool `toml:"enable_tls"` EnableTLS bool `toml:"enable_tls"`
internaltls.ClientConfig internaltls.ClientConfig
Log telegraf.Logger
sensorsConfig []sensorConfig sensorsConfig []sensorConfig
grpcClientConns []*grpc.ClientConn grpcClientConns []*grpc.ClientConn
wg *sync.WaitGroup wg *sync.WaitGroup
@ -243,7 +244,7 @@ func (m *OpenConfigTelemetry) splitSensorConfig() int {
} }
if len(spathSplit) == 0 { if len(spathSplit) == 0 {
log.Printf("E! No sensors are specified") m.Log.Error("No sensors are specified")
continue continue
} }
@ -257,7 +258,7 @@ func (m *OpenConfigTelemetry) splitSensorConfig() int {
} }
if len(spathSplit) == 0 { if len(spathSplit) == 0 {
log.Printf("E! No valid sensors are specified") m.Log.Error("No valid sensors are specified")
continue continue
} }
@ -294,13 +295,13 @@ func (m *OpenConfigTelemetry) collectData(ctx context.Context,
rpcStatus, _ := status.FromError(err) rpcStatus, _ := status.FromError(err)
// If service is currently unavailable and may come back later, retry // If service is currently unavailable and may come back later, retry
if rpcStatus.Code() != codes.Unavailable { if rpcStatus.Code() != codes.Unavailable {
acc.AddError(fmt.Errorf("E! Could not subscribe to %s: %v", grpcServer, acc.AddError(fmt.Errorf("could not subscribe to %s: %v", grpcServer,
err)) err))
return return
} else { } else {
// Retry with delay. If delay is not provided, use default // Retry with delay. If delay is not provided, use default
if m.RetryDelay.Duration > 0 { if m.RetryDelay.Duration > 0 {
log.Printf("D! Retrying %s with timeout %v", grpcServer, m.Log.Debugf("Retrying %s with timeout %v", grpcServer,
m.RetryDelay.Duration) m.RetryDelay.Duration)
time.Sleep(m.RetryDelay.Duration) time.Sleep(m.RetryDelay.Duration)
continue continue
@ -314,11 +315,11 @@ func (m *OpenConfigTelemetry) collectData(ctx context.Context,
if err != nil { if err != nil {
// If we encounter error in the stream, break so we can retry // If we encounter error in the stream, break so we can retry
// the connection // the connection
acc.AddError(fmt.Errorf("E! Failed to read from %s: %v", err, grpcServer)) acc.AddError(fmt.Errorf("failed to read from %s: %s", grpcServer, err))
break break
} }
log.Printf("D! Received from %s: %v", grpcServer, r) m.Log.Debugf("Received from %s: %v", grpcServer, r)
// Create a point and add to batch // Create a point and add to batch
tags := make(map[string]string) tags := make(map[string]string)
@ -329,7 +330,7 @@ func (m *OpenConfigTelemetry) collectData(ctx context.Context,
dgroups := m.extractData(r, grpcServer) dgroups := m.extractData(r, grpcServer)
// Print final data collection // Print final data collection
log.Printf("D! Available collection for %s is: %v", grpcServer, dgroups) m.Log.Debugf("Available collection for %s is: %v", grpcServer, dgroups)
tnow := time.Now() tnow := time.Now()
// Iterate through data groups and add them // Iterate through data groups and add them
@ -349,10 +350,9 @@ func (m *OpenConfigTelemetry) collectData(ctx context.Context,
} }
func (m *OpenConfigTelemetry) Start(acc telegraf.Accumulator) error { func (m *OpenConfigTelemetry) Start(acc telegraf.Accumulator) error {
// Build sensors config // Build sensors config
if m.splitSensorConfig() == 0 { if m.splitSensorConfig() == 0 {
return fmt.Errorf("E! No valid sensor configuration available") return fmt.Errorf("no valid sensor configuration available")
} }
// Parse TLS config // Parse TLS config
@ -376,15 +376,15 @@ func (m *OpenConfigTelemetry) Start(acc telegraf.Accumulator) error {
// Extract device address and port // Extract device address and port
grpcServer, grpcPort, err := net.SplitHostPort(server) grpcServer, grpcPort, err := net.SplitHostPort(server)
if err != nil { if err != nil {
log.Printf("E! Invalid server address: %v", err) m.Log.Errorf("Invalid server address: %s", err.Error())
continue continue
} }
grpcClientConn, err = grpc.Dial(server, opts...) grpcClientConn, err = grpc.Dial(server, opts...)
if err != nil { if err != nil {
log.Printf("E! Failed to connect to %s: %v", server, err) m.Log.Errorf("Failed to connect to %s: %s", server, err.Error())
} else { } else {
log.Printf("D! Opened a new gRPC session to %s on port %s", grpcServer, grpcPort) m.Log.Debugf("Opened a new gRPC session to %s on port %s", grpcServer, grpcPort)
} }
// Add to the list of client connections // Add to the list of client connections
@ -396,13 +396,13 @@ func (m *OpenConfigTelemetry) Start(acc telegraf.Accumulator) error {
&authentication.LoginRequest{UserName: m.Username, &authentication.LoginRequest{UserName: m.Username,
Password: m.Password, ClientId: m.ClientID}) Password: m.Password, ClientId: m.ClientID})
if loginErr != nil { if loginErr != nil {
log.Printf("E! Could not initiate login check for %s: %v", server, loginErr) m.Log.Errorf("Could not initiate login check for %s: %v", server, loginErr)
continue continue
} }
// Check if the user is authenticated. Bail if auth error // Check if the user is authenticated. Bail if auth error
if !loginReply.Result { if !loginReply.Result {
log.Printf("E! Failed to authenticate the user for %s", server) m.Log.Errorf("Failed to authenticate the user for %s", server)
continue continue
} }
} }

View File

@ -17,6 +17,7 @@ import (
) )
var cfg = &OpenConfigTelemetry{ var cfg = &OpenConfigTelemetry{
Log: testutil.Logger{},
Servers: []string{"127.0.0.1:50051"}, Servers: []string{"127.0.0.1:50051"},
SampleFrequency: internal.Duration{Duration: time.Second * 2}, SampleFrequency: internal.Duration{Duration: time.Second * 2},
} }

View File

@ -13,12 +13,16 @@ from the same topic in parallel.
[[inputs.kafka_consumer]] [[inputs.kafka_consumer]]
## topic(s) to consume ## topic(s) to consume
topics = ["telegraf"] topics = ["telegraf"]
## an array of Zookeeper connection strings ## an array of Zookeeper connection strings
zookeeper_peers = ["localhost:2181"] zookeeper_peers = ["localhost:2181"]
## Zookeeper Chroot ## Zookeeper Chroot
zookeeper_chroot = "" zookeeper_chroot = ""
## the name of the consumer group ## the name of the consumer group
consumer_group = "telegraf_metrics_consumers" consumer_group = "telegraf_metrics_consumers"
## Offset (must be either "oldest" or "newest") ## Offset (must be either "oldest" or "newest")
offset = "oldest" offset = "oldest"

View File

@ -2,7 +2,6 @@ package kafka_consumer_legacy
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"sync" "sync"
@ -30,6 +29,8 @@ type Kafka struct {
Offset string Offset string
parser parsers.Parser parser parsers.Parser
Log telegraf.Logger
sync.Mutex sync.Mutex
// channel for all incoming kafka messages // channel for all incoming kafka messages
@ -49,12 +50,16 @@ type Kafka struct {
var sampleConfig = ` var sampleConfig = `
## topic(s) to consume ## topic(s) to consume
topics = ["telegraf"] topics = ["telegraf"]
## an array of Zookeeper connection strings ## an array of Zookeeper connection strings
zookeeper_peers = ["localhost:2181"] zookeeper_peers = ["localhost:2181"]
## Zookeeper Chroot ## Zookeeper Chroot
zookeeper_chroot = "" zookeeper_chroot = ""
## the name of the consumer group ## the name of the consumer group
consumer_group = "telegraf_metrics_consumers" consumer_group = "telegraf_metrics_consumers"
## Offset (must be either "oldest" or "newest") ## Offset (must be either "oldest" or "newest")
offset = "oldest" offset = "oldest"
@ -96,7 +101,7 @@ func (k *Kafka) Start(acc telegraf.Accumulator) error {
case "newest": case "newest":
config.Offsets.Initial = sarama.OffsetNewest config.Offsets.Initial = sarama.OffsetNewest
default: default:
log.Printf("I! WARNING: Kafka consumer invalid offset '%s', using 'oldest'\n", k.Log.Infof("WARNING: Kafka consumer invalid offset '%s', using 'oldest'\n",
k.Offset) k.Offset)
config.Offsets.Initial = sarama.OffsetOldest config.Offsets.Initial = sarama.OffsetOldest
} }
@ -121,7 +126,7 @@ func (k *Kafka) Start(acc telegraf.Accumulator) error {
// Start the kafka message reader // Start the kafka message reader
go k.receiver() go k.receiver()
log.Printf("I! Started the kafka consumer service, peers: %v, topics: %v\n", k.Log.Infof("Started the kafka consumer service, peers: %v, topics: %v\n",
k.ZookeeperPeers, k.Topics) k.ZookeeperPeers, k.Topics)
return nil return nil
} }

View File

@ -37,6 +37,7 @@ func TestReadsMetricsFromKafka(t *testing.T) {
// Start the Kafka Consumer // Start the Kafka Consumer
k := &Kafka{ k := &Kafka{
Log: testutil.Logger{},
ConsumerGroup: "telegraf_test_consumers", ConsumerGroup: "telegraf_test_consumers",
Topics: []string{testTopic}, Topics: []string{testTopic},
ZookeeperPeers: zkPeers, ZookeeperPeers: zkPeers,

View File

@ -21,6 +21,7 @@ const (
func newTestKafka() (*Kafka, chan *sarama.ConsumerMessage) { func newTestKafka() (*Kafka, chan *sarama.ConsumerMessage) {
in := make(chan *sarama.ConsumerMessage, 1000) in := make(chan *sarama.ConsumerMessage, 1000)
k := Kafka{ k := Kafka{
Log: testutil.Logger{},
ConsumerGroup: "test", ConsumerGroup: "test",
Topics: []string{"telegraf"}, Topics: []string{"telegraf"},
ZookeeperPeers: []string{"localhost:2181"}, ZookeeperPeers: []string{"localhost:2181"},

View File

@ -3,7 +3,6 @@ package kinesis_consumer
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"math/big" "math/big"
"strings" "strings"
"sync" "sync"
@ -40,6 +39,8 @@ type (
DynamoDB *DynamoDB `toml:"checkpoint_dynamodb"` DynamoDB *DynamoDB `toml:"checkpoint_dynamodb"`
MaxUndeliveredMessages int `toml:"max_undelivered_messages"` MaxUndeliveredMessages int `toml:"max_undelivered_messages"`
Log telegraf.Logger
cons *consumer.Consumer cons *consumer.Consumer
parser parsers.Parser parser parsers.Parser
cancel context.CancelFunc cancel context.CancelFunc
@ -220,7 +221,7 @@ func (k *KinesisConsumer) connect(ac telegraf.Accumulator) error {
}) })
if err != nil { if err != nil {
k.cancel() k.cancel()
log.Printf("E! [inputs.kinesis_consumer] Scan encounterred an error - %s", err.Error()) k.Log.Errorf("Scan encounterred an error: %s", err.Error())
k.cons = nil k.cons = nil
} }
}() }()
@ -285,7 +286,7 @@ func (k *KinesisConsumer) onDelivery(ctx context.Context) {
k.lastSeqNum = strToBint(sequenceNum) k.lastSeqNum = strToBint(sequenceNum)
k.checkpoint.Set(chk.streamName, chk.shardID, sequenceNum) k.checkpoint.Set(chk.streamName, chk.shardID, sequenceNum)
} else { } else {
log.Println("D! [inputs.kinesis_consumer] Metric group failed to process") k.Log.Debug("Metric group failed to process")
} }
} }
} }

View File

@ -114,11 +114,11 @@ var availableCollectors = map[string]func(ctx context.Context, acc telegraf.Accu
"endpoints": collectEndpoints, "endpoints": collectEndpoints,
"ingress": collectIngress, "ingress": collectIngress,
"nodes": collectNodes, "nodes": collectNodes,
"persistentvolumes": collectPersistentVolumes,
"persistentvolumeclaims": collectPersistentVolumeClaims,
"pods": collectPods, "pods": collectPods,
"services": collectServices, "services": collectServices,
"statefulsets": collectStatefulSets, "statefulsets": collectStatefulSets,
"persistentvolumes": collectPersistentVolumes,
"persistentvolumeclaims": collectPersistentVolumeClaims,
} }
func (ki *KubernetesInventory) initClient() (*client, error) { func (ki *KubernetesInventory) initClient() (*client, error) {
@ -144,12 +144,12 @@ func atoi(s string) int64 {
func convertQuantity(s string, m float64) int64 { func convertQuantity(s string, m float64) int64 {
q, err := resource.ParseQuantity(s) q, err := resource.ParseQuantity(s)
if err != nil { if err != nil {
log.Printf("E! Failed to parse quantity - %v", err) log.Printf("D! [inputs.kube_inventory] failed to parse quantity: %s", err.Error())
return 0 return 0
} }
f, err := strconv.ParseFloat(fmt.Sprint(q.AsDec()), 64) f, err := strconv.ParseFloat(fmt.Sprint(q.AsDec()), 64)
if err != nil { if err != nil {
log.Printf("E! Failed to parse float - %v", err) log.Printf("D! [inputs.kube_inventory] failed to parse float: %s", err.Error())
return 0 return 0
} }
if m < 1 { if m < 1 {

View File

@ -4,7 +4,6 @@ package logparser
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"sync" "sync"
@ -14,7 +13,6 @@ import (
"github.com/influxdata/telegraf/internal/globpath" "github.com/influxdata/telegraf/internal/globpath"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/parsers" "github.com/influxdata/telegraf/plugins/parsers"
// Parsers
) )
const ( const (
@ -48,6 +46,8 @@ type LogParserPlugin struct {
FromBeginning bool FromBeginning bool
WatchMethod string WatchMethod string
Log telegraf.Logger
tailers map[string]*tail.Tail tailers map[string]*tail.Tail
offsets map[string]int64 offsets map[string]int64
lines chan logEntry lines chan logEntry
@ -207,7 +207,7 @@ func (l *LogParserPlugin) tailNewfiles(fromBeginning bool) error {
for _, filepath := range l.Files { for _, filepath := range l.Files {
g, err := globpath.Compile(filepath) g, err := globpath.Compile(filepath)
if err != nil { if err != nil {
log.Printf("E! [inputs.logparser] Error Glob %s failed to compile, %s", filepath, err) l.Log.Errorf("Glob %q failed to compile: %s", filepath, err)
continue continue
} }
files := g.Match() files := g.Match()
@ -221,7 +221,7 @@ func (l *LogParserPlugin) tailNewfiles(fromBeginning bool) error {
var seek *tail.SeekInfo var seek *tail.SeekInfo
if !fromBeginning { if !fromBeginning {
if offset, ok := l.offsets[file]; ok { if offset, ok := l.offsets[file]; ok {
log.Printf("D! [inputs.tail] using offset %d for file: %v", offset, file) l.Log.Debugf("Using offset %d for file: %v", offset, file)
seek = &tail.SeekInfo{ seek = &tail.SeekInfo{
Whence: 0, Whence: 0,
Offset: offset, Offset: offset,
@ -248,7 +248,7 @@ func (l *LogParserPlugin) tailNewfiles(fromBeginning bool) error {
continue continue
} }
log.Printf("D! [inputs.logparser] tail added for file: %v", file) l.Log.Debugf("Tail added for file: %v", file)
// create a goroutine for each "tailer" // create a goroutine for each "tailer"
l.wg.Add(1) l.wg.Add(1)
@ -269,7 +269,7 @@ func (l *LogParserPlugin) receiver(tailer *tail.Tail) {
for line = range tailer.Lines { for line = range tailer.Lines {
if line.Err != nil { if line.Err != nil {
log.Printf("E! [inputs.logparser] Error tailing file %s, Error: %s", l.Log.Errorf("Error tailing file %s, Error: %s",
tailer.Filename, line.Err) tailer.Filename, line.Err)
continue continue
} }
@ -315,7 +315,7 @@ func (l *LogParserPlugin) parser() {
l.acc.AddFields(m.Name(), m.Fields(), tags, m.Time()) l.acc.AddFields(m.Name(), m.Fields(), tags, m.Time())
} }
} else { } else {
log.Println("E! [inputs.logparser] Error parsing log line: " + err.Error()) l.Log.Errorf("Error parsing log line: %s", err.Error())
} }
} }
@ -332,7 +332,7 @@ func (l *LogParserPlugin) Stop() {
offset, err := t.Tell() offset, err := t.Tell()
if err == nil { if err == nil {
l.offsets[t.Filename] = offset l.offsets[t.Filename] = offset
log.Printf("D! [inputs.logparser] recording offset %d for file: %v", offset, t.Filename) l.Log.Debugf("Recording offset %d for file: %v", offset, t.Filename)
} else { } else {
l.acc.AddError(fmt.Errorf("error recording offset for file %s", t.Filename)) l.acc.AddError(fmt.Errorf("error recording offset for file %s", t.Filename))
} }
@ -340,10 +340,10 @@ func (l *LogParserPlugin) Stop() {
err := t.Stop() err := t.Stop()
//message for a stopped tailer //message for a stopped tailer
log.Printf("D! [inputs.logparser] tail dropped for file: %v", t.Filename) l.Log.Debugf("Tail dropped for file: %v", t.Filename)
if err != nil { if err != nil {
log.Printf("E! [inputs.logparser] Error stopping tail on file %s", t.Filename) l.Log.Errorf("Error stopping tail on file %s", t.Filename)
} }
} }
close(l.done) close(l.done)

View File

@ -14,6 +14,7 @@ import (
func TestStartNoParsers(t *testing.T) { func TestStartNoParsers(t *testing.T) {
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
Log: testutil.Logger{},
FromBeginning: true, FromBeginning: true,
Files: []string{"testdata/*.log"}, Files: []string{"testdata/*.log"},
} }
@ -26,6 +27,7 @@ func TestGrokParseLogFilesNonExistPattern(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
Log: testutil.Logger{},
FromBeginning: true, FromBeginning: true,
Files: []string{thisdir + "testdata/*.log"}, Files: []string{thisdir + "testdata/*.log"},
GrokConfig: GrokConfig{ GrokConfig: GrokConfig{
@ -43,6 +45,7 @@ func TestGrokParseLogFiles(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
Log: testutil.Logger{},
GrokConfig: GrokConfig{ GrokConfig: GrokConfig{
MeasurementName: "logparser_grok", MeasurementName: "logparser_grok",
Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"}, Patterns: []string{"%{TEST_LOG_A}", "%{TEST_LOG_B}"},
@ -89,6 +92,7 @@ func TestGrokParseLogFilesAppearLater(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
Log: testutil.Logger{},
FromBeginning: true, FromBeginning: true,
Files: []string{emptydir + "/*.log"}, Files: []string{emptydir + "/*.log"},
GrokConfig: GrokConfig{ GrokConfig: GrokConfig{
@ -128,6 +132,7 @@ func TestGrokParseLogFilesOneBad(t *testing.T) {
thisdir := getCurrentDir() thisdir := getCurrentDir()
logparser := &LogParserPlugin{ logparser := &LogParserPlugin{
Log: testutil.Logger{},
FromBeginning: true, FromBeginning: true,
Files: []string{thisdir + "testdata/test_a.log"}, Files: []string{thisdir + "testdata/test_a.log"},
GrokConfig: GrokConfig{ GrokConfig: GrokConfig{

View File

@ -134,7 +134,7 @@ func runChimp(api *ChimpAPI, params ReportsParams) ([]byte, error) {
req.URL.RawQuery = params.String() req.URL.RawQuery = params.String()
req.Header.Set("User-Agent", "Telegraf-MailChimp-Plugin") req.Header.Set("User-Agent", "Telegraf-MailChimp-Plugin")
if api.Debug { if api.Debug {
log.Printf("D! Request URL: %s", req.URL.String()) log.Printf("D! [inputs.mailchimp] request URL: %s", req.URL.String())
} }
resp, err := client.Do(req) resp, err := client.Do(req)
@ -148,7 +148,7 @@ func runChimp(api *ChimpAPI, params ReportsParams) ([]byte, error) {
return nil, err return nil, err
} }
if api.Debug { if api.Debug {
log.Printf("D! Response Body:%s", string(body)) log.Printf("D! [inputs.mailchimp] response Body: %q", string(body))
} }
if err = chimpErrorCheck(body); err != nil { if err = chimpErrorCheck(body); err != nil {

View File

@ -10,8 +10,10 @@ For more information, please check the [Mesos Observability Metrics](http://meso
[[inputs.mesos]] [[inputs.mesos]]
## Timeout, in ms. ## Timeout, in ms.
timeout = 100 timeout = 100
## A list of Mesos masters. ## A list of Mesos masters.
masters = ["http://localhost:5050"] masters = ["http://localhost:5050"]
## Master metrics groups to be collected, by default, all enabled. ## Master metrics groups to be collected, by default, all enabled.
master_collections = [ master_collections = [
"resources", "resources",
@ -26,8 +28,10 @@ For more information, please check the [Mesos Observability Metrics](http://meso
"registrar", "registrar",
"allocator", "allocator",
] ]
## A list of Mesos slaves, default is [] ## A list of Mesos slaves, default is []
# slaves = [] # slaves = []
## Slave metrics groups to be collected, by default, all enabled. ## Slave metrics groups to be collected, by default, all enabled.
# slave_collections = [ # slave_collections = [
# "resources", # "resources",

View File

@ -32,9 +32,10 @@ type Mesos struct {
MasterCols []string `toml:"master_collections"` MasterCols []string `toml:"master_collections"`
Slaves []string Slaves []string
SlaveCols []string `toml:"slave_collections"` SlaveCols []string `toml:"slave_collections"`
//SlaveTasks bool
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
initialized bool initialized bool
client *http.Client client *http.Client
masterURLs []*url.URL masterURLs []*url.URL
@ -49,8 +50,10 @@ var allMetrics = map[Role][]string{
var sampleConfig = ` var sampleConfig = `
## Timeout, in ms. ## Timeout, in ms.
timeout = 100 timeout = 100
## A list of Mesos masters. ## A list of Mesos masters.
masters = ["http://localhost:5050"] masters = ["http://localhost:5050"]
## Master metrics groups to be collected, by default, all enabled. ## Master metrics groups to be collected, by default, all enabled.
master_collections = [ master_collections = [
"resources", "resources",
@ -65,8 +68,10 @@ var sampleConfig = `
"registrar", "registrar",
"allocator", "allocator",
] ]
## A list of Mesos slaves, default is [] ## A list of Mesos slaves, default is []
# slaves = [] # slaves = []
## Slave metrics groups to be collected, by default, all enabled. ## Slave metrics groups to be collected, by default, all enabled.
# slave_collections = [ # slave_collections = [
# "resources", # "resources",
@ -110,7 +115,7 @@ func parseURL(s string, role Role) (*url.URL, error) {
} }
s = "http://" + host + ":" + port s = "http://" + host + ":" + port
log.Printf("W! [inputs.mesos] Using %q as connection URL; please update your configuration to use an URL", s) log.Printf("W! [inputs.mesos] using %q as connection URL; please update your configuration to use an URL", s)
} }
return url.Parse(s) return url.Parse(s)
@ -126,7 +131,7 @@ func (m *Mesos) initialize() error {
} }
if m.Timeout == 0 { if m.Timeout == 0 {
log.Println("I! [inputs.mesos] Missing timeout value, setting default value (100ms)") m.Log.Info("Missing timeout value, setting default value (100ms)")
m.Timeout = 100 m.Timeout = 100
} }
@ -191,17 +196,6 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error {
wg.Done() wg.Done()
return return
}(slave) }(slave)
// if !m.SlaveTasks {
// continue
// }
// wg.Add(1)
// go func(c string) {
// acc.AddError(m.gatherSlaveTaskMetrics(slave, acc))
// wg.Done()
// return
// }(v)
} }
wg.Wait() wg.Wait()
@ -487,7 +481,7 @@ func getMetrics(role Role, group string) []string {
ret, ok := m[group] ret, ok := m[group]
if !ok { if !ok {
log.Printf("I! [mesos] Unknown %s metrics group: %s\n", role, group) log.Printf("I! [inputs.mesos] unknown role %q metrics group: %s", role, group)
return []string{} return []string{}
} }

View File

@ -349,6 +349,7 @@ func TestMesosMaster(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
m := Mesos{ m := Mesos{
Log: testutil.Logger{},
Masters: []string{masterTestServer.Listener.Addr().String()}, Masters: []string{masterTestServer.Listener.Addr().String()},
Timeout: 10, Timeout: 10,
} }
@ -364,6 +365,7 @@ func TestMesosMaster(t *testing.T) {
func TestMasterFilter(t *testing.T) { func TestMasterFilter(t *testing.T) {
m := Mesos{ m := Mesos{
Log: testutil.Logger{},
MasterCols: []string{ MasterCols: []string{
"resources", "master", "registrar", "allocator", "resources", "master", "registrar", "allocator",
}, },
@ -416,6 +418,7 @@ func TestMesosSlave(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
m := Mesos{ m := Mesos{
Log: testutil.Logger{},
Masters: []string{}, Masters: []string{},
Slaves: []string{slaveTestServer.Listener.Addr().String()}, Slaves: []string{slaveTestServer.Listener.Addr().String()},
// SlaveTasks: true, // SlaveTasks: true,
@ -433,6 +436,7 @@ func TestMesosSlave(t *testing.T) {
func TestSlaveFilter(t *testing.T) { func TestSlaveFilter(t *testing.T) {
m := Mesos{ m := Mesos{
Log: testutil.Logger{},
SlaveCols: []string{ SlaveCols: []string{
"resources", "agent", "tasks", "resources", "agent", "tasks",
}, },

View File

@ -4,7 +4,6 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"log"
"net" "net"
"net/url" "net/url"
"strings" "strings"
@ -25,6 +24,8 @@ type MongoDB struct {
GatherColStats bool GatherColStats bool
ColStatsDbs []string ColStatsDbs []string
tlsint.ClientConfig tlsint.ClientConfig
Log telegraf.Logger
} }
type Ssl struct { type Ssl struct {
@ -82,24 +83,24 @@ func (m *MongoDB) Gather(acc telegraf.Accumulator) error {
// Preserve backwards compatibility for hostnames without a // Preserve backwards compatibility for hostnames without a
// scheme, broken in go 1.8. Remove in Telegraf 2.0 // scheme, broken in go 1.8. Remove in Telegraf 2.0
serv = "mongodb://" + serv serv = "mongodb://" + serv
log.Printf("W! [inputs.mongodb] Using %q as connection URL; please update your configuration to use an URL", serv) m.Log.Warnf("Using %q as connection URL; please update your configuration to use an URL", serv)
m.Servers[i] = serv m.Servers[i] = serv
} }
u, err := url.Parse(serv) u, err := url.Parse(serv)
if err != nil { if err != nil {
acc.AddError(fmt.Errorf("Unable to parse address %q: %s", serv, err)) m.Log.Errorf("Unable to parse address %q: %s", serv, err.Error())
continue continue
} }
if u.Host == "" { if u.Host == "" {
acc.AddError(fmt.Errorf("Unable to parse address %q", serv)) m.Log.Errorf("Unable to parse address %q", serv)
continue continue
} }
wg.Add(1) wg.Add(1)
go func(srv *Server) { go func(srv *Server) {
defer wg.Done() defer wg.Done()
acc.AddError(m.gatherServer(srv, acc)) m.Log.Error(m.gatherServer(srv, acc))
}(m.getMongoServer(u)) }(m.getMongoServer(u))
} }
@ -110,6 +111,7 @@ func (m *MongoDB) Gather(acc telegraf.Accumulator) error {
func (m *MongoDB) getMongoServer(url *url.URL) *Server { func (m *MongoDB) getMongoServer(url *url.URL) *Server {
if _, ok := m.mongos[url.Host]; !ok { if _, ok := m.mongos[url.Host]; !ok {
m.mongos[url.Host] = &Server{ m.mongos[url.Host] = &Server{
Log: m.Log,
Url: url, Url: url,
} }
} }
@ -126,8 +128,7 @@ func (m *MongoDB) gatherServer(server *Server, acc telegraf.Accumulator) error {
} }
dialInfo, err := mgo.ParseURL(dialAddrs[0]) dialInfo, err := mgo.ParseURL(dialAddrs[0])
if err != nil { if err != nil {
return fmt.Errorf("Unable to parse URL (%s), %s\n", return fmt.Errorf("unable to parse URL %q: %s", dialAddrs[0], err.Error())
dialAddrs[0], err.Error())
} }
dialInfo.Direct = true dialInfo.Direct = true
dialInfo.Timeout = 5 * time.Second dialInfo.Timeout = 5 * time.Second
@ -169,7 +170,7 @@ func (m *MongoDB) gatherServer(server *Server, acc telegraf.Accumulator) error {
sess, err := mgo.DialWithInfo(dialInfo) sess, err := mgo.DialWithInfo(dialInfo)
if err != nil { if err != nil {
return fmt.Errorf("Unable to connect to MongoDB, %s\n", err.Error()) return fmt.Errorf("unable to connect to MongoDB: %s", err.Error())
} }
server.Session = sess server.Session = sess
} }

View File

@ -1,7 +1,7 @@
package mongodb package mongodb
import ( import (
"log" "fmt"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -15,6 +15,8 @@ type Server struct {
Url *url.URL Url *url.URL
Session *mgo.Session Session *mgo.Session
lastResult *MongoStatus lastResult *MongoStatus
Log telegraf.Logger
} }
func (s *Server) getDefaultTags() map[string]string { func (s *Server) getDefaultTags() map[string]string {
@ -31,11 +33,11 @@ func IsAuthorization(err error) bool {
return strings.Contains(err.Error(), "not authorized") return strings.Contains(err.Error(), "not authorized")
} }
func authLogLevel(err error) string { func (s *Server) authLog(err error) {
if IsAuthorization(err) { if IsAuthorization(err) {
return "D!" s.Log.Debug(err.Error())
} else { } else {
return "E!" s.Log.Error(err.Error())
} }
} }
@ -158,30 +160,30 @@ func (s *Server) gatherCollectionStats(colStatsDbs []string) (*ColStats, error)
} }
results := &ColStats{} results := &ColStats{}
for _, db_name := range names { for _, dbName := range names {
if stringInSlice(db_name, colStatsDbs) || len(colStatsDbs) == 0 { if stringInSlice(dbName, colStatsDbs) || len(colStatsDbs) == 0 {
var colls []string var colls []string
colls, err = s.Session.DB(db_name).CollectionNames() colls, err = s.Session.DB(dbName).CollectionNames()
if err != nil { if err != nil {
log.Printf("E! [inputs.mongodb] Error getting collection names: %v", err) s.Log.Errorf("Error getting collection names: %s", err.Error())
continue continue
} }
for _, col_name := range colls { for _, colName := range colls {
col_stat_line := &ColStatsData{} colStatLine := &ColStatsData{}
err = s.Session.DB(db_name).Run(bson.D{ err = s.Session.DB(dbName).Run(bson.D{
{ {
Name: "collStats", Name: "collStats",
Value: col_name, Value: colName,
}, },
}, col_stat_line) }, colStatLine)
if err != nil { if err != nil {
log.Printf("%s [inputs.mongodb] Error getting col stats from %q: %v", authLogLevel(err), col_name, err) s.authLog(fmt.Errorf("error getting col stats from %q: %v", colName, err))
continue continue
} }
collection := &Collection{ collection := &Collection{
Name: col_name, Name: colName,
DbName: db_name, DbName: dbName,
ColStatsData: col_stat_line, ColStatsData: colStatLine,
} }
results.Collections = append(results.Collections, *collection) results.Collections = append(results.Collections, *collection)
} }
@ -203,7 +205,7 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool, gather
// member of a replica set. // member of a replica set.
replSetStatus, err := s.gatherReplSetStatus() replSetStatus, err := s.gatherReplSetStatus()
if err != nil { if err != nil {
log.Printf("D! [inputs.mongodb] Unable to gather replica set status: %v", err) s.Log.Debugf("Unable to gather replica set status: %s", err.Error())
} }
// Gather the oplog if we are a member of a replica set. Non-replica set // Gather the oplog if we are a member of a replica set. Non-replica set
@ -218,13 +220,12 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool, gather
clusterStatus, err := s.gatherClusterStatus() clusterStatus, err := s.gatherClusterStatus()
if err != nil { if err != nil {
log.Printf("D! [inputs.mongodb] Unable to gather cluster status: %v", err) s.Log.Debugf("Unable to gather cluster status: %s", err.Error())
} }
shardStats, err := s.gatherShardConnPoolStats() shardStats, err := s.gatherShardConnPoolStats()
if err != nil { if err != nil {
log.Printf("%s [inputs.mongodb] Unable to gather shard connection pool stats: %v", s.authLog(fmt.Errorf("unable to gather shard connection pool stats: %s", err.Error()))
authLogLevel(err), err)
} }
var collectionStats *ColStats var collectionStats *ColStats
@ -246,7 +247,7 @@ func (s *Server) gatherData(acc telegraf.Accumulator, gatherDbStats bool, gather
for _, name := range names { for _, name := range names {
db, err := s.gatherDBStats(name) db, err := s.gatherDBStats(name)
if err != nil { if err != nil {
log.Printf("D! [inputs.mongodb] Error getting db stats from %q: %v", name, err) s.Log.Debugf("Error getting db stats from %q: %s", name, err.Error())
} }
dbStats.Dbs = append(dbStats.Dbs, *db) dbStats.Dbs = append(dbStats.Dbs, *db)
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
@ -61,6 +60,8 @@ type MQTTConsumer struct {
ClientID string `toml:"client_id"` ClientID string `toml:"client_id"`
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
clientFactory ClientFactory clientFactory ClientFactory
client Client client Client
opts *mqtt.ClientOptions opts *mqtt.ClientOptions
@ -212,7 +213,7 @@ func (m *MQTTConsumer) connect() error {
return err return err
} }
log.Printf("I! [inputs.mqtt_consumer] Connected %v", m.Servers) m.Log.Infof("Connected %v", m.Servers)
m.state = Connected m.state = Connected
m.sem = make(semaphore, m.MaxUndeliveredMessages) m.sem = make(semaphore, m.MaxUndeliveredMessages)
m.messages = make(map[telegraf.TrackingID]bool) m.messages = make(map[telegraf.TrackingID]bool)
@ -223,7 +224,7 @@ func (m *MQTTConsumer) connect() error {
SessionPresent() bool SessionPresent() bool
} }
if t, ok := token.(sessionPresent); ok && t.SessionPresent() { if t, ok := token.(sessionPresent); ok && t.SessionPresent() {
log.Printf("D! [inputs.mqtt_consumer] Session found %v", m.Servers) m.Log.Debugf("Session found %v", m.Servers)
return nil return nil
} }
@ -244,7 +245,7 @@ func (m *MQTTConsumer) connect() error {
func (m *MQTTConsumer) onConnectionLost(c mqtt.Client, err error) { func (m *MQTTConsumer) onConnectionLost(c mqtt.Client, err error) {
m.acc.AddError(fmt.Errorf("connection lost: %v", err)) m.acc.AddError(fmt.Errorf("connection lost: %v", err))
log.Printf("D! [inputs.mqtt_consumer] Disconnected %v", m.Servers) m.Log.Debugf("Disconnected %v", m.Servers)
m.state = Disconnected m.state = Disconnected
return return
} }
@ -292,9 +293,9 @@ func (m *MQTTConsumer) onMessage(acc telegraf.TrackingAccumulator, msg mqtt.Mess
func (m *MQTTConsumer) Stop() { func (m *MQTTConsumer) Stop() {
if m.state == Connected { if m.state == Connected {
log.Printf("D! [inputs.mqtt_consumer] Disconnecting %v", m.Servers) m.Log.Debugf("Disconnecting %v", m.Servers)
m.client.Disconnect(200) m.client.Disconnect(200)
log.Printf("D! [inputs.mqtt_consumer] Disconnected %v", m.Servers) m.Log.Debugf("Disconnected %v", m.Servers)
m.state = Disconnected m.state = Disconnected
} }
m.cancel() m.cancel()
@ -303,7 +304,7 @@ func (m *MQTTConsumer) Stop() {
func (m *MQTTConsumer) Gather(acc telegraf.Accumulator) error { func (m *MQTTConsumer) Gather(acc telegraf.Accumulator) error {
if m.state == Disconnected { if m.state == Disconnected {
m.state = Connecting m.state = Connecting
log.Printf("D! [inputs.mqtt_consumer] Connecting %v", m.Servers) m.Log.Debugf("Connecting %v", m.Servers)
m.connect() m.connect()
} }
@ -346,7 +347,7 @@ func (m *MQTTConsumer) createOpts() (*mqtt.ClientOptions, error) {
for _, server := range m.Servers { for _, server := range m.Servers {
// Preserve support for host:port style servers; deprecated in Telegraf 1.4.4 // Preserve support for host:port style servers; deprecated in Telegraf 1.4.4
if !strings.Contains(server, "://") { if !strings.Contains(server, "://") {
log.Printf("W! [inputs.mqtt_consumer] Server %q should be updated to use `scheme://host:port` format", server) m.Log.Warnf("Server %q should be updated to use `scheme://host:port` format", server)
if tlsCfg == nil { if tlsCfg == nil {
server = "tcp://" + server server = "tcp://" + server
} else { } else {

View File

@ -102,6 +102,7 @@ func TestLifecycleSanity(t *testing.T) {
}, },
} }
}) })
plugin.Log = testutil.Logger{}
plugin.Servers = []string{"tcp://127.0.0.1"} plugin.Servers = []string{"tcp://127.0.0.1"}
parser := &FakeParser{} parser := &FakeParser{}
@ -124,10 +125,12 @@ func TestRandomClientID(t *testing.T) {
var err error var err error
m1 := New(nil) m1 := New(nil)
m1.Log = testutil.Logger{}
err = m1.Init() err = m1.Init()
require.NoError(t, err) require.NoError(t, err)
m2 := New(nil) m2 := New(nil)
m2.Log = testutil.Logger{}
err = m2.Init() err = m2.Init()
require.NoError(t, err) require.NoError(t, err)
@ -137,6 +140,7 @@ func TestRandomClientID(t *testing.T) {
// PersistentSession requires ClientID // PersistentSession requires ClientID
func TestPersistentClientIDFail(t *testing.T) { func TestPersistentClientIDFail(t *testing.T) {
plugin := New(nil) plugin := New(nil)
plugin.Log = testutil.Logger{}
plugin.PersistentSession = true plugin.PersistentSession = true
err := plugin.Init() err := plugin.Init()
@ -255,6 +259,7 @@ func TestTopicTag(t *testing.T) {
plugin := New(func(o *mqtt.ClientOptions) Client { plugin := New(func(o *mqtt.ClientOptions) Client {
return client return client
}) })
plugin.Log = testutil.Logger{}
plugin.Topics = []string{"telegraf"} plugin.Topics = []string{"telegraf"}
plugin.TopicTag = tt.topicTag() plugin.TopicTag = tt.topicTag()
@ -295,6 +300,7 @@ func TestAddRouteCalledForEachTopic(t *testing.T) {
plugin := New(func(o *mqtt.ClientOptions) Client { plugin := New(func(o *mqtt.ClientOptions) Client {
return client return client
}) })
plugin.Log = testutil.Logger{}
plugin.Topics = []string{"a", "b"} plugin.Topics = []string{"a", "b"}
err := plugin.Init() err := plugin.Init()
@ -325,6 +331,7 @@ func TestSubscribeCalledIfNoSession(t *testing.T) {
plugin := New(func(o *mqtt.ClientOptions) Client { plugin := New(func(o *mqtt.ClientOptions) Client {
return client return client
}) })
plugin.Log = testutil.Logger{}
plugin.Topics = []string{"b"} plugin.Topics = []string{"b"}
err := plugin.Init() err := plugin.Init()
@ -355,6 +362,7 @@ func TestSubscribeNotCalledIfSession(t *testing.T) {
plugin := New(func(o *mqtt.ClientOptions) Client { plugin := New(func(o *mqtt.ClientOptions) Client {
return client return client
}) })
plugin.Log = testutil.Logger{}
plugin.Topics = []string{"b"} plugin.Topics = []string{"b"}
err := plugin.Init() err := plugin.Init()

View File

@ -12,8 +12,10 @@ instances of telegraf can read from a NATS cluster in parallel.
[[inputs.nats_consumer]] [[inputs.nats_consumer]]
## urls of NATS servers ## urls of NATS servers
servers = ["nats://localhost:4222"] servers = ["nats://localhost:4222"]
## subject(s) to consume ## subject(s) to consume
subjects = ["telegraf"] subjects = ["telegraf"]
## name a queue group ## name a queue group
queue_group = "telegraf_consumers" queue_group = "telegraf_consumers"

View File

@ -3,7 +3,6 @@ package natsconsumer
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"sync" "sync"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -40,6 +39,8 @@ type natsConsumer struct {
Password string `toml:"password"` Password string `toml:"password"`
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
// Client pending limits: // Client pending limits:
PendingMessageLimit int `toml:"pending_message_limit"` PendingMessageLimit int `toml:"pending_message_limit"`
PendingBytesLimit int `toml:"pending_bytes_limit"` PendingBytesLimit int `toml:"pending_bytes_limit"`
@ -68,6 +69,7 @@ var sampleConfig = `
## subject(s) to consume ## subject(s) to consume
subjects = ["telegraf"] subjects = ["telegraf"]
## name a queue group ## name a queue group
queue_group = "telegraf_consumers" queue_group = "telegraf_consumers"
@ -198,7 +200,7 @@ func (n *natsConsumer) Start(acc telegraf.Accumulator) error {
go n.receiver(ctx) go n.receiver(ctx)
}() }()
log.Printf("I! Started the NATS consumer service, nats: %v, subjects: %v, queue: %v\n", n.Log.Infof("Started the NATS consumer service, nats: %v, subjects: %v, queue: %v",
n.conn.ConnectedUrl(), n.Subjects, n.QueueGroup) n.conn.ConnectedUrl(), n.Subjects, n.QueueGroup)
return nil return nil
@ -216,21 +218,21 @@ func (n *natsConsumer) receiver(ctx context.Context) {
case <-n.acc.Delivered(): case <-n.acc.Delivered():
<-sem <-sem
case err := <-n.errs: case err := <-n.errs:
n.acc.AddError(err) n.Log.Error(err)
case sem <- empty{}: case sem <- empty{}:
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case err := <-n.errs: case err := <-n.errs:
<-sem <-sem
n.acc.AddError(err) n.Log.Error(err)
case <-n.acc.Delivered(): case <-n.acc.Delivered():
<-sem <-sem
<-sem <-sem
case msg := <-n.in: case msg := <-n.in:
metrics, err := n.parser.Parse(msg.Data) metrics, err := n.parser.Parse(msg.Data)
if err != nil { if err != nil {
n.acc.AddError(fmt.Errorf("subject: %s, error: %s", msg.Subject, err.Error())) n.Log.Errorf("Subject: %s, error: %s", msg.Subject, err.Error())
<-sem <-sem
continue continue
} }
@ -244,8 +246,8 @@ func (n *natsConsumer) receiver(ctx context.Context) {
func (n *natsConsumer) clean() { func (n *natsConsumer) clean() {
for _, sub := range n.subs { for _, sub := range n.subs {
if err := sub.Unsubscribe(); err != nil { if err := sub.Unsubscribe(); err != nil {
n.acc.AddError(fmt.Errorf("Error unsubscribing from subject %s in queue %s: %s\n", n.Log.Errorf("Error unsubscribing from subject %s in queue %s: %s",
sub.Subject, sub.Queue, err.Error())) sub.Subject, sub.Queue, err.Error())
} }
} }

View File

@ -10,8 +10,10 @@ of the supported [input data formats][].
[[inputs.nsq_consumer]] [[inputs.nsq_consumer]]
## Server option still works but is deprecated, we just prepend it to the nsqd array. ## Server option still works but is deprecated, we just prepend it to the nsqd array.
# server = "localhost:4150" # server = "localhost:4150"
## An array representing the NSQD TCP HTTP Endpoints ## An array representing the NSQD TCP HTTP Endpoints
nsqd = ["localhost:4150"] nsqd = ["localhost:4150"]
## An array representing the NSQLookupd HTTP Endpoints ## An array representing the NSQLookupd HTTP Endpoints
nsqlookupd = ["localhost:4161"] nsqlookupd = ["localhost:4161"]
topic = "telegraf" topic = "telegraf"

View File

@ -2,7 +2,6 @@ package nsq_consumer
import ( import (
"context" "context"
"log"
"sync" "sync"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -18,10 +17,12 @@ const (
type empty struct{} type empty struct{}
type semaphore chan empty type semaphore chan empty
type logger struct{} type logger struct {
log telegraf.Logger
}
func (l *logger) Output(calldepth int, s string) error { func (l *logger) Output(calldepth int, s string) error {
log.Println("D! [inputs.nsq_consumer] " + s) l.log.Debug(s)
return nil return nil
} }
@ -39,6 +40,8 @@ type NSQConsumer struct {
parser parsers.Parser parser parsers.Parser
consumer *nsq.Consumer consumer *nsq.Consumer
Log telegraf.Logger
mu sync.Mutex mu sync.Mutex
messages map[telegraf.TrackingID]*nsq.Message messages map[telegraf.TrackingID]*nsq.Message
wg sync.WaitGroup wg sync.WaitGroup
@ -48,8 +51,10 @@ type NSQConsumer struct {
var sampleConfig = ` var sampleConfig = `
## Server option still works but is deprecated, we just prepend it to the nsqd array. ## Server option still works but is deprecated, we just prepend it to the nsqd array.
# server = "localhost:4150" # server = "localhost:4150"
## An array representing the NSQD TCP HTTP Endpoints ## An array representing the NSQD TCP HTTP Endpoints
nsqd = ["localhost:4150"] nsqd = ["localhost:4150"]
## An array representing the NSQLookupd HTTP Endpoints ## An array representing the NSQLookupd HTTP Endpoints
nsqlookupd = ["localhost:4161"] nsqlookupd = ["localhost:4161"]
topic = "telegraf" topic = "telegraf"
@ -98,7 +103,7 @@ func (n *NSQConsumer) Start(ac telegraf.Accumulator) error {
n.cancel = cancel n.cancel = cancel
n.connect() n.connect()
n.consumer.SetLogger(&logger{}, nsq.LogLevelInfo) n.consumer.SetLogger(&logger{log: n.Log}, nsq.LogLevelInfo)
n.consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error { n.consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
metrics, err := n.parser.Parse(message.Body) metrics, err := n.parser.Parse(message.Body)
if err != nil { if err != nil {

View File

@ -36,6 +36,7 @@ func TestReadsMetricsFromNSQ(t *testing.T) {
newMockNSQD(script, addr.String()) newMockNSQD(script, addr.String())
consumer := &NSQConsumer{ consumer := &NSQConsumer{
Log: testutil.Logger{},
Server: "127.0.0.1:4155", Server: "127.0.0.1:4155",
Topic: "telegraf", Topic: "telegraf",
Channel: "consume", Channel: "consume",

View File

@ -4,11 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
// register in driver.
_ "github.com/jackc/pgx/stdlib" _ "github.com/jackc/pgx/stdlib"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -23,6 +21,8 @@ type Postgresql struct {
AdditionalTags []string AdditionalTags []string
Query query Query query
Debug bool Debug bool
Log telegraf.Logger
} }
type query []struct { type query []struct {
@ -186,7 +186,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
if p.Query[i].Version <= db_version { if p.Query[i].Version <= db_version {
rows, err := p.DB.Query(sql_query) rows, err := p.DB.Query(sql_query)
if err != nil { if err != nil {
acc.AddError(err) p.Log.Error(err.Error())
continue continue
} }
@ -194,7 +194,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
// grab the column information from the result // grab the column information from the result
if columns, err = rows.Columns(); err != nil { if columns, err = rows.Columns(); err != nil {
acc.AddError(err) p.Log.Error(err.Error())
continue continue
} }
@ -209,7 +209,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
for rows.Next() { for rows.Next() {
err = p.accRow(meas_name, rows, acc, columns) err = p.accRow(meas_name, rows, acc, columns)
if err != nil { if err != nil {
acc.AddError(err) p.Log.Error(err.Error())
break break
} }
} }
@ -272,7 +272,7 @@ func (p *Postgresql) accRow(meas_name string, row scanner, acc telegraf.Accumula
fields := make(map[string]interface{}) fields := make(map[string]interface{})
COLUMN: COLUMN:
for col, val := range columnMap { for col, val := range columnMap {
log.Printf("D! postgresql_extensible: column: %s = %T: %v\n", col, *val, *val) p.Log.Debugf("Column: %s = %T: %v\n", col, *val, *val)
_, ignore := ignoredColumns[col] _, ignore := ignoredColumns[col]
if ignore || *val == nil { if ignore || *val == nil {
continue continue
@ -290,7 +290,7 @@ COLUMN:
case int64, int32, int: case int64, int32, int:
tags[col] = fmt.Sprintf("%d", v) tags[col] = fmt.Sprintf("%d", v)
default: default:
log.Println("failed to add additional tag", col) p.Log.Debugf("Failed to add %q as additional tag", col)
} }
continue COLUMN continue COLUMN
} }

View File

@ -13,6 +13,7 @@ import (
func queryRunner(t *testing.T, q query) *testutil.Accumulator { func queryRunner(t *testing.T, q query) *testutil.Accumulator {
p := &Postgresql{ p := &Postgresql{
Log: testutil.Logger{},
Service: postgresql.Service{ Service: postgresql.Service{
Address: fmt.Sprintf( Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable", "host=%s user=postgres sslmode=disable",
@ -232,6 +233,7 @@ func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
} }
p := &Postgresql{ p := &Postgresql{
Log: testutil.Logger{},
Service: postgresql.Service{ Service: postgresql.Service{
Address: fmt.Sprintf( Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable", "host=%s user=postgres sslmode=disable",
@ -251,7 +253,10 @@ func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
} }
func TestAccRow(t *testing.T) { func TestAccRow(t *testing.T) {
p := Postgresql{} p := Postgresql{
Log: testutil.Logger{},
}
var acc testutil.Accumulator var acc testutil.Accumulator
columns := []string{"datname", "cat"} columns := []string{"datname", "cat"}

View File

@ -110,8 +110,8 @@ func parseResponse(metrics string) map[string]interface{} {
i, err := strconv.ParseInt(m[1], 10, 64) i, err := strconv.ParseInt(m[1], 10, 64)
if err != nil { if err != nil {
log.Printf("E! powerdns: Error parsing integer for metric [%s]: %s", log.Printf("E! [inputs.powerdns] error parsing integer for metric %q: %s",
metric, err) metric, err.Error())
continue continue
} }
values[m[0]] = i values[m[0]] = i

View File

@ -139,8 +139,8 @@ func parseResponse(metrics string) map[string]interface{} {
i, err := strconv.ParseInt(m[1], 10, 64) i, err := strconv.ParseInt(m[1], 10, 64)
if err != nil { if err != nil {
log.Printf("E! [inputs.powerdns_recursor] Error parsing integer for metric [%s] %v", log.Printf("E! [inputs.powerdns_recursor] error parsing integer for metric %q: %s",
metric, err) metric, err.Error())
continue continue
} }
values[m[0]] = i values[m[0]] = i

View File

@ -6,7 +6,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -23,6 +22,8 @@ type Processes struct {
execPS func() ([]byte, error) execPS func() ([]byte, error)
readProcFile func(filename string) ([]byte, error) readProcFile func(filename string) ([]byte, error)
Log telegraf.Logger
forcePS bool forcePS bool
forceProc bool forceProc bool
} }
@ -124,8 +125,7 @@ func (p *Processes) gatherFromPS(fields map[string]interface{}) error {
case '?': case '?':
fields["unknown"] = fields["unknown"].(int64) + int64(1) fields["unknown"] = fields["unknown"].(int64) + int64(1)
default: default:
log.Printf("I! processes: Unknown state [ %s ] from ps", p.Log.Infof("Unknown state %q from ps", string(status[0]))
string(status[0]))
} }
fields["total"] = fields["total"].(int64) + int64(1) fields["total"] = fields["total"].(int64) + int64(1)
} }
@ -184,14 +184,13 @@ func (p *Processes) gatherFromProc(fields map[string]interface{}) error {
} }
fields["parked"] = int64(1) fields["parked"] = int64(1)
default: default:
log.Printf("I! processes: Unknown state [ %s ] in file %s", p.Log.Infof("Unknown state %q in file %q", string(stats[0][0]), filename)
string(stats[0][0]), filename)
} }
fields["total"] = fields["total"].(int64) + int64(1) fields["total"] = fields["total"].(int64) + int64(1)
threads, err := strconv.Atoi(string(stats[17])) threads, err := strconv.Atoi(string(stats[17]))
if err != nil { if err != nil {
log.Printf("I! processes: Error parsing thread count: %s", err) p.Log.Infof("Error parsing thread count: %s", err.Error())
continue continue
} }
fields["total_threads"] = fields["total_threads"].(int64) + int64(threads) fields["total_threads"] = fields["total_threads"].(int64) + int64(threads)

View File

@ -16,6 +16,7 @@ import (
func TestProcesses(t *testing.T) { func TestProcesses(t *testing.T) {
processes := &Processes{ processes := &Processes{
Log: testutil.Logger{},
execPS: execPS, execPS: execPS,
readProcFile: readProcFile, readProcFile: readProcFile,
} }
@ -35,6 +36,7 @@ func TestProcesses(t *testing.T) {
func TestFromPS(t *testing.T) { func TestFromPS(t *testing.T) {
processes := &Processes{ processes := &Processes{
Log: testutil.Logger{},
execPS: testExecPS, execPS: testExecPS,
forcePS: true, forcePS: true,
} }
@ -56,6 +58,7 @@ func TestFromPS(t *testing.T) {
func TestFromPSError(t *testing.T) { func TestFromPSError(t *testing.T) {
processes := &Processes{ processes := &Processes{
Log: testutil.Logger{},
execPS: testExecPSError, execPS: testExecPSError,
forcePS: true, forcePS: true,
} }
@ -71,6 +74,7 @@ func TestFromProcFiles(t *testing.T) {
} }
tester := tester{} tester := tester{}
processes := &Processes{ processes := &Processes{
Log: testutil.Logger{},
readProcFile: tester.testProcFile, readProcFile: tester.testProcFile,
forceProc: true, forceProc: true,
} }
@ -93,6 +97,7 @@ func TestFromProcFilesWithSpaceInCmd(t *testing.T) {
} }
tester := tester{} tester := tester{}
processes := &Processes{ processes := &Processes{
Log: testutil.Logger{},
readProcFile: tester.testProcFile2, readProcFile: tester.testProcFile2,
forceProc: true, forceProc: true,
} }
@ -120,6 +125,7 @@ func TestParkedProcess(t *testing.T) {
procstat := `88 (watchdog/13) P 2 0 0 0 -1 69238848 0 0 0 0 0 0 0 0 20 0 1 0 20 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 1 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 procstat := `88 (watchdog/13) P 2 0 0 0 -1 69238848 0 0 0 0 0 0 0 0 20 0 1 0 20 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 1 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0
` `
plugin := &Processes{ plugin := &Processes{
Log: testutil.Logger{},
readProcFile: func(string) ([]byte, error) { readProcFile: func(string) ([]byte, error) {
return []byte(procstat), nil return []byte(procstat), nil
}, },

View File

@ -68,7 +68,7 @@ func (p *Prometheus) start(ctx context.Context) error {
case <-time.After(time.Second): case <-time.After(time.Second):
err := p.watch(ctx, client) err := p.watch(ctx, client)
if err != nil { if err != nil {
log.Printf("E! [inputs.prometheus] unable to watch resources: %v", err) p.Log.Errorf("Unable to watch resources: %s", err.Error())
} }
} }
} }
@ -144,7 +144,7 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
return return
} }
log.Printf("D! [inputs.prometheus] will scrape metrics from %s", *targetURL) log.Printf("D! [inputs.prometheus] will scrape metrics from %q", *targetURL)
// add annotation as metrics tags // add annotation as metrics tags
tags := pod.GetMetadata().GetAnnotations() tags := pod.GetMetadata().GetAnnotations()
if tags == nil { if tags == nil {
@ -158,7 +158,7 @@ func registerPod(pod *corev1.Pod, p *Prometheus) {
} }
URL, err := url.Parse(*targetURL) URL, err := url.Parse(*targetURL)
if err != nil { if err != nil {
log.Printf("E! [inputs.prometheus] could not parse URL %s: %v", *targetURL, err) log.Printf("E! [inputs.prometheus] could not parse URL %q: %s", *targetURL, err.Error())
return return
} }
podURL := p.AddressToURL(URL, URL.Hostname()) podURL := p.AddressToURL(URL, URL.Hostname())
@ -211,13 +211,13 @@ func unregisterPod(pod *corev1.Pod, p *Prometheus) {
return return
} }
log.Printf("D! [inputs.prometheus] registered a delete request for %s in namespace %s", log.Printf("D! [inputs.prometheus] registered a delete request for %q in namespace %q",
pod.GetMetadata().GetName(), pod.GetMetadata().GetNamespace()) pod.GetMetadata().GetName(), pod.GetMetadata().GetNamespace())
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
if _, ok := p.kubernetesPods[*url]; ok { if _, ok := p.kubernetesPods[*url]; ok {
delete(p.kubernetesPods, *url) delete(p.kubernetesPods, *url)
log.Printf("D! [inputs.prometheus] will stop scraping for %s", *url) log.Printf("D! [inputs.prometheus] will stop scraping for %q", *url)
} }
} }

View File

@ -3,6 +3,7 @@ package prometheus
import ( import (
"testing" "testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
v1 "github.com/ericchiang/k8s/apis/core/v1" v1 "github.com/ericchiang/k8s/apis/core/v1"
@ -53,7 +54,7 @@ func TestScrapeURLAnnotationsCustomPathWithSep(t *testing.T) {
} }
func TestAddPod(t *testing.T) { func TestAddPod(t *testing.T) {
prom := &Prometheus{} prom := &Prometheus{Log: testutil.Logger{}}
p := pod() p := pod()
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"} p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
@ -62,7 +63,7 @@ func TestAddPod(t *testing.T) {
} }
func TestAddMultipleDuplicatePods(t *testing.T) { func TestAddMultipleDuplicatePods(t *testing.T) {
prom := &Prometheus{} prom := &Prometheus{Log: testutil.Logger{}}
p := pod() p := pod()
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"} p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
@ -73,7 +74,7 @@ func TestAddMultipleDuplicatePods(t *testing.T) {
} }
func TestAddMultiplePods(t *testing.T) { func TestAddMultiplePods(t *testing.T) {
prom := &Prometheus{} prom := &Prometheus{Log: testutil.Logger{}}
p := pod() p := pod()
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"} p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}
@ -85,7 +86,7 @@ func TestAddMultiplePods(t *testing.T) {
} }
func TestDeletePods(t *testing.T) { func TestDeletePods(t *testing.T) {
prom := &Prometheus{} prom := &Prometheus{Log: testutil.Logger{}}
p := pod() p := pod()
p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"} p.Metadata.Annotations = map[string]string{"prometheus.io/scrape": "true"}

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -42,6 +41,8 @@ type Prometheus struct {
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
client *http.Client client *http.Client
// Should we scrape Kubernetes services for prometheus annotations // Should we scrape Kubernetes services for prometheus annotations
@ -136,7 +137,7 @@ func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) {
for _, u := range p.URLs { for _, u := range p.URLs {
URL, err := url.Parse(u) URL, err := url.Parse(u)
if err != nil { if err != nil {
log.Printf("prometheus: Could not parse %s, skipping it. Error: %s", u, err.Error()) p.Log.Errorf("Could not parse %q, skipping it. Error: %s", u, err.Error())
continue continue
} }
allURLs[URL.String()] = URLAndAddress{URL: URL, OriginalURL: URL} allURLs[URL.String()] = URLAndAddress{URL: URL, OriginalURL: URL}
@ -157,7 +158,7 @@ func (p *Prometheus) GetAllURLs() (map[string]URLAndAddress, error) {
resolvedAddresses, err := net.LookupHost(URL.Hostname()) resolvedAddresses, err := net.LookupHost(URL.Hostname())
if err != nil { if err != nil {
log.Printf("prometheus: Could not resolve %s, skipping it. Error: %s", URL.Host, err.Error()) p.Log.Errorf("Could not resolve %q, skipping it. Error: %s", URL.Host, err.Error())
continue continue
} }
for _, resolved := range resolvedAddresses { for _, resolved := range resolvedAddresses {

View File

@ -37,6 +37,7 @@ func TestPrometheusGeneratesMetrics(t *testing.T) {
defer ts.Close() defer ts.Close()
p := &Prometheus{ p := &Prometheus{
Log: testutil.Logger{},
URLs: []string{ts.URL}, URLs: []string{ts.URL},
} }
@ -60,6 +61,7 @@ func TestPrometheusGeneratesMetricsWithHostNameTag(t *testing.T) {
defer ts.Close() defer ts.Close()
p := &Prometheus{ p := &Prometheus{
Log: testutil.Logger{},
KubernetesServices: []string{ts.URL}, KubernetesServices: []string{ts.URL},
} }
u, _ := url.Parse(ts.URL) u, _ := url.Parse(ts.URL)
@ -89,6 +91,7 @@ func TestPrometheusGeneratesMetricsAlthoughFirstDNSFails(t *testing.T) {
defer ts.Close() defer ts.Close()
p := &Prometheus{ p := &Prometheus{
Log: testutil.Logger{},
URLs: []string{ts.URL}, URLs: []string{ts.URL},
KubernetesServices: []string{"http://random.telegraf.local:88/metrics"}, KubernetesServices: []string{"http://random.telegraf.local:88/metrics"},
} }

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"log"
"net/url" "net/url"
"regexp" "regexp"
"strconv" "strconv"
@ -23,6 +22,8 @@ type Redis struct {
Password string Password string
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
clients []Client clients []Client
initialized bool initialized bool
} }
@ -101,13 +102,13 @@ func (r *Redis) init(acc telegraf.Accumulator) error {
for i, serv := range r.Servers { for i, serv := range r.Servers {
if !strings.HasPrefix(serv, "tcp://") && !strings.HasPrefix(serv, "unix://") { if !strings.HasPrefix(serv, "tcp://") && !strings.HasPrefix(serv, "unix://") {
log.Printf("W! [inputs.redis]: server URL found without scheme; please update your configuration file") r.Log.Warn("Server URL found without scheme; please update your configuration file")
serv = "tcp://" + serv serv = "tcp://" + serv
} }
u, err := url.Parse(serv) u, err := url.Parse(serv)
if err != nil { if err != nil {
return fmt.Errorf("Unable to parse to address %q: %v", serv, err) return fmt.Errorf("unable to parse to address %q: %s", serv, err.Error())
} }
password := "" password := ""

View File

@ -20,6 +20,7 @@ func TestRedisConnect(t *testing.T) {
addr := fmt.Sprintf(testutil.GetLocalHost() + ":6379") addr := fmt.Sprintf(testutil.GetLocalHost() + ":6379")
r := &Redis{ r := &Redis{
Log: testutil.Logger{},
Servers: []string{addr}, Servers: []string{addr},
} }

View File

@ -3,7 +3,6 @@ package smart
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"path" "path"
"regexp" "regexp"
@ -120,6 +119,7 @@ type Smart struct {
Devices []string Devices []string
UseSudo bool UseSudo bool
Timeout internal.Duration Timeout internal.Duration
Log telegraf.Logger
} }
var sampleConfig = ` var sampleConfig = `
@ -209,10 +209,10 @@ func (m *Smart) scan() ([]string, error) {
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {
dev := strings.Split(line, " ") dev := strings.Split(line, " ")
if len(dev) > 1 && !excludedDev(m.Excludes, strings.TrimSpace(dev[0])) { if len(dev) > 1 && !excludedDev(m.Excludes, strings.TrimSpace(dev[0])) {
log.Printf("D! [inputs.smart] adding device: %+#v", dev) m.Log.Debugf("Adding device: %+#v", dev)
devices = append(devices, strings.TrimSpace(dev[0])) devices = append(devices, strings.TrimSpace(dev[0]))
} else { } else {
log.Printf("D! [inputs.smart] skipping device: %+#v", dev) m.Log.Debugf("Skipping device: %+#v", dev)
} }
} }
return devices, nil return devices, nil

View File

@ -15,6 +15,7 @@ import (
func TestGatherAttributes(t *testing.T) { func TestGatherAttributes(t *testing.T) {
s := NewSmart() s := NewSmart()
s.Log = testutil.Logger{}
s.Path = "smartctl" s.Path = "smartctl"
s.Attributes = true s.Attributes = true
@ -330,6 +331,7 @@ func TestGatherAttributes(t *testing.T) {
func TestGatherNoAttributes(t *testing.T) { func TestGatherNoAttributes(t *testing.T) {
s := NewSmart() s := NewSmart()
s.Log = testutil.Logger{}
s.Path = "smartctl" s.Path = "smartctl"
s.Attributes = false s.Attributes = false

View File

@ -90,7 +90,7 @@ func execCmd(arg0 string, args ...string) ([]byte, error) {
for _, arg := range args { for _, arg := range args {
quoted = append(quoted, fmt.Sprintf("%q", arg)) quoted = append(quoted, fmt.Sprintf("%q", arg))
} }
log.Printf("D! [inputs.snmp] Executing %q %s", arg0, strings.Join(quoted, " ")) log.Printf("D! [inputs.snmp] executing %q %s", arg0, strings.Join(quoted, " "))
} }
out, err := execCommand(arg0, args...).Output() out, err := execCommand(arg0, args...).Output()

View File

@ -1,7 +1,6 @@
package snmp_legacy package snmp_legacy
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net"
@ -24,6 +23,8 @@ type Snmp struct {
Subtable []Subtable Subtable []Subtable
SnmptranslateFile string SnmptranslateFile string
Log telegraf.Logger
nameToOid map[string]string nameToOid map[string]string
initNode Node initNode Node
subTableMap map[string]Subtable subTableMap map[string]Subtable
@ -297,7 +298,7 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
data, err := ioutil.ReadFile(s.SnmptranslateFile) data, err := ioutil.ReadFile(s.SnmptranslateFile)
if err != nil { if err != nil {
log.Printf("E! Reading SNMPtranslate file error: %s", err) s.Log.Errorf("Reading SNMPtranslate file error: %s", err.Error())
return err return err
} else { } else {
for _, line := range strings.Split(string(data), "\n") { for _, line := range strings.Split(string(data), "\n") {
@ -395,16 +396,16 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
// only if len(s.OidInstanceMapping) == 0 // only if len(s.OidInstanceMapping) == 0
if len(host.OidInstanceMapping) >= 0 { if len(host.OidInstanceMapping) >= 0 {
if err := host.SNMPMap(acc, s.nameToOid, s.subTableMap); err != nil { if err := host.SNMPMap(acc, s.nameToOid, s.subTableMap); err != nil {
acc.AddError(fmt.Errorf("E! SNMP Mapping error for host '%s': %s", host.Address, err)) s.Log.Errorf("Mapping error for host %q: %s", host.Address, err.Error())
continue continue
} }
} }
// Launch Get requests // Launch Get requests
if err := host.SNMPGet(acc, s.initNode); err != nil { if err := host.SNMPGet(acc, s.initNode); err != nil {
acc.AddError(fmt.Errorf("E! SNMP Error for host '%s': %s", host.Address, err)) s.Log.Errorf("Error for host %q: %s", host.Address, err.Error())
} }
if err := host.SNMPBulk(acc, s.initNode); err != nil { if err := host.SNMPBulk(acc, s.initNode); err != nil {
acc.AddError(fmt.Errorf("E! SNMP Error for host '%s': %s", host.Address, err)) s.Log.Errorf("Error for host %q: %s", host.Address, err.Error())
} }
} }
return nil return nil
@ -801,7 +802,7 @@ func (h *Host) HandleResponse(
acc.AddFields(field_name, fields, tags) acc.AddFields(field_name, fields, tags)
case gosnmp.NoSuchObject, gosnmp.NoSuchInstance: case gosnmp.NoSuchObject, gosnmp.NoSuchInstance:
// Oid not found // Oid not found
log.Printf("E! [snmp input] Oid not found: %s", oid_key) log.Printf("E! [inputs.snmp_legacy] oid %q not found", oid_key)
default: default:
// delete other data // delete other data
} }

View File

@ -5,7 +5,6 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io" "io"
"log"
"net" "net"
"os" "os"
"strconv" "strconv"
@ -43,7 +42,7 @@ func (ssl *streamSocketListener) listen() {
c, err := ssl.Accept() c, err := ssl.Accept()
if err != nil { if err != nil {
if !strings.HasSuffix(err.Error(), ": use of closed network connection") { if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
ssl.AddError(err) ssl.Log.Error(err.Error())
} }
break break
} }
@ -52,7 +51,7 @@ func (ssl *streamSocketListener) listen() {
if srb, ok := c.(setReadBufferer); ok { if srb, ok := c.(setReadBufferer); ok {
srb.SetReadBuffer(int(ssl.ReadBufferSize.Size)) srb.SetReadBuffer(int(ssl.ReadBufferSize.Size))
} else { } else {
log.Printf("W! Unable to set read buffer on a %s socket", ssl.sockType) ssl.Log.Warnf("Unable to set read buffer on a %s socket", ssl.sockType)
} }
} }
@ -66,7 +65,7 @@ func (ssl *streamSocketListener) listen() {
ssl.connectionsMtx.Unlock() ssl.connectionsMtx.Unlock()
if err := ssl.setKeepAlive(c); err != nil { if err := ssl.setKeepAlive(c); err != nil {
ssl.AddError(fmt.Errorf("unable to configure keep alive (%s): %s", ssl.ServiceAddress, err)) ssl.Log.Errorf("Unable to configure keep alive %q: %s", ssl.ServiceAddress, err.Error())
} }
wg.Add(1) wg.Add(1)
@ -122,7 +121,7 @@ func (ssl *streamSocketListener) read(c net.Conn) {
} }
metrics, err := ssl.Parse(scnr.Bytes()) metrics, err := ssl.Parse(scnr.Bytes())
if err != nil { if err != nil {
ssl.AddError(fmt.Errorf("unable to parse incoming line: %s", err)) ssl.Log.Errorf("Unable to parse incoming line: %s", err.Error())
// TODO rate limit // TODO rate limit
continue continue
} }
@ -133,9 +132,9 @@ func (ssl *streamSocketListener) read(c net.Conn) {
if err := scnr.Err(); err != nil { if err := scnr.Err(); err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() { if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Printf("D! Timeout in plugin [input.socket_listener]: %s", err) ssl.Log.Debugf("Timeout in plugin: %s", err.Error())
} else if netErr != nil && !strings.HasSuffix(err.Error(), ": use of closed network connection") { } else if netErr != nil && !strings.HasSuffix(err.Error(), ": use of closed network connection") {
ssl.AddError(err) ssl.Log.Error(err.Error())
} }
} }
} }
@ -151,14 +150,14 @@ func (psl *packetSocketListener) listen() {
n, _, err := psl.ReadFrom(buf) n, _, err := psl.ReadFrom(buf)
if err != nil { if err != nil {
if !strings.HasSuffix(err.Error(), ": use of closed network connection") { if !strings.HasSuffix(err.Error(), ": use of closed network connection") {
psl.AddError(err) psl.Log.Error(err.Error())
} }
break break
} }
metrics, err := psl.Parse(buf[:n]) metrics, err := psl.Parse(buf[:n])
if err != nil { if err != nil {
psl.AddError(fmt.Errorf("unable to parse incoming packet: %s", err)) psl.Log.Errorf("Unable to parse incoming packet: %s", err.Error())
// TODO rate limit // TODO rate limit
continue continue
} }
@ -179,6 +178,8 @@ type SocketListener struct {
wg sync.WaitGroup wg sync.WaitGroup
Log telegraf.Logger
parsers.Parser parsers.Parser
telegraf.Accumulator telegraf.Accumulator
io.Closer io.Closer
@ -292,7 +293,7 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error {
return err return err
} }
log.Printf("I! [inputs.socket_listener] Listening on %s://%s", protocol, l.Addr()) sl.Log.Infof("Listening on %s://%s", protocol, l.Addr())
// Set permissions on socket // Set permissions on socket
if (spl[0] == "unix" || spl[0] == "unixpacket") && sl.SocketMode != "" { if (spl[0] == "unix" || spl[0] == "unixpacket") && sl.SocketMode != "" {
@ -339,11 +340,11 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error {
if srb, ok := pc.(setReadBufferer); ok { if srb, ok := pc.(setReadBufferer); ok {
srb.SetReadBuffer(int(sl.ReadBufferSize.Size)) srb.SetReadBuffer(int(sl.ReadBufferSize.Size))
} else { } else {
log.Printf("W! Unable to set read buffer on a %s socket", protocol) sl.Log.Warnf("Unable to set read buffer on a %s socket", protocol)
} }
} }
log.Printf("I! [inputs.socket_listener] Listening on %s://%s", protocol, pc.LocalAddr()) sl.Log.Infof("Listening on %s://%s", protocol, pc.LocalAddr())
psl := &packetSocketListener{ psl := &packetSocketListener{
PacketConn: pc, PacketConn: pc,

View File

@ -48,6 +48,7 @@ func TestSocketListener_tcp_tls(t *testing.T) {
defer testEmptyLog(t)() defer testEmptyLog(t)()
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "tcp://127.0.0.1:0" sl.ServiceAddress = "tcp://127.0.0.1:0"
sl.ServerConfig = *pki.TLSServerConfig() sl.ServerConfig = *pki.TLSServerConfig()
@ -72,6 +73,7 @@ func TestSocketListener_unix_tls(t *testing.T) {
sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix_tls.sock") sock := filepath.Join(tmpdir, "sl.TestSocketListener_unix_tls.sock")
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "unix://" + sock sl.ServiceAddress = "unix://" + sock
sl.ServerConfig = *pki.TLSServerConfig() sl.ServerConfig = *pki.TLSServerConfig()
@ -94,6 +96,7 @@ func TestSocketListener_tcp(t *testing.T) {
defer testEmptyLog(t)() defer testEmptyLog(t)()
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "tcp://127.0.0.1:0" sl.ServiceAddress = "tcp://127.0.0.1:0"
sl.ReadBufferSize = internal.Size{Size: 1024} sl.ReadBufferSize = internal.Size{Size: 1024}
@ -112,6 +115,7 @@ func TestSocketListener_udp(t *testing.T) {
defer testEmptyLog(t)() defer testEmptyLog(t)()
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "udp://127.0.0.1:0" sl.ServiceAddress = "udp://127.0.0.1:0"
sl.ReadBufferSize = internal.Size{Size: 1024} sl.ReadBufferSize = internal.Size{Size: 1024}
@ -136,6 +140,7 @@ func TestSocketListener_unix(t *testing.T) {
os.Create(sock) os.Create(sock)
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "unix://" + sock sl.ServiceAddress = "unix://" + sock
sl.ReadBufferSize = internal.Size{Size: 1024} sl.ReadBufferSize = internal.Size{Size: 1024}
@ -160,6 +165,7 @@ func TestSocketListener_unixgram(t *testing.T) {
os.Create(sock) os.Create(sock)
sl := newSocketListener() sl := newSocketListener()
sl.Log = testutil.Logger{}
sl.ServiceAddress = "unixgram://" + sock sl.ServiceAddress = "unixgram://" + sock
sl.ReadBufferSize = internal.Size{Size: 1024} sl.ReadBufferSize = internal.Size{Size: 1024}

View File

@ -3,7 +3,6 @@ package stackdriver
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"math" "math"
"strconv" "strconv"
"strings" "strings"
@ -128,6 +127,8 @@ type (
DistributionAggregationAligners []string `toml:"distribution_aggregation_aligners"` DistributionAggregationAligners []string `toml:"distribution_aggregation_aligners"`
Filter *ListTimeSeriesFilter `toml:"filter"` Filter *ListTimeSeriesFilter `toml:"filter"`
Log telegraf.Logger
client metricClient client metricClient
timeSeriesConfCache *timeSeriesConfCache timeSeriesConfCache *timeSeriesConfCache
prevEnd time.Time prevEnd time.Time
@ -167,6 +168,7 @@ type (
// stackdriverMetricClient is a metric client for stackdriver // stackdriverMetricClient is a metric client for stackdriver
stackdriverMetricClient struct { stackdriverMetricClient struct {
log telegraf.Logger
conn *monitoring.MetricClient conn *monitoring.MetricClient
listMetricDescriptorsCalls selfstat.Stat listMetricDescriptorsCalls selfstat.Stat
@ -206,7 +208,7 @@ func (c *stackdriverMetricClient) ListMetricDescriptors(
mdChan := make(chan *metricpb.MetricDescriptor, 1000) mdChan := make(chan *metricpb.MetricDescriptor, 1000)
go func() { go func() {
log.Printf("D! [inputs.stackdriver] ListMetricDescriptors: %s", req.Filter) c.log.Debugf("List metric descriptor request filter: %s", req.Filter)
defer close(mdChan) defer close(mdChan)
// Iterate over metric descriptors and send them to buffered channel // Iterate over metric descriptors and send them to buffered channel
@ -216,7 +218,7 @@ func (c *stackdriverMetricClient) ListMetricDescriptors(
mdDesc, mdErr := mdResp.Next() mdDesc, mdErr := mdResp.Next()
if mdErr != nil { if mdErr != nil {
if mdErr != iterator.Done { if mdErr != iterator.Done {
log.Printf("E! [inputs.stackdriver] Received error response: %s: %v", req, mdErr) c.log.Errorf("Failed iterating metric desciptor responses: %q: %v", req.String(), mdErr)
} }
break break
} }
@ -235,7 +237,7 @@ func (c *stackdriverMetricClient) ListTimeSeries(
tsChan := make(chan *monitoringpb.TimeSeries, 1000) tsChan := make(chan *monitoringpb.TimeSeries, 1000)
go func() { go func() {
log.Printf("D! [inputs.stackdriver] ListTimeSeries: %s", req.Filter) c.log.Debugf("List time series request filter: %s", req.Filter)
defer close(tsChan) defer close(tsChan)
// Iterate over timeseries and send them to buffered channel // Iterate over timeseries and send them to buffered channel
@ -245,7 +247,7 @@ func (c *stackdriverMetricClient) ListTimeSeries(
tsDesc, tsErr := tsResp.Next() tsDesc, tsErr := tsResp.Next()
if tsErr != nil { if tsErr != nil {
if tsErr != iterator.Done { if tsErr != iterator.Done {
log.Printf("E! [inputs.stackdriver] Received error response: %s: %v", req, tsErr) c.log.Errorf("Failed iterating time series responses: %q: %v", req.String(), tsErr)
} }
break break
} }
@ -458,6 +460,7 @@ func (s *Stackdriver) initializeStackdriverClient(ctx context.Context) error {
"stackdriver", "list_timeseries_calls", tags) "stackdriver", "list_timeseries_calls", tags)
s.client = &stackdriverMetricClient{ s.client = &stackdriverMetricClient{
log: s.Log,
conn: client, conn: client,
listMetricDescriptorsCalls: listMetricDescriptorsCalls, listMetricDescriptorsCalls: listMetricDescriptorsCalls,
listTimeSeriesCalls: listTimeSeriesCalls, listTimeSeriesCalls: listTimeSeriesCalls,

View File

@ -640,6 +640,7 @@ func TestGather(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator
s := &Stackdriver{ s := &Stackdriver{
Log: testutil.Logger{},
Project: "test", Project: "test",
RateLimit: 10, RateLimit: 10,
GatherRawDistributionBuckets: true, GatherRawDistributionBuckets: true,
@ -775,6 +776,7 @@ func TestGatherAlign(t *testing.T) {
} }
s := &Stackdriver{ s := &Stackdriver{
Log: testutil.Logger{},
Project: "test", Project: "test",
RateLimit: 10, RateLimit: 10,
GatherRawDistributionBuckets: false, GatherRawDistributionBuckets: false,

View File

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"log"
"net" "net"
"sort" "sort"
"strconv" "strconv"
@ -34,13 +33,6 @@ const (
MaxTCPConnections = 250 MaxTCPConnections = 250
) )
var dropwarn = "E! [inputs.statsd] Error: statsd message queue full. " +
"We have dropped %d messages so far. " +
"You may want to increase allowed_pending_messages in the config\n"
var malformedwarn = "E! [inputs.statsd] Statsd over TCP has received %d malformed packets" +
" thus far."
// Statsd allows the importing of statsd and dogstatsd data. // Statsd allows the importing of statsd and dogstatsd data.
type Statsd struct { type Statsd struct {
// Protocol used on listener - udp or tcp // Protocol used on listener - udp or tcp
@ -133,6 +125,8 @@ type Statsd struct {
PacketsRecv selfstat.Stat PacketsRecv selfstat.Stat
BytesRecv selfstat.Stat BytesRecv selfstat.Stat
Log telegraf.Logger
// A pool of byte slices to handle parsing // A pool of byte slices to handle parsing
bufPool sync.Pool bufPool sync.Pool
} }
@ -312,7 +306,7 @@ func (s *Statsd) Gather(acc telegraf.Accumulator) error {
func (s *Statsd) Start(ac telegraf.Accumulator) error { func (s *Statsd) Start(ac telegraf.Accumulator) error {
if s.ParseDataDogTags { if s.ParseDataDogTags {
s.DataDogExtensions = true s.DataDogExtensions = true
log.Printf("W! [inputs.statsd] The parse_data_dog_tags option is deprecated, use datadog_extensions instead.") s.Log.Warn("'parse_data_dog_tags' config option is deprecated, please use 'datadog_extensions' instead")
} }
s.acc = ac s.acc = ac
@ -350,8 +344,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
} }
if s.ConvertNames { if s.ConvertNames {
log.Printf("W! [inputs.statsd] statsd: convert_names config option is deprecated," + s.Log.Warn("'convert_names' config option is deprecated, please use 'metric_separator' instead")
" please use metric_separator instead")
} }
if s.MetricSeparator == "" { if s.MetricSeparator == "" {
@ -369,7 +362,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
return err return err
} }
log.Println("I! [inputs.statsd] Statsd UDP listener listening on: ", conn.LocalAddr().String()) s.Log.Infof("UDP listening on %q", conn.LocalAddr().String())
s.UDPlistener = conn s.UDPlistener = conn
s.wg.Add(1) s.wg.Add(1)
@ -387,7 +380,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
return err return err
} }
log.Println("I! [inputs.statsd] TCP Statsd listening on: ", listener.Addr().String()) s.Log.Infof("TCP listening on %q", listener.Addr().String())
s.TCPlistener = listener s.TCPlistener = listener
s.wg.Add(1) s.wg.Add(1)
@ -403,7 +396,7 @@ func (s *Statsd) Start(ac telegraf.Accumulator) error {
defer s.wg.Done() defer s.wg.Done()
s.parser() s.parser()
}() }()
log.Printf("I! [inputs.statsd] Started the statsd service on %s\n", s.ServiceAddress) s.Log.Infof("Started the statsd service on %q", s.ServiceAddress)
return nil return nil
} }
@ -463,7 +456,7 @@ func (s *Statsd) udpListen(conn *net.UDPConn) error {
n, addr, err := conn.ReadFromUDP(buf) n, addr, err := conn.ReadFromUDP(buf)
if err != nil { if err != nil {
if !strings.Contains(err.Error(), "closed network") { if !strings.Contains(err.Error(), "closed network") {
log.Printf("E! [inputs.statsd] Error READ: %s\n", err.Error()) s.Log.Errorf("Error reading: %s", err.Error())
continue continue
} }
return err return err
@ -479,7 +472,9 @@ func (s *Statsd) udpListen(conn *net.UDPConn) error {
default: default:
s.drops++ s.drops++
if s.drops == 1 || s.AllowedPendingMessages == 0 || s.drops%s.AllowedPendingMessages == 0 { if s.drops == 1 || s.AllowedPendingMessages == 0 || s.drops%s.AllowedPendingMessages == 0 {
log.Printf(dropwarn, s.drops) s.Log.Errorf("Statsd message queue full. "+
"We have dropped %d messages so far. "+
"You may want to increase allowed_pending_messages in the config", s.drops)
} }
} }
} }
@ -540,8 +535,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
// Validate splitting the line on ":" // Validate splitting the line on ":"
bits := strings.Split(line, ":") bits := strings.Split(line, ":")
if len(bits) < 2 { if len(bits) < 2 {
log.Printf("E! [inputs.statsd] Error: splitting ':', Unable to parse metric: %s\n", line) s.Log.Errorf("Splitting ':', unable to parse metric: %s", line)
return errors.New("Error Parsing statsd line") return errors.New("error Parsing statsd line")
} }
// Extract bucket name from individual metric bits // Extract bucket name from individual metric bits
@ -556,22 +551,22 @@ func (s *Statsd) parseStatsdLine(line string) error {
// Validate splitting the bit on "|" // Validate splitting the bit on "|"
pipesplit := strings.Split(bit, "|") pipesplit := strings.Split(bit, "|")
if len(pipesplit) < 2 { if len(pipesplit) < 2 {
log.Printf("E! [inputs.statsd] Error: splitting '|', Unable to parse metric: %s\n", line) s.Log.Errorf("Splitting '|', unable to parse metric: %s", line)
return errors.New("Error Parsing statsd line") return errors.New("error parsing statsd line")
} else if len(pipesplit) > 2 { } else if len(pipesplit) > 2 {
sr := pipesplit[2] sr := pipesplit[2]
errmsg := "E! [inputs.statsd] parsing sample rate, %s, it must be in format like: " +
"@0.1, @0.5, etc. Ignoring sample rate for line: %s\n"
if strings.Contains(sr, "@") && len(sr) > 1 { if strings.Contains(sr, "@") && len(sr) > 1 {
samplerate, err := strconv.ParseFloat(sr[1:], 64) samplerate, err := strconv.ParseFloat(sr[1:], 64)
if err != nil { if err != nil {
log.Printf(errmsg, err.Error(), line) s.Log.Errorf("Parsing sample rate: %s", err.Error())
} else { } else {
// sample rate successfully parsed // sample rate successfully parsed
m.samplerate = samplerate m.samplerate = samplerate
} }
} else { } else {
log.Printf(errmsg, "", line) s.Log.Debugf("Sample rate must be in format like: "+
"@0.1, @0.5, etc. Ignoring sample rate for line: %s", line)
} }
} }
@ -580,15 +575,15 @@ func (s *Statsd) parseStatsdLine(line string) error {
case "g", "c", "s", "ms", "h": case "g", "c", "s", "ms", "h":
m.mtype = pipesplit[1] m.mtype = pipesplit[1]
default: default:
log.Printf("E! [inputs.statsd] Error: Statsd Metric type %s unsupported", pipesplit[1]) s.Log.Errorf("Metric type %q unsupported", pipesplit[1])
return errors.New("Error Parsing statsd line") return errors.New("error parsing statsd line")
} }
// Parse the value // Parse the value
if strings.HasPrefix(pipesplit[0], "-") || strings.HasPrefix(pipesplit[0], "+") { if strings.HasPrefix(pipesplit[0], "-") || strings.HasPrefix(pipesplit[0], "+") {
if m.mtype != "g" && m.mtype != "c" { if m.mtype != "g" && m.mtype != "c" {
log.Printf("E! [inputs.statsd] Error: +- values are only supported for gauges & counters: %s\n", line) s.Log.Errorf("+- values are only supported for gauges & counters, unable to parse metric: %s", line)
return errors.New("Error Parsing statsd line") return errors.New("error parsing statsd line")
} }
m.additive = true m.additive = true
} }
@ -597,8 +592,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
case "g", "ms", "h": case "g", "ms", "h":
v, err := strconv.ParseFloat(pipesplit[0], 64) v, err := strconv.ParseFloat(pipesplit[0], 64)
if err != nil { if err != nil {
log.Printf("E! [inputs.statsd] Error: parsing value to float64: %s\n", line) s.Log.Errorf("Parsing value to float64, unable to parse metric: %s", line)
return errors.New("Error Parsing statsd line") return errors.New("error parsing statsd line")
} }
m.floatvalue = v m.floatvalue = v
case "c": case "c":
@ -607,8 +602,8 @@ func (s *Statsd) parseStatsdLine(line string) error {
if err != nil { if err != nil {
v2, err2 := strconv.ParseFloat(pipesplit[0], 64) v2, err2 := strconv.ParseFloat(pipesplit[0], 64)
if err2 != nil { if err2 != nil {
log.Printf("E! [inputs.statsd] Error: parsing value to int64: %s\n", line) s.Log.Errorf("Parsing value to int64, unable to parse metric: %s", line)
return errors.New("Error Parsing statsd line") return errors.New("error parsing statsd line")
} }
v = int64(v2) v = int64(v2)
} }
@ -852,7 +847,9 @@ func (s *Statsd) handler(conn *net.TCPConn, id string) {
default: default:
s.drops++ s.drops++
if s.drops == 1 || s.drops%s.AllowedPendingMessages == 0 { if s.drops == 1 || s.drops%s.AllowedPendingMessages == 0 {
log.Printf(dropwarn, s.drops) s.Log.Errorf("Statsd message queue full. "+
"We have dropped %d messages so far. "+
"You may want to increase allowed_pending_messages in the config", s.drops)
} }
} }
} }
@ -862,9 +859,8 @@ func (s *Statsd) handler(conn *net.TCPConn, id string) {
// refuser refuses a TCP connection // refuser refuses a TCP connection
func (s *Statsd) refuser(conn *net.TCPConn) { func (s *Statsd) refuser(conn *net.TCPConn) {
conn.Close() conn.Close()
log.Printf("I! [inputs.statsd] Refused TCP Connection from %s", conn.RemoteAddr()) s.Log.Infof("Refused TCP Connection from %s", conn.RemoteAddr())
log.Printf("I! [inputs.statsd] WARNING: Maximum TCP Connections reached, you may want to" + s.Log.Warn("Maximum TCP Connections reached, you may want to adjust max_tcp_connections")
" adjust max_tcp_connections")
} }
// forget a TCP connection // forget a TCP connection
@ -883,7 +879,7 @@ func (s *Statsd) remember(id string, conn *net.TCPConn) {
func (s *Statsd) Stop() { func (s *Statsd) Stop() {
s.Lock() s.Lock()
log.Println("I! [inputs.statsd] Stopping the statsd service") s.Log.Infof("Stopping the statsd service")
close(s.done) close(s.done)
if s.isUDP() { if s.isUDP() {
s.UDPlistener.Close() s.UDPlistener.Close()
@ -909,7 +905,7 @@ func (s *Statsd) Stop() {
s.Lock() s.Lock()
close(s.in) close(s.in)
log.Println("I! Stopped Statsd listener service on ", s.ServiceAddress) s.Log.Infof("Stopped listener service on %q", s.ServiceAddress)
s.Unlock() s.Unlock()
} }

View File

@ -18,7 +18,7 @@ const (
) )
func NewTestStatsd() *Statsd { func NewTestStatsd() *Statsd {
s := Statsd{} s := Statsd{Log: testutil.Logger{}}
// Make data structures // Make data structures
s.done = make(chan struct{}) s.done = make(chan struct{})
@ -36,6 +36,7 @@ func NewTestStatsd() *Statsd {
// Test that MaxTCPConections is respected // Test that MaxTCPConections is respected
func TestConcurrentConns(t *testing.T) { func TestConcurrentConns(t *testing.T) {
listener := Statsd{ listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp", Protocol: "tcp",
ServiceAddress: "localhost:8125", ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
@ -66,6 +67,7 @@ func TestConcurrentConns(t *testing.T) {
// Test that MaxTCPConections is respected when max==1 // Test that MaxTCPConections is respected when max==1
func TestConcurrentConns1(t *testing.T) { func TestConcurrentConns1(t *testing.T) {
listener := Statsd{ listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp", Protocol: "tcp",
ServiceAddress: "localhost:8125", ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
@ -94,6 +96,7 @@ func TestConcurrentConns1(t *testing.T) {
// Test that MaxTCPConections is respected // Test that MaxTCPConections is respected
func TestCloseConcurrentConns(t *testing.T) { func TestCloseConcurrentConns(t *testing.T) {
listener := Statsd{ listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp", Protocol: "tcp",
ServiceAddress: "localhost:8125", ServiceAddress: "localhost:8125",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
@ -115,6 +118,7 @@ func TestCloseConcurrentConns(t *testing.T) {
// benchmark how long it takes to accept & process 100,000 metrics: // benchmark how long it takes to accept & process 100,000 metrics:
func BenchmarkUDP(b *testing.B) { func BenchmarkUDP(b *testing.B) {
listener := Statsd{ listener := Statsd{
Log: testutil.Logger{},
Protocol: "udp", Protocol: "udp",
ServiceAddress: "localhost:8125", ServiceAddress: "localhost:8125",
AllowedPendingMessages: 250000, AllowedPendingMessages: 250000,
@ -145,6 +149,7 @@ func BenchmarkUDP(b *testing.B) {
// benchmark how long it takes to accept & process 100,000 metrics: // benchmark how long it takes to accept & process 100,000 metrics:
func BenchmarkTCP(b *testing.B) { func BenchmarkTCP(b *testing.B) {
listener := Statsd{ listener := Statsd{
Log: testutil.Logger{},
Protocol: "tcp", Protocol: "tcp",
ServiceAddress: "localhost:8125", ServiceAddress: "localhost:8125",
AllowedPendingMessages: 250000, AllowedPendingMessages: 250000,
@ -1625,6 +1630,7 @@ func testValidateGauge(
func TestTCP(t *testing.T) { func TestTCP(t *testing.T) {
statsd := Statsd{ statsd := Statsd{
Log: testutil.Logger{},
Protocol: "tcp", Protocol: "tcp",
ServiceAddress: "localhost:0", ServiceAddress: "localhost:0",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,

View File

@ -16,18 +16,15 @@ the created binary data file with the `sadf` utility.
## On Debian and Arch Linux the default path is /usr/lib/sa/sadc whereas ## On Debian and Arch Linux the default path is /usr/lib/sa/sadc whereas
## on RHEL and CentOS the default path is /usr/lib64/sa/sadc ## on RHEL and CentOS the default path is /usr/lib64/sa/sadc
sadc_path = "/usr/lib/sa/sadc" # required sadc_path = "/usr/lib/sa/sadc" # required
#
#
## Path to the sadf command, if it is not in PATH ## Path to the sadf command, if it is not in PATH
# sadf_path = "/usr/bin/sadf" # sadf_path = "/usr/bin/sadf"
#
#
## Activities is a list of activities, that are passed as argument to the ## Activities is a list of activities, that are passed as argument to the
## sadc collector utility (e.g: DISK, SNMP etc...) ## sadc collector utility (e.g: DISK, SNMP etc...)
## The more activities that are added, the more data is collected. ## The more activities that are added, the more data is collected.
# activities = ["DISK"] # activities = ["DISK"]
#
#
## Group metrics to measurements. ## Group metrics to measurements.
## ##
## If group is false each metric will be prefixed with a description ## If group is false each metric will be prefixed with a description
@ -35,8 +32,7 @@ the created binary data file with the `sadf` utility.
## ##
## If Group is true, corresponding metrics are grouped to a single measurement. ## If Group is true, corresponding metrics are grouped to a single measurement.
# group = true # group = true
#
#
## Options for the sadf command. The values on the left represent the sadf options and ## Options for the sadf command. The values on the left represent the sadf options and
## the values on the right their description (wich are used for grouping and prefixing metrics). ## the values on the right their description (wich are used for grouping and prefixing metrics).
## ##
@ -58,8 +54,7 @@ the created binary data file with the `sadf` utility.
-w = "task" -w = "task"
# -H = "hugepages" # only available for newer linux distributions # -H = "hugepages" # only available for newer linux distributions
# "-I ALL" = "interrupts" # requires INT activity # "-I ALL" = "interrupts" # requires INT activity
#
#
## Device tags can be used to add additional tags for devices. For example the configuration below ## Device tags can be used to add additional tags for devices. For example the configuration below
## adds a tag vg with value rootvg for all metrics with sda devices. ## adds a tag vg with value rootvg for all metrics with sda devices.
# [[inputs.sysstat.device_tags.sda]] # [[inputs.sysstat.device_tags.sda]]

View File

@ -7,7 +7,6 @@ import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -67,6 +66,8 @@ type Sysstat struct {
DeviceTags map[string][]map[string]string `toml:"device_tags"` DeviceTags map[string][]map[string]string `toml:"device_tags"`
tmpFile string tmpFile string
interval int interval int
Log telegraf.Logger
} }
func (*Sysstat) Description() string { func (*Sysstat) Description() string {
@ -81,18 +82,15 @@ var sampleConfig = `
## Arch: /usr/lib/sa/sadc ## Arch: /usr/lib/sa/sadc
## RHEL/CentOS: /usr/lib64/sa/sadc ## RHEL/CentOS: /usr/lib64/sa/sadc
sadc_path = "/usr/lib/sa/sadc" # required sadc_path = "/usr/lib/sa/sadc" # required
#
#
## Path to the sadf command, if it is not in PATH ## Path to the sadf command, if it is not in PATH
# sadf_path = "/usr/bin/sadf" # sadf_path = "/usr/bin/sadf"
#
#
## Activities is a list of activities, that are passed as argument to the ## Activities is a list of activities, that are passed as argument to the
## sadc collector utility (e.g: DISK, SNMP etc...) ## sadc collector utility (e.g: DISK, SNMP etc...)
## The more activities that are added, the more data is collected. ## The more activities that are added, the more data is collected.
# activities = ["DISK"] # activities = ["DISK"]
#
#
## Group metrics to measurements. ## Group metrics to measurements.
## ##
## If group is false each metric will be prefixed with a description ## If group is false each metric will be prefixed with a description
@ -100,8 +98,7 @@ var sampleConfig = `
## ##
## If Group is true, corresponding metrics are grouped to a single measurement. ## If Group is true, corresponding metrics are grouped to a single measurement.
# group = true # group = true
#
#
## Options for the sadf command. The values on the left represent the sadf ## Options for the sadf command. The values on the left represent the sadf
## options and the values on the right their description (which are used for ## options and the values on the right their description (which are used for
## grouping and prefixing metrics). ## grouping and prefixing metrics).
@ -125,8 +122,7 @@ var sampleConfig = `
-w = "task" -w = "task"
# -H = "hugepages" # only available for newer linux distributions # -H = "hugepages" # only available for newer linux distributions
# "-I ALL" = "interrupts" # requires INT activity # "-I ALL" = "interrupts" # requires INT activity
#
#
## Device tags can be used to add additional tags for devices. ## Device tags can be used to add additional tags for devices.
## For example the configuration below adds a tag vg with value rootvg for ## For example the configuration below adds a tag vg with value rootvg for
## all metrics with sda devices. ## all metrics with sda devices.
@ -196,7 +192,7 @@ func (s *Sysstat) collect() error {
out, err := internal.CombinedOutputTimeout(cmd, time.Second*time.Duration(collectInterval+parseInterval)) out, err := internal.CombinedOutputTimeout(cmd, time.Second*time.Duration(collectInterval+parseInterval))
if err != nil { if err != nil {
if err := os.Remove(s.tmpFile); err != nil { if err := os.Remove(s.tmpFile); err != nil {
log.Printf("E! failed to remove tmp file after %s command: %s", strings.Join(cmd.Args, " "), err) s.Log.Errorf("Failed to remove tmp file after %q command: %s", strings.Join(cmd.Args, " "), err.Error())
} }
return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out)) return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out))
} }

View File

@ -13,6 +13,7 @@ import (
) )
var s = Sysstat{ var s = Sysstat{
Log: testutil.Logger{},
interval: 10, interval: 10,
Sadc: "/usr/lib/sa/sadc", Sadc: "/usr/lib/sa/sadc",
Sadf: "/usr/bin/sadf", Sadf: "/usr/bin/sadf",

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
"time" "time"
@ -16,20 +15,22 @@ import (
"github.com/shirou/gopsutil/load" "github.com/shirou/gopsutil/load"
) )
type SystemStats struct{} type SystemStats struct {
Log telegraf.Logger
}
func (_ *SystemStats) Description() string { func (*SystemStats) Description() string {
return "Read metrics about system load & uptime" return "Read metrics about system load & uptime"
} }
func (_ *SystemStats) SampleConfig() string { func (*SystemStats) SampleConfig() string {
return ` return `
## Uncomment to remove deprecated metrics. ## Uncomment to remove deprecated metrics.
# fielddrop = ["uptime_format"] # fielddrop = ["uptime_format"]
` `
} }
func (_ *SystemStats) Gather(acc telegraf.Accumulator) error { func (s *SystemStats) Gather(acc telegraf.Accumulator) error {
loadavg, err := load.Avg() loadavg, err := load.Avg()
if err != nil && !strings.Contains(err.Error(), "not implemented") { if err != nil && !strings.Contains(err.Error(), "not implemented") {
return err return err
@ -51,9 +52,9 @@ func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
if err == nil { if err == nil {
fields["n_users"] = len(users) fields["n_users"] = len(users)
} else if os.IsNotExist(err) { } else if os.IsNotExist(err) {
log.Printf("D! [inputs.system] Error reading users: %v", err) s.Log.Debugf("Reading users: %s", err.Error())
} else if os.IsPermission(err) { } else if os.IsPermission(err) {
log.Printf("D! [inputs.system] %v", err) s.Log.Debug(err.Error())
} }
now := time.Now() now := time.Now()

View File

@ -3,8 +3,6 @@
package tail package tail
import ( import (
"fmt"
"log"
"strings" "strings"
"sync" "sync"
@ -31,6 +29,8 @@ type Tail struct {
Pipe bool Pipe bool
WatchMethod string WatchMethod string
Log telegraf.Logger
tailers map[string]*tail.Tail tailers map[string]*tail.Tail
offsets map[string]int64 offsets map[string]int64
parserFunc parsers.ParserFunc parserFunc parsers.ParserFunc
@ -124,7 +124,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
for _, filepath := range t.Files { for _, filepath := range t.Files {
g, err := globpath.Compile(filepath) g, err := globpath.Compile(filepath)
if err != nil { if err != nil {
t.acc.AddError(fmt.Errorf("glob %s failed to compile, %s", filepath, err)) t.Log.Errorf("Glob %q failed to compile: %s", filepath, err.Error())
} }
for _, file := range g.Match() { for _, file := range g.Match() {
if _, ok := t.tailers[file]; ok { if _, ok := t.tailers[file]; ok {
@ -135,7 +135,7 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
var seek *tail.SeekInfo var seek *tail.SeekInfo
if !t.Pipe && !fromBeginning { if !t.Pipe && !fromBeginning {
if offset, ok := t.offsets[file]; ok { if offset, ok := t.offsets[file]; ok {
log.Printf("D! [inputs.tail] using offset %d for file: %v", offset, file) t.Log.Debugf("Using offset %d for %q", offset, file)
seek = &tail.SeekInfo{ seek = &tail.SeekInfo{
Whence: 0, Whence: 0,
Offset: offset, Offset: offset,
@ -163,11 +163,11 @@ func (t *Tail) tailNewFiles(fromBeginning bool) error {
continue continue
} }
log.Printf("D! [inputs.tail] tail added for file: %v", file) t.Log.Debugf("Tail added for %q", file)
parser, err := t.parserFunc() parser, err := t.parserFunc()
if err != nil { if err != nil {
t.acc.AddError(fmt.Errorf("error creating parser: %v", err)) t.Log.Errorf("Creating parser: %s", err.Error())
} }
// create a goroutine for each "tailer" // create a goroutine for each "tailer"
@ -213,7 +213,7 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
var firstLine = true var firstLine = true
for line := range tailer.Lines { for line := range tailer.Lines {
if line.Err != nil { if line.Err != nil {
t.acc.AddError(fmt.Errorf("error tailing file %s, Error: %s", tailer.Filename, line.Err)) t.Log.Errorf("Tailing %q: %s", tailer.Filename, line.Err.Error())
continue continue
} }
// Fix up files with Windows line endings. // Fix up files with Windows line endings.
@ -221,8 +221,8 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
metrics, err := parseLine(parser, text, firstLine) metrics, err := parseLine(parser, text, firstLine)
if err != nil { if err != nil {
t.acc.AddError(fmt.Errorf("malformed log line in %s: [%s], Error: %s", t.Log.Errorf("Malformed log line in %q: [%q]: %s",
tailer.Filename, line.Text, err)) tailer.Filename, line.Text, err.Error())
continue continue
} }
firstLine = false firstLine = false
@ -233,10 +233,10 @@ func (t *Tail) receiver(parser parsers.Parser, tailer *tail.Tail) {
} }
} }
log.Printf("D! [inputs.tail] tail removed for file: %v", tailer.Filename) t.Log.Debugf("Tail removed for %q", tailer.Filename)
if err := tailer.Err(); err != nil { if err := tailer.Err(); err != nil {
t.acc.AddError(fmt.Errorf("error tailing file %s, Error: %s", tailer.Filename, err)) t.Log.Errorf("Tailing %q: %s", tailer.Filename, err.Error())
} }
} }
@ -249,14 +249,14 @@ func (t *Tail) Stop() {
// store offset for resume // store offset for resume
offset, err := tailer.Tell() offset, err := tailer.Tell()
if err == nil { if err == nil {
log.Printf("D! [inputs.tail] recording offset %d for file: %v", offset, tailer.Filename) t.Log.Debugf("Recording offset %d for %q", offset, tailer.Filename)
} else { } else {
t.acc.AddError(fmt.Errorf("error recording offset for file %s", tailer.Filename)) t.Log.Errorf("Recording offset for %q: %s", tailer.Filename, err.Error())
} }
} }
err := tailer.Stop() err := tailer.Stop()
if err != nil { if err != nil {
t.acc.AddError(fmt.Errorf("error stopping tail on file %s", tailer.Filename)) t.Log.Errorf("Stopping tail on %q: %s", tailer.Filename, err.Error())
} }
} }

View File

@ -1,7 +1,9 @@
package tail package tail
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"runtime" "runtime"
"testing" "testing"
@ -28,6 +30,7 @@ func TestTailFromBeginning(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
tt := NewTail() tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()} tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser) tt.SetParserFunc(parsers.NewInfluxParser)
@ -61,6 +64,7 @@ func TestTailFromEnd(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
tt := NewTail() tt := NewTail()
tt.Log = testutil.Logger{}
tt.Files = []string{tmpfile.Name()} tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser) tt.SetParserFunc(parsers.NewInfluxParser)
defer tt.Stop() defer tt.Stop()
@ -97,6 +101,7 @@ func TestTailBadLine(t *testing.T) {
defer os.Remove(tmpfile.Name()) defer os.Remove(tmpfile.Name())
tt := NewTail() tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()} tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser) tt.SetParserFunc(parsers.NewInfluxParser)
@ -105,13 +110,17 @@ func TestTailBadLine(t *testing.T) {
acc := testutil.Accumulator{} acc := testutil.Accumulator{}
require.NoError(t, tt.Start(&acc)) require.NoError(t, tt.Start(&acc))
buf := &bytes.Buffer{}
log.SetOutput(buf)
require.NoError(t, acc.GatherError(tt.Gather)) require.NoError(t, acc.GatherError(tt.Gather))
_, err = tmpfile.WriteString("cpu mytag= foo usage_idle= 100\n") _, err = tmpfile.WriteString("cpu mytag= foo usage_idle= 100\n")
require.NoError(t, err) require.NoError(t, err)
acc.WaitError(1) time.Sleep(500 * time.Millisecond)
assert.Contains(t, acc.Errors[0].Error(), "malformed log line") assert.Contains(t, buf.String(), "Malformed log line")
} }
func TestTailDosLineendings(t *testing.T) { func TestTailDosLineendings(t *testing.T) {
@ -122,6 +131,7 @@ func TestTailDosLineendings(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
tt := NewTail() tt := NewTail()
tt.Log = testutil.Logger{}
tt.FromBeginning = true tt.FromBeginning = true
tt.Files = []string{tmpfile.Name()} tt.Files = []string{tmpfile.Name()}
tt.SetParserFunc(parsers.NewInfluxParser) tt.SetParserFunc(parsers.NewInfluxParser)
@ -160,6 +170,7 @@ cpu,42
require.NoError(t, err) require.NoError(t, err)
plugin := NewTail() plugin := NewTail()
plugin.Log = testutil.Logger{}
plugin.FromBeginning = true plugin.FromBeginning = true
plugin.Files = []string{tmpfile.Name()} plugin.Files = []string{tmpfile.Name()}
plugin.SetParserFunc(func() (parsers.Parser, error) { plugin.SetParserFunc(func() (parsers.Parser, error) {
@ -217,6 +228,7 @@ func TestMultipleMetricsOnFirstLine(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
plugin := NewTail() plugin := NewTail()
plugin.Log = testutil.Logger{}
plugin.FromBeginning = true plugin.FromBeginning = true
plugin.Files = []string{tmpfile.Name()} plugin.Files = []string{tmpfile.Name()}
plugin.SetParserFunc(func() (parsers.Parser, error) { plugin.SetParserFunc(func() (parsers.Parser, error) {

View File

@ -48,13 +48,15 @@ type TcpListener struct {
TotalConnections selfstat.Stat TotalConnections selfstat.Stat
PacketsRecv selfstat.Stat PacketsRecv selfstat.Stat
BytesRecv selfstat.Stat BytesRecv selfstat.Stat
Log telegraf.Logger
} }
var dropwarn = "E! Error: tcp_listener message queue full. " + var dropwarn = "tcp_listener message queue full. " +
"We have dropped %d messages so far. " + "We have dropped %d messages so far. " +
"You may want to increase allowed_pending_messages in the config\n" "You may want to increase allowed_pending_messages in the config"
var malformedwarn = "E! tcp_listener has received %d malformed packets" + var malformedwarn = "tcp_listener has received %d malformed packets" +
" thus far." " thus far."
const sampleConfig = ` const sampleConfig = `
@ -114,16 +116,15 @@ func (t *TcpListener) Start(acc telegraf.Accumulator) error {
address, _ := net.ResolveTCPAddr("tcp", t.ServiceAddress) address, _ := net.ResolveTCPAddr("tcp", t.ServiceAddress)
t.listener, err = net.ListenTCP("tcp", address) t.listener, err = net.ListenTCP("tcp", address)
if err != nil { if err != nil {
log.Fatalf("ERROR: ListenUDP - %s", err) t.Log.Errorf("Failed to listen: %s", err.Error())
return err return err
} }
log.Println("I! TCP server listening on: ", t.listener.Addr().String())
t.wg.Add(2) t.wg.Add(2)
go t.tcpListen() go t.tcpListen()
go t.tcpParser() go t.tcpParser()
log.Printf("I! Started TCP listener service on %s\n", t.ServiceAddress) t.Log.Infof("Started TCP listener service on %q", t.ServiceAddress)
return nil return nil
} }
@ -150,7 +151,7 @@ func (t *TcpListener) Stop() {
t.wg.Wait() t.wg.Wait()
close(t.in) close(t.in)
log.Println("I! Stopped TCP listener service on ", t.ServiceAddress) t.Log.Infof("Stopped TCP listener service on %q", t.ServiceAddress)
} }
// tcpListen listens for incoming TCP connections. // tcpListen listens for incoming TCP connections.
@ -191,9 +192,8 @@ func (t *TcpListener) refuser(conn *net.TCPConn) {
" reached, closing.\nYou may want to increase max_tcp_connections in"+ " reached, closing.\nYou may want to increase max_tcp_connections in"+
" the Telegraf tcp listener configuration.\n", t.MaxTCPConnections) " the Telegraf tcp listener configuration.\n", t.MaxTCPConnections)
conn.Close() conn.Close()
log.Printf("I! Refused TCP Connection from %s", conn.RemoteAddr()) t.Log.Infof("Refused TCP Connection from %s", conn.RemoteAddr())
log.Printf("I! WARNING: Maximum TCP Connections reached, you may want to" + t.Log.Warn("Maximum TCP Connections reached, you may want to adjust max_tcp_connections")
" adjust max_tcp_connections")
} }
// handler handles a single TCP Connection // handler handles a single TCP Connection
@ -235,7 +235,7 @@ func (t *TcpListener) handler(conn *net.TCPConn, id string) {
default: default:
t.drops++ t.drops++
if t.drops == 1 || t.drops%t.AllowedPendingMessages == 0 { if t.drops == 1 || t.drops%t.AllowedPendingMessages == 0 {
log.Printf(dropwarn, t.drops) t.Log.Errorf(dropwarn, t.drops)
} }
} }
} }
@ -268,7 +268,7 @@ func (t *TcpListener) tcpParser() error {
} else { } else {
t.malformed++ t.malformed++
if t.malformed == 1 || t.malformed%1000 == 0 { if t.malformed == 1 || t.malformed%1000 == 0 {
log.Printf(malformedwarn, t.malformed) t.Log.Errorf(malformedwarn, t.malformed)
} }
} }
} }

View File

@ -33,6 +33,7 @@ cpu_load_short,host=server06 value=12.0 1422568543702900257
func newTestTcpListener() (*TcpListener, chan []byte) { func newTestTcpListener() (*TcpListener, chan []byte) {
in := make(chan []byte, 1500) in := make(chan []byte, 1500)
listener := &TcpListener{ listener := &TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8194", ServiceAddress: "localhost:8194",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
MaxTCPConnections: 250, MaxTCPConnections: 250,
@ -45,6 +46,7 @@ func newTestTcpListener() (*TcpListener, chan []byte) {
// benchmark how long it takes to accept & process 100,000 metrics: // benchmark how long it takes to accept & process 100,000 metrics:
func BenchmarkTCP(b *testing.B) { func BenchmarkTCP(b *testing.B) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8198", ServiceAddress: "localhost:8198",
AllowedPendingMessages: 100000, AllowedPendingMessages: 100000,
MaxTCPConnections: 250, MaxTCPConnections: 250,
@ -76,6 +78,7 @@ func BenchmarkTCP(b *testing.B) {
func TestHighTrafficTCP(t *testing.T) { func TestHighTrafficTCP(t *testing.T) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8199", ServiceAddress: "localhost:8199",
AllowedPendingMessages: 100000, AllowedPendingMessages: 100000,
MaxTCPConnections: 250, MaxTCPConnections: 250,
@ -103,6 +106,7 @@ func TestHighTrafficTCP(t *testing.T) {
func TestConnectTCP(t *testing.T) { func TestConnectTCP(t *testing.T) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8194", ServiceAddress: "localhost:8194",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
MaxTCPConnections: 250, MaxTCPConnections: 250,
@ -140,6 +144,7 @@ func TestConnectTCP(t *testing.T) {
// Test that MaxTCPConections is respected // Test that MaxTCPConections is respected
func TestConcurrentConns(t *testing.T) { func TestConcurrentConns(t *testing.T) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8195", ServiceAddress: "localhost:8195",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
MaxTCPConnections: 2, MaxTCPConnections: 2,
@ -175,6 +180,7 @@ func TestConcurrentConns(t *testing.T) {
// Test that MaxTCPConections is respected when max==1 // Test that MaxTCPConections is respected when max==1
func TestConcurrentConns1(t *testing.T) { func TestConcurrentConns1(t *testing.T) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8196", ServiceAddress: "localhost:8196",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
MaxTCPConnections: 1, MaxTCPConnections: 1,
@ -208,6 +214,7 @@ func TestConcurrentConns1(t *testing.T) {
// Test that MaxTCPConections is respected // Test that MaxTCPConections is respected
func TestCloseConcurrentConns(t *testing.T) { func TestCloseConcurrentConns(t *testing.T) {
listener := TcpListener{ listener := TcpListener{
Log: testutil.Logger{},
ServiceAddress: "localhost:8195", ServiceAddress: "localhost:8195",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
MaxTCPConnections: 2, MaxTCPConnections: 2,

View File

@ -53,17 +53,19 @@ type UdpListener struct {
PacketsRecv selfstat.Stat PacketsRecv selfstat.Stat
BytesRecv selfstat.Stat BytesRecv selfstat.Stat
Log telegraf.Logger
} }
// UDP_MAX_PACKET_SIZE is packet limit, see // UDP_MAX_PACKET_SIZE is packet limit, see
// https://en.wikipedia.org/wiki/User_Datagram_Protocol#Packet_structure // https://en.wikipedia.org/wiki/User_Datagram_Protocol#Packet_structure
const UDP_MAX_PACKET_SIZE int = 64 * 1024 const UDP_MAX_PACKET_SIZE int = 64 * 1024
var dropwarn = "E! Error: udp_listener message queue full. " + var dropwarn = "udp_listener message queue full. " +
"We have dropped %d messages so far. " + "We have dropped %d messages so far. " +
"You may want to increase allowed_pending_messages in the config\n" "You may want to increase allowed_pending_messages in the config"
var malformedwarn = "E! udp_listener has received %d malformed packets" + var malformedwarn = "udp_listener has received %d malformed packets" +
" thus far." " thus far."
const sampleConfig = ` const sampleConfig = `
@ -113,7 +115,7 @@ func (u *UdpListener) Start(acc telegraf.Accumulator) error {
u.wg.Add(1) u.wg.Add(1)
go u.udpParser() go u.udpParser()
log.Printf("I! Started UDP listener service on %s (ReadBuffer: %d)\n", u.ServiceAddress, u.UDPBufferSize) u.Log.Infof("Started service on %q (ReadBuffer: %d)", u.ServiceAddress, u.UDPBufferSize)
return nil return nil
} }
@ -124,7 +126,7 @@ func (u *UdpListener) Stop() {
u.wg.Wait() u.wg.Wait()
u.listener.Close() u.listener.Close()
close(u.in) close(u.in)
log.Println("I! Stopped UDP listener service on ", u.ServiceAddress) u.Log.Infof("Stopped service on %q", u.ServiceAddress)
} }
func (u *UdpListener) udpListen() error { func (u *UdpListener) udpListen() error {
@ -134,15 +136,15 @@ func (u *UdpListener) udpListen() error {
u.listener, err = net.ListenUDP("udp", address) u.listener, err = net.ListenUDP("udp", address)
if err != nil { if err != nil {
return fmt.Errorf("E! Error: ListenUDP - %s", err) return err
} }
log.Println("I! UDP server listening on: ", u.listener.LocalAddr().String()) u.Log.Infof("Server listening on %q", u.listener.LocalAddr().String())
if u.UDPBufferSize > 0 { if u.UDPBufferSize > 0 {
err = u.listener.SetReadBuffer(u.UDPBufferSize) // if we want to move away from OS default err = u.listener.SetReadBuffer(u.UDPBufferSize) // if we want to move away from OS default
if err != nil { if err != nil {
return fmt.Errorf("E! Failed to set UDP read buffer to %d: %s", u.UDPBufferSize, err) return fmt.Errorf("failed to set UDP read buffer to %d: %s", u.UDPBufferSize, err)
} }
} }
@ -166,7 +168,7 @@ func (u *UdpListener) udpListenLoop() {
if err != nil { if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() { if err, ok := err.(net.Error); ok && err.Timeout() {
} else { } else {
log.Printf("E! Error: %s\n", err.Error()) u.Log.Error(err.Error())
} }
continue continue
} }
@ -180,7 +182,7 @@ func (u *UdpListener) udpListenLoop() {
default: default:
u.drops++ u.drops++
if u.drops == 1 || u.drops%u.AllowedPendingMessages == 0 { if u.drops == 1 || u.drops%u.AllowedPendingMessages == 0 {
log.Printf(dropwarn, u.drops) u.Log.Errorf(dropwarn, u.drops)
} }
} }
} }
@ -208,7 +210,7 @@ func (u *UdpListener) udpParser() error {
} else { } else {
u.malformed++ u.malformed++
if u.malformed == 1 || u.malformed%1000 == 0 { if u.malformed == 1 || u.malformed%1000 == 0 {
log.Printf(malformedwarn, u.malformed) u.Log.Errorf(malformedwarn, u.malformed)
} }
} }
} }

View File

@ -31,6 +31,7 @@ cpu_load_short,host=server06 value=12.0 1422568543702900257
func newTestUdpListener() (*UdpListener, chan []byte) { func newTestUdpListener() (*UdpListener, chan []byte) {
in := make(chan []byte, 1500) in := make(chan []byte, 1500)
listener := &UdpListener{ listener := &UdpListener{
Log: testutil.Logger{},
ServiceAddress: ":8125", ServiceAddress: ":8125",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
in: in, in: in,
@ -78,6 +79,7 @@ func newTestUdpListener() (*UdpListener, chan []byte) {
func TestConnectUDP(t *testing.T) { func TestConnectUDP(t *testing.T) {
listener := UdpListener{ listener := UdpListener{
Log: testutil.Logger{},
ServiceAddress: ":8127", ServiceAddress: ":8127",
AllowedPendingMessages: 10000, AllowedPendingMessages: 10000,
} }

View File

@ -4,13 +4,13 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"log"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/influxdata/telegraf"
"github.com/vmware/govmomi" "github.com/vmware/govmomi"
"github.com/vmware/govmomi/object" "github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/performance" "github.com/vmware/govmomi/performance"
@ -45,6 +45,7 @@ type Client struct {
Valid bool Valid bool
Timeout time.Duration Timeout time.Duration
closeGate sync.Once closeGate sync.Once
log telegraf.Logger
} }
// NewClientFactory creates a new ClientFactory and prepares it for use. // NewClientFactory creates a new ClientFactory and prepares it for use.
@ -76,7 +77,7 @@ func (cf *ClientFactory) GetClient(ctx context.Context) (*Client, error) {
ctx1, cancel1 := context.WithTimeout(ctx, cf.parent.Timeout.Duration) ctx1, cancel1 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
defer cancel1() defer cancel1()
if _, err := methods.GetCurrentTime(ctx1, cf.client.Client); err != nil { if _, err := methods.GetCurrentTime(ctx1, cf.client.Client); err != nil {
log.Printf("I! [inputs.vsphere]: Client session seems to have time out. Reauthenticating!") cf.parent.Log.Info("Client session seems to have time out. Reauthenticating!")
ctx2, cancel2 := context.WithTimeout(ctx, cf.parent.Timeout.Duration) ctx2, cancel2 := context.WithTimeout(ctx, cf.parent.Timeout.Duration)
defer cancel2() defer cancel2()
if err := cf.client.Client.SessionManager.Login(ctx2, url.UserPassword(cf.parent.Username, cf.parent.Password)); err != nil { if err := cf.client.Client.SessionManager.Login(ctx2, url.UserPassword(cf.parent.Username, cf.parent.Password)); err != nil {
@ -88,7 +89,7 @@ func (cf *ClientFactory) GetClient(ctx context.Context) (*Client, error) {
cf.client = nil cf.client = nil
continue continue
} }
return nil, fmt.Errorf("Renewing authentication failed: %v", err) return nil, fmt.Errorf("renewing authentication failed: %s", err.Error())
} }
} }
@ -113,7 +114,7 @@ func NewClient(ctx context.Context, u *url.URL, vs *VSphere) (*Client, error) {
u.User = url.UserPassword(vs.Username, vs.Password) u.User = url.UserPassword(vs.Username, vs.Password)
} }
log.Printf("D! [inputs.vsphere]: Creating client: %s", u.Host) vs.Log.Debugf("Creating client: %s", u.Host)
soapClient := soap.NewClient(u, tlsCfg.InsecureSkipVerify) soapClient := soap.NewClient(u, tlsCfg.InsecureSkipVerify)
// Add certificate if we have it. Use it to log us in. // Add certificate if we have it. Use it to log us in.
@ -170,6 +171,7 @@ func NewClient(ctx context.Context, u *url.URL, vs *VSphere) (*Client, error) {
p := performance.NewManager(c.Client) p := performance.NewManager(c.Client)
client := &Client{ client := &Client{
log: vs.Log,
Client: c, Client: c,
Views: m, Views: m,
Root: v, Root: v,
@ -184,9 +186,9 @@ func NewClient(ctx context.Context, u *url.URL, vs *VSphere) (*Client, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("D! [inputs.vsphere] vCenter says max_query_metrics should be %d", n) vs.Log.Debugf("vCenter says max_query_metrics should be %d", n)
if n < vs.MaxQueryMetrics { if n < vs.MaxQueryMetrics {
log.Printf("W! [inputs.vsphere] Configured max_query_metrics is %d, but server limits it to %d. Reducing.", vs.MaxQueryMetrics, n) vs.Log.Warnf("Configured max_query_metrics is %d, but server limits it to %d. Reducing.", vs.MaxQueryMetrics, n)
vs.MaxQueryMetrics = n vs.MaxQueryMetrics = n
} }
return client, nil return client, nil
@ -202,7 +204,6 @@ func (cf *ClientFactory) Close() {
} }
func (c *Client) close() { func (c *Client) close() {
// Use a Once to prevent us from panics stemming from trying // Use a Once to prevent us from panics stemming from trying
// to close it multiple times. // to close it multiple times.
c.closeGate.Do(func() { c.closeGate.Do(func() {
@ -210,7 +211,7 @@ func (c *Client) close() {
defer cancel() defer cancel()
if c.Client != nil { if c.Client != nil {
if err := c.Client.Logout(ctx); err != nil { if err := c.Client.Logout(ctx); err != nil {
log.Printf("E! [inputs.vsphere]: Error during logout: %s", err) c.log.Errorf("Logout: %s", err.Error())
} }
} }
}) })
@ -239,7 +240,7 @@ func (c *Client) GetMaxQueryMetrics(ctx context.Context) (int, error) {
if s, ok := res[0].GetOptionValue().Value.(string); ok { if s, ok := res[0].GetOptionValue().Value.(string); ok {
v, err := strconv.Atoi(s) v, err := strconv.Atoi(s)
if err == nil { if err == nil {
log.Printf("D! [inputs.vsphere] vCenter maxQueryMetrics is defined: %d", v) c.log.Debugf("vCenter maxQueryMetrics is defined: %d", v)
if v == -1 { if v == -1 {
// Whatever the server says, we never ask for more metrics than this. // Whatever the server says, we never ask for more metrics than this.
return absoluteMaxMetrics, nil return absoluteMaxMetrics, nil
@ -250,17 +251,17 @@ func (c *Client) GetMaxQueryMetrics(ctx context.Context) (int, error) {
// Fall through version-based inference if value isn't usable // Fall through version-based inference if value isn't usable
} }
} else { } else {
log.Println("D! [inputs.vsphere] Option query for maxQueryMetrics failed. Using default") c.log.Debug("Option query for maxQueryMetrics failed. Using default")
} }
// No usable maxQueryMetrics setting. Infer based on version // No usable maxQueryMetrics setting. Infer based on version
ver := c.Client.Client.ServiceContent.About.Version ver := c.Client.Client.ServiceContent.About.Version
parts := strings.Split(ver, ".") parts := strings.Split(ver, ".")
if len(parts) < 2 { if len(parts) < 2 {
log.Printf("W! [inputs.vsphere] vCenter returned an invalid version string: %s. Using default query size=64", ver) c.log.Warnf("vCenter returned an invalid version string: %s. Using default query size=64", ver)
return 64, nil return 64, nil
} }
log.Printf("D! [inputs.vsphere] vCenter version is: %s", ver) c.log.Debugf("vCenter version is: %s", ver)
major, err := strconv.Atoi(parts[0]) major, err := strconv.Atoi(parts[0])
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -250,10 +250,10 @@ func (e *Endpoint) startDiscovery(ctx context.Context) {
case <-e.discoveryTicker.C: case <-e.discoveryTicker.C:
err := e.discover(ctx) err := e.discover(ctx)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
log.Printf("E! [inputs.vsphere]: Error in discovery for %s: %v", e.URL.Host, err) e.Parent.Log.Errorf("Discovery for %s: %s", e.URL.Host, err.Error())
} }
case <-ctx.Done(): case <-ctx.Done():
log.Printf("D! [inputs.vsphere]: Exiting discovery goroutine for %s", e.URL.Host) e.Parent.Log.Debugf("Exiting discovery goroutine for %s", e.URL.Host)
e.discoveryTicker.Stop() e.discoveryTicker.Stop()
return return
} }
@ -264,7 +264,7 @@ func (e *Endpoint) startDiscovery(ctx context.Context) {
func (e *Endpoint) initalDiscovery(ctx context.Context) { func (e *Endpoint) initalDiscovery(ctx context.Context) {
err := e.discover(ctx) err := e.discover(ctx)
if err != nil && err != context.Canceled { if err != nil && err != context.Canceled {
log.Printf("E! [inputs.vsphere]: Error in discovery for %s: %v", e.URL.Host, err) e.Parent.Log.Errorf("Discovery for %s: %s", e.URL.Host, err.Error())
} }
e.startDiscovery(ctx) e.startDiscovery(ctx)
} }
@ -279,7 +279,7 @@ func (e *Endpoint) init(ctx context.Context) error {
if e.customAttrEnabled { if e.customAttrEnabled {
fields, err := client.GetCustomFields(ctx) fields, err := client.GetCustomFields(ctx)
if err != nil { if err != nil {
log.Println("W! [inputs.vsphere] Could not load custom field metadata") e.Parent.Log.Warn("Could not load custom field metadata")
} else { } else {
e.customFields = fields e.customFields = fields
} }
@ -291,7 +291,7 @@ func (e *Endpoint) init(ctx context.Context) error {
// goroutine without waiting for it. This will probably cause us to report an empty // goroutine without waiting for it. This will probably cause us to report an empty
// dataset on the first collection, but it solves the issue of the first collection timing out. // dataset on the first collection, but it solves the issue of the first collection timing out.
if e.Parent.ForceDiscoverOnInit { if e.Parent.ForceDiscoverOnInit {
log.Printf("D! [inputs.vsphere]: Running initial discovery and waiting for it to finish") e.Parent.Log.Debug("Running initial discovery and waiting for it to finish")
e.initalDiscovery(ctx) e.initalDiscovery(ctx)
} else { } else {
// Otherwise, just run it in the background. We'll probably have an incomplete first metric // Otherwise, just run it in the background. We'll probably have an incomplete first metric
@ -354,7 +354,7 @@ func (e *Endpoint) getDatacenterName(ctx context.Context, client *Client, cache
defer cancel1() defer cancel1()
err := o.Properties(ctx1, here, []string{"parent", "name"}, &result) err := o.Properties(ctx1, here, []string{"parent", "name"}, &result)
if err != nil { if err != nil {
log.Printf("W! [inputs.vsphere]: Error while resolving parent. Assuming no parent exists. Error: %s", err) e.Parent.Log.Warnf("Error while resolving parent. Assuming no parent exists. Error: %s", err.Error())
break break
} }
if result.Reference().Type == "Datacenter" { if result.Reference().Type == "Datacenter" {
@ -363,7 +363,7 @@ func (e *Endpoint) getDatacenterName(ctx context.Context, client *Client, cache
break break
} }
if result.Parent == nil { if result.Parent == nil {
log.Printf("D! [inputs.vsphere]: No parent found for %s (ascending from %s)", here.Reference(), r.Reference()) e.Parent.Log.Debugf("No parent found for %s (ascending from %s)", here.Reference(), r.Reference())
break break
} }
here = result.Parent.Reference() here = result.Parent.Reference()
@ -393,7 +393,7 @@ func (e *Endpoint) discover(ctx context.Context) error {
return err return err
} }
log.Printf("D! [inputs.vsphere]: Discover new objects for %s", e.URL.Host) e.Parent.Log.Debugf("Discover new objects for %s", e.URL.Host)
dcNameCache := make(map[string]string) dcNameCache := make(map[string]string)
numRes := int64(0) numRes := int64(0)
@ -401,7 +401,7 @@ func (e *Endpoint) discover(ctx context.Context) error {
// Populate resource objects, and endpoint instance info. // Populate resource objects, and endpoint instance info.
newObjects := make(map[string]objectMap) newObjects := make(map[string]objectMap)
for k, res := range e.resourceKinds { for k, res := range e.resourceKinds {
log.Printf("D! [inputs.vsphere] Discovering resources for %s", res.name) e.Parent.Log.Debugf("Discovering resources for %s", res.name)
// Need to do this for all resource types even if they are not enabled // Need to do this for all resource types even if they are not enabled
if res.enabled || k != "vm" { if res.enabled || k != "vm" {
rf := ResourceFilter{ rf := ResourceFilter{
@ -457,7 +457,7 @@ func (e *Endpoint) discover(ctx context.Context) error {
if e.customAttrEnabled { if e.customAttrEnabled {
fields, err = client.GetCustomFields(ctx) fields, err = client.GetCustomFields(ctx)
if err != nil { if err != nil {
log.Println("W! [inputs.vsphere] Could not load custom field metadata") e.Parent.Log.Warn("Could not load custom field metadata")
fields = nil fields = nil
} }
} }
@ -481,10 +481,10 @@ func (e *Endpoint) discover(ctx context.Context) error {
} }
func (e *Endpoint) simpleMetadataSelect(ctx context.Context, client *Client, res *resourceKind) { func (e *Endpoint) simpleMetadataSelect(ctx context.Context, client *Client, res *resourceKind) {
log.Printf("D! [inputs.vsphere] Using fast metric metadata selection for %s", res.name) e.Parent.Log.Debugf("Using fast metric metadata selection for %s", res.name)
m, err := client.CounterInfoByName(ctx) m, err := client.CounterInfoByName(ctx)
if err != nil { if err != nil {
log.Printf("E! [inputs.vsphere]: Error while getting metric metadata. Discovery will be incomplete. Error: %s", err) e.Parent.Log.Errorf("Getting metric metadata. Discovery will be incomplete. Error: %s", err.Error())
return return
} }
res.metrics = make(performance.MetricList, 0, len(res.include)) res.metrics = make(performance.MetricList, 0, len(res.include))
@ -500,7 +500,7 @@ func (e *Endpoint) simpleMetadataSelect(ctx context.Context, client *Client, res
} }
res.metrics = append(res.metrics, cnt) res.metrics = append(res.metrics, cnt)
} else { } else {
log.Printf("W! [inputs.vsphere] Metric name %s is unknown. Will not be collected", s) e.Parent.Log.Warnf("Metric name %s is unknown. Will not be collected", s)
} }
} }
} }
@ -533,7 +533,7 @@ func (e *Endpoint) complexMetadataSelect(ctx context.Context, res *resourceKind,
te.Run(ctx, func() { te.Run(ctx, func() {
metrics, err := e.getMetadata(ctx, obj, res.sampling) metrics, err := e.getMetadata(ctx, obj, res.sampling)
if err != nil { if err != nil {
log.Printf("E! [inputs.vsphere]: Error while getting metric metadata. Discovery will be incomplete. Error: %s", err) e.Parent.Log.Errorf("Getting metric metadata. Discovery will be incomplete. Error: %s", err.Error())
} }
mMap := make(map[string]types.PerfMetricId) mMap := make(map[string]types.PerfMetricId)
for _, m := range metrics { for _, m := range metrics {
@ -546,7 +546,7 @@ func (e *Endpoint) complexMetadataSelect(ctx context.Context, res *resourceKind,
mMap[strconv.Itoa(int(m.CounterId))+"|"+m.Instance] = m mMap[strconv.Itoa(int(m.CounterId))+"|"+m.Instance] = m
} }
} }
log.Printf("D! [inputs.vsphere] Found %d metrics for %s", len(mMap), obj.name) e.Parent.Log.Debugf("Found %d metrics for %s", len(mMap), obj.name)
instInfoMux.Lock() instInfoMux.Lock()
defer instInfoMux.Unlock() defer instInfoMux.Unlock()
if len(mMap) > len(res.metrics) { if len(mMap) > len(res.metrics) {
@ -605,7 +605,7 @@ func getClusters(ctx context.Context, e *Endpoint, filter *ResourceFilter) (obje
defer cancel3() defer cancel3()
err = o.Properties(ctx3, *r.Parent, []string{"parent"}, &folder) err = o.Properties(ctx3, *r.Parent, []string{"parent"}, &folder)
if err != nil { if err != nil {
log.Printf("W! [inputs.vsphere] Error while getting folder parent: %e", err) e.Parent.Log.Warnf("Error while getting folder parent: %s", err.Error())
p = nil p = nil
} else { } else {
pp := folder.Parent.Reference() pp := folder.Parent.Reference()
@ -702,7 +702,7 @@ func getVMs(ctx context.Context, e *Endpoint, filter *ResourceFilter) (objectMap
} }
key, ok := e.customFields[val.Key] key, ok := e.customFields[val.Key]
if !ok { if !ok {
log.Printf("W! [inputs.vsphere] Metadata for custom field %d not found. Skipping", val.Key) e.Parent.Log.Warnf("Metadata for custom field %d not found. Skipping", val.Key)
continue continue
} }
if e.customAttrFilter.Match(key) { if e.customAttrFilter.Match(key) {
@ -847,7 +847,7 @@ func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Tim
// Make sure endtime is always after start time. We may occasionally see samples from the future // Make sure endtime is always after start time. We may occasionally see samples from the future
// returned from vCenter. This is presumably due to time drift between vCenter and EXSi nodes. // returned from vCenter. This is presumably due to time drift between vCenter and EXSi nodes.
if pq.StartTime.After(*pq.EndTime) { if pq.StartTime.After(*pq.EndTime) {
log.Printf("D! [inputs.vsphere] Future sample. Res: %s, StartTime: %s, EndTime: %s, Now: %s", pq.Entity, *pq.StartTime, *pq.EndTime, now) e.Parent.Log.Debugf("Future sample. Res: %s, StartTime: %s, EndTime: %s, Now: %s", pq.Entity, *pq.StartTime, *pq.EndTime, now)
end := start.Add(time.Second) end := start.Add(time.Second)
pq.EndTime = &end pq.EndTime = &end
} }
@ -861,7 +861,7 @@ func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Tim
// 2) We are at the last resource and have no more data to process. // 2) We are at the last resource and have no more data to process.
// 3) The query contains more than 100,000 individual metrics // 3) The query contains more than 100,000 individual metrics
if mr > 0 || nRes >= e.Parent.MaxQueryObjects || len(pqs) > 100000 { if mr > 0 || nRes >= e.Parent.MaxQueryObjects || len(pqs) > 100000 {
log.Printf("D! [inputs.vsphere]: Queueing query: %d objects, %d metrics (%d remaining) of type %s for %s. Processed objects: %d. Total objects %d", e.Parent.Log.Debugf("Queueing query: %d objects, %d metrics (%d remaining) of type %s for %s. Processed objects: %d. Total objects %d",
len(pqs), metrics, mr, res.name, e.URL.Host, total+1, len(res.objects)) len(pqs), metrics, mr, res.name, e.URL.Host, total+1, len(res.objects))
// Don't send work items if the context has been cancelled. // Don't send work items if the context has been cancelled.
@ -882,7 +882,7 @@ func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Tim
// Handle final partially filled chunk // Handle final partially filled chunk
if len(pqs) > 0 { if len(pqs) > 0 {
// Run collection job // Run collection job
log.Printf("D! [inputs.vsphere]: Queuing query: %d objects, %d metrics (0 remaining) of type %s for %s. Total objects %d (final chunk)", e.Parent.Log.Debugf("Queuing query: %d objects, %d metrics (0 remaining) of type %s for %s. Total objects %d (final chunk)",
len(pqs), metrics, res.name, e.URL.Host, len(res.objects)) len(pqs), metrics, res.name, e.URL.Host, len(res.objects))
submitChunkJob(ctx, te, job, pqs) submitChunkJob(ctx, te, job, pqs)
} }
@ -914,18 +914,18 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
if estInterval < s { if estInterval < s {
estInterval = s estInterval = s
} }
log.Printf("D! [inputs.vsphere] Raw interval %s, padded: %s, estimated: %s", rawInterval, paddedInterval, estInterval) e.Parent.Log.Debugf("Raw interval %s, padded: %s, estimated: %s", rawInterval, paddedInterval, estInterval)
} }
log.Printf("D! [inputs.vsphere] Interval estimated to %s", estInterval) e.Parent.Log.Debugf("Interval estimated to %s", estInterval)
res.lastColl = localNow res.lastColl = localNow
latest := res.latestSample latest := res.latestSample
if !latest.IsZero() { if !latest.IsZero() {
elapsed := now.Sub(latest).Seconds() + 5.0 // Allow 5 second jitter. elapsed := now.Sub(latest).Seconds() + 5.0 // Allow 5 second jitter.
log.Printf("D! [inputs.vsphere]: Latest: %s, elapsed: %f, resource: %s", latest, elapsed, resourceType) e.Parent.Log.Debugf("Latest: %s, elapsed: %f, resource: %s", latest, elapsed, resourceType)
if !res.realTime && elapsed < float64(res.sampling) { if !res.realTime && elapsed < float64(res.sampling) {
// No new data would be available. We're outta here! // No new data would be available. We're outta here!
log.Printf("D! [inputs.vsphere]: Sampling period for %s of %d has not elapsed on %s", e.Parent.Log.Debugf("Sampling period for %s of %d has not elapsed on %s",
resourceType, res.sampling, e.URL.Host) resourceType, res.sampling, e.URL.Host)
return nil return nil
} }
@ -936,7 +936,7 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
internalTags := map[string]string{"resourcetype": resourceType} internalTags := map[string]string{"resourcetype": resourceType}
sw := NewStopwatchWithTags("gather_duration", e.URL.Host, internalTags) sw := NewStopwatchWithTags("gather_duration", e.URL.Host, internalTags)
log.Printf("D! [inputs.vsphere]: Collecting metrics for %d objects of type %s for %s", e.Parent.Log.Debugf("Collecting metrics for %d objects of type %s for %s",
len(res.objects), resourceType, e.URL.Host) len(res.objects), resourceType, e.URL.Host)
count := int64(0) count := int64(0)
@ -948,9 +948,9 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
e.chunkify(ctx, res, now, latest, acc, e.chunkify(ctx, res, now, latest, acc,
func(chunk []types.PerfQuerySpec) { func(chunk []types.PerfQuerySpec) {
n, localLatest, err := e.collectChunk(ctx, chunk, res, acc, now, estInterval) n, localLatest, err := e.collectChunk(ctx, chunk, res, acc, now, estInterval)
log.Printf("D! [inputs.vsphere] CollectChunk for %s returned %d metrics", resourceType, n) e.Parent.Log.Debugf("CollectChunk for %s returned %d metrics", resourceType, n)
if err != nil { if err != nil {
acc.AddError(errors.New("While collecting " + res.name + ": " + err.Error())) acc.AddError(errors.New("while collecting " + res.name + ": " + err.Error()))
} }
atomic.AddInt64(&count, int64(n)) atomic.AddInt64(&count, int64(n))
tsMux.Lock() tsMux.Lock()
@ -960,7 +960,7 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
} }
}) })
log.Printf("D! [inputs.vsphere] Latest sample for %s set to %s", resourceType, latestSample) e.Parent.Log.Debugf("Latest sample for %s set to %s", resourceType, latestSample)
if !latestSample.IsZero() { if !latestSample.IsZero() {
res.latestSample = latestSample res.latestSample = latestSample
} }
@ -1004,12 +1004,11 @@ func alignSamples(info []types.PerfSampleInfo, values []int64, interval time.Dur
lastBucket = roundedTs lastBucket = roundedTs
} }
} }
//log.Printf("D! [inputs.vsphere] Aligned samples: %d collapsed into %d", len(info), len(rInfo))
return rInfo, rValues return rInfo, rValues
} }
func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec, res *resourceKind, acc telegraf.Accumulator, now time.Time, interval time.Duration) (int, time.Time, error) { func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec, res *resourceKind, acc telegraf.Accumulator, now time.Time, interval time.Duration) (int, time.Time, error) {
log.Printf("D! [inputs.vsphere] Query for %s has %d QuerySpecs", res.name, len(pqs)) e.Parent.Log.Debugf("Query for %s has %d QuerySpecs", res.name, len(pqs))
latestSample := time.Time{} latestSample := time.Time{}
count := 0 count := 0
resourceType := res.name resourceType := res.name
@ -1030,14 +1029,14 @@ func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec,
return count, latestSample, err return count, latestSample, err
} }
log.Printf("D! [inputs.vsphere] Query for %s returned metrics for %d objects", resourceType, len(ems)) e.Parent.Log.Debugf("Query for %s returned metrics for %d objects", resourceType, len(ems))
// Iterate through results // Iterate through results
for _, em := range ems { for _, em := range ems {
moid := em.Entity.Reference().Value moid := em.Entity.Reference().Value
instInfo, found := res.objects[moid] instInfo, found := res.objects[moid]
if !found { if !found {
log.Printf("E! [inputs.vsphere]: MOID %s not found in cache. Skipping! (This should not happen!)", moid) e.Parent.Log.Errorf("MOID %s not found in cache. Skipping! (This should not happen!)", moid)
continue continue
} }
buckets := make(map[string]metricEntry) buckets := make(map[string]metricEntry)
@ -1052,7 +1051,7 @@ func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec,
// Populate tags // Populate tags
objectRef, ok := res.objects[moid] objectRef, ok := res.objects[moid]
if !ok { if !ok {
log.Printf("E! [inputs.vsphere]: MOID %s not found in cache. Skipping", moid) e.Parent.Log.Errorf("MOID %s not found in cache. Skipping", moid)
continue continue
} }
e.populateTags(&objectRef, resourceType, res, t, &v) e.populateTags(&objectRef, resourceType, res, t, &v)
@ -1064,7 +1063,7 @@ func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec,
// According to the docs, SampleInfo and Value should have the same length, but we've seen corrupted // According to the docs, SampleInfo and Value should have the same length, but we've seen corrupted
// data coming back with missing values. Take care of that gracefully! // data coming back with missing values. Take care of that gracefully!
if idx >= len(alignedValues) { if idx >= len(alignedValues) {
log.Printf("D! [inputs.vsphere] len(SampleInfo)>len(Value) %d > %d", len(alignedInfo), len(alignedValues)) e.Parent.Log.Debugf("Len(SampleInfo)>len(Value) %d > %d", len(alignedInfo), len(alignedValues))
break break
} }
ts := sample.Timestamp ts := sample.Timestamp
@ -1085,7 +1084,7 @@ func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec,
// Percentage values must be scaled down by 100. // Percentage values must be scaled down by 100.
info, ok := metricInfo[name] info, ok := metricInfo[name]
if !ok { if !ok {
log.Printf("E! [inputs.vsphere]: Could not determine unit for %s. Skipping", name) e.Parent.Log.Errorf("Could not determine unit for %s. Skipping", name)
} }
v := alignedValues[idx] v := alignedValues[idx]
if info.UnitInfo.GetElementDescription().Key == "percent" { if info.UnitInfo.GetElementDescription().Key == "percent" {
@ -1103,7 +1102,7 @@ func (e *Endpoint) collectChunk(ctx context.Context, pqs []types.PerfQuerySpec,
e.hwMarks.Put(moid, ts) e.hwMarks.Put(moid, ts)
} }
if nValues == 0 { if nValues == 0 {
log.Printf("D! [inputs.vsphere]: Missing value for: %s, %s", name, objectRef.name) e.Parent.Log.Debugf("Missing value for: %s, %s", name, objectRef.name)
continue continue
} }
} }

View File

@ -2,7 +2,6 @@ package vsphere
import ( import (
"context" "context"
"log"
"reflect" "reflect"
"strings" "strings"
@ -54,7 +53,7 @@ func (f *Finder) Find(ctx context.Context, resType, path string, dst interface{}
return err return err
} }
objectContentToTypedArray(objs, dst) objectContentToTypedArray(objs, dst)
log.Printf("D! [inputs.vsphere] Find(%s, %s) returned %d objects", resType, path, len(objs)) f.client.log.Debugf("Find(%s, %s) returned %d objects", resType, path, len(objs))
return nil return nil
} }

View File

@ -34,7 +34,7 @@ func (t *TSCache) Purge() {
n++ n++
} }
} }
log.Printf("D! [inputs.vsphere] Purged timestamp cache. %d deleted with %d remaining", n, len(t.table)) log.Printf("D! [inputs.vsphere] purged timestamp cache. %d deleted with %d remaining", n, len(t.table))
} }
// IsNew returns true if the supplied timestamp for the supplied key is more recent than the // IsNew returns true if the supplied timestamp for the supplied key is more recent than the

View File

@ -2,7 +2,6 @@ package vsphere
import ( import (
"context" "context"
"log"
"sync" "sync"
"time" "time"
@ -58,6 +57,8 @@ type VSphere struct {
// Mix in the TLS/SSL goodness from core // Mix in the TLS/SSL goodness from core
tls.ClientConfig tls.ClientConfig
Log telegraf.Logger
} }
var sampleConfig = ` var sampleConfig = `
@ -243,7 +244,7 @@ func (v *VSphere) Description() string {
// Start is called from telegraf core when a plugin is started and allows it to // Start is called from telegraf core when a plugin is started and allows it to
// perform initialization tasks. // perform initialization tasks.
func (v *VSphere) Start(acc telegraf.Accumulator) error { func (v *VSphere) Start(acc telegraf.Accumulator) error {
log.Println("D! [inputs.vsphere]: Starting plugin") v.Log.Info("Starting plugin")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
v.cancel = cancel v.cancel = cancel
@ -266,7 +267,7 @@ func (v *VSphere) Start(acc telegraf.Accumulator) error {
// Stop is called from telegraf core when a plugin is stopped and allows it to // Stop is called from telegraf core when a plugin is stopped and allows it to
// perform shutdown tasks. // perform shutdown tasks.
func (v *VSphere) Stop() { func (v *VSphere) Stop() {
log.Println("D! [inputs.vsphere]: Stopping plugin") v.Log.Info("Stopping plugin")
v.cancel() v.cancel()
// Wait for all endpoints to finish. No need to wait for // Wait for all endpoints to finish. No need to wait for
@ -275,7 +276,7 @@ func (v *VSphere) Stop() {
// wait for any discovery to complete by trying to grab the // wait for any discovery to complete by trying to grab the
// "busy" mutex. // "busy" mutex.
for _, ep := range v.endpoints { for _, ep := range v.endpoints {
log.Printf("D! [inputs.vsphere]: Waiting for endpoint %s to finish", ep.URL.Host) v.Log.Debugf("Waiting for endpoint %q to finish", ep.URL.Host)
func() { func() {
ep.busy.Lock() // Wait until discovery is finished ep.busy.Lock() // Wait until discovery is finished
defer ep.busy.Unlock() defer ep.busy.Unlock()

Some files were not shown because too many files have changed in this diff Show More