2019-10-10 19:30:37 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-10-31 13:59:24 +00:00
|
|
|
"bytes"
|
2019-10-10 19:30:37 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2019-10-15 19:14:25 +00:00
|
|
|
"strings"
|
2019-10-10 19:30:37 +00:00
|
|
|
"time"
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/monitor"
|
2019-10-10 19:30:37 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-10-31 13:59:24 +00:00
|
|
|
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
|
2019-10-10 19:30:37 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
)
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
type Agent struct {
|
2019-10-10 19:30:37 +00:00
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:36:39 +00:00
|
|
|
func NewAgentEndpoint(c *Client) *Agent {
|
2019-10-30 13:28:24 +00:00
|
|
|
m := &Agent{c: c}
|
2019-10-15 14:33:07 +00:00
|
|
|
m.c.streamingRpcs.Register("Agent.Monitor", m.monitor)
|
2019-10-10 19:30:37 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
func (m *Agent) monitor(conn io.ReadWriteCloser) {
|
2019-11-04 19:17:15 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "agent", "monitor"}, time.Now())
|
2019-10-15 14:33:07 +00:00
|
|
|
defer conn.Close()
|
2019-10-10 19:30:37 +00:00
|
|
|
|
|
|
|
// Decode arguments
|
2019-10-24 16:47:46 +00:00
|
|
|
var args cstructs.MonitorRequest
|
2019-10-10 19:30:37 +00:00
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
2019-10-24 16:47:46 +00:00
|
|
|
if err := decoder.Decode(&args); err != nil {
|
2019-10-10 19:30:37 +00:00
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check acl
|
2019-10-25 18:25:19 +00:00
|
|
|
if aclObj, err := m.c.ResolveToken(args.AuthToken); err != nil {
|
2019-10-10 19:30:37 +00:00
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(403), encoder)
|
|
|
|
return
|
2019-10-25 18:25:19 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentRead() {
|
2019-10-10 19:30:37 +00:00
|
|
|
handleStreamResultError(structs.ErrPermissionDenied, helper.Int64ToPtr(403), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:36:39 +00:00
|
|
|
logLevel := log.LevelFromString(args.LogLevel)
|
2019-10-24 16:47:46 +00:00
|
|
|
if args.LogLevel == "" {
|
2019-10-10 19:30:37 +00:00
|
|
|
logLevel = log.LevelFromString("INFO")
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel == log.NoLevel {
|
|
|
|
handleStreamResultError(errors.New("Unknown log level"), helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
stopCh := make(chan struct{})
|
2019-10-10 19:30:37 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2019-10-15 19:14:25 +00:00
|
|
|
defer close(stopCh)
|
2019-10-10 19:30:37 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
monitor := monitor.New(512, m.c.logger, &log.LoggerOptions{
|
2019-10-24 16:47:46 +00:00
|
|
|
JSONFormat: args.LogJSON,
|
2019-10-15 19:14:25 +00:00
|
|
|
Level: logLevel,
|
2019-10-10 19:30:37 +00:00
|
|
|
})
|
|
|
|
|
2019-10-31 13:59:24 +00:00
|
|
|
frames := make(chan *sframer.StreamFrame, streamFramesBuffer)
|
|
|
|
errCh := make(chan error)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle)
|
|
|
|
|
|
|
|
framer := sframer.NewStreamFramer(frames, 1*time.Second, 200*time.Millisecond, 1024)
|
|
|
|
framer.Run()
|
|
|
|
defer framer.Destroy()
|
|
|
|
|
|
|
|
// goroutine to detect remote side closing
|
2019-10-10 19:30:37 +00:00
|
|
|
go func() {
|
2019-10-15 19:14:25 +00:00
|
|
|
if _, err := conn.Read(nil); err != nil {
|
2019-10-24 16:47:46 +00:00
|
|
|
// One end of the pipe explicitly closed, exit
|
2019-10-15 19:14:25 +00:00
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2019-10-10 19:30:37 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
logCh := monitor.Start(stopCh)
|
2019-10-31 13:59:24 +00:00
|
|
|
initialOffset := int64(0)
|
|
|
|
|
|
|
|
// receive logs and build frames
|
|
|
|
go func() {
|
|
|
|
defer framer.Destroy()
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-logCh:
|
|
|
|
if err := framer.Send("", "log", log, initialOffset); err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-10-15 19:14:25 +00:00
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
var streamErr error
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
2019-10-31 13:59:24 +00:00
|
|
|
case frame, ok := <-frames:
|
|
|
|
if !ok {
|
|
|
|
// frame may have been closed when an error
|
|
|
|
// occurred. Check once more for an error.
|
|
|
|
select {
|
|
|
|
case streamErr = <-errCh:
|
|
|
|
// There was a pending error!
|
|
|
|
default:
|
|
|
|
// No error, continue on
|
|
|
|
}
|
|
|
|
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
var resp cstructs.StreamErrWrapper
|
2019-10-31 13:59:24 +00:00
|
|
|
if args.PlainText {
|
|
|
|
resp.Payload = frame.Data
|
|
|
|
} else {
|
|
|
|
if err := frameCodec.Encode(frame); err != nil {
|
|
|
|
streamErr = err
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Payload = buf.Bytes()
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
|
2019-10-10 19:30:37 +00:00
|
|
|
if err := encoder.Encode(resp); err != nil {
|
|
|
|
streamErr = err
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
encoder.Reset(conn)
|
|
|
|
case <-ctx.Done():
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if streamErr != nil {
|
2019-10-15 19:14:25 +00:00
|
|
|
// Nothing to do as conn is closed
|
|
|
|
if streamErr == io.EOF || strings.Contains(streamErr.Error(), "closed") {
|
|
|
|
return
|
|
|
|
}
|
2019-10-10 19:30:37 +00:00
|
|
|
}
|
|
|
|
}
|