2016-04-06 14:49:24 +00:00
|
|
|
package uwsgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-04-06 19:05:23 +00:00
|
|
|
"fmt"
|
2016-04-06 14:49:24 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
2016-04-14 18:48:27 +00:00
|
|
|
"io"
|
2016-04-06 19:05:23 +00:00
|
|
|
"net"
|
2016-04-14 18:48:27 +00:00
|
|
|
"net/http"
|
2016-04-06 19:05:23 +00:00
|
|
|
"net/url"
|
2016-04-06 14:49:24 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-04-06 19:05:23 +00:00
|
|
|
var timeout = 5 * time.Second
|
2016-04-06 14:49:24 +00:00
|
|
|
|
|
|
|
type Uwsgi struct {
|
2016-04-06 19:05:23 +00:00
|
|
|
Servers []string `toml:"server"`
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Uwsgi) Description() string {
|
|
|
|
return "Read uWSGI metrics."
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Uwsgi) SampleConfig() string {
|
|
|
|
return `
|
2016-04-06 19:05:23 +00:00
|
|
|
## List with urls of uWSGI Stats servers. Url must match pattern:
|
|
|
|
## scheme://address[:port]
|
|
|
|
##
|
|
|
|
## For example:
|
|
|
|
## servers = ["tcp://localhost:5050", "http://localhost:1717", "unix:///tmp/statsock"]
|
|
|
|
servers = []
|
2016-04-06 14:49:24 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Uwsgi) Gather(acc telegraf.Accumulator) error {
|
2016-04-06 19:05:23 +00:00
|
|
|
for _, s := range u.Servers {
|
|
|
|
n, err := url.Parse(s)
|
2016-04-06 14:49:24 +00:00
|
|
|
if err != nil {
|
2016-04-06 19:05:23 +00:00
|
|
|
return fmt.Errorf("Could not parse uWSGI Stats Server url '%s': %s", s, err)
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 19:05:23 +00:00
|
|
|
u.gatherServer(acc, n)
|
|
|
|
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:05:23 +00:00
|
|
|
func (u *Uwsgi) gatherServer(acc telegraf.Accumulator, url *url.URL) error {
|
|
|
|
var err error
|
2016-04-14 18:48:27 +00:00
|
|
|
var r io.ReadCloser
|
|
|
|
|
|
|
|
switch url.Scheme {
|
|
|
|
case "unix":
|
|
|
|
r, err = net.DialTimeout(url.Scheme, url.Path, timeout)
|
|
|
|
case "tcp":
|
|
|
|
r, err = net.DialTimeout(url.Scheme, url.Host, timeout)
|
|
|
|
case "http":
|
|
|
|
resp, err := http.Get(url.String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not connect to uWSGI Stats Server '%s': %s", url.String(), err)
|
|
|
|
}
|
|
|
|
r = resp.Body
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("'%s' is not a valid URL", url.String())
|
2016-04-06 19:05:23 +00:00
|
|
|
}
|
2016-04-06 14:49:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2016-04-06 19:05:23 +00:00
|
|
|
return fmt.Errorf("Could not connect to uWSGI Stats Server '%s': %s", url.String(), err)
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
2016-04-14 18:48:27 +00:00
|
|
|
defer r.Close()
|
2016-04-06 14:49:24 +00:00
|
|
|
|
|
|
|
var s StatsServer
|
2016-04-06 19:05:23 +00:00
|
|
|
s.Url = url.String()
|
2016-04-06 14:49:24 +00:00
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
dec := json.NewDecoder(r)
|
2016-04-06 14:49:24 +00:00
|
|
|
dec.Decode(&s)
|
|
|
|
|
2016-04-06 15:14:49 +00:00
|
|
|
u.gatherStatServer(acc, &s)
|
2016-04-06 14:49:24 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
func (u *Uwsgi) gatherStatServer(acc telegraf.Accumulator, s *StatsServer) {
|
2016-04-06 15:14:49 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"listen_queue": s.ListenQueue,
|
|
|
|
"listen_queue_errors": s.ListenQueueErrors,
|
|
|
|
"signal_queue": s.SignalQueue,
|
|
|
|
"load": s.Load,
|
|
|
|
}
|
|
|
|
|
|
|
|
tags := map[string]string{
|
|
|
|
"url": s.Url,
|
|
|
|
"pid": strconv.Itoa(s.Pid),
|
|
|
|
"uid": strconv.Itoa(s.Uid),
|
|
|
|
"gid": strconv.Itoa(s.Gid),
|
|
|
|
"version": s.Version,
|
|
|
|
"cwd": s.Cwd,
|
|
|
|
}
|
|
|
|
acc.AddFields("uwsgi_overview", fields, tags)
|
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
u.gatherWorkers(acc, s)
|
|
|
|
u.gatherApps(acc, s)
|
|
|
|
u.gatherCores(acc, s)
|
2016-04-06 15:14:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
func (u *Uwsgi) gatherWorkers(acc telegraf.Accumulator, s *StatsServer) {
|
2016-04-06 14:49:24 +00:00
|
|
|
for _, w := range s.Workers {
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"requests": w.Requests,
|
|
|
|
"accepting": w.Accepting,
|
|
|
|
"delta_request": w.DeltaRequests,
|
2016-04-14 11:09:07 +00:00
|
|
|
"exceptions": w.Exceptions,
|
2016-04-06 14:49:24 +00:00
|
|
|
"harakiri_count": w.HarakiriCount,
|
|
|
|
"signals": w.Signals,
|
|
|
|
"signal_queue": w.SignalQueue,
|
|
|
|
"status": w.Status,
|
2016-04-06 15:14:49 +00:00
|
|
|
"rss": w.Rss,
|
|
|
|
"vsz": w.Vsz,
|
2016-04-06 14:49:24 +00:00
|
|
|
"running_time": w.RunningTime,
|
|
|
|
"last_spawn": w.LastSpawn,
|
|
|
|
"respawn_count": w.RespawnCount,
|
2016-04-06 15:14:49 +00:00
|
|
|
"tx": w.Tx,
|
|
|
|
"avg_rt": w.AvgRt,
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
tags := map[string]string{
|
2016-04-11 14:37:58 +00:00
|
|
|
"worker_id": strconv.Itoa(w.WorkerId),
|
|
|
|
"url": s.Url,
|
|
|
|
"pid": strconv.Itoa(w.Pid),
|
2016-04-06 14:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.AddFields("uwsgi_workers", fields, tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
func (u *Uwsgi) gatherApps(acc telegraf.Accumulator, s *StatsServer) {
|
2016-04-11 14:37:58 +00:00
|
|
|
for _, w := range s.Workers {
|
|
|
|
for _, a := range w.Apps {
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"modifier1": a.Modifier1,
|
|
|
|
"requests": a.Requests,
|
|
|
|
"startup_time": a.StartupTime,
|
|
|
|
"exceptions": a.Exceptions,
|
|
|
|
}
|
|
|
|
tags := map[string]string{
|
|
|
|
"app_id": strconv.Itoa(a.AppId),
|
|
|
|
"worker_id": strconv.Itoa(w.WorkerId),
|
|
|
|
"mountpoint": a.MountPoint,
|
|
|
|
"chdir": a.Chdir,
|
|
|
|
}
|
|
|
|
acc.AddFields("uwsgi_apps", fields, tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:48:27 +00:00
|
|
|
func (u *Uwsgi) gatherCores(acc telegraf.Accumulator, s *StatsServer) {
|
2016-04-11 14:56:37 +00:00
|
|
|
for _, w := range s.Workers {
|
|
|
|
for _, c := range w.Cores {
|
|
|
|
fields := map[string]interface{}{
|
|
|
|
"requests": c.Requests,
|
|
|
|
"static_requests": c.StaticRequests,
|
|
|
|
"routed_requests": c.RoutedRequests,
|
|
|
|
"offloaded_requests": c.OffloadedRequests,
|
|
|
|
"write_errors": c.WriteErrors,
|
|
|
|
"read_errors": c.ReadErrors,
|
|
|
|
"in_request": c.InRequest,
|
|
|
|
}
|
|
|
|
tags := map[string]string{
|
|
|
|
"core_id": strconv.Itoa(c.CoreId),
|
|
|
|
"worker_id": strconv.Itoa(w.WorkerId),
|
|
|
|
}
|
|
|
|
acc.AddFields("uwsgi_cores", fields, tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 14:49:24 +00:00
|
|
|
func init() {
|
|
|
|
inputs.Add("uwsgi", func() telegraf.Input { return &Uwsgi{} })
|
|
|
|
}
|