@@ -39,7 +39,6 @@ import (
39
39
)
40
40
41
41
var (
42
- whitelistedUlimits = [... ]string {"max_open_files" }
43
42
referencedResetInterval = flag .Uint64 ("referenced_reset_interval" , 0 ,
44
43
"Reset interval for referenced bytes (container_referenced_bytes metric), number of measurement cycles after which referenced bytes are cleared, if set to 0 referenced bytes are never cleared (default: 0)" )
45
44
@@ -205,51 +204,53 @@ func parseUlimit(value string) (int64, error) {
205
204
return num , nil
206
205
}
207
206
208
- func isUlimitWhitelisted (name string ) bool {
209
- for _ , whitelist := range whitelistedUlimits {
210
- if name == whitelist {
211
- return true
212
- }
213
- }
214
- return false
215
- }
216
-
217
207
func processLimitsFile (fileData string ) []info.UlimitSpec {
208
+ const maxOpenFilesLinePrefix = "Max open files"
209
+
218
210
limits := strings .Split (fileData , "\n " )
219
211
ulimits := make ([]info.UlimitSpec , 0 , len (limits ))
220
212
for _ , lim := range limits {
221
213
// Skip any headers/footers
222
- if strings .HasPrefix (lim , "Max" ) {
223
-
224
- // Line format: Max open files 16384 16384 files
225
- fields := regexp .MustCompile (`[\s]{2,}` ).Split (lim , - 1 )
226
- name := strings .Replace (strings .ToLower (strings .TrimSpace (fields [0 ])), " " , "_" , - 1 )
227
-
228
- found := isUlimitWhitelisted (name )
229
- if ! found {
230
- continue
231
- }
232
-
233
- soft := strings .TrimSpace (fields [1 ])
234
- softNum , softErr := parseUlimit (soft )
235
-
236
- hard := strings .TrimSpace (fields [2 ])
237
- hardNum , hardErr := parseUlimit (hard )
238
-
239
- // Omit metric if there were any parsing errors
240
- if softErr == nil && hardErr == nil {
241
- ulimitSpec := info.UlimitSpec {
242
- Name : name ,
243
- SoftLimit : int64 (softNum ),
244
- HardLimit : int64 (hardNum ),
245
- }
246
- ulimits = append (ulimits , ulimitSpec )
214
+ if strings .HasPrefix (lim , "Max open files" ) {
215
+ // Remove line prefix
216
+ ulimit , err := processMaxOpenFileLimitLine (
217
+ "max_open_files" ,
218
+ lim [len (maxOpenFilesLinePrefix ):],
219
+ )
220
+ if err == nil {
221
+ ulimits = append (ulimits , ulimit )
247
222
}
248
223
}
249
224
}
250
225
return ulimits
251
226
}
252
227
228
+ // Any caller of processMaxOpenFileLimitLine must ensure that the name prefix is already removed from the limit line.
229
+ // with the "Max open files" prefix.
230
+ func processMaxOpenFileLimitLine (name , line string ) (info.UlimitSpec , error ) {
231
+ // Remove any leading whitespace
232
+ line = strings .TrimSpace (line )
233
+ // Split on whitespace
234
+ fields := strings .Fields (line )
235
+ if len (fields ) != 3 {
236
+ return info.UlimitSpec {}, fmt .Errorf ("unable to parse max open files line: %s" , line )
237
+ }
238
+ // The first field is the soft limit, the second is the hard limit
239
+ soft , err := parseUlimit (fields [0 ])
240
+ if err != nil {
241
+ return info.UlimitSpec {}, err
242
+ }
243
+ hard , err := parseUlimit (fields [1 ])
244
+ if err != nil {
245
+ return info.UlimitSpec {}, err
246
+ }
247
+ return info.UlimitSpec {
248
+ Name : name ,
249
+ SoftLimit : soft ,
250
+ HardLimit : hard ,
251
+ }, nil
252
+ }
253
+
253
254
func processRootProcUlimits (rootFs string , rootPid int ) []info.UlimitSpec {
254
255
filePath := path .Join (rootFs , "/proc" , strconv .Itoa (rootPid ), "limits" )
255
256
out , err := os .ReadFile (filePath )
0 commit comments