Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Apache Traffic Server plugin and network interface speed #785

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/metrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import (
_ "flashcat.cloud/categraf/inputs/systemd"
_ "flashcat.cloud/categraf/inputs/tengine"
_ "flashcat.cloud/categraf/inputs/tomcat"
_ "flashcat.cloud/categraf/inputs/traffic_server"
_ "flashcat.cloud/categraf/inputs/vsphere"
_ "flashcat.cloud/categraf/inputs/whois"
_ "flashcat.cloud/categraf/inputs/xskyapi"
Expand Down
15 changes: 15 additions & 0 deletions conf/input.traffic_server/traffic_server.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# # collect interval
# interval = 15

[[instances]]

targets = [
# "http://127.0.0.1:9999/_stats",
]


## HTTP Request Method
# method = "GET"

## Set timeout (default 5 seconds)
# timeout = "5s"
6 changes: 6 additions & 0 deletions inputs/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func (s *NetIOStats) Gather(slist *types.SampleList) {
continue
}

speed, err := Speed(iface.Name)
if err != nil {
continue
}

tags := map[string]string{
"interface": io.Name,
}
Expand All @@ -115,6 +120,7 @@ func (s *NetIOStats) Gather(slist *types.SampleList) {
"err_out": io.Errout,
"drop_in": io.Dropin,
"drop_out": io.Dropout,
"speed": speed,
}

slist.PushSamples(inputName, fields, tags)
Expand Down
69 changes: 69 additions & 0 deletions inputs/net/speed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package net

import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
)

func Speed(iface string) (int64, error) {
speedFile := fmt.Sprintf("/sys/class/net/%s/speed", iface)
var speed int64
if content, err := os.ReadFile(speedFile); err == nil {
speed, err = strconv.ParseInt(strings.TrimSpace(string(content)), 10, 64)
if err != nil {
return 0, err
}
} else {
cmd := exec.Command("ethtool", iface)
if content, err := cmd.CombinedOutput(); err == nil {
var speedStr string

contentReader := bufio.NewReader(bytes.NewBuffer(content))
for {
line, err := readLine(contentReader)

if err == io.EOF {
err = nil
break
}

if err != nil {
break
}

line = bytes.Trim(line, "\t")

if bytes.HasPrefix(line, []byte("Speed:")) && bytes.HasSuffix(line, []byte("Mb/s")) {
speedStr = string(line[7 : len(line)-4])
break
}
}

speed, err = strconv.ParseInt(strings.TrimSpace(speedStr), 10, 64)
if err != nil {
return 0, err
}
}
}
if speed < 0 {
return 0, nil
}
return speed, nil
}

func readLine(r *bufio.Reader) ([]byte, error) {
line, isPrefix, err := r.ReadLine()
for isPrefix && err == nil {
var bs []byte
bs, isPrefix, err = r.ReadLine()
line = append(line, bs...)
}

return line, err
}
160 changes: 160 additions & 0 deletions inputs/traffic_server/traffic_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package traffic_server

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
)

const inputName = "traffic_server"

type TrafficServer struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
}

func init() {
inputs.Add(inputName, func() inputs.Input {
return &TrafficServer{}
})
}

func (r *TrafficServer) Clone() inputs.Input {
return &TrafficServer{}
}

func (r *TrafficServer) Name() string {
return inputName
}

func (r *TrafficServer) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(r.Instances))
for i := 0; i < len(r.Instances); i++ {
ret[i] = r.Instances[i]
}
return ret
}

type Instance struct {
config.InstanceConfig

Targets []string `toml:"targets"`
Method string `toml:"method"`
Timeout config.Duration `toml:"timeout"`
client httpClient
}

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}

func (ins *Instance) Init() error {
if len(ins.Targets) == 0 {
return types.ErrInstancesEmpty
}

if ins.Timeout < config.Duration(time.Second) {
ins.Timeout = config.Duration(time.Second * 5)
}

if ins.Method == "" {
ins.Method = "GET"
}

ins.client = &http.Client{
Timeout: time.Duration(ins.Timeout),
}

for _, target := range ins.Targets {
addr, err := url.Parse(target)
if err != nil {
return fmt.Errorf("failed to parse target url: %s, error: %v", target, err)
}

if addr.Scheme != "http" {
return fmt.Errorf("only http are supported, target: %s", target)
}
}

return nil
}

func (ins *Instance) Gather(slist *types.SampleList) {
wg := new(sync.WaitGroup)
for _, target := range ins.Targets {
wg.Add(1)
go func(target string) {
defer wg.Done()
ins.gather(slist, target)
}(target)
}
wg.Wait()
}

type Data struct {
Global map[string]string `json:"global"`
}

func (ins *Instance) gather(slist *types.SampleList, target string) {
if ins.DebugMod {
log.Println("D! traffic_server... target:", target)
}

labels := map[string]string{"target": target}

data := &Data{}

err := ins.gatherJSONData(target, data)
if err != nil {
log.Println("E! failed to gather json data:", err)
return
}
var fields = make(map[string]interface{})
for key, value := range data.Global {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
continue
}
fields[strings.ReplaceAll(strings.ToLower(key), ".", "_")] = v
}
slist.PushSamples(inputName, fields, labels)

}

// gatherJSONData query the data source and parse the response JSON
func (ins *Instance) gatherJSONData(address string, value interface{}) error {
request, err := http.NewRequest(ins.Method, address, nil)
if err != nil {
return err
}

response, err := ins.client.Do(request)
if err != nil {
return err
}

defer response.Body.Close()
if response.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := io.ReadAll(io.LimitReader(response.Body, 200))
return fmt.Errorf("%s returned HTTP status %s: %q", address, response.Status, body)
}

err = json.NewDecoder(response.Body).Decode(value)
if err != nil {
return err
}

return nil
}
Loading