2019-10-15 19:14:25 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2019-11-01 14:33:28 +00:00
|
|
|
"bytes"
|
2019-10-15 19:14:25 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2019-11-01 14:33:28 +00:00
|
|
|
"time"
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-11-01 14:33:28 +00:00
|
|
|
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
|
2019-10-15 19:14:25 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/command/agent/monitor"
|
2019-12-09 15:55:43 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/profile"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
|
|
|
|
"github.com/ugorji/go/codec"
|
|
|
|
)
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
type Agent struct {
|
2019-10-15 19:14:25 +00:00
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
func (m *Agent) register() {
|
2019-10-15 19:14:25 +00:00
|
|
|
m.srv.streamingRpcs.Register("Agent.Monitor", m.monitor)
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
func (m *Agent) Profile(args *cstructs.AgentPprofRequest, reply *cstructs.AgentPprofResponse) error {
|
|
|
|
// Targeting a node, forward request to node
|
|
|
|
if args.NodeID != "" {
|
|
|
|
return m.forwardProfileClient(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentServer := m.srv.serf.LocalMember().Name
|
|
|
|
var forwardServer bool
|
|
|
|
// Targeting a remote server which is not the leader and not this server
|
|
|
|
if args.ServerID != "" && args.ServerID != "leader" && args.ServerID != currentServer {
|
|
|
|
forwardServer = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Targeting leader and this server is not current leader
|
|
|
|
if args.ServerID == "leader" && !m.srv.IsLeader() {
|
|
|
|
forwardServer = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if forwardServer {
|
|
|
|
// forward the request
|
|
|
|
return m.forwardProfileServer(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Mark which server fulfilled the request
|
|
|
|
reply.AgentID = m.srv.serf.LocalMember().Name
|
|
|
|
|
|
|
|
// Determine which profile to run
|
|
|
|
// and generate profile. Blocks for args.Seconds
|
|
|
|
switch args.ReqType {
|
|
|
|
case profile.CPUReq:
|
|
|
|
resp, err = profile.CPUProfile(context.TODO(), args.Seconds)
|
|
|
|
case profile.CmdReq:
|
|
|
|
resp, err = profile.Cmdline()
|
|
|
|
case profile.LookupReq:
|
|
|
|
resp, err = profile.Profile(args.Profile, args.Debug)
|
|
|
|
case profile.TraceReq:
|
|
|
|
resp, err = profile.Trace(context.TODO(), args.Seconds)
|
|
|
|
default:
|
|
|
|
err = structs.NewErrRPCCoded(404, "Unknown profile request type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if profile.IsErrProfileNotFound(err) {
|
|
|
|
return structs.NewErrRPCCoded(404, err.Error())
|
|
|
|
}
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy profile response to reply
|
|
|
|
reply.Payload = resp
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
func (m *Agent) monitor(conn io.ReadWriteCloser) {
|
2019-10-15 19:14:25 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Decode args
|
|
|
|
var args cstructs.MonitorRequest
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&args); err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:04:47 +00:00
|
|
|
// Check agent read permissions
|
2019-10-15 19:14:25 +00:00
|
|
|
if aclObj, err := m.srv.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
2019-10-25 18:25:19 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentRead() {
|
|
|
|
handleStreamResultError(structs.ErrPermissionDenied, helper.Int64ToPtr(403), encoder)
|
2019-10-15 19:14:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:36:39 +00:00
|
|
|
logLevel := log.LevelFromString(args.LogLevel)
|
2019-10-15 19:14:25 +00:00
|
|
|
if args.LogLevel == "" {
|
|
|
|
logLevel = log.LevelFromString("INFO")
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel == log.NoLevel {
|
|
|
|
handleStreamResultError(errors.New("Unknown log level"), helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:33:53 +00:00
|
|
|
// Targeting a node, forward request to node
|
2019-11-13 14:32:18 +00:00
|
|
|
if args.NodeID != "" {
|
2019-11-12 21:23:39 +00:00
|
|
|
m.forwardMonitorClient(conn, args, encoder, decoder)
|
2019-11-07 13:33:53 +00:00
|
|
|
// forwarded request has ended, return
|
|
|
|
return
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 14:32:18 +00:00
|
|
|
currentServer := m.srv.serf.LocalMember().Name
|
|
|
|
var forwardServer bool
|
|
|
|
// Targeting a remote server which is not the leader and not this server
|
|
|
|
if args.ServerID != "" && args.ServerID != "leader" && args.ServerID != currentServer {
|
|
|
|
forwardServer = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Targeting leader and this server is not current leader
|
|
|
|
if args.ServerID == "leader" && !m.srv.IsLeader() {
|
|
|
|
forwardServer = true
|
2019-11-12 19:22:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 14:32:18 +00:00
|
|
|
if forwardServer {
|
2019-11-12 21:23:39 +00:00
|
|
|
m.forwardMonitorServer(conn, args, encoder, decoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
// NodeID was empty, so monitor this current server
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
monitor := monitor.New(512, m.srv.logger, &log.LoggerOptions{
|
|
|
|
Level: logLevel,
|
2019-10-24 16:47:46 +00:00
|
|
|
JSONFormat: args.LogJSON,
|
2019-10-15 19:14:25 +00:00
|
|
|
})
|
|
|
|
|
2019-11-01 14:33:28 +00:00
|
|
|
frames := make(chan *sframer.StreamFrame, 32)
|
|
|
|
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-15 19:14:25 +00:00
|
|
|
go func() {
|
|
|
|
if _, err := conn.Read(nil); err != nil {
|
2019-11-01 14:33:28 +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-11-05 14:16:51 +00:00
|
|
|
logCh := monitor.Start()
|
|
|
|
defer monitor.Stop()
|
2019-11-01 14:33:28 +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
|
|
|
|
|
|
|
var streamErr error
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-01 14:33:28 +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-15 19:14:25 +00:00
|
|
|
var resp cstructs.StreamErrWrapper
|
2019-11-01 14:33:28 +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-15 19:14:25 +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-11-04 19:32:53 +00:00
|
|
|
handleStreamResultError(streamErr, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 14:25:19 +00:00
|
|
|
|
2019-11-12 21:23:39 +00:00
|
|
|
func (m *Agent) forwardMonitorClient(conn io.ReadWriteCloser, args cstructs.MonitorRequest, encoder *codec.Encoder, decoder *codec.Decoder) {
|
2019-11-04 14:25:19 +00:00
|
|
|
nodeID := args.NodeID
|
|
|
|
|
|
|
|
snap, err := m.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := snap.NodeByID(nil, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
err := fmt.Errorf("Unknown node %q", nodeID)
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeSupportsRpc(node); err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Connection to the client either by fowarding to another server
|
|
|
|
// or creating direct stream
|
|
|
|
var clientConn net.Conn
|
|
|
|
state, ok := m.srv.getNodeConn(nodeID)
|
|
|
|
if !ok {
|
|
|
|
// Determine the server that has a connection to the node
|
|
|
|
srv, err := m.srv.serverWithNodeConn(nodeID, m.srv.Region())
|
|
|
|
if err != nil {
|
|
|
|
var code *int64
|
|
|
|
if structs.IsErrNoNodeConn(err) {
|
|
|
|
code = helper.Int64ToPtr(404)
|
|
|
|
}
|
|
|
|
handleStreamResultError(err, code, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn, err := m.srv.streamingRpc(srv, "Agent.Monitor")
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConn = conn
|
|
|
|
} else {
|
|
|
|
stream, err := NodeStreamingRpc(state.Session, "Agent.Monitor")
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clientConn = stream
|
|
|
|
}
|
|
|
|
defer clientConn.Close()
|
|
|
|
|
|
|
|
// Send the Request
|
|
|
|
outEncoder := codec.NewEncoder(clientConn, structs.MsgpackHandle)
|
|
|
|
if err := outEncoder.Encode(args); err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
structs.Bridge(conn, clientConn)
|
|
|
|
return
|
|
|
|
}
|
2019-11-12 19:22:51 +00:00
|
|
|
|
2019-11-12 21:23:39 +00:00
|
|
|
func (m *Agent) forwardMonitorServer(conn io.ReadWriteCloser, args cstructs.MonitorRequest, encoder *codec.Encoder, decoder *codec.Decoder) {
|
2019-11-13 14:32:18 +00:00
|
|
|
var target *serverParts
|
2019-11-12 21:23:39 +00:00
|
|
|
serverID := args.ServerID
|
2019-11-13 14:32:18 +00:00
|
|
|
|
2019-11-12 21:23:39 +00:00
|
|
|
// empty ServerID to prevent forwarding loop
|
|
|
|
args.ServerID = ""
|
|
|
|
|
2019-11-13 14:32:18 +00:00
|
|
|
if serverID == "leader" {
|
|
|
|
isLeader, remoteServer := m.srv.getLeader()
|
|
|
|
if !isLeader && remoteServer != nil {
|
|
|
|
target = remoteServer
|
|
|
|
}
|
|
|
|
if !isLeader && remoteServer == nil {
|
|
|
|
handleStreamResultError(structs.ErrNoLeader, helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// See if the server ID is a known member
|
|
|
|
serfMembers := m.srv.Members()
|
|
|
|
for _, mem := range serfMembers {
|
|
|
|
if mem.Name == serverID {
|
|
|
|
if ok, srv := isNomadServer(mem); ok {
|
|
|
|
target = srv
|
|
|
|
}
|
2019-11-12 21:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:32:18 +00:00
|
|
|
// Unable to find a server
|
|
|
|
if target == nil {
|
|
|
|
err := fmt.Errorf("unknown nomad server %s", serverID)
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(400), encoder)
|
2019-11-12 21:23:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 14:32:18 +00:00
|
|
|
serverConn, err := m.srv.streamingRpc(target, "Agent.Monitor")
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
2019-11-12 21:23:39 +00:00
|
|
|
defer serverConn.Close()
|
|
|
|
|
|
|
|
// Send the Request
|
|
|
|
outEncoder := codec.NewEncoder(serverConn, structs.MsgpackHandle)
|
|
|
|
if err := outEncoder.Encode(args); err != nil {
|
2019-11-13 14:32:18 +00:00
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
2019-11-12 21:23:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
structs.Bridge(conn, serverConn)
|
|
|
|
return
|
|
|
|
}
|
2019-12-09 15:55:43 +00:00
|
|
|
|
|
|
|
func (m *Agent) forwardProfileServer(args *cstructs.AgentPprofRequest, reply *cstructs.AgentPprofResponse) error {
|
|
|
|
var target *serverParts
|
|
|
|
serverID := args.ServerID
|
|
|
|
|
|
|
|
// empty ServerID to prevent forwarding loop
|
|
|
|
args.ServerID = ""
|
|
|
|
|
|
|
|
if serverID == "leader" {
|
|
|
|
isLeader, remoteServer := m.srv.getLeader()
|
|
|
|
if !isLeader && remoteServer != nil {
|
|
|
|
target = remoteServer
|
|
|
|
}
|
|
|
|
if !isLeader && remoteServer == nil {
|
|
|
|
return structs.NewErrRPCCoded(400, structs.ErrNoLeader.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// See if the server ID is a known member
|
|
|
|
serfMembers := m.srv.Members()
|
|
|
|
for _, mem := range serfMembers {
|
|
|
|
if mem.Name == serverID {
|
|
|
|
if ok, srv := isNomadServer(mem); ok {
|
|
|
|
target = srv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unable to find a server
|
|
|
|
if target == nil {
|
|
|
|
err := fmt.Errorf("unknown nomad server %s", serverID)
|
|
|
|
return structs.NewErrRPCCoded(400, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward the request
|
|
|
|
rpcErr := m.srv.forwardServer(target, "Agent.Profile", args, reply)
|
|
|
|
if rpcErr != nil {
|
|
|
|
return structs.NewErrRPCCoded(500, rpcErr.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Agent) forwardProfileClient(args *cstructs.AgentPprofRequest, reply *cstructs.AgentPprofResponse) error {
|
|
|
|
nodeID := args.NodeID
|
|
|
|
|
|
|
|
snap, err := m.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := snap.NodeByID(nil, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
err := fmt.Errorf("Unknown node %q", nodeID)
|
|
|
|
return structs.NewErrRPCCoded(400, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeSupportsRpc(node); err != nil {
|
|
|
|
return structs.NewErrRPCCoded(400, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Connection to the client either by fowarding to another server
|
|
|
|
// or creating direct stream
|
|
|
|
state, ok := m.srv.getNodeConn(nodeID)
|
|
|
|
if !ok {
|
|
|
|
// Determine the server that has a connection to the node
|
|
|
|
srv, err := m.srv.serverWithNodeConn(nodeID, m.srv.Region())
|
|
|
|
if err != nil {
|
|
|
|
code := 500
|
|
|
|
if structs.IsErrNoNodeConn(err) {
|
|
|
|
code = 404
|
|
|
|
}
|
|
|
|
return structs.NewErrRPCCoded(code, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcErr := m.srv.forwardServer(srv, "Agent.Profile", args, reply)
|
|
|
|
if rpcErr != nil {
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// NodeRpc
|
|
|
|
rpcErr := NodeRpc(state.Session, "Agent.Profile", args, reply)
|
|
|
|
if rpcErr != nil {
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|