@@ -4,10 +4,12 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "os"
78 "strconv"
89 "strings"
910 "time"
1011
12+ "github.com/YLonely/sysdig-monitor/errdefs"
1113 "github.com/YLonely/sysdig-monitor/log"
1214 "github.com/YLonely/sysdig-monitor/server/model"
1315)
@@ -29,10 +31,9 @@ type containerEvent struct {
2931 rawRes int
3032 syscallType string
3133 virtualtid int
32- image string
3334}
3435
35- func processLoop (ctx context.Context , c * container , ch chan containerEvent ) error {
36+ func processLoop (ctx context.Context , c * mutexContainer , ch chan containerEvent ) error {
3637 var e containerEvent
3738 var err error
3839 for {
@@ -64,8 +65,8 @@ func processLoop(ctx context.Context, c *container, ch chan containerEvent) erro
6465 }
6566 } else {
6667 // may have some other handler?
67- if err = handleNetwork (c .Container , e ); err != nil {
68- // log.L.WithField("container-id", c.ID).WithError(err).Error ("network handler error")
68+ if err = handleNetwork (c .Container , e ); err != nil && ! errdefs . IsErrWrongFormat ( err ) {
69+ log .L .WithField ("container-id" , c .ID ).WithError (err ).Warning ("network handler error" )
6970 }
7071 }
7172 c .m .Unlock ()
@@ -79,10 +80,14 @@ func handleSysCall(c *model.Container, e containerEvent) error {
7980 if len (syscall ) <= 0 {
8081 return nil
8182 }
82- if _ , exists := c .IndividualCalls [syscall ]; ! exists {
83- c .IndividualCalls [syscall ] = & model.SystemCall {Name : syscall }
83+ var (
84+ call * model.SystemCall
85+ exists bool
86+ )
87+ if call , exists = c .IndividualCalls [syscall ]; ! exists {
88+ call = & model.SystemCall {Name : syscall }
89+ c .IndividualCalls [syscall ] = call
8490 }
85- call := c .IndividualCalls [syscall ]
8691 call .Calls ++
8792 call .TotalTime += latency
8893 c .SystemCalls .TotalCalls ++
@@ -106,10 +111,14 @@ func handleNetIO(c *model.Container, e containerEvent) error {
106111 }
107112 // if event shows that a net io begins before "connect" or "accpet",
108113 // we just ignore the error sequence and add a new connection
109- if _ , exists := c .ActiveConnections [meta ]; ! exists {
110- c .ActiveConnections [meta ] = & model.Connection {Type : e .fdType }
114+ var (
115+ conn * model.Connection
116+ exists bool
117+ )
118+ if conn , exists = c .ActiveConnections [meta ]; ! exists {
119+ conn = & model.Connection {Type : e .fdType }
120+ c .ActiveConnections [meta ] = conn
111121 }
112- conn := c .ActiveConnections [meta ]
113122 if e .isIORead {
114123 conn .ReadIn += int64 (bufLen )
115124 c .Network .TotalReadIn += int64 (bufLen )
@@ -123,14 +132,25 @@ func handleNetIO(c *model.Container, e containerEvent) error {
123132func handleFileIO (c * model.Container , e containerEvent ) error {
124133 fileName := e .fdName
125134 bufLen := e .bufferLen
126- if _ , exists := c .AccessedFiles [fileName ]; ! exists {
127- c .AccessedFiles [fileName ] = & model.File {Name : fileName }
135+ var (
136+ file * model.File
137+ exists bool
138+ )
139+
140+ if file , exists = c .AccessedFiles [fileName ]; ! exists {
141+ file = & model.File {Name : fileName }
142+ err := attachToLayer (c , file )
143+ if err != nil {
144+ return nil
145+ }
146+ c .AccessedFiles [fileName ] = file
128147 }
129- file := c .AccessedFiles [fileName ]
130148 if e .isIOWrite {
131149 file .WriteOut += int64 (bufLen )
150+ file .Layer .WriteOut += int64 (bufLen )
132151 c .FileSystem .TotalWriteOut += int64 (bufLen )
133152 } else if e .isIORead {
153+ file .Layer .ReadIn += int64 (bufLen )
134154 file .ReadIn += int64 (bufLen )
135155 c .FileSystem .TotalReadIn += int64 (bufLen )
136156 }
@@ -179,7 +199,7 @@ func connectionMeta(fdname string) (model.ConnectionMeta, error) {
179199 parts := strings .Split (fdname , "->" )
180200 meta := model.ConnectionMeta {}
181201 if len (parts ) != 2 {
182- return meta , fmt . Errorf ( "wrong connection meta format:%v" , fdname )
202+ return meta , errdefs . NewErrWrongFormat ( fdname )
183203 }
184204 source , dest := parts [0 ], parts [1 ]
185205
@@ -199,15 +219,27 @@ func splitAddress(address string) (string, int, error) {
199219 err error
200220 )
201221 if len (address ) <= 1 {
202- return "" , - 1 , errors . New ( "empty address" )
222+ return "" , - 1 , errdefs . NewErrWrongFormat ( address )
203223 }
204224 for portStart = len (address ) - 1 ; portStart >= 0 && address [portStart ] != ':' ; portStart -- {
205225 }
206226 if portStart <= 0 {
207- return "" , - 1 , errors . New ( "no port address" )
227+ return "" , - 1 , errdefs . NewErrWrongFormat ( address )
208228 }
209229 if port , err = strconv .Atoi (address [portStart + 1 :]); err != nil {
210- return "" , - 1 , fmt . Errorf ( "wrong address format:%v" , address )
230+ return "" , - 1 , errdefs . NewErrWrongFormat ( address )
211231 }
212232 return address [:portStart ], port , nil
213233}
234+
235+ func attachToLayer (c * model.Container , file * model.File ) error {
236+ fileName := file .Name
237+ for _ , dir := range c .LayersInOrder {
238+ if _ , err := os .Stat (dir + fileName ); err == nil {
239+ c .AccessedLayers [dir ].AccessedFiles [fileName ] = file
240+ file .Layer = c .AccessedLayers [dir ]
241+ return nil
242+ }
243+ }
244+ return fmt .Errorf ("cant find file %v in any of lower layers" , fileName )
245+ }
0 commit comments