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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package ping
     4  
     5  import (
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  )
    11  
    12  const (
    13  	prioHostRTT = module.Priority + iota
    14  	prioHostStdDevRTT
    15  	prioHostPingPacketLoss
    16  	prioHostPingPackets
    17  )
    18  
    19  var hostChartsTmpl = module.Charts{
    20  	hostRTTChartTmpl.Copy(),
    21  	hostStdDevRTTChartTmpl.Copy(),
    22  	hostPacketLossChartTmpl.Copy(),
    23  	hostPacketsChartTmpl.Copy(),
    24  }
    25  
    26  var (
    27  	hostRTTChartTmpl = module.Chart{
    28  		ID:       "host_%s_rtt",
    29  		Title:    "Ping round-trip time",
    30  		Units:    "milliseconds",
    31  		Fam:      "latency",
    32  		Ctx:      "ping.host_rtt",
    33  		Priority: prioHostRTT,
    34  		Type:     module.Area,
    35  		Dims: module.Dims{
    36  			{ID: "host_%s_min_rtt", Name: "min", Div: 1e3},
    37  			{ID: "host_%s_max_rtt", Name: "max", Div: 1e3},
    38  			{ID: "host_%s_avg_rtt", Name: "avg", Div: 1e3},
    39  		},
    40  	}
    41  	hostStdDevRTTChartTmpl = module.Chart{
    42  		ID:       "host_%s_std_dev_rtt",
    43  		Title:    "Ping round-trip time standard deviation",
    44  		Units:    "milliseconds",
    45  		Fam:      "latency",
    46  		Ctx:      "ping.host_std_dev_rtt",
    47  		Priority: prioHostStdDevRTT,
    48  		Dims: module.Dims{
    49  			{ID: "host_%s_std_dev_rtt", Name: "std_dev", Div: 1e3},
    50  		},
    51  	}
    52  )
    53  
    54  var hostPacketLossChartTmpl = module.Chart{
    55  	ID:       "host_%s_packet_loss",
    56  	Title:    "Ping packet loss",
    57  	Units:    "percentage",
    58  	Fam:      "packet loss",
    59  	Ctx:      "ping.host_packet_loss",
    60  	Priority: prioHostPingPacketLoss,
    61  	Dims: module.Dims{
    62  		{ID: "host_%s_packet_loss", Name: "loss", Div: 1000},
    63  	},
    64  }
    65  
    66  var hostPacketsChartTmpl = module.Chart{
    67  	ID:       "host_%s_packets",
    68  	Title:    "Ping packets transferred",
    69  	Units:    "packets",
    70  	Fam:      "packets",
    71  	Ctx:      "ping.host_packets",
    72  	Priority: prioHostPingPackets,
    73  	Dims: module.Dims{
    74  		{ID: "host_%s_packets_recv", Name: "received"},
    75  		{ID: "host_%s_packets_sent", Name: "sent"},
    76  	},
    77  }
    78  
    79  func newHostCharts(host string) *module.Charts {
    80  	charts := hostChartsTmpl.Copy()
    81  
    82  	for _, chart := range *charts {
    83  		chart.ID = fmt.Sprintf(chart.ID, strings.ReplaceAll(host, ".", "_"))
    84  		chart.Labels = []module.Label{
    85  			{Key: "host", Value: host},
    86  		}
    87  		for _, dim := range chart.Dims {
    88  			dim.ID = fmt.Sprintf(dim.ID, host)
    89  		}
    90  	}
    91  
    92  	return charts
    93  }
    94  
    95  func (p *Ping) addHostCharts(host string) {
    96  	charts := newHostCharts(host)
    97  
    98  	if err := p.Charts().Add(*charts...); err != nil {
    99  		p.Warning(err)
   100  	}
   101  }