2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-07-30 15:30:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-01-08 12:23:11 +00:00
|
|
|
"log"
|
2020-08-30 11:44:15 +00:00
|
|
|
"net"
|
2020-07-30 15:30:50 +00:00
|
|
|
"net/http"
|
2021-01-15 17:50:31 +00:00
|
|
|
"strconv"
|
2021-08-12 09:48:47 +00:00
|
|
|
"sync"
|
2023-04-11 18:47:19 +00:00
|
|
|
"time"
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2020-07-30 15:30:50 +00:00
|
|
|
)
|
|
|
|
|
2023-04-11 18:47:29 +00:00
|
|
|
func metric(key string, tags string, value int64) string {
|
|
|
|
return key + tags + " " + strconv.FormatInt(value, 10) + "\n"
|
2021-01-15 17:50:31 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type metricsParent interface {
|
2023-05-04 18:16:41 +00:00
|
|
|
logger.Writer
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type metrics struct {
|
2021-11-15 19:13:54 +00:00
|
|
|
parent metricsParent
|
2021-08-12 09:48:47 +00:00
|
|
|
|
2023-05-16 13:59:37 +00:00
|
|
|
ln net.Listener
|
|
|
|
httpServer *http.Server
|
|
|
|
mutex sync.Mutex
|
|
|
|
pathManager apiPathManager
|
|
|
|
rtspServer apiRTSPServer
|
|
|
|
rtspsServer apiRTSPServer
|
|
|
|
rtmpServer apiRTMPServer
|
|
|
|
hlsManager apiHLSManager
|
|
|
|
webRTCManager apiWebRTCManager
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newMetrics(
|
2021-04-24 16:25:19 +00:00
|
|
|
address string,
|
2023-04-11 18:47:19 +00:00
|
|
|
readTimeout conf.StringDuration,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent metricsParent,
|
|
|
|
) (*metrics, error) {
|
2023-05-08 08:27:43 +00:00
|
|
|
ln, err := net.Listen(restrictNetwork("tcp", address))
|
2020-08-30 11:44:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
m := &metrics{
|
2021-11-15 19:13:54 +00:00
|
|
|
parent: parent,
|
|
|
|
ln: ln,
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
router := gin.New()
|
2022-06-21 11:41:15 +00:00
|
|
|
router.SetTrustedProxies(nil)
|
2020-08-30 11:44:15 +00:00
|
|
|
|
2023-04-11 18:42:40 +00:00
|
|
|
mwLog := httpLoggerMiddleware(m)
|
|
|
|
router.NoRoute(mwLog)
|
|
|
|
router.GET("/metrics", mwLog, m.onMetrics)
|
|
|
|
|
|
|
|
m.httpServer = &http.Server{
|
2023-04-11 18:47:19 +00:00
|
|
|
Handler: router,
|
|
|
|
ReadHeaderTimeout: time.Duration(readTimeout),
|
|
|
|
ErrorLog: log.New(&nilWriter{}, "", 0),
|
2023-01-08 12:23:11 +00:00
|
|
|
}
|
2020-07-30 15:30:50 +00:00
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
m.Log(logger.Info, "listener opened on "+address)
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2023-04-11 18:42:40 +00:00
|
|
|
go m.httpServer.Serve(m.ln)
|
2021-07-24 13:55:42 +00:00
|
|
|
|
2020-08-30 11:44:15 +00:00
|
|
|
return m, nil
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (m *metrics) close() {
|
2023-05-04 18:16:41 +00:00
|
|
|
m.Log(logger.Info, "listener is closing")
|
2023-04-11 18:42:40 +00:00
|
|
|
m.httpServer.Shutdown(context.Background())
|
2022-08-31 06:53:05 +00:00
|
|
|
m.ln.Close() // in case Shutdown() is called before Serve()
|
2021-11-15 19:13:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
func (m *metrics) Log(level logger.Level, format string, args ...interface{}) {
|
2021-11-15 19:13:54 +00:00
|
|
|
m.parent.Log(level, "[metrics] "+format, args...)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
func (m *metrics) onMetrics(ctx *gin.Context) {
|
2020-07-30 15:30:50 +00:00
|
|
|
out := ""
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2022-11-09 17:31:31 +00:00
|
|
|
res := m.pathManager.apiPathsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for name, i := range res.data.Items {
|
|
|
|
var state string
|
|
|
|
if i.SourceReady {
|
|
|
|
state = "ready"
|
2021-08-12 09:48:47 +00:00
|
|
|
} else {
|
2022-11-11 10:59:52 +00:00
|
|
|
state = "notReady"
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2022-11-11 10:59:52 +00:00
|
|
|
|
|
|
|
tags := "{name=\"" + name + "\",state=\"" + state + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("paths", tags, 1)
|
|
|
|
out += metric("paths_bytes_received", tags, int64(i.BytesReceived))
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("paths", "", 0)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 13:59:37 +00:00
|
|
|
if !interfaceIsEmpty(m.hlsManager) {
|
|
|
|
res := m.hlsManager.apiMuxersList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-12-15 23:50:47 +00:00
|
|
|
for name, i := range res.data.Items {
|
|
|
|
tags := "{name=\"" + name + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("hls_muxers", tags, 1)
|
|
|
|
out += metric("hls_muxers_bytes_sent", tags, int64(i.BytesSent))
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("hls_muxers", "", 0)
|
|
|
|
out += metric("hls_muxers_bytes_sent", "", 0)
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:59:52 +00:00
|
|
|
if !interfaceIsEmpty(m.rtspServer) { //nolint:dupl
|
2022-11-09 17:31:31 +00:00
|
|
|
func() {
|
|
|
|
res := m.rtspServer.apiConnsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("rtsp_conns", tags, 1)
|
|
|
|
out += metric("rtsp_conns_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("rtsp_conns_bytes_sent", tags, int64(i.BytesSent))
|
2022-11-11 10:59:52 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("rtsp_conns", "", 0)
|
|
|
|
out += metric("rtsp_conns_bytes_received", "", 0)
|
|
|
|
out += metric("rtsp_conns_bytes_sent", "", 0)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2022-11-09 17:31:31 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
res := m.rtspServer.apiSessionsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\",state=\"" + i.State + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("rtsp_sessions", tags, 1)
|
|
|
|
out += metric("rtsp_sessions_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("rtsp_sessions_bytes_sent", tags, int64(i.BytesSent))
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("rtsp_sessions", "", 0)
|
|
|
|
out += metric("rtsp_sessions_bytes_received", "", 0)
|
|
|
|
out += metric("rtsp_sessions_bytes_sent", "", 0)
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2022-11-11 10:59:52 +00:00
|
|
|
if !interfaceIsEmpty(m.rtspsServer) { //nolint:dupl
|
2022-11-09 17:31:31 +00:00
|
|
|
func() {
|
|
|
|
res := m.rtspsServer.apiConnsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("rtsps_conns", tags, 1)
|
|
|
|
out += metric("rtsps_conns_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("rtsps_conns_bytes_sent", tags, int64(i.BytesSent))
|
2022-11-11 10:59:52 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("rtsps_conns", "", 0)
|
|
|
|
out += metric("rtsps_conns_bytes_received", "", 0)
|
|
|
|
out += metric("rtsps_conns_bytes_sent", "", 0)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2022-11-09 17:31:31 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
res := m.rtspsServer.apiSessionsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\",state=\"" + i.State + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("rtsps_sessions", tags, 1)
|
|
|
|
out += metric("rtsps_sessions_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("rtsps_sessions_bytes_sent", tags, int64(i.BytesSent))
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("rtsps_sessions", "", 0)
|
|
|
|
out += metric("rtsps_sessions_bytes_received", "", 0)
|
|
|
|
out += metric("rtsps_sessions_bytes_sent", "", 0)
|
2022-11-09 17:31:31 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2021-05-08 20:52:10 +00:00
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
if !interfaceIsEmpty(m.rtmpServer) {
|
2022-11-09 17:31:31 +00:00
|
|
|
res := m.rtmpServer.apiConnsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-11-11 10:59:52 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\",state=\"" + i.State + "\"}"
|
2023-04-11 18:47:29 +00:00
|
|
|
out += metric("rtmp_conns", tags, 1)
|
|
|
|
out += metric("rtmp_conns_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("rtmp_conns_bytes_sent", tags, int64(i.BytesSent))
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
|
|
|
out += metric("rtmp_conns", "", 0)
|
|
|
|
out += metric("rtmp_conns_bytes_received", "", 0)
|
|
|
|
out += metric("rtmp_conns_bytes_sent", "", 0)
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 15:30:50 +00:00
|
|
|
|
2023-05-16 13:59:37 +00:00
|
|
|
if !interfaceIsEmpty(m.webRTCManager) {
|
|
|
|
res := m.webRTCManager.apiSessionsList()
|
2023-04-11 18:47:29 +00:00
|
|
|
if res.err == nil && len(res.data.Items) != 0 {
|
2022-12-15 23:50:47 +00:00
|
|
|
for id, i := range res.data.Items {
|
|
|
|
tags := "{id=\"" + id + "\"}"
|
2023-05-16 13:59:37 +00:00
|
|
|
out += metric("webrtc_sessions", tags, 1)
|
|
|
|
out += metric("webrtc_sessions_bytes_received", tags, int64(i.BytesReceived))
|
|
|
|
out += metric("webrtc_sessions_bytes_sent", tags, int64(i.BytesSent))
|
2021-11-05 16:29:13 +00:00
|
|
|
}
|
2023-04-11 18:47:29 +00:00
|
|
|
} else {
|
2023-05-16 13:59:37 +00:00
|
|
|
out += metric("webrtc_sessions", "", 0)
|
|
|
|
out += metric("webrtc_sessions_bytes_received", "", 0)
|
|
|
|
out += metric("webrtc_sessions_bytes_sent", "", 0)
|
2021-11-05 16:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 10:14:23 +00:00
|
|
|
ctx.Writer.WriteHeader(http.StatusOK)
|
|
|
|
io.WriteString(ctx.Writer, out)
|
2020-07-30 15:30:50 +00:00
|
|
|
}
|
2021-08-12 09:48:47 +00:00
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// pathManagerSet is called by pathManager.
|
2022-12-15 23:50:47 +00:00
|
|
|
func (m *metrics) pathManagerSet(s apiPathManager) {
|
2021-08-12 09:48:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.pathManager = s
|
|
|
|
}
|
|
|
|
|
2023-05-16 13:59:37 +00:00
|
|
|
// hlsManagerSet is called by hlsManager.
|
|
|
|
func (m *metrics) hlsManagerSet(s apiHLSManager) {
|
2022-12-15 23:50:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-05-16 13:59:37 +00:00
|
|
|
m.hlsManager = s
|
2022-12-15 23:50:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// rtspServerSet is called by rtspServer (plain).
|
2022-12-15 23:50:47 +00:00
|
|
|
func (m *metrics) rtspServerSet(s apiRTSPServer) {
|
2021-08-12 09:48:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.rtspServer = s
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// rtspsServerSet is called by rtspServer (tls).
|
2022-12-15 23:50:47 +00:00
|
|
|
func (m *metrics) rtspsServerSet(s apiRTSPServer) {
|
2021-08-12 09:48:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.rtspsServer = s
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// rtmpServerSet is called by rtmpServer.
|
2022-12-15 23:50:47 +00:00
|
|
|
func (m *metrics) rtmpServerSet(s apiRTMPServer) {
|
2021-08-12 09:48:47 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
m.rtmpServer = s
|
|
|
|
}
|
2021-11-05 16:29:13 +00:00
|
|
|
|
2023-05-16 13:59:37 +00:00
|
|
|
// webRTCManagerSet is called by webRTCManager.
|
|
|
|
func (m *metrics) webRTCManagerSet(s apiWebRTCManager) {
|
2021-11-05 16:29:13 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2023-05-16 13:59:37 +00:00
|
|
|
m.webRTCManager = s
|
2021-11-05 16:29:13 +00:00
|
|
|
}
|