2015-11-10 21:40:39 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-04-29 01:23:45 +00:00
|
|
|
"bytes"
|
2018-10-05 22:06:41 +00:00
|
|
|
"compress/gzip"
|
2018-11-05 21:34:28 +00:00
|
|
|
"context"
|
2016-02-03 19:59:34 +00:00
|
|
|
"crypto/rand"
|
2015-11-10 21:40:39 +00:00
|
|
|
"errors"
|
2019-02-06 01:43:35 +00:00
|
|
|
"fmt"
|
2018-10-05 22:06:41 +00:00
|
|
|
"io"
|
2019-02-06 01:43:35 +00:00
|
|
|
"math"
|
2016-05-30 22:24:42 +00:00
|
|
|
"math/big"
|
2015-11-10 21:40:39 +00:00
|
|
|
"os"
|
2016-04-29 01:23:45 +00:00
|
|
|
"os/exec"
|
2019-02-06 01:43:35 +00:00
|
|
|
"runtime"
|
2016-05-23 12:33:43 +00:00
|
|
|
"strconv"
|
2015-11-10 21:40:39 +00:00
|
|
|
"strings"
|
2019-11-13 20:56:01 +00:00
|
|
|
"sync"
|
2018-06-19 18:47:13 +00:00
|
|
|
"syscall"
|
2015-11-10 21:40:39 +00:00
|
|
|
"time"
|
2016-03-23 15:40:38 +00:00
|
|
|
"unicode"
|
2018-10-19 18:17:18 +00:00
|
|
|
|
|
|
|
"github.com/alecthomas/units"
|
2015-11-10 21:40:39 +00:00
|
|
|
)
|
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
const alphanum string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
2016-04-29 01:23:45 +00:00
|
|
|
var (
|
|
|
|
TimeoutErr = errors.New("Command timed out.")
|
|
|
|
|
|
|
|
NotImplementedError = errors.New("not implemented yet")
|
2018-10-09 20:45:07 +00:00
|
|
|
|
|
|
|
VersionAlreadySetError = errors.New("version has already been set")
|
2016-04-29 01:23:45 +00:00
|
|
|
)
|
|
|
|
|
2018-10-09 20:45:07 +00:00
|
|
|
// Set via the main module
|
|
|
|
var version string
|
|
|
|
|
2015-11-10 21:40:39 +00:00
|
|
|
// Duration just wraps time.Duration
|
|
|
|
type Duration struct {
|
2015-12-04 18:44:56 +00:00
|
|
|
Duration time.Duration
|
2015-11-10 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:17:18 +00:00
|
|
|
// Size just wraps an int64
|
|
|
|
type Size struct {
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:50:20 +00:00
|
|
|
type Number struct {
|
|
|
|
Value float64
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:56:01 +00:00
|
|
|
type ReadWaitCloser struct {
|
|
|
|
pipeReader *io.PipeReader
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2018-10-09 20:45:07 +00:00
|
|
|
// SetVersion sets the telegraf agent version
|
|
|
|
func SetVersion(v string) error {
|
|
|
|
if version != "" {
|
|
|
|
return VersionAlreadySetError
|
|
|
|
}
|
|
|
|
version = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the telegraf agent version
|
|
|
|
func Version() string {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:20:07 +00:00
|
|
|
// ProductToken returns a tag for Telegraf that can be used in user agents.
|
|
|
|
func ProductToken() string {
|
2019-05-30 01:31:06 +00:00
|
|
|
return fmt.Sprintf("Telegraf/%s Go/%s",
|
|
|
|
Version(), strings.TrimPrefix(runtime.Version(), "go"))
|
2019-01-04 00:20:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-10 21:40:39 +00:00
|
|
|
// UnmarshalTOML parses the duration from the TOML config file
|
|
|
|
func (d *Duration) UnmarshalTOML(b []byte) error {
|
2016-05-23 12:33:43 +00:00
|
|
|
var err error
|
2016-11-09 16:35:58 +00:00
|
|
|
b = bytes.Trim(b, `'`)
|
2016-10-25 07:30:01 +00:00
|
|
|
|
2016-11-09 16:35:58 +00:00
|
|
|
// see if we can directly convert it
|
2016-10-25 12:11:32 +00:00
|
|
|
d.Duration, err = time.ParseDuration(string(b))
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
// Parse string duration, ie, "1s"
|
2016-10-25 07:30:01 +00:00
|
|
|
if uq, err := strconv.Unquote(string(b)); err == nil && len(uq) > 0 {
|
|
|
|
d.Duration, err = time.ParseDuration(uq)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-10 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:33:43 +00:00
|
|
|
// First try parsing as integer seconds
|
|
|
|
sI, err := strconv.ParseInt(string(b), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
d.Duration = time.Second * time.Duration(sI)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Second try parsing as float seconds
|
|
|
|
sF, err := strconv.ParseFloat(string(b), 64)
|
|
|
|
if err == nil {
|
|
|
|
d.Duration = time.Second * time.Duration(sF)
|
|
|
|
return nil
|
|
|
|
}
|
2015-11-10 21:40:39 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:17:18 +00:00
|
|
|
func (s *Size) UnmarshalTOML(b []byte) error {
|
|
|
|
var err error
|
|
|
|
b = bytes.Trim(b, `'`)
|
|
|
|
|
|
|
|
val, err := strconv.ParseInt(string(b), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
s.Size = val
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
uq, err := strconv.Unquote(string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
val, err = units.ParseStrictBytes(uq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Size = val
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:50:20 +00:00
|
|
|
func (n *Number) UnmarshalTOML(b []byte) error {
|
|
|
|
value, err := strconv.ParseFloat(string(b), 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.Value = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:40:39 +00:00
|
|
|
// ReadLines reads contents from a file and splits them by new lines.
|
|
|
|
// A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).
|
|
|
|
func ReadLines(filename string) ([]string, error) {
|
|
|
|
return ReadLinesOffsetN(filename, 0, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLines reads contents from file and splits them by new line.
|
|
|
|
// The offset tells at which line number to start.
|
|
|
|
// The count determines the number of lines to read (starting from offset):
|
|
|
|
// n >= 0: at most n lines
|
|
|
|
// n < 0: whole file
|
|
|
|
func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return []string{""}, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var ret []string
|
|
|
|
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
for i := 0; i < n+int(offset) || n < 0; i++ {
|
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i < int(offset) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, strings.Trim(line, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2015-12-07 22:37:05 +00:00
|
|
|
|
2016-02-03 19:59:34 +00:00
|
|
|
// RandomString returns a random string of alpha-numeric characters
|
|
|
|
func RandomString(n int) string {
|
|
|
|
var bytes = make([]byte, n)
|
|
|
|
rand.Read(bytes)
|
|
|
|
for i, b := range bytes {
|
|
|
|
bytes[i] = alphanum[b%byte(len(alphanum))]
|
|
|
|
}
|
|
|
|
return string(bytes)
|
|
|
|
}
|
|
|
|
|
2016-03-23 15:40:38 +00:00
|
|
|
// SnakeCase converts the given string to snake case following the Golang format:
|
|
|
|
// acronyms are converted to lower-case and preceded by an underscore.
|
|
|
|
func SnakeCase(in string) string {
|
|
|
|
runes := []rune(in)
|
|
|
|
length := len(runes)
|
|
|
|
|
|
|
|
var out []rune
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
|
|
|
|
out = append(out, '_')
|
|
|
|
}
|
|
|
|
out = append(out, unicode.ToLower(runes[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
2016-04-29 01:23:45 +00:00
|
|
|
|
2016-05-30 22:24:42 +00:00
|
|
|
// RandomSleep will sleep for a random amount of time up to max.
|
|
|
|
// If the shutdown channel is closed, it will return before it has finished
|
|
|
|
// sleeping.
|
|
|
|
func RandomSleep(max time.Duration, shutdown chan struct{}) {
|
|
|
|
if max == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
maxSleep := big.NewInt(max.Nanoseconds())
|
|
|
|
|
|
|
|
var sleepns int64
|
|
|
|
if j, err := rand.Int(rand.Reader, maxSleep); err == nil {
|
|
|
|
sleepns = j.Int64()
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.NewTimer(time.Nanosecond * time.Duration(sleepns))
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return
|
|
|
|
case <-shutdown:
|
|
|
|
t.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 18:47:13 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
// RandomDuration returns a random duration between 0 and max.
|
|
|
|
func RandomDuration(max time.Duration) time.Duration {
|
|
|
|
if max == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var sleepns int64
|
|
|
|
maxSleep := big.NewInt(max.Nanoseconds())
|
|
|
|
if j, err := rand.Int(rand.Reader, maxSleep); err == nil {
|
|
|
|
sleepns = j.Int64()
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Duration(sleepns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SleepContext sleeps until the context is closed or the duration is reached.
|
|
|
|
func SleepContext(ctx context.Context, duration time.Duration) error {
|
|
|
|
if duration == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.NewTimer(duration)
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Stop()
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlignDuration returns the duration until next aligned interval.
|
2019-03-29 22:40:33 +00:00
|
|
|
// If the current time is aligned a 0 duration is returned.
|
2018-11-05 21:34:28 +00:00
|
|
|
func AlignDuration(tm time.Time, interval time.Duration) time.Duration {
|
|
|
|
return AlignTime(tm, interval).Sub(tm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AlignTime returns the time of the next aligned interval.
|
2019-03-29 22:40:33 +00:00
|
|
|
// If the current time is aligned the current time is returned.
|
2018-11-05 21:34:28 +00:00
|
|
|
func AlignTime(tm time.Time, interval time.Duration) time.Time {
|
|
|
|
truncated := tm.Truncate(interval)
|
|
|
|
if truncated == tm {
|
|
|
|
return tm
|
|
|
|
}
|
|
|
|
return truncated.Add(interval)
|
|
|
|
}
|
|
|
|
|
2018-06-19 18:47:13 +00:00
|
|
|
// Exit status takes the error from exec.Command
|
|
|
|
// and returns the exit status and true
|
|
|
|
// if error is not exit status, will return 0 and false
|
|
|
|
func ExitStatus(err error) (int, bool) {
|
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
return status.ExitStatus(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
2018-10-05 22:06:41 +00:00
|
|
|
|
2019-11-13 20:56:01 +00:00
|
|
|
func (r *ReadWaitCloser) Close() error {
|
|
|
|
err := r.pipeReader.Close()
|
|
|
|
r.wg.Wait() // wait for the gzip goroutine finish
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-05 22:06:41 +00:00
|
|
|
// CompressWithGzip takes an io.Reader as input and pipes
|
|
|
|
// it through a gzip.Writer returning an io.Reader containing
|
|
|
|
// the gzipped data.
|
|
|
|
// An error is returned if passing data to the gzip.Writer fails
|
2019-11-13 20:56:01 +00:00
|
|
|
func CompressWithGzip(data io.Reader) (io.ReadCloser, error) {
|
2018-10-05 22:06:41 +00:00
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
gzipWriter := gzip.NewWriter(pipeWriter)
|
|
|
|
|
2019-11-13 20:56:01 +00:00
|
|
|
rc := &ReadWaitCloser{
|
|
|
|
pipeReader: pipeReader,
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.wg.Add(1)
|
2018-10-05 22:06:41 +00:00
|
|
|
var err error
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(gzipWriter, data)
|
|
|
|
gzipWriter.Close()
|
|
|
|
// subsequent reads from the read half of the pipe will
|
|
|
|
// return no bytes and the error err, or EOF if err is nil.
|
|
|
|
pipeWriter.CloseWithError(err)
|
2019-11-13 20:56:01 +00:00
|
|
|
rc.wg.Done()
|
2018-10-05 22:06:41 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, err
|
|
|
|
}
|
2019-02-06 01:43:35 +00:00
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
// ParseTimestamp parses a Time according to the standard Telegraf options.
|
|
|
|
// These are generally displayed in the toml similar to:
|
|
|
|
// json_time_key= "timestamp"
|
|
|
|
// json_time_format = "2006-01-02T15:04:05Z07:00"
|
|
|
|
// json_timezone = "America/Los_Angeles"
|
|
|
|
//
|
|
|
|
// The format can be one of "unix", "unix_ms", "unix_us", "unix_ns", or a Go
|
|
|
|
// time layout suitable for time.Parse.
|
|
|
|
//
|
|
|
|
// When using the "unix" format, a optional fractional component is allowed.
|
|
|
|
// Specific unix time precisions cannot have a fractional component.
|
|
|
|
//
|
|
|
|
// Unix times may be an int64, float64, or string. When using a Go format
|
|
|
|
// string the timestamp must be a string.
|
|
|
|
//
|
|
|
|
// The location is a location string suitable for time.LoadLocation. Unix
|
|
|
|
// times do not use the location string, a unix time is always return in the
|
|
|
|
// UTC location.
|
|
|
|
func ParseTimestamp(format string, timestamp interface{}, location string) (time.Time, error) {
|
|
|
|
switch format {
|
|
|
|
case "unix", "unix_ms", "unix_us", "unix_ns":
|
|
|
|
return parseUnix(format, timestamp)
|
|
|
|
default:
|
|
|
|
if location == "" {
|
|
|
|
location = "UTC"
|
|
|
|
}
|
|
|
|
return parseTime(format, timestamp, location)
|
|
|
|
}
|
2019-02-25 19:30:33 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
func parseUnix(format string, timestamp interface{}) (time.Time, error) {
|
|
|
|
integer, fractional, err := parseComponents(timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return time.Unix(0, 0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(format) {
|
|
|
|
case "unix":
|
|
|
|
return time.Unix(integer, fractional).UTC(), nil
|
|
|
|
case "unix_ms":
|
|
|
|
return time.Unix(0, integer*1e6).UTC(), nil
|
|
|
|
case "unix_us":
|
|
|
|
return time.Unix(0, integer*1e3).UTC(), nil
|
|
|
|
case "unix_ns":
|
|
|
|
return time.Unix(0, integer).UTC(), nil
|
|
|
|
default:
|
|
|
|
return time.Unix(0, 0), errors.New("unsupported type")
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 01:43:35 +00:00
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
// Returns the integers before and after an optional decimal point. Both '.'
|
|
|
|
// and ',' are supported for the decimal point. The timestamp can be an int64,
|
|
|
|
// float64, or string.
|
|
|
|
// ex: "42.5" -> (42, 5, nil)
|
|
|
|
func parseComponents(timestamp interface{}) (int64, int64, error) {
|
2019-05-14 18:29:44 +00:00
|
|
|
switch ts := timestamp.(type) {
|
|
|
|
case string:
|
2019-09-16 23:32:14 +00:00
|
|
|
parts := strings.SplitN(ts, ".", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parseUnixTimeComponents(parts[0], parts[1])
|
2019-02-06 01:43:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
parts = strings.SplitN(ts, ",", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parseUnixTimeComponents(parts[0], parts[1])
|
|
|
|
}
|
2019-02-06 01:43:35 +00:00
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
integer, err := strconv.ParseInt(ts, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
2019-02-06 01:43:35 +00:00
|
|
|
}
|
2019-09-16 23:32:14 +00:00
|
|
|
return integer, 0, nil
|
2019-05-14 18:29:44 +00:00
|
|
|
case int64:
|
2019-09-16 23:32:14 +00:00
|
|
|
return ts, 0, nil
|
2019-05-14 18:29:44 +00:00
|
|
|
case float64:
|
2019-09-16 23:32:14 +00:00
|
|
|
integer, fractional := math.Modf(ts)
|
|
|
|
return int64(integer), int64(fractional * 1e9), nil
|
2019-05-14 18:29:44 +00:00
|
|
|
default:
|
2019-09-16 23:32:14 +00:00
|
|
|
return 0, 0, errors.New("unsupported type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseUnixTimeComponents(first, second string) (int64, int64, error) {
|
|
|
|
integer, err := strconv.ParseInt(first, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
2019-02-06 01:43:35 +00:00
|
|
|
}
|
2019-05-14 18:29:44 +00:00
|
|
|
|
2019-09-16 23:32:14 +00:00
|
|
|
// Convert to nanoseconds, dropping any greater precision.
|
|
|
|
buf := []byte("000000000")
|
|
|
|
copy(buf, second)
|
|
|
|
|
|
|
|
fractional, err := strconv.ParseInt(string(buf), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
return integer, fractional, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseTime parses a string timestamp according to the format string.
|
|
|
|
func parseTime(format string, timestamp interface{}, location string) (time.Time, error) {
|
|
|
|
switch ts := timestamp.(type) {
|
|
|
|
case string:
|
|
|
|
loc, err := time.LoadLocation(location)
|
|
|
|
if err != nil {
|
|
|
|
return time.Unix(0, 0), err
|
|
|
|
}
|
|
|
|
return time.ParseInLocation(format, ts, loc)
|
|
|
|
default:
|
|
|
|
return time.Unix(0, 0), errors.New("unsupported type")
|
2019-02-06 01:43:35 +00:00
|
|
|
}
|
|
|
|
}
|