11package dockergen
22
33import (
4+ "io/ioutil"
45 "os"
6+ "regexp"
7+ "strings"
58 "sync"
69
710 "github.com/fsouza/go-dockerclient"
@@ -29,14 +32,16 @@ func SetServerInfo(d *docker.Env) {
2932 mu .Lock ()
3033 defer mu .Unlock ()
3134 dockerInfo = Docker {
32- Name : d .Get ("Name" ),
33- NumContainers : d .GetInt ("Containers" ),
34- NumImages : d .GetInt ("Images" ),
35- Version : dockerEnv .Get ("Version" ),
36- ApiVersion : dockerEnv .Get ("ApiVersion" ),
37- GoVersion : dockerEnv .Get ("GoVersion" ),
38- OperatingSystem : dockerEnv .Get ("Os" ),
39- Architecture : dockerEnv .Get ("Arch" ),
35+ Name : d .Get ("Name" ),
36+ NumContainers : d .GetInt ("Containers" ),
37+ NumImages : d .GetInt ("Images" ),
38+ Version : dockerEnv .Get ("Version" ),
39+ ApiVersion : dockerEnv .Get ("ApiVersion" ),
40+ GoVersion : dockerEnv .Get ("GoVersion" ),
41+ OperatingSystem : dockerEnv .Get ("Os" ),
42+ Architecture : dockerEnv .Get ("Arch" ),
43+ CurrentContainerName : GetCurrentContainerName (),
44+
4045 }
4146}
4247
@@ -139,12 +144,35 @@ type Mount struct {
139144}
140145
141146type Docker struct {
142- Name string
143- NumContainers int
144- NumImages int
145- Version string
146- ApiVersion string
147- GoVersion string
148- OperatingSystem string
149- Architecture string
147+ Name string
148+ NumContainers int
149+ NumImages int
150+ Version string
151+ ApiVersion string
152+ GoVersion string
153+ OperatingSystem string
154+ Architecture string
155+ CurrentContainerName string
150156}
157+
158+ func GetCurrentContainerName () string {
159+ contents , err := ioutil .ReadFile ("/proc/self/cgroup" )
160+ if err != nil {
161+ println ("No /proc/self/cgroup file. Fail to detect current container ID" )
162+ return ""
163+ }
164+
165+ lines := strings .Split (string (contents ), "\n " )
166+ re := regexp .MustCompilePOSIX ("^1" )
167+
168+ for _ , line := range lines {
169+ if re .MatchString (line ) == true {
170+ splited := strings .SplitN (line , "/docker-" , 2 )
171+ splited = strings .SplitN (splited [len (splited ) - 1 ], ".scope" , 2 )
172+
173+ return splited [0 ]
174+ }
175+ }
176+
177+ return ""
178+ }
0 commit comments