Skip to content

Commit

Permalink
Improve systemd configuration and metric names (#3974)
Browse files Browse the repository at this point in the history
upgrade:
- Update to the configuration of the systemd check: ``unit_names`` is now required and only matching units will be monitored, ``unit_regexes`` configuration has been removed.
- Several metrics sent by the systemd check have been renamed. The integration is now stable.

enhancements:
- Add `private_socket` configuration to the systemd check. Defaults to `/run/systemd/private` (or `/host/run/systemd/private` when using Docker Agent).
  • Loading branch information
AlexandreYang authored Aug 23, 2019
1 parent 3ab2c2b commit dad4b81
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 199 deletions.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Datadog datadog-agent
Copyright 2016-2019 Datadog, Inc.

This product includes software developed at Datadog (https://www.datadoghq.com/).
This product includes software developed at CoreOS, Inc (http://www.coreos.com/).
30 changes: 13 additions & 17 deletions cmd/agent/dist/conf.d/systemd.d/conf.yaml.example
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
init_config:

instances:
-
## @param unit_names - list of strings - optional
## List of systemd units to monitor (full name e.g. ssh.service).
## If `unit_names` and `unit_regexes` are both NOT specified, the check monitors all units.
## @param unit_names - list of strings - required
## List of systemd units to monitor.
## Full names must be used. Examples: ssh.service, docker.socket
#
# unit_names:
# - <UNIT_NAME>
- unit_names:
- <UNIT_NAME>

## @param unit_regexes - list of strings - optional
## Regex pattern[s] matching the names of units to monitor.
## If `unit_names` and `unit_regexes` are both NOT specified, the check monitors all units.
##
## If the number of units managed by systemd is unbounded, it might increase
## the number of custom metrics send by this check which may impact your billing.
## Learn more about custom metrics: https://docs.datadoghq.com/developers/metrics/custom_metrics/#how-is-a-custom-metric-defined
#
# unit_regexes:
# - ^<UNIT_NAME>$ # match exactly the unit name "<UNIT_NAME>"
## @param private_socket - string - optional
## Path to systemd private socket needed to retrieve systemd data.
## Defaults to `/run/systemd/private` or `/host/run/systemd/private` when
## using Docker Agent (https://docs.datadoghq.com/agent/docker/).
##
# private_socket: <PATH_TO_SYSTEMD_PRIVATE_SOCKET>

## @param tags - list of key:value elements - optional
## List of tags to attach to every metric, event, and service check emitted by this integration.
## List of tags to attach to every metric, event, and service check emitted
## by this integration.
##
## Learn more about tagging at https://docs.datadoghq.com/tagging
#
Expand Down
59 changes: 59 additions & 0 deletions pkg/collector/corechecks/systemd/dbus_conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// This file includes software developed at CoreOS, Inc (http://www.coreos.com/).

// Copyright 2016-2019 Datadog, Inc.
// Copyright 2015 CoreOS, Inc.
//
// Use of this source code is governed by Apache License 2.0
// license that can be found here: https://github.com/coreos/go-systemd/blob/master/LICENSE

// +build systemd

package systemd

import (
"fmt"
"os"
"strconv"

"github.com/coreos/go-systemd/dbus"
godbus "github.com/godbus/dbus"
)

// NewSystemdConnection establishes a private, direct connection to systemd.
// This can be used for communicating with systemd without a dbus daemon.
// Callers should call Close() when done with the connection.
// Note: method borrowed from `go-systemd/dbus` to provide custom path for systemd private socket
// Source: https://github.com/coreos/go-systemd/blob/master/dbus/dbus.go
func NewSystemdConnection(privateSocket string) (*dbus.Conn, error) {
return dbus.NewConnection(func() (*godbus.Conn, error) {
// We skip Hello when talking directly to systemd.
return dbusAuthConnection(func() (*godbus.Conn, error) {
return godbus.Dial(fmt.Sprintf("unix:path=%s", privateSocket))
})
})
}

// Note: method borrowed from `go-systemd/dbus` to provide custom path for systemd private socket
// Source: https://github.com/coreos/go-systemd/blob/master/dbus/dbus.go
func dbusAuthConnection(createBus func() (*godbus.Conn, error)) (*godbus.Conn, error) {
conn, err := createBus()
if err != nil {
return nil, err
}

// Only use EXTERNAL method, and hardcode the uid (not username)
// to avoid a username lookup (which requires a dynamically linked
// libc)
methods := []godbus.Auth{godbus.AuthExternal(strconv.Itoa(os.Getuid()))}

err = conn.Auth(methods)
if err != nil {
conn.Close()
return nil, err
}

return conn, nil
}
Loading

0 comments on commit dad4b81

Please sign in to comment.