github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/procfs/procfs.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package procfs
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"path"
    23  	"strconv"
    24  	"strings"
    25  )
    26  
    27  type ProcFS struct{}
    28  
    29  func NewProcFS() ProcFSInterface {
    30  	return &ProcFS{}
    31  }
    32  
    33  func containerNameFromProcCgroup(content string) (string, error) {
    34  	lines := strings.Split(content, "\n")
    35  	for _, line := range lines {
    36  		entries := strings.SplitN(line, ":", 3)
    37  		if len(entries) == 3 && entries[1] == "devices" {
    38  			return strings.TrimSpace(entries[2]), nil
    39  		}
    40  	}
    41  	return "", fmt.Errorf("could not find devices cgroup location")
    42  }
    43  
    44  // getFullContainerName gets the container name given the root process id of the container.
    45  // Eg. If the devices cgroup for the container is stored in /sys/fs/cgroup/devices/docker/nginx,
    46  // return docker/nginx. Assumes that the process is part of exactly one cgroup hierarchy.
    47  func (pfs *ProcFS) GetFullContainerName(pid int) (string, error) {
    48  	filePath := path.Join("/proc", strconv.Itoa(pid), "cgroup")
    49  	content, err := ioutil.ReadFile(filePath)
    50  	if err != nil {
    51  		return "", err
    52  	}
    53  	return containerNameFromProcCgroup(string(content))
    54  }