Documentation
¶
Index ¶
- Constants
- Variables
- func ConnectToDocker() (*client.Client, error)
- func ContainerIDForPID(pid int) (string, error)
- func ContainerIDToEntityName(cid string) string
- func FindRancherIPInLabels(labels map[string]string) (string, bool)
- func HostnameProvider(hostName string) (string, error)
- func IsContainerized() bool
- func ScrapeAllCgroups() (map[string]*ContainerCgroup, error)
- func SplitImageName(image string) (string, string, string, error)
- type CgroupIOStat
- type CgroupMemStat
- type CgroupTimesStat
- type Container
- type ContainerCgroup
- func (c ContainerCgroup) CPU() (*CgroupTimesStat, error)
- func (c ContainerCgroup) CPULimit() (float64, error)
- func (c ContainerCgroup) CPUNrThrottled() (uint64, error)
- func (c ContainerCgroup) ContainerStartTime() (int64, error)
- func (c ContainerCgroup) IO() (*CgroupIOStat, error)
- func (c ContainerCgroup) Mem() (*CgroupMemStat, error)
- func (c ContainerCgroup) MemLimit() (uint64, error)
- func (c ContainerCgroup) ParseSingleStat(target, file string) (uint64, error)
- type ContainerEvent
- type ContainerNetStats
- type InterfaceNetStats
Constants ¶
const ( ContainerCreatedState string = "created" ContainerRunningState string = "running" ContainerRestartingState string = "restarting" ContainerPausedState string = "paused" ContainerExitedState string = "exited" ContainerDeadState string = "dead" )
Expose container states
const (
// DockerEntityPrefix is the entity prefix for docker containers
DockerEntityPrefix = "docker://"
)
const NanoToUserHZDivisor float64 = 1e9 / 100
NanoToUserHZDivisor holds the divisor to convert cpu.usage to the same unit as cpu.system (USER_HZ = 1/100) TODO: get USER_HZ from gopsutil? Needs to patch it
Variables ¶
var ( ErrAlreadySubscribed = errors.New("already subscribed") ErrNotSubscribed = errors.New("not subscribed") ErrEventTimeout = errors.New("timeout on event sending, re-subscribe") )
Errors client might receive
var ( // ErrNotImplemented is the "not implemented" error given by `gopsutil` when an // OS doesn't support and API. Unfortunately it's in an internal package so // we can't import it so we'll copy it here. ErrNotImplemented = errors.New("not implemented yet") // ErrDockerNotAvailable is returned if Docker is not running on the current machine. // We'll use this when configuring the DockerUtil so we don't error on non-docker machines. ErrDockerNotAvailable = errors.New("docker not available") // ErrDockerNotCompiled is returned if docker support is not compiled in. // User classes should handle that case as gracefully as possible. ErrDockerNotCompiled = errors.New("docker support not compiled in") )
var ( // ErrMissingTarget is an error set when a cgroup target is missing. ErrMissingTarget = errors.New("Missing cgroup target") )
var ( // NullContainer is an empty container object that has // default values for all fields including sub-fields. // If new sub-structs are added to Container this must // be updated. NullContainer = &Container{ CPU: &CgroupTimesStat{}, Memory: &CgroupMemStat{}, IO: &CgroupIOStat{}, Network: ContainerNetStats{}, } )
Functions ¶
func ConnectToDocker ¶
ConnectToDocker connects to a local docker socket. Returns ErrDockerNotAvailable if the socket or mounts file is missing otherwise it returns either a valid client or an error.
TODO: REMOVE USES AND MOVE TO PRIVATE
func ContainerIDForPID ¶
ContainerIDForPID is a lighter version of CgroupsForPids to only retrieve the container ID for origin detection. Returns container id as a string, empty if the PID is not in a container.
func ContainerIDToEntityName ¶
ContainerIDToEntityName returns a prefixed entity name from a container ID
func FindRancherIPInLabels ¶
FindRancherIPInLabels looks for the `io.rancher.container.ip` label and parses it. Rancher 1.x containers don't have docker networks as the orchestrator provides its own CNI.
func HostnameProvider ¶
HostnameProvider docker implementation for the hostname provider
func IsContainerized ¶
func IsContainerized() bool
IsContainerized returns True if we're running in the docker-dd-agent container.
func ScrapeAllCgroups ¶
func ScrapeAllCgroups() (map[string]*ContainerCgroup, error)
ScrapeAllCgroups returns ContainerCgroup for every container that's in a Cgroup. This version iterates on /{host/}proc to retrieve processes out of the namespace. We return as a map[containerID]Cgroup for easy look-up.
func SplitImageName ¶
SplitImageName splits a valid image name (from ResolveImageName) and returns:
- the "long image name" with registry and prefix, without tag
- the "short image name", without registry, prefix nor tag
- the image tag if present
- an error if parsing failed
Types ¶
type CgroupIOStat ¶
CgroupIOStat store I/O statistics about a cgroup.
type CgroupMemStat ¶
type CgroupMemStat struct {
ContainerID string
Cache uint64
Swap uint64 // See SwapPresent to make sure it's a real zero
SwapPresent bool
RSS uint64
RSSHuge uint64
MappedFile uint64
Pgpgin uint64
Pgpgout uint64
Pgfault uint64
Pgmajfault uint64
InactiveAnon uint64
ActiveAnon uint64
InactiveFile uint64
ActiveFile uint64
Unevictable uint64
HierarchicalMemoryLimit uint64
HierarchicalMemSWLimit uint64 // One can safely assume 0 == absent
TotalCache uint64
TotalRSS uint64
TotalRSSHuge uint64
TotalMappedFile uint64
TotalPgpgIn uint64
TotalPgpgOut uint64
TotalPgFault uint64
TotalPgMajFault uint64
TotalInactiveAnon uint64
TotalActiveAnon uint64
TotalInactiveFile uint64
TotalActiveFile uint64
TotalUnevictable uint64
MemUsageInBytes uint64
MemFailCnt uint64
}
CgroupMemStat stores memory statistics about a cgroup.
type CgroupTimesStat ¶
type CgroupTimesStat struct {
ContainerID string
System uint64
User uint64
UsageTotal float64
SystemUsage uint64
}
CgroupTimesStat stores CPU times for a cgroup. Unit is userspace scheduling unit (USER_HZ, usually 1/100)
type Container ¶
type Container struct {
Type string
ID string
EntityID string
Name string
Image string
ImageID string
Created int64
State string
Health string
Pids []int32
Excluded bool
CPULimit float64
MemLimit uint64
CPUNrThrottled uint64
CPU *CgroupTimesStat
Memory *CgroupMemStat
IO *CgroupIOStat
Network ContainerNetStats
StartedAt int64
// contains filtered or unexported fields
}
Container represents a single Docker container on a machine and includes Cgroup-level statistics about the container.
type ContainerCgroup ¶
type ContainerCgroup struct {
ContainerID string
Pids []int32
Paths map[string]string
Mounts map[string]string
}
ContainerCgroup is a structure that stores paths and mounts for a cgroup. It provides several methods for collecting stats about the cgroup using the paths and mounts metadata.
func (ContainerCgroup) CPU ¶
func (c ContainerCgroup) CPU() (*CgroupTimesStat, error)
CPU returns the CPU status for this cgroup instance If the cgroup file does not exist then we just log debug return nothing.
func (ContainerCgroup) CPULimit ¶
func (c ContainerCgroup) CPULimit() (float64, error)
CPULimit would show CPU limit for this cgroup. It does so by checking the cpu period and cpu quota config if a user does this:
docker run --cpus='0.5' ubuntu:latest
we should return 50% for that container If the limits files aren't available (on older version) then we'll return the default value of 100.
func (ContainerCgroup) CPUNrThrottled ¶
func (c ContainerCgroup) CPUNrThrottled() (uint64, error)
CPUNrThrottled returns the number of times the cgroup has been throttle/limited because of CPU quota / limit If the cgroup file does not exist then we just log debug and return 0.
func (ContainerCgroup) ContainerStartTime ¶
func (c ContainerCgroup) ContainerStartTime() (int64, error)
ContainerStartTime gets the stat for cgroup directory and use the mtime for that dir to determine the start time for the container this should work because the cgroup dir for the container would be created only when it's started
func (ContainerCgroup) IO ¶
func (c ContainerCgroup) IO() (*CgroupIOStat, error)
IO returns the disk read and write bytes stats for this cgroup. Format:
8:0 Read 49225728 8:0 Write 9850880 8:0 Sync 0 8:0 Async 59076608 8:0 Total 59076608 252:0 Read 49094656 252:0 Write 9850880 252:0 Sync 0 252:0 Async 58945536 252:0 Total 58945536
func (ContainerCgroup) Mem ¶
func (c ContainerCgroup) Mem() (*CgroupMemStat, error)
Mem returns the memory statistics for a Cgroup. If the cgroup file is not available then we return an empty stats file.
func (ContainerCgroup) MemLimit ¶
func (c ContainerCgroup) MemLimit() (uint64, error)
MemLimit returns the memory limit of the cgroup, if it exists. If the file does not exist or there is no limit then this will default to 0.
func (ContainerCgroup) ParseSingleStat ¶
func (c ContainerCgroup) ParseSingleStat(target, file string) (uint64, error)
ParseSingleStat reads and converts a single-value cgroup stat file content to uint64.
type ContainerEvent ¶
type ContainerEvent struct {
ContainerID string
ContainerName string
ImageName string
Action string
Timestamp time.Time
Attributes map[string]string
}
ContainerEvent describes an event from the docker daemon
func (*ContainerEvent) ContainerEntityName ¶
func (ev *ContainerEvent) ContainerEntityName() string
ContainerEntityName returns the event's container as a tagger entity name
type ContainerNetStats ¶
type ContainerNetStats []*InterfaceNetStats
ContainerNetStats stores network statistics about a Docker container per interface