github.com/influxdata/influxdb/v2@v2.7.6/telegraf/plugins/outputs/file.go (about)

     1  package outputs
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  // File is based on telegraf file output plugin.
    11  type File struct {
    12  	baseOutput
    13  	Files []FileConfig `json:"files"`
    14  }
    15  
    16  // FileConfig is the config settings of outpu file plugin.
    17  type FileConfig struct {
    18  	Typ  string `json:"type"`
    19  	Path string `json:"path"`
    20  }
    21  
    22  // PluginName is based on telegraf plugin name.
    23  func (f *File) PluginName() string {
    24  	return "file"
    25  }
    26  
    27  // TOML encodes to toml string.
    28  func (f *File) TOML() string {
    29  	s := make([]string, len(f.Files))
    30  	for k, v := range f.Files {
    31  		if v.Typ == "stdout" {
    32  			s[k] = strconv.Quote(v.Typ)
    33  			continue
    34  		}
    35  		s[k] = strconv.Quote(v.Path)
    36  	}
    37  	return fmt.Sprintf(`[[outputs.%s]]
    38    ## Files to write to, "stdout" is a specially handled file.
    39    files = [%s]
    40  
    41    ## Use batch serialization format instead of line based delimiting.  The
    42    ## batch format allows for the production of non line based output formats and
    43    ## may more efficiently encode metric groups.
    44    # use_batch_format = false
    45  
    46    ## The file will be rotated after the time interval specified.  When set
    47    ## to 0 no time based rotation is performed.
    48    # rotation_interval = "0d"
    49  
    50    ## The logfile will be rotated when it becomes larger than the specified
    51    ## size.  When set to 0 no size based rotation is performed.
    52    # rotation_max_size = "0MB"
    53  
    54    ## Maximum number of rotated archives to keep, any older logs are deleted.
    55    ## If set to -1, no archives are removed.
    56    # rotation_max_archives = 5
    57  
    58    ## Data format to output.
    59    ## Each data format has its own unique set of configuration options, read
    60    ## more about them here:
    61    ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
    62    data_format = "influx"
    63  `, f.PluginName(), strings.Join(s, ", "))
    64  }
    65  
    66  // UnmarshalTOML decodes the parsed data to the object
    67  func (f *File) UnmarshalTOML(data interface{}) error {
    68  	dataOK, ok := data.(map[string]interface{})
    69  	if !ok {
    70  		return errors.New("bad files for file output plugin")
    71  	}
    72  	files, ok := dataOK["files"].([]interface{})
    73  	if !ok {
    74  		return errors.New("not an array for file output plugin")
    75  	}
    76  	for _, fi := range files {
    77  		fl := fi.(string)
    78  		if fl == "stdout" {
    79  			f.Files = append(f.Files, FileConfig{
    80  				Typ: "stdout",
    81  			})
    82  			continue
    83  		}
    84  		f.Files = append(f.Files, FileConfig{
    85  			Path: fl,
    86  		})
    87  	}
    88  	return nil
    89  }