github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/daemon/metrics_unix.go (about)

     1  // +build !windows
     2  
     3  package daemon
     4  
     5  import (
     6  	"net"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  	"syscall"
    11  
    12  	"github.com/Sirupsen/logrus"
    13  	"github.com/docker/docker/pkg/mount"
    14  	"github.com/docker/docker/pkg/plugingetter"
    15  	"github.com/docker/docker/pkg/plugins"
    16  	metrics "github.com/docker/go-metrics"
    17  	"github.com/pkg/errors"
    18  )
    19  
    20  func (daemon *Daemon) listenMetricsSock() (string, error) {
    21  	path := filepath.Join(daemon.configStore.ExecRoot, "metrics.sock")
    22  	syscall.Unlink(path)
    23  	l, err := net.Listen("unix", path)
    24  	if err != nil {
    25  		return "", errors.Wrap(err, "error setting up metrics plugin listener")
    26  	}
    27  
    28  	mux := http.NewServeMux()
    29  	mux.Handle("/metrics", metrics.Handler())
    30  	go func() {
    31  		http.Serve(l, mux)
    32  	}()
    33  	daemon.metricsPluginListener = l
    34  	return path, nil
    35  }
    36  
    37  func registerMetricsPluginCallback(getter plugingetter.PluginGetter, sockPath string) {
    38  	getter.Handle(metricsPluginType, func(name string, client *plugins.Client) {
    39  		// Use lookup since nothing in the system can really reference it, no need
    40  		// to protect against removal
    41  		p, err := getter.Get(name, metricsPluginType, plugingetter.Lookup)
    42  		if err != nil {
    43  			return
    44  		}
    45  
    46  		mp := metricsPlugin{p}
    47  		sockBase := mp.sockBase()
    48  		if err := os.MkdirAll(sockBase, 0755); err != nil {
    49  			logrus.WithError(err).WithField("name", name).WithField("path", sockBase).Error("error creating metrics plugin base path")
    50  			return
    51  		}
    52  
    53  		defer func() {
    54  			if err != nil {
    55  				os.RemoveAll(sockBase)
    56  			}
    57  		}()
    58  
    59  		pluginSockPath := filepath.Join(sockBase, mp.sock())
    60  		_, err = os.Stat(pluginSockPath)
    61  		if err == nil {
    62  			mount.Unmount(pluginSockPath)
    63  		} else {
    64  			logrus.WithField("path", pluginSockPath).Debugf("creating plugin socket")
    65  			f, err := os.OpenFile(pluginSockPath, os.O_CREATE, 0600)
    66  			if err != nil {
    67  				return
    68  			}
    69  			f.Close()
    70  		}
    71  
    72  		if err := mount.Mount(sockPath, pluginSockPath, "none", "bind,ro"); err != nil {
    73  			logrus.WithError(err).WithField("name", name).Error("could not mount metrics socket to plugin")
    74  			return
    75  		}
    76  
    77  		if err := pluginStartMetricsCollection(p); err != nil {
    78  			if err := mount.Unmount(pluginSockPath); err != nil {
    79  				if mounted, _ := mount.Mounted(pluginSockPath); mounted {
    80  					logrus.WithError(err).WithField("sock_path", pluginSockPath).Error("error unmounting metrics socket from plugin during cleanup")
    81  				}
    82  			}
    83  			logrus.WithError(err).WithField("name", name).Error("error while initializing metrics plugin")
    84  		}
    85  	})
    86  }