@@ -17,16 +17,20 @@ package api
1717
1818import (
1919 "encoding/json"
20+ "errors"
2021 "fmt"
2122 "io"
2223 "net/http"
24+ "net/url"
2325 "path"
2426 "regexp"
2527 "sort"
28+ "strconv"
2629 "strings"
2730 "time"
2831
2932 "github.com/golang/glog"
33+ "github.com/google/cadvisor/events"
3034 "github.com/google/cadvisor/info"
3135 "github.com/google/cadvisor/manager"
3236)
@@ -125,6 +129,7 @@ func writeResult(res interface{}, w http.ResponseWriter) error {
125129 w .Header ().Set ("Content-Type" , "application/json" )
126130 w .Write (out )
127131 return nil
132+
128133}
129134
130135func getContainerInfoRequest (body io.ReadCloser ) (* info.ContainerInfoRequest , error ) {
@@ -142,6 +147,84 @@ func getContainerInfoRequest(body io.ReadCloser) (*info.ContainerInfoRequest, er
142147 return & query , nil
143148}
144149
150+ // request is the rawQuery portion of the url input by the user. It should have
151+ // length 1, otherwise the url was ill formatted (backslashes must have
152+ // been inserted in the argument section of the url) and function will return an
153+ // error. The user can set any or none of the following arguments in any order
154+ // with any twice defined arguments being assigned the first value.
155+ // If the value type for the argument is wrong the field will be assumed to be
156+ // unassigned
157+ // bools: all_time, subcontainers, oom_events, creation_events, deletion_events
158+ // ints: max_events, start_time (unix timestamp), end_time (unix timestamp)
159+ // example request: ["oom_events=true&max_events=10"]
160+ func getEventRequest (request []string ) (* events.Request , bool , error ) {
161+ if len (request ) <= 0 || len (request [0 ]) == 0 {
162+ return events .NewAllEventsRequest (), true , nil
163+ } else if len (request ) > 1 {
164+ return nil , false , errors .New (
165+ "should have received one url rawQuery but got multiple" )
166+ }
167+ rawQuery := strings .Trim (request [0 ], "/ " )
168+
169+ query := events .NewRequest ()
170+ getEventsFromAllTime := false
171+
172+ urlMap , err := url .ParseQuery (rawQuery )
173+ if err != nil {
174+ glog .Errorf ("url parse error of %v is %v" , rawQuery , err )
175+ return nil , false , err
176+ }
177+
178+ for key , val := range urlMap {
179+ if key == "all_time" {
180+ newBool , err := strconv .ParseBool (val [0 ])
181+ if err == nil {
182+ getEventsFromAllTime = newBool
183+ }
184+ } else if key == "subcontainers" {
185+ newBool , err := strconv .ParseBool (val [0 ])
186+ if err == nil {
187+ query .IncludeSubcontainers = newBool
188+ }
189+ } else if key == "oom_events" {
190+ newBool , err := strconv .ParseBool (val [0 ])
191+ if err == nil {
192+ query .EventType [events .TypeOom ] = newBool
193+ }
194+ } else if key == "creation_events" {
195+ newBool , err := strconv .ParseBool (val [0 ])
196+ if err == nil {
197+ query .EventType [events .TypeContainerCreation ] = newBool
198+ }
199+ } else if key == "deletion_events" {
200+ newBool , err := strconv .ParseBool (val [0 ])
201+ if err == nil {
202+ query .EventType [events .TypeContainerDeletion ] = newBool
203+ }
204+ } else if key == "max_events" {
205+ newInt , err := strconv .Atoi (val [0 ])
206+ if err == nil {
207+ query .MaxEventsReturned = int (newInt )
208+ }
209+ } else if key == "start_time" {
210+ newTime , err := time .Parse (time .RFC3339 , val [0 ])
211+ if err == nil {
212+ query .StartTime = newTime
213+ }
214+ } else if key == "end_time" {
215+ newTime , err := time .Parse (time .RFC3339 , val [0 ])
216+ if err == nil {
217+ query .EndTime = newTime
218+ }
219+ }
220+ }
221+
222+ glog .V (2 ).Infof (
223+ "%v was returned in api/handler.go:getEventRequest from the url rawQuery %v" ,
224+ query , rawQuery )
225+ return query , getEventsFromAllTime , nil
226+ }
227+
145228func getContainerName (request []string ) string {
146229 return path .Join ("/" , strings .Join (request , "/" ))
147230}
0 commit comments