Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix linter findings for revive:exported in plugins/inputs/[t-v]* #16408

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions plugins/inputs/tacacs/tacacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

//go:embed sample.conf
var sampleConfig string

type Tacacs struct {
Servers []string `toml:"servers"`
Username config.Secret `toml:"username"`
Expand All @@ -31,9 +34,6 @@ type Tacacs struct {
authStart tacplus.AuthenStart
}

//go:embed sample.conf
var sampleConfig string

func (*Tacacs) SampleConfig() string {
return sampleConfig
}
Expand Down Expand Up @@ -74,7 +74,22 @@ func (t *Tacacs) Init() error {
return nil
}

func AuthenReplyToString(code uint8) string {
func (t *Tacacs) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

for idx := range t.clients {
wg.Add(1)
go func(client *tacplus.Client) {
defer wg.Done()
acc.AddError(t.pollServer(acc, client))
}(&t.clients[idx])
}

wg.Wait()
return nil
}

func authenReplyToString(code uint8) string {
switch code {
case tacplus.AuthenStatusPass:
return `AuthenStatusPass`
Expand All @@ -96,21 +111,6 @@ func AuthenReplyToString(code uint8) string {
return "AuthenStatusUnknown(" + strconv.FormatUint(uint64(code), 10) + ")"
}

func (t *Tacacs) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup

for idx := range t.clients {
wg.Add(1)
go func(client *tacplus.Client) {
defer wg.Done()
acc.AddError(t.pollServer(acc, client))
}(&t.clients[idx])
}

wg.Wait()
return nil
}

func (t *Tacacs) pollServer(acc telegraf.Accumulator, client *tacplus.Client) error {
// Create the fields for this metric
tags := map[string]string{"source": client.Addr}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (t *Tacacs) pollServer(acc telegraf.Accumulator, client *tacplus.Client) er
defer session.Close()
if reply.Status != tacplus.AuthenStatusGetUser {
fields["responsetime_ms"] = time.Since(startTime).Milliseconds()
fields["response_status"] = AuthenReplyToString(reply.Status)
fields["response_status"] = authenReplyToString(reply.Status)
acc.AddFields("tacacs", fields, tags)
return nil
}
Expand All @@ -174,7 +174,7 @@ func (t *Tacacs) pollServer(acc telegraf.Accumulator, client *tacplus.Client) er
}
if reply.Status != tacplus.AuthenStatusGetPass {
fields["responsetime_ms"] = time.Since(startTime).Milliseconds()
fields["response_status"] = AuthenReplyToString(reply.Status)
fields["response_status"] = authenReplyToString(reply.Status)
acc.AddFields("tacacs", fields, tags)
return nil
}
Expand All @@ -191,13 +191,13 @@ func (t *Tacacs) pollServer(acc telegraf.Accumulator, client *tacplus.Client) er
}
if reply.Status != tacplus.AuthenStatusPass {
fields["responsetime_ms"] = time.Since(startTime).Milliseconds()
fields["response_status"] = AuthenReplyToString(reply.Status)
fields["response_status"] = authenReplyToString(reply.Status)
acc.AddFields("tacacs", fields, tags)
return nil
}

fields["responsetime_ms"] = time.Since(startTime).Milliseconds()
fields["response_status"] = AuthenReplyToString(reply.Status)
fields["response_status"] = authenReplyToString(reply.Status)
acc.AddFields("tacacs", fields, tags)
return nil
}
Expand Down
148 changes: 74 additions & 74 deletions plugins/inputs/tail/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,76 +10,38 @@ import (
"github.com/influxdata/telegraf/config"
)

const (
// previous => Append current line to previous line
previous multilineMatchWhichLine = iota
// next => next line will be appended to current line
next
)

// Indicates relation to the multiline event: previous or next
type MultilineMatchWhichLine int
type multilineMatchWhichLine int

type Multiline struct {
config *MultilineConfig
type multiline struct {
config *multilineConfig
enabled bool
patternRegexp *regexp.Regexp
quote byte
inQuote bool
}

type MultilineConfig struct {
type multilineConfig struct {
Pattern string `toml:"pattern"`
MatchWhichLine MultilineMatchWhichLine `toml:"match_which_line"`
MatchWhichLine multilineMatchWhichLine `toml:"match_which_line"`
InvertMatch bool `toml:"invert_match"`
PreserveNewline bool `toml:"preserve_newline"`
Quotation string `toml:"quotation"`
Timeout *config.Duration `toml:"timeout"`
}

const (
// Previous => Append current line to previous line
Previous MultilineMatchWhichLine = iota
// Next => Next line will be appended to current line
Next
)

func (m *MultilineConfig) NewMultiline() (*Multiline, error) {
var r *regexp.Regexp

if m.Pattern != "" {
var err error
if r, err = regexp.Compile(m.Pattern); err != nil {
return nil, err
}
}

var quote byte
switch m.Quotation {
case "", "ignore":
m.Quotation = "ignore"
case "single-quotes":
quote = '\''
case "double-quotes":
quote = '"'
case "backticks":
quote = '`'
default:
return nil, errors.New("invalid 'quotation' setting")
}

enabled := m.Pattern != "" || quote != 0
if m.Timeout == nil || time.Duration(*m.Timeout).Nanoseconds() == int64(0) {
d := config.Duration(5 * time.Second)
m.Timeout = &d
}

return &Multiline{
config: m,
enabled: enabled,
patternRegexp: r,
quote: quote,
}, nil
}

func (m *Multiline) IsEnabled() bool {
func (m *multiline) isEnabled() bool {
return m.enabled
}

func (m *Multiline) ProcessLine(text string, buffer *bytes.Buffer) string {
func (m *multiline) processLine(text string, buffer *bytes.Buffer) string {
if m.matchQuotation(text) || m.matchString(text) {
// Restore the newline removed by tail's scanner
if buffer.Len() > 0 && m.config.PreserveNewline {
Expand All @@ -89,13 +51,13 @@ func (m *Multiline) ProcessLine(text string, buffer *bytes.Buffer) string {
return ""
}

if m.config.MatchWhichLine == Previous {
if m.config.MatchWhichLine == previous {
previousText := buffer.String()
buffer.Reset()
buffer.WriteString(text)
text = previousText
} else {
// Next
// next
if buffer.Len() > 0 {
if m.config.PreserveNewline {
buffer.WriteString("\n")
Expand All @@ -109,16 +71,7 @@ func (m *Multiline) ProcessLine(text string, buffer *bytes.Buffer) string {
return text
}

func Flush(buffer *bytes.Buffer) string {
if buffer.Len() == 0 {
return ""
}
text := buffer.String()
buffer.Reset()
return text
}

func (m *Multiline) matchQuotation(text string) bool {
func (m *multiline) matchQuotation(text string) bool {
if m.config.Quotation == "ignore" {
return false
}
Expand Down Expand Up @@ -146,46 +99,93 @@ func (m *Multiline) matchQuotation(text string) bool {
return m.inQuote
}

func (m *Multiline) matchString(text string) bool {
func (m *multiline) matchString(text string) bool {
if m.patternRegexp != nil {
return m.patternRegexp.MatchString(text) != m.config.InvertMatch
}
return false
}

func (w MultilineMatchWhichLine) String() string {
func (m *multilineConfig) newMultiline() (*multiline, error) {
var r *regexp.Regexp

if m.Pattern != "" {
var err error
if r, err = regexp.Compile(m.Pattern); err != nil {
return nil, err
}
}

var quote byte
switch m.Quotation {
case "", "ignore":
m.Quotation = "ignore"
case "single-quotes":
quote = '\''
case "double-quotes":
quote = '"'
case "backticks":
quote = '`'
default:
return nil, errors.New("invalid 'quotation' setting")
}

enabled := m.Pattern != "" || quote != 0
if m.Timeout == nil || time.Duration(*m.Timeout).Nanoseconds() == int64(0) {
d := config.Duration(5 * time.Second)
m.Timeout = &d
}

return &multiline{
config: m,
enabled: enabled,
patternRegexp: r,
quote: quote,
}, nil
}

func flush(buffer *bytes.Buffer) string {
if buffer.Len() == 0 {
return ""
}
text := buffer.String()
buffer.Reset()
return text
}

func (w multilineMatchWhichLine) String() string {
switch w {
case Previous:
case previous:
return "previous"
case Next:
case next:
return "next"
}
return ""
}

// UnmarshalTOML implements ability to unmarshal MultilineMatchWhichLine from TOML files.
func (w *MultilineMatchWhichLine) UnmarshalTOML(data []byte) (err error) {
// UnmarshalTOML implements ability to unmarshal multilineMatchWhichLine from TOML files.
func (w *multilineMatchWhichLine) UnmarshalTOML(data []byte) (err error) {
return w.UnmarshalText(data)
}

// UnmarshalText implements encoding.TextUnmarshaler
func (w *MultilineMatchWhichLine) UnmarshalText(data []byte) (err error) {
func (w *multilineMatchWhichLine) UnmarshalText(data []byte) (err error) {
s := string(data)
switch strings.ToUpper(s) {
case `PREVIOUS`, `"PREVIOUS"`, `'PREVIOUS'`:
*w = Previous
*w = previous
return nil

case `NEXT`, `"NEXT"`, `'NEXT'`:
*w = Next
*w = next
return nil
}
*w = -1
return errors.New("unknown multiline MatchWhichLine")
}

// MarshalText implements encoding.TextMarshaler
func (w MultilineMatchWhichLine) MarshalText() ([]byte, error) {
func (w multilineMatchWhichLine) MarshalText() ([]byte, error) {
s := w.String()
if s != "" {
return []byte(s), nil
Expand Down
Loading
Loading