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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package openvpn_status_log
     4  
     5  import (
     6  	_ "embed"
     7  
     8  	"github.com/netdata/go.d.plugin/agent/module"
     9  	"github.com/netdata/go.d.plugin/pkg/matcher"
    10  )
    11  
    12  //go:embed "config_schema.json"
    13  var configSchema string
    14  
    15  func init() {
    16  	module.Register("openvpn_status_log", module.Creator{
    17  		JobConfigSchema: configSchema,
    18  		Create:          func() module.Module { return New() },
    19  	})
    20  }
    21  
    22  func New() *OpenVPNStatusLog {
    23  	config := Config{
    24  		LogPath: "/var/log/openvpn/status.log",
    25  	}
    26  	return &OpenVPNStatusLog{
    27  		Config:         config,
    28  		charts:         charts.Copy(),
    29  		collectedUsers: make(map[string]bool),
    30  	}
    31  }
    32  
    33  type Config struct {
    34  	LogPath      string             `yaml:"log_path"`
    35  	PerUserStats matcher.SimpleExpr `yaml:"per_user_stats"`
    36  }
    37  
    38  type OpenVPNStatusLog struct {
    39  	module.Base
    40  
    41  	Config `yaml:",inline"`
    42  
    43  	charts *module.Charts
    44  
    45  	collectedUsers map[string]bool
    46  	perUserMatcher matcher.Matcher
    47  }
    48  
    49  func (o *OpenVPNStatusLog) Init() bool {
    50  	if err := o.validateConfig(); err != nil {
    51  		o.Errorf("error on validating config: %v", err)
    52  		return false
    53  	}
    54  
    55  	m, err := o.initPerUserStatsMatcher()
    56  	if err != nil {
    57  		o.Errorf("error on creating 'per_user_stats' matcher: %v", err)
    58  		return false
    59  	}
    60  
    61  	if m != nil {
    62  		o.perUserMatcher = m
    63  	}
    64  
    65  	return true
    66  }
    67  
    68  func (o *OpenVPNStatusLog) Check() bool {
    69  	return len(o.Collect()) > 0
    70  }
    71  
    72  func (o OpenVPNStatusLog) Charts() *module.Charts {
    73  	return o.charts
    74  }
    75  
    76  func (o *OpenVPNStatusLog) Collect() map[string]int64 {
    77  	mx, err := o.collect()
    78  	if err != nil {
    79  		o.Error(err)
    80  	}
    81  
    82  	if len(mx) == 0 {
    83  		return nil
    84  	}
    85  	return mx
    86  }
    87  
    88  func (o *OpenVPNStatusLog) Cleanup() {}