Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Support mac/darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
conorbranagan committed Aug 2, 2017
1 parent fa616dd commit 327856c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
5 changes: 4 additions & 1 deletion checks/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/DataDog/datadog-process-agent/config"
"github.com/DataDog/datadog-process-agent/model"
"github.com/DataDog/datadog-process-agent/util"
)

// ConnectionsCheck collects statistics about live TCP and UDP connections.
Expand All @@ -29,7 +30,9 @@ func (c *ConnectionsCheck) Name() string { return "connections" }
func (c *ConnectionsCheck) Run(cfg *config.AgentConfig, groupID int32) ([]model.MessageBody, error) {
start := time.Now()
connections, err := net.ConnectionsMax("tcp", cfg.MaxProcFDs)
if err != nil {
if err != nil && err.Error() == util.ErrNotImplemented.Error() {
return nil, nil
} else if err != nil {
return nil, err
}

Expand Down
11 changes: 6 additions & 5 deletions checks/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func (p *ProcessCheck) Run(cfg *config.AgentConfig, groupID int32) ([]model.Mess
for _, fp := range fps {
pids = append(pids, fp.Pid)
}
containerByPID, err := docker.ContainersForPIDs(pids)
if err != nil {
log.Warnf("unable to get docker stats: %s", err)
}
containerByPID := docker.ContainersForPIDs(pids)
kubeMeta := kubernetes.GetMetadata()

// Pre-filter the list to get an accurate group size.
Expand Down Expand Up @@ -163,12 +160,16 @@ func formatCommand(fp *process.FilledProcess) *model.Command {
Root: "", // TODO
OnDisk: false, // TODO
Ppid: fp.Ppid,
Pgroup: fp.Pgrp,
Exe: fp.Exe,
}
}

func formatIO(fp *process.FilledProcess, lastIO *process.IOCountersStat, before time.Time) *model.IOStat {
// This will be nill for Mac
if fp.IOStat == nil {
return &model.IOStat{}
}

diff := time.Now().Unix() - before.Unix()
if before.IsZero() || diff <= 0 {
return nil
Expand Down
7 changes: 1 addition & 6 deletions checks/real_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/DataDog/gopsutil/cpu"
"github.com/DataDog/gopsutil/process"

log "github.com/cihub/seelog"

"github.com/DataDog/datadog-process-agent/config"
"github.com/DataDog/datadog-process-agent/model"
"github.com/DataDog/datadog-process-agent/util/docker"
Expand Down Expand Up @@ -53,10 +51,7 @@ func (r *RealTimeCheck) Run(cfg *config.AgentConfig, groupID int32) ([]model.Mes
for _, fp := range fps {
pids = append(pids, fp.Pid)
}
containerByPID, err := docker.ContainersForPIDs(pids)
if err != nil {
log.Warnf("unable to get docker stats: %s", err)
}
containerByPID := docker.ContainersForPIDs(pids)

// Pre-filter the list to get an accurate grou psize.
filteredFps := make([]*process.FilledProcess, 0, len(fps))
Expand Down
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 18 additions & 5 deletions util/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import (

var (
ErrDockerNotAvailable = errors.New("docker not available")
globalDockerUtil *dockerUtil
invalidationInterval = 5 * time.Minute

globalDockerUtil *dockerUtil
invalidationInterval = 5 * time.Minute
lastErr string

// NullContainer is an empty container object that has
// default values for all fields including sub-fields.
Expand Down Expand Up @@ -77,11 +79,17 @@ type dockerUtil struct {
//
// Expose module-level functions that will interact with a Singleton dockerUtil.

func ContainersForPIDs(pids []int32) (map[int32]*Container, error) {
func ContainersForPIDs(pids []int32) map[int32]*Container {
if globalDockerUtil != nil {
return globalDockerUtil.containersForPIDs(pids)
r, err := globalDockerUtil.containersForPIDs(pids)
if err != nil && err.Error() != lastErr {
log.Warnf("unable to collect docker stats: %s", err)
lastErr = err.Error()
} else {
return r
}
}
return map[int32]*Container{}, nil
return map[int32]*Container{}
}

func GetHostname() (string, error) {
Expand All @@ -99,6 +107,11 @@ func InitDockerUtil() error {
if !util.PathExists(sockPath) {
return ErrDockerNotAvailable
}
// The /proc/mounts file won't be availble on non-Linux systems.
mountsFile := "/proc/mounts"
if !util.PathExists(mountsFile) {
return ErrDockerNotAvailable
}

serverVersion, err := detectServerAPIVersion()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package util

import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
)

// 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.
var ErrNotImplemented = errors.New("not implemented yet")

// ReadLines reads contents from a file and splits them by new lines.
func ReadLines(filename string) ([]string, error) {
f, err := os.Open(filename)
Expand Down

0 comments on commit 327856c

Please sign in to comment.