github.com/netdata/go.d.plugin@v0.58.1/modules/squidlog/squidlog.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package squidlog
     4  
     5  import (
     6  	_ "embed"
     7  
     8  	"github.com/netdata/go.d.plugin/agent/module"
     9  	"github.com/netdata/go.d.plugin/pkg/logs"
    10  )
    11  
    12  //go:embed "config_schema.json"
    13  var configSchema string
    14  
    15  func init() {
    16  	module.Register("squidlog", module.Creator{
    17  		JobConfigSchema: configSchema,
    18  		Create:          func() module.Module { return New() },
    19  	})
    20  }
    21  
    22  func New() *SquidLog {
    23  	cfg := logs.ParserConfig{
    24  		LogType: logs.TypeCSV,
    25  		CSV: logs.CSVConfig{
    26  			FieldsPerRecord:  -1,
    27  			Delimiter:        " ",
    28  			TrimLeadingSpace: true,
    29  			Format:           "- $resp_time $client_address $result_code $resp_size $req_method - - $hierarchy $mime_type",
    30  			CheckField:       checkCSVFormatField,
    31  		},
    32  	}
    33  	return &SquidLog{
    34  		Config: Config{
    35  			Path:        "/var/log/squid/access.log",
    36  			ExcludePath: "*.gz",
    37  			Parser:      cfg,
    38  		},
    39  	}
    40  }
    41  
    42  type (
    43  	Config struct {
    44  		Parser      logs.ParserConfig `yaml:",inline"`
    45  		Path        string            `yaml:"path"`
    46  		ExcludePath string            `yaml:"exclude_path"`
    47  	}
    48  
    49  	SquidLog struct {
    50  		module.Base
    51  		Config `yaml:",inline"`
    52  
    53  		file   *logs.Reader
    54  		parser logs.Parser
    55  		line   *logLine
    56  
    57  		mx     *metricsData
    58  		charts *module.Charts
    59  	}
    60  )
    61  
    62  func (s *SquidLog) Init() bool {
    63  	s.line = newEmptyLogLine()
    64  	s.mx = newMetricsData()
    65  	return true
    66  }
    67  
    68  func (s *SquidLog) Check() bool {
    69  	// Note: these inits are here to make auto-detection retry working
    70  	if err := s.createLogReader(); err != nil {
    71  		s.Warning("check failed: ", err)
    72  		return false
    73  	}
    74  
    75  	if err := s.createParser(); err != nil {
    76  		s.Warning("check failed: ", err)
    77  		return false
    78  	}
    79  
    80  	if err := s.createCharts(s.line); err != nil {
    81  		s.Warning("check failed: ", err)
    82  		return false
    83  	}
    84  	return true
    85  }
    86  
    87  func (s *SquidLog) Charts() *module.Charts {
    88  	return s.charts
    89  }
    90  
    91  func (s *SquidLog) Collect() map[string]int64 {
    92  	mx, err := s.collect()
    93  	if err != nil {
    94  		s.Error(err)
    95  	}
    96  
    97  	if len(mx) == 0 {
    98  		return nil
    99  	}
   100  	return mx
   101  }
   102  
   103  func (s *SquidLog) Cleanup() {
   104  	if s.file != nil {
   105  		_ = s.file.Close()
   106  	}
   107  }