Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
e393c3af3f
commit
4978089aab
4963 changed files with 677545 additions and 0 deletions
58
plugins/inputs/raindrops/README.md
Normal file
58
plugins/inputs/raindrops/README.md
Normal file
|
@ -0,0 +1,58 @@
|
|||
# Raindrops Input Plugin
|
||||
|
||||
The [raindrops](http://raindrops.bogomips.org/) plugin reads from specified
|
||||
raindops [middleware](http://raindrops.bogomips.org/Raindrops/Middleware.html)
|
||||
URI and adds stats to InfluxDB.
|
||||
|
||||
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
|
||||
|
||||
In addition to the plugin-specific configuration settings, plugins support
|
||||
additional global and plugin configuration settings. These settings are used to
|
||||
modify metrics, tags, and field or create aliases and configure ordering, etc.
|
||||
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||
|
||||
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
|
||||
|
||||
## Configuration
|
||||
|
||||
```toml @sample.conf
|
||||
# Read raindrops stats (raindrops - real-time stats for preforking Rack servers)
|
||||
[[inputs.raindrops]]
|
||||
## An array of raindrops middleware URI to gather stats.
|
||||
urls = ["http://localhost:8080/_raindrops"]
|
||||
```
|
||||
|
||||
## Metrics
|
||||
|
||||
- raindrops
|
||||
- calling (integer, count)
|
||||
- writing (integer, count)
|
||||
- raindrops_listen
|
||||
- active (integer, bytes)
|
||||
- queued (integer, bytes)
|
||||
|
||||
### Tags
|
||||
|
||||
- Raindops calling/writing of all the workers:
|
||||
- server
|
||||
- port
|
||||
|
||||
- raindrops_listen (ip:port):
|
||||
- ip
|
||||
- port
|
||||
|
||||
- raindrops_listen (Unix Socket):
|
||||
- socket
|
||||
|
||||
## Example Output
|
||||
|
||||
```text
|
||||
raindrops,port=8080,server=localhost calling=0i,writing=0i 1455479896806238204
|
||||
raindrops_listen,ip=0.0.0.0,port=8080 active=0i,queued=0i 1455479896806561938
|
||||
raindrops_listen,ip=0.0.0.0,port=8081 active=1i,queued=0i 1455479896806605749
|
||||
raindrops_listen,ip=127.0.0.1,port=8082 active=0i,queued=0i 1455479896806646315
|
||||
raindrops_listen,ip=0.0.0.0,port=8083 active=0i,queued=0i 1455479896806683252
|
||||
raindrops_listen,ip=0.0.0.0,port=8084 active=0i,queued=0i 1455479896806712025
|
||||
raindrops_listen,ip=0.0.0.0,port=3000 active=0i,queued=0i 1455479896806779197
|
||||
raindrops_listen,socket=/tmp/listen.me active=0i,queued=0i 1455479896806813907
|
||||
```
|
181
plugins/inputs/raindrops/raindrops.go
Normal file
181
plugins/inputs/raindrops/raindrops.go
Normal file
|
@ -0,0 +1,181 @@
|
|||
//go:generate ../../../tools/readme_config_includer/generator
|
||||
package raindrops
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
)
|
||||
|
||||
//go:embed sample.conf
|
||||
var sampleConfig string
|
||||
|
||||
type Raindrops struct {
|
||||
Urls []string `toml:"urls"`
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func (*Raindrops) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func (r *Raindrops) Gather(acc telegraf.Accumulator) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, u := range r.Urls {
|
||||
addr, err := url.Parse(u)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("unable to parse address %q: %w", u, err))
|
||||
continue
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func(addr *url.URL) {
|
||||
defer wg.Done()
|
||||
acc.AddError(r.gatherURL(addr, acc))
|
||||
}(addr)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Raindrops) gatherURL(addr *url.URL, acc telegraf.Accumulator) error {
|
||||
resp, err := r.httpClient.Get(addr.String())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error making HTTP request to %q: %w", addr.String(), err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%s returned HTTP status %s", addr.String(), resp.Status)
|
||||
}
|
||||
buf := bufio.NewReader(resp.Body)
|
||||
|
||||
// Calling
|
||||
_, err = buf.ReadString(':')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
line, err := buf.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
calling, err := strconv.ParseUint(strings.TrimSpace(line), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Writing
|
||||
_, err = buf.ReadString(':')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
line, err = buf.ReadString('\n')
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writing, err := strconv.ParseUint(strings.TrimSpace(line), 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tags := getTags(addr)
|
||||
fields := map[string]interface{}{
|
||||
"calling": calling,
|
||||
"writing": writing,
|
||||
}
|
||||
acc.AddFields("raindrops", fields, tags)
|
||||
|
||||
iterate := true
|
||||
var queuedLineStr string
|
||||
var activeLineStr string
|
||||
var activeErr error
|
||||
var queuedErr error
|
||||
|
||||
for iterate {
|
||||
// Listen
|
||||
var tags map[string]string
|
||||
|
||||
lis := map[string]interface{}{
|
||||
"active": 0,
|
||||
"queued": 0,
|
||||
}
|
||||
activeLineStr, activeErr = buf.ReadString('\n')
|
||||
if activeErr != nil {
|
||||
break
|
||||
}
|
||||
if strings.Compare(activeLineStr, "\n") == 0 {
|
||||
break
|
||||
}
|
||||
queuedLineStr, queuedErr = buf.ReadString('\n')
|
||||
if queuedErr != nil {
|
||||
iterate = false
|
||||
}
|
||||
activeLine := strings.Split(activeLineStr, " ")
|
||||
listenName := activeLine[0]
|
||||
|
||||
active, err := strconv.ParseUint(strings.TrimSpace(activeLine[2]), 10, 64)
|
||||
if err != nil {
|
||||
active = 0
|
||||
}
|
||||
lis["active"] = active
|
||||
|
||||
queuedLine := strings.Split(queuedLineStr, " ")
|
||||
queued, err := strconv.ParseUint(strings.TrimSpace(queuedLine[2]), 10, 64)
|
||||
if err != nil {
|
||||
queued = 0
|
||||
}
|
||||
lis["queued"] = queued
|
||||
if strings.Contains(listenName, ":") {
|
||||
listener := strings.Split(listenName, ":")
|
||||
tags = map[string]string{
|
||||
"ip": listener[0],
|
||||
"port": listener[1],
|
||||
}
|
||||
} else {
|
||||
tags = map[string]string{
|
||||
"socket": listenName,
|
||||
}
|
||||
}
|
||||
acc.AddFields("raindrops_listen", lis, tags)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get tag(s) for the raindrops calling/writing plugin
|
||||
func getTags(addr *url.URL) map[string]string {
|
||||
h := addr.Host
|
||||
host, port, err := net.SplitHostPort(h)
|
||||
if err != nil {
|
||||
host = addr.Host
|
||||
if addr.Scheme == "http" {
|
||||
port = "80"
|
||||
} else if addr.Scheme == "https" {
|
||||
port = "443"
|
||||
} else {
|
||||
port = ""
|
||||
}
|
||||
}
|
||||
return map[string]string{"server": host, "port": port}
|
||||
}
|
||||
|
||||
func init() {
|
||||
inputs.Add("raindrops", func() telegraf.Input {
|
||||
return &Raindrops{httpClient: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
ResponseHeaderTimeout: 3 * time.Second,
|
||||
},
|
||||
Timeout: 4 * time.Second,
|
||||
}}
|
||||
})
|
||||
}
|
107
plugins/inputs/raindrops/raindrops_test.go
Normal file
107
plugins/inputs/raindrops/raindrops_test.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
package raindrops
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/influxdata/telegraf/testutil"
|
||||
)
|
||||
|
||||
const sampleResponse = `
|
||||
calling: 100
|
||||
writing: 200
|
||||
0.0.0.0:8080 active: 1
|
||||
0.0.0.0:8080 queued: 2
|
||||
0.0.0.0:8081 active: 3
|
||||
0.0.0.0:8081 queued: 4
|
||||
127.0.0.1:8082 active: 5
|
||||
127.0.0.1:8082 queued: 6
|
||||
0.0.0.0:8083 active: 7
|
||||
0.0.0.0:8083 queued: 8
|
||||
0.0.0.0:8084 active: 9
|
||||
0.0.0.0:8084 queued: 10
|
||||
0.0.0.0:3000 active: 11
|
||||
0.0.0.0:3000 queued: 12
|
||||
/tmp/listen.me active: 13
|
||||
/tmp/listen.me queued: 14`
|
||||
|
||||
// Verify that raindrops tags are properly parsed based on the server
|
||||
func TestRaindropsTags(t *testing.T) {
|
||||
urls := []string{"http://localhost/_raindrops", "http://localhost:80/_raindrops"}
|
||||
for _, url1 := range urls {
|
||||
addr, err := url.Parse(url1)
|
||||
require.NoError(t, err)
|
||||
tagMap := getTags(addr)
|
||||
require.Contains(t, tagMap["server"], "localhost")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRaindropsGeneratesMetrics(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/_raindrops" {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Errorf("Cannot handle request, expected: %q, actual: %q", "/_raindrops", r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprintln(w, sampleResponse); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
n := &Raindrops{
|
||||
Urls: []string{ts.URL + "/_raindrops"},
|
||||
httpClient: &http.Client{Transport: &http.Transport{
|
||||
ResponseHeaderTimeout: 3 * time.Second,
|
||||
}},
|
||||
}
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
err := acc.GatherError(n.Gather)
|
||||
require.NoError(t, err)
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"calling": uint64(100),
|
||||
"writing": uint64(200),
|
||||
}
|
||||
addr, err := url.Parse(ts.URL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(addr.Host)
|
||||
if err != nil {
|
||||
host = addr.Host
|
||||
if addr.Scheme == "http" {
|
||||
port = "80"
|
||||
} else if addr.Scheme == "https" {
|
||||
port = "443"
|
||||
} else {
|
||||
port = ""
|
||||
}
|
||||
}
|
||||
|
||||
tags := map[string]string{"server": host, "port": port}
|
||||
acc.AssertContainsTaggedFields(t, "raindrops", fields, tags)
|
||||
|
||||
tags = map[string]string{
|
||||
"port": "8081",
|
||||
"ip": "0.0.0.0",
|
||||
}
|
||||
fields = map[string]interface{}{
|
||||
"active": uint64(3),
|
||||
"queued": uint64(4),
|
||||
}
|
||||
acc.AssertContainsTaggedFields(t, "raindrops_listen", fields, tags)
|
||||
}
|
4
plugins/inputs/raindrops/sample.conf
Normal file
4
plugins/inputs/raindrops/sample.conf
Normal file
|
@ -0,0 +1,4 @@
|
|||
# Read raindrops stats (raindrops - real-time stats for preforking Rack servers)
|
||||
[[inputs.raindrops]]
|
||||
## An array of raindrops middleware URI to gather stats.
|
||||
urls = ["http://localhost:8080/_raindrops"]
|
Loading…
Add table
Add a link
Reference in a new issue