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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package openvpn_status_log
     4  
     5  import (
     6  	"time"
     7  )
     8  
     9  func (o *OpenVPNStatusLog) collect() (map[string]int64, error) {
    10  	clients, err := parse(o.LogPath)
    11  	if err != nil {
    12  		return nil, err
    13  	}
    14  
    15  	mx := make(map[string]int64)
    16  
    17  	collectTotalStats(mx, clients)
    18  
    19  	if o.perUserMatcher != nil && numOfClients(clients) > 0 {
    20  		o.collectUsers(mx, clients)
    21  	}
    22  
    23  	return mx, nil
    24  }
    25  
    26  func collectTotalStats(mx map[string]int64, clients []clientInfo) {
    27  	var in, out int64
    28  	for _, c := range clients {
    29  		in += c.bytesReceived
    30  		out += c.bytesSent
    31  	}
    32  	mx["clients"] = numOfClients(clients)
    33  	mx["bytes_in"] = in
    34  	mx["bytes_out"] = out
    35  }
    36  
    37  func (o *OpenVPNStatusLog) collectUsers(mx map[string]int64, clients []clientInfo) {
    38  	now := time.Now().Unix()
    39  
    40  	for _, user := range clients {
    41  		name := user.commonName
    42  		if !o.perUserMatcher.MatchString(name) {
    43  			continue
    44  		}
    45  		if !o.collectedUsers[name] {
    46  			o.collectedUsers[name] = true
    47  			if err := o.addUserCharts(name); err != nil {
    48  				o.Warning(err)
    49  			}
    50  		}
    51  		mx[name+"_bytes_in"] = user.bytesReceived
    52  		mx[name+"_bytes_out"] = user.bytesSent
    53  		mx[name+"_connection_time"] = now - user.connectedSince
    54  	}
    55  }
    56  
    57  func numOfClients(clients []clientInfo) int64 {
    58  	var num int64
    59  	for _, v := range clients {
    60  		if v.commonName != "" && v.commonName != "UNDEF" {
    61  			num++
    62  		}
    63  	}
    64  	return num
    65  }