2016-02-14 10:27:13 +00:00
|
|
|
package raindrops
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Raindrops struct {
|
|
|
|
Urls []string
|
|
|
|
http_client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## An array of raindrops middleware URI to gather stats.
|
2016-02-14 19:12:21 +00:00
|
|
|
urls = ["http://localhost:8080/_raindrops"]
|
2016-02-14 10:27:13 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r *Raindrops) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Raindrops) Description() string {
|
|
|
|
return "Read raindrops stats (raindrops - real-time stats for preforking Rack servers)"
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(fmt.Errorf("Unable to parse address '%s': %s", u, err))
|
|
|
|
continue
|
2016-02-14 10:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(addr *url.URL) {
|
|
|
|
defer wg.Done()
|
2017-04-24 18:13:26 +00:00
|
|
|
acc.AddError(r.gatherUrl(addr, acc))
|
2016-02-14 10:27:13 +00:00
|
|
|
}(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-04-24 18:13:26 +00:00
|
|
|
return nil
|
2016-02-14 10:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Raindrops) gatherUrl(addr *url.URL, acc telegraf.Accumulator) error {
|
|
|
|
resp, err := r.http_client.Get(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error making HTTP request to %s: %s", 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 := r.getTags(addr)
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"calling": calling,
|
|
|
|
"writing": writing,
|
|
|
|
}
|
|
|
|
acc.AddFields("raindrops", fields, tags)
|
|
|
|
|
|
|
|
iterate := true
|
|
|
|
var queued_line_str string
|
|
|
|
var active_line_str string
|
|
|
|
var active_err error
|
|
|
|
var queued_err error
|
|
|
|
|
|
|
|
for iterate {
|
|
|
|
// Listen
|
|
|
|
var tags map[string]string
|
|
|
|
|
|
|
|
lis := map[string]interface{}{
|
|
|
|
"active": 0,
|
|
|
|
"queued": 0,
|
|
|
|
}
|
|
|
|
active_line_str, active_err = buf.ReadString('\n')
|
|
|
|
if active_err != nil {
|
|
|
|
iterate = false
|
|
|
|
break
|
|
|
|
}
|
2016-02-14 19:12:21 +00:00
|
|
|
if strings.Compare(active_line_str, "\n") == 0 {
|
2016-02-14 10:27:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
queued_line_str, queued_err = buf.ReadString('\n')
|
|
|
|
if queued_err != nil {
|
|
|
|
iterate = false
|
|
|
|
}
|
|
|
|
active_line := strings.Split(active_line_str, " ")
|
|
|
|
listen_name := active_line[0]
|
|
|
|
|
|
|
|
active, err := strconv.ParseUint(strings.TrimSpace(active_line[2]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
active = 0
|
|
|
|
}
|
|
|
|
lis["active"] = active
|
|
|
|
|
|
|
|
queued_line := strings.Split(queued_line_str, " ")
|
|
|
|
queued, err := strconv.ParseUint(strings.TrimSpace(queued_line[2]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
queued = 0
|
|
|
|
}
|
|
|
|
lis["queued"] = queued
|
|
|
|
if strings.Contains(listen_name, ":") {
|
|
|
|
listener := strings.Split(listen_name, ":")
|
|
|
|
tags = map[string]string{
|
|
|
|
"ip": listener[0],
|
|
|
|
"port": listener[1],
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
tags = map[string]string{
|
|
|
|
"socket": listen_name,
|
|
|
|
}
|
|
|
|
}
|
2016-02-14 19:12:21 +00:00
|
|
|
acc.AddFields("raindrops_listen", lis, tags)
|
2016-02-14 10:27:13 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get tag(s) for the raindrops calling/writing plugin
|
|
|
|
func (r *Raindrops) 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 {
|
2016-02-29 16:52:58 +00:00
|
|
|
return &Raindrops{http_client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
ResponseHeaderTimeout: time.Duration(3 * time.Second),
|
|
|
|
},
|
|
|
|
Timeout: time.Duration(4 * time.Second),
|
|
|
|
}}
|
2016-02-14 10:27:13 +00:00
|
|
|
})
|
|
|
|
}
|