1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// Copyright 2015 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !nosupervisord
package collector
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/mattn/go-xmlrpc"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
supervisordURL = kingpin.Flag("collector.supervisord.url", "XML RPC endpoint.").Default("http://localhost:9001/RPC2").String()
xrpc *xmlrpc.Client
)
type supervisordCollector struct {
upDesc *prometheus.Desc
stateDesc *prometheus.Desc
exitStatusDesc *prometheus.Desc
startTimeDesc *prometheus.Desc
logger log.Logger
}
func init() {
registerCollector("supervisord", defaultDisabled, NewSupervisordCollector)
}
// NewSupervisordCollector returns a new Collector exposing supervisord statistics.
func NewSupervisordCollector(logger log.Logger) (Collector, error) {
var (
subsystem = "supervisord"
labelNames = []string{"name", "group"}
)
if u, err := url.Parse(*supervisordURL); err == nil && u.Scheme == "unix" {
// Fake the URI scheme as http, since net/http.*Transport.roundTrip will complain
// about a non-http(s) transport.
xrpc = xmlrpc.NewClient("http://unix/RPC2")
xrpc.HttpClient.Transport = &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
d := net.Dialer{Timeout: 10 * time.Second}
return d.DialContext(ctx, "unix", u.Path)
},
}
} else {
xrpc = xmlrpc.NewClient(*supervisordURL)
}
return &supervisordCollector{
upDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "up"),
"Process Up",
labelNames,
nil,
),
stateDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "state"),
"Process State",
labelNames,
nil,
),
exitStatusDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "exit_status"),
"Process Exit Status",
labelNames,
nil,
),
startTimeDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "start_time_seconds"),
"Process start time",
labelNames,
nil,
),
logger: logger,
}, nil
}
func (c *supervisordCollector) isRunning(state int) bool {
// http://supervisord.org/subprocess.html#process-states
const (
// STOPPED = 0
STARTING = 10
RUNNING = 20
// BACKOFF = 30
STOPPING = 40
// EXITED = 100
// FATAL = 200
// UNKNOWN = 1000
)
switch state {
case STARTING, RUNNING, STOPPING:
return true
}
return false
}
func (c *supervisordCollector) Update(ch chan<- prometheus.Metric) error {
var info struct {
Name string `xmlrpc:"name"`
Group string `xmlrpc:"group"`
Start int `xmlrpc:"start"`
Stop int `xmlrpc:"stop"`
Now int `xmlrpc:"now"`
State int `xmlrpc:"state"`
StateName string `xmlrpc:"statename"`
SpawnErr string `xmlrpc:"spanerr"`
ExitStatus int `xmlrpc:"exitstatus"`
StdoutLogfile string `xmlrcp:"stdout_logfile"`
StderrLogfile string `xmlrcp:"stderr_logfile"`
PID int `xmlrpc:"pid"`
}
res, err := xrpc.Call("supervisor.getAllProcessInfo")
if err != nil {
return fmt.Errorf("unable to call supervisord: %s", err)
}
for _, p := range res.(xmlrpc.Array) {
for k, v := range p.(xmlrpc.Struct) {
switch k {
case "name":
info.Name = v.(string)
case "group":
info.Group = v.(string)
case "start":
info.Start = v.(int)
case "stop":
info.Stop = v.(int)
case "now":
info.Now = v.(int)
case "state":
info.State = v.(int)
case "statename":
info.StateName = v.(string)
case "exitstatus":
info.ExitStatus = v.(int)
case "pid":
info.PID = v.(int)
}
}
labels := []string{info.Name, info.Group}
ch <- prometheus.MustNewConstMetric(c.stateDesc, prometheus.GaugeValue, float64(info.State), labels...)
ch <- prometheus.MustNewConstMetric(c.exitStatusDesc, prometheus.GaugeValue, float64(info.ExitStatus), labels...)
if c.isRunning(info.State) {
ch <- prometheus.MustNewConstMetric(c.upDesc, prometheus.GaugeValue, 1, labels...)
ch <- prometheus.MustNewConstMetric(c.startTimeDesc, prometheus.CounterValue, float64(info.Start), labels...)
} else {
ch <- prometheus.MustNewConstMetric(c.upDesc, prometheus.GaugeValue, 0, labels...)
}
level.Debug(c.logger).Log("msg", "process info", "group", info.Group, "name", info.Name, "state", info.StateName, "pid", info.PID)
}
return nil
}
|