forked from newrelic/node-newrelic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-dockerinfo.js
57 lines (50 loc) · 1.67 KB
/
parse-dockerinfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'
var logger = require('./logger.js').child({component: 'dockerinfo'})
var NAMES = require('./metrics/names.js')
module.exports = parseDockerInfo
function parseDockerInfo(agent, data) {
if (!agent.config.utilization || !agent.config.utilization.detect_docker) return null
var cpuCgroup = parseCgroupIds(data).cpu
// if we can't parse the cgroups, or if the cpu is not in a cgroup
var dockerError = agent.metrics.getOrCreateMetric(NAMES.UTILIZATION.DOCKER_ERROR)
if (!cpuCgroup) {
logger.debug('Could not parse cgroup data from: ' + data)
dockerError.incrementCallCount()
return null
}
// if cpu isn't in a cgroup
if (cpuCgroup === '/') return null
var patterns = [
/^\/docker\/([0-9a-f]+)$/, // docker native driver w/out systemd
/^\/system\.slice\/docker-([0-9a-f]+)\.scope$/, // with systemd
/^\/lxc\/([0-9a-f]+)$/ // docker lxc driver
]
for (var i = 0; i < patterns.length; i++) {
var pattern = patterns[i]
var matches = cpuCgroup.match(pattern)
if (matches) {
var id = matches[1]
if (id.length !== 64) {
dockerError.incrementCallCount()
logger.debug('Encountered a malformed docker id: ', id)
return null
}
return id
}
}
logger.debug('Unable to recognise cgroup format')
return null
}
function parseCgroupIds(cgroupInfo) {
var cgroupIds = {}
cgroupInfo.split('\n').forEach(function parseCgroupInfo(line) {
var parts = line.split(':')
if (parts.length !== 3) return
var subsystems = parts[1]
var cgroupId = parts[2]
subsystems.split(',').forEach(function assignGroupIds(subsystem) {
cgroupIds[subsystem] = cgroupId
})
})
return cgroupIds
}