Skip to content

Commit 0c3d339

Browse files
committed
1. fix sysdig exit bug
2. fix container net info collecting bug 3. add a route to return container info
1 parent 32a46d0 commit 0c3d339

File tree

9 files changed

+73
-49
lines changed

9 files changed

+73
-49
lines changed

log/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ var L = logrus.New()
1111

1212
func init() {
1313
L.SetOutput(os.Stdout)
14-
L.SetLevel(logrus.DebugLevel)
14+
L.SetLevel(logrus.InfoLevel)
1515
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func main() {
3737
signal.Notify(signals, handledSignals...)
3838
log.L.Info("sysdig-monitor successfully booted")
3939
<-done
40+
log.L.Info("sysdig-monitor exits")
4041
return nil
4142
}
4243
app.Run(os.Args)

server/controller/container/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func newMutexContainer(id, name string) *mutexContainer {
3636
return &mutexContainer{Container: c}
3737
}
3838

39-
const eventBufferLen = 1024
39+
const eventBufferLen = 512
4040
const unknownContainerName = "<unknown>"
4141
const incompleteContainerName = "incomplete"
4242

server/controller/container/routes.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package container
22

33
import (
4+
"github.com/YLonely/sysdig-monitor/server/model"
45
"github.com/gin-gonic/gin"
56
)
67

@@ -14,6 +15,16 @@ func (cc *ContainerController) getAllContainers(c *gin.Context) {
1415
c.JSON(200, res)
1516
}
1617

18+
type FlattenConnection struct {
19+
model.ConnectionMeta
20+
model.Connection
21+
}
22+
23+
type GetContainerResponse struct {
24+
*model.Container
25+
ActiveConnections []FlattenConnection `json:"active_connections"`
26+
}
27+
1728
func (cc *ContainerController) getContainer(c *gin.Context) {
1829
cid := c.Param("id")
1930
cc.cm.RLock()
@@ -25,5 +36,9 @@ func (cc *ContainerController) getContainer(c *gin.Context) {
2536
}
2637
container.m.RLock()
2738
defer container.m.RUnlock()
28-
c.JSON(200, container.Container)
39+
flattenConns := []FlattenConnection{}
40+
for meta, conn := range container.ActiveConnections {
41+
flattenConns = append(flattenConns, FlattenConnection{Connection: *conn, ConnectionMeta: meta})
42+
}
43+
c.JSON(200, GetContainerResponse{Container: container.Container, ActiveConnections: flattenConns})
2944
}

server/controller/container/util.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func processLoop(ctx context.Context, c *mutexContainer, ch chan containerEvent)
6464
} else {
6565
// may have some other handler?
6666
if err = handleNetwork(c.Container, e); err != nil {
67-
log.L.WithField("container-id", c.ID).WithError(err).Error("network handler error")
67+
//log.L.WithField("container-id", c.ID).WithError(err).Error("network handler error")
6868
}
6969
}
7070
c.m.Unlock()
@@ -134,8 +134,9 @@ func handleFileIO(c *model.Container, e containerEvent) error {
134134
c.FileSystem.TotalReadIn += int64(bufLen)
135135
}
136136
latency := e.latency
137+
latency /= time.Millisecond
137138
if !strings.HasPrefix(e.fdName, "/dev/") {
138-
iocall := &model.IOCall{FileName: fileName, Latency: latency}
139+
iocall := &model.IOCall{FileName: fileName, Latency: e.latency}
139140
if latency > latency100 {
140141
c.IOCalls100 = append(c.IOCalls100, iocall)
141142
}
@@ -196,10 +197,13 @@ func splitAddress(address string) (string, int, error) {
196197
portStart, port int
197198
err error
198199
)
199-
if len(address) <= 0 {
200+
if len(address) <= 1 {
200201
return "", -1, errors.New("empty address")
201202
}
202-
for portStart := len(address) - 1; portStart >= 0 && address[portStart] != ':'; portStart-- {
203+
for portStart = len(address) - 1; portStart >= 0 && address[portStart] != ':'; portStart-- {
204+
}
205+
if portStart <= 0 {
206+
return "", -1, errors.New("no port address")
203207
}
204208
if port, err = strconv.Atoi(address[portStart+1:]); err != nil {
205209
return "", -1, fmt.Errorf("wrong address format:%v", address)

server/model/container.go

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package model
33
import "time"
44

55
type Container struct {
6-
ID string
7-
Name string
6+
ID string `json:"id"`
7+
Name string `json:"name"`
88

99
SystemCalls
1010

@@ -22,62 +22,57 @@ func NewContainer(id, name string) *Container {
2222

2323
type SystemCalls struct {
2424
// map system call name to SystemCall
25-
IndividualCalls map[string]*SystemCall
26-
TotalCalls int64
25+
IndividualCalls map[string]*SystemCall `json:"individual_calls"`
26+
TotalCalls int64 `json:"total_calls"`
2727
}
2828

2929
type FileSystem struct {
3030
// map file name to file
31-
AccessedFiles map[string]*File
31+
AccessedFiles map[string]*File `json:"accessed_files"`
3232
// io calls whose latency is bigger than 1ms
33-
IOCalls1 []*IOCall
33+
IOCalls1 []*IOCall `json:"io_calls_more_than_1ms"`
3434
// io calls whose latency is bigger than 10ms
35-
IOCalls10 []*IOCall
35+
IOCalls10 []*IOCall `json:"io_calls_more_than_10ms"`
3636
// io calls whose latency is bigger than 100ms
37-
IOCalls100 []*IOCall
38-
TotalReadIn int64
39-
TotalWriteOut int64
37+
IOCalls100 []*IOCall `json:"io_calls_more_than_100ms"`
38+
TotalReadIn int64 `json:"file_total_read_in"`
39+
TotalWriteOut int64 `json:"file_total_write_out"`
4040
}
4141

4242
type Network struct {
43-
ActiveConnections map[ConnectionMeta]*Connection `json:"-"`
44-
FlattenConnections []*FlattenConnection `json:"active_connections"`
45-
TotalReadIn, TotalWriteOut int64
43+
ActiveConnections map[ConnectionMeta]*Connection `json:"-"`
44+
TotalReadIn int64 `json:"net_total_read_in"`
45+
TotalWriteOut int64 `json:"net_total_wirte_out"`
4646
}
4747

4848
type SystemCall struct {
4949
Name string `json:"-"`
5050
// total number of times it is invoked
51-
Calls int64
52-
TotalTime time.Duration
51+
Calls int64 `json:"calls"`
52+
TotalTime time.Duration `json:"total_time"`
5353
}
5454

5555
type File struct {
5656
Name string `json:"-"`
57-
WriteOut int64
58-
ReadIn int64
57+
WriteOut int64 `json:"write_out"`
58+
ReadIn int64 `json:"read_in"`
5959
}
6060

6161
type Connection struct {
6262
// ipv4 or ipv6
63-
Type string
64-
WriteOut int64
65-
ReadIn int64
63+
Type string `json:"type"`
64+
WriteOut int64 `json:"write_out"`
65+
ReadIn int64 `json:"read_in"`
6666
}
6767

6868
type ConnectionMeta struct {
69-
SourceIP string
70-
DestIP string
71-
SourcePort int
72-
DestPort int
73-
}
74-
75-
type FlattenConnection struct {
76-
ConnectionMeta
77-
Connection
69+
SourceIP string `json:"source_ip"`
70+
DestIP string `json:"dest_ip"`
71+
SourcePort int `json:"source_port"`
72+
DestPort int `json:"dest_port"`
7873
}
7974

8075
type IOCall struct {
81-
FileName string
82-
Latency time.Duration
76+
FileName string `json:"file_name"`
77+
Latency time.Duration `json:"latency"`
8378
}

server/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (s *server) Start(ctx context.Context) chan error {
4646
errch <- err
4747
return errch
4848
}
49+
gin.SetMode(gin.ReleaseMode)
4950
ginServer := gin.Default()
5051
initRoutes(ginServer, containerContorller) // may be more controller?
5152
s.httpServer = &http.Server{Addr: s.conf.Port, Handler: ginServer}
@@ -59,6 +60,7 @@ func (s *server) Start(ctx context.Context) chan error {
5960
var e error
6061
select {
6162
case e = <-sysdigErrorCh:
63+
errch <- e
6264
case e = <-httpServerErrorCh:
6365
errch <- e
6466
}

sysdig-monitor

17.7 MB
Binary file not shown.

sysdig/sysdig.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@ package sysdig
33
import (
44
"context"
55
"encoding/json"
6-
"io"
6+
"errors"
77
"os/exec"
8+
"strings"
89

910
"github.com/YLonely/sysdig-monitor/log"
1011
)
1112

1213
const binaryName = "sysdig"
13-
const bufferSize = 10240
14-
const formatString = "*%evt.num %evt.outputtime %evt.cpu %thread.tid %thread.vtid %proc.name %evt.dir %evt.type %evt.info " +
14+
const bufferSize = 2048
15+
16+
// well, cant find a better filter right now
17+
// use something others will draining the cpu
18+
const filter = "container.name!=host"
19+
20+
var formatString = []string{
21+
// common part
22+
"*%evt.num %evt.outputtime %evt.cpu %thread.tid %thread.vtid %proc.name %evt.dir %evt.type %evt.info",
1523
//syscall
16-
"%syscall.type " +
24+
"%syscall.type",
1725
//container parts
18-
"%container.name %container.id " +
26+
"%container.name %container.id",
1927
//file or network parts
20-
"%fd.name %fd.type %evt.is_io_write %evt.is_io_read %evt.buffer %evt.buflen " +
28+
"%fd.name %fd.type %evt.is_io_write %evt.is_io_read %evt.buffer %evt.buflen",
2129
//performance
22-
"%evt.latency"
30+
"%evt.latency",
31+
}
2332

2433
// Server starts sysdig and dispatch events
2534
type Server interface {
@@ -49,7 +58,7 @@ func (ls *localServer) Start(ctx context.Context) (error, chan error) {
4958
log.L.WithError(err).Error("sysdig server pre check failed.")
5059
return err, nil
5160
}
52-
cmd := exec.CommandContext(ctx, binaryName, "-p", formatString, "-j", "container.name!=host")
61+
cmd := exec.CommandContext(ctx, binaryName, "-p", strings.Join(formatString, " "), "-j", filter)
5362
rd, err := cmd.StdoutPipe()
5463
if err != nil {
5564
return err, nil
@@ -74,9 +83,7 @@ func (ls *localServer) Start(ctx context.Context) (error, chan error) {
7483
for {
7584
var e Event
7685
if err := dec.Decode(&e); err != nil {
77-
if err != io.EOF {
78-
errCh <- err
79-
}
86+
errCh <- errors.New("sysdig server unexpectedly exit")
8087
return
8188
}
8289
for _, subscriber := range ls.subscribers {

0 commit comments

Comments
 (0)