码迷,mamicode.com
首页 > 其他好文 > 详细

CAdvisor container monitor

时间:2018-04-16 23:48:01      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:code   too   port   one   gen   test   git clone   dead   string   

Now cadvisor is useful as a container montor tool. Not only it can monitor many container level metrics, but also can expand some metrics which we need.

It`s a open source project URL: https://github.com/google/cadvisor.git

its master branch has a lot of defects. One of the problem is the daemon. url was wrote dead.In docker/container/clients.go, it was wrote to"unix://xxxxxxx",

In order to expand, it should be a dynamic parameter.

eg: func clients (clients string){

   ******

  }

run a docker container:

download the images:

go get githun.com/google/cadvisor /  git clone https://github.com/google/cadvisor.git /  download the zip package and unzip it

docker command

sudo docker run   --volume=/:/rootfs:ro   --volume=/var/run:/var/run:rw   --volume=/sys:/sys:ro   --volume=/var/lib/docker/:/var/lib/docker:ro   --volume=/dev/disk/:/dev/disk:ro   --publish=8080:8080   --detach=true   --name=cadvisor   google/cadvisor:latest

the default port is 8080.

the cadvisor container is running on localhost:8080. The metrics infomation is on localhost:8080/metrics The graph show on localhost:8080/graph

Every metrics colector is achived in google/cadvisor/collector/.... We can overwrite the collector and describle interface to register new collector.But if we update this change in vendor, we can not promisor the tags version.(glide up -v or glide up install)

 1 // Returns a new CollectorManager that is thread-compatible.
 2 func NewCollectorManager() (CollectorManager, error) {
 3     return &GenericCollectorManager{
 4         Collectors:         []*collectorData{},
 5         NextCollectionTime: time.Now(),
 6     }, nil
 7 }
 8 
 9 func GetCollectorConfigs(labels map[string]string) map[string]string {
10     configs := map[string]string{}
11     for k, v := range labels {
12         if strings.HasPrefix(k, metricLabelPrefix) {
13             name := strings.TrimPrefix(k, metricLabelPrefix)
14             configs[name] = v
15         }
16     }
17     return configs
18 }
19 
20 func (cm *GenericCollectorManager) RegisterCollector(collector Collector) error {
21     cm.Collectors = append(cm.Collectors, &collectorData{
22         collector:          collector,
23         nextCollectionTime: time.Now(),
24     })
25     return nil
26 }
27 
28 func (cm *GenericCollectorManager) GetSpec() ([]v1.MetricSpec, error) {
29     metricSpec := []v1.MetricSpec{}
30     for _, c := range cm.Collectors {
31         specs := c.collector.GetSpec()
32         metricSpec = append(metricSpec, specs...)
33     }
34 
35     return metricSpec, nil
36 }
37 
38 func (cm *GenericCollectorManager) Collect() (time.Time, map[string][]v1.MetricVal, error) {
39     var errors []error
40 
41     // Collect from all collectors that are ready.
42     var next time.Time
43     metrics := map[string][]v1.MetricVal{}
44     for _, c := range cm.Collectors {
45         if c.nextCollectionTime.Before(time.Now()) {
46             var err error3
47             c.nextCollectionTime, metrics, err = c.collector.Collect(metrics)
48             if err != nil {
49                 errors = append(errors, err)
50             }
51         }
52 
53         // Keep track of the next collector that will be ready.
54         if next.IsZero() || next.After(c.nextCollectionTime) {
55             next = c.nextCollectionTime
56         }
57     }
58     cm.NextCollectionTime = next
59     return next, metrics, compileErrors(errors)
60 }

We can build a new image in the localhost by achive the interface instead of use the default package by glide up -v.

for example:

we can set up a socket client with docker daemon. and get the inspect infomation.Docker inspect info has a container restart count.

we can expand this metrics by use follow step:

(1) register the collector

(2) new a collector client including some infomation (metric name , mode, help and so on)

(3) achive the collect and the descible interface.

 

CAdvisor container monitor

标签:code   too   port   one   gen   test   git clone   dead   string   

原文地址:https://www.cnblogs.com/13224ACMer/p/8858985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!