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