-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1374 from alienth/systemd
Add collector for systemd service status.
- Loading branch information
Showing
70 changed files
with
8,762 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Integration with the systemd D-Bus API. See http://www.freedesktop.org/wiki/Software/systemd/dbus/ | ||
package dbus | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
|
||
"bosun.org/_third_party/github.com/godbus/dbus" | ||
) | ||
|
||
const ( | ||
alpha = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` | ||
num = `0123456789` | ||
alphanum = alpha + num | ||
signalBuffer = 100 | ||
) | ||
|
||
// needsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped | ||
func needsEscape(i int, b byte) bool { | ||
// Escape everything that is not a-z-A-Z-0-9 | ||
// Also escape 0-9 if it's the first character | ||
return strings.IndexByte(alphanum, b) == -1 || | ||
(i == 0 && strings.IndexByte(num, b) != -1) | ||
} | ||
|
||
// PathBusEscape sanitizes a constituent string of a dbus ObjectPath using the | ||
// rules that systemd uses for serializing special characters. | ||
func PathBusEscape(path string) string { | ||
// Special case the empty string | ||
if len(path) == 0 { | ||
return "_" | ||
} | ||
n := []byte{} | ||
for i := 0; i < len(path); i++ { | ||
c := path[i] | ||
if needsEscape(i, c) { | ||
e := fmt.Sprintf("_%x", c) | ||
n = append(n, []byte(e)...) | ||
} else { | ||
n = append(n, c) | ||
} | ||
} | ||
return string(n) | ||
} | ||
|
||
// Conn is a connection to systemd's dbus endpoint. | ||
type Conn struct { | ||
// sysconn/sysobj are only used to call dbus methods | ||
sysconn *dbus.Conn | ||
sysobj dbus.BusObject | ||
|
||
// sigconn/sigobj are only used to receive dbus signals | ||
sigconn *dbus.Conn | ||
sigobj dbus.BusObject | ||
|
||
jobListener struct { | ||
jobs map[dbus.ObjectPath]chan<- string | ||
sync.Mutex | ||
} | ||
subscriber struct { | ||
updateCh chan<- *SubStateUpdate | ||
errCh chan<- error | ||
sync.Mutex | ||
ignore map[dbus.ObjectPath]int64 | ||
cleanIgnore int64 | ||
} | ||
} | ||
|
||
// New establishes a connection to the system bus and authenticates. | ||
// Callers should call Close() when done with the connection. | ||
func New() (*Conn, error) { | ||
return newConnection(func() (*dbus.Conn, error) { | ||
return dbusAuthHelloConnection(dbus.SystemBusPrivate) | ||
}) | ||
} | ||
|
||
// NewUserConnection establishes a connection to the session bus and | ||
// authenticates. This can be used to connect to systemd user instances. | ||
// Callers should call Close() when done with the connection. | ||
func NewUserConnection() (*Conn, error) { | ||
return newConnection(func() (*dbus.Conn, error) { | ||
return dbusAuthHelloConnection(dbus.SessionBusPrivate) | ||
}) | ||
} | ||
|
||
// 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. | ||
func NewSystemdConnection() (*Conn, error) { | ||
return newConnection(func() (*dbus.Conn, error) { | ||
// We skip Hello when talking directly to systemd. | ||
return dbusAuthConnection(func() (*dbus.Conn, error) { | ||
return dbus.Dial("unix:path=/run/systemd/private") | ||
}) | ||
}) | ||
} | ||
|
||
// Close closes an established connection | ||
func (c *Conn) Close() { | ||
c.sysconn.Close() | ||
c.sigconn.Close() | ||
} | ||
|
||
func newConnection(createBus func() (*dbus.Conn, error)) (*Conn, error) { | ||
sysconn, err := createBus() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
sigconn, err := createBus() | ||
if err != nil { | ||
sysconn.Close() | ||
return nil, err | ||
} | ||
|
||
c := &Conn{ | ||
sysconn: sysconn, | ||
sysobj: systemdObject(sysconn), | ||
sigconn: sigconn, | ||
sigobj: systemdObject(sigconn), | ||
} | ||
|
||
c.subscriber.ignore = make(map[dbus.ObjectPath]int64) | ||
c.jobListener.jobs = make(map[dbus.ObjectPath]chan<- string) | ||
|
||
// Setup the listeners on jobs so that we can get completions | ||
c.sigconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, | ||
"type='signal', interface='org.freedesktop.systemd1.Manager', member='JobRemoved'") | ||
|
||
c.dispatch() | ||
return c, nil | ||
} | ||
|
||
func dbusAuthConnection(createBus func() (*dbus.Conn, error)) (*dbus.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 := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))} | ||
|
||
err = conn.Auth(methods) | ||
if err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func dbusAuthHelloConnection(createBus func() (*dbus.Conn, error)) (*dbus.Conn, error) { | ||
conn, err := dbusAuthConnection(createBus) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = conn.Hello(); err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func systemdObject(conn *dbus.Conn) dbus.BusObject { | ||
return conn.Object("org.freedesktop.systemd1", dbus.ObjectPath("/org/freedesktop/systemd1")) | ||
} |
77 changes: 77 additions & 0 deletions
77
_third_party/github.com/coreos/go-systemd/dbus/dbus_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2015 CoreOS, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dbus | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNeedsEscape(t *testing.T) { | ||
// Anything not 0-9a-zA-Z should always be escaped | ||
for want, vals := range map[bool][]byte{ | ||
false: []byte{'a', 'b', 'z', 'A', 'Q', '1', '4', '9'}, | ||
true: []byte{'#', '%', '$', '!', '.', '_', '-', '%', '\\'}, | ||
} { | ||
for i := 1; i < 10; i++ { | ||
for _, b := range vals { | ||
got := needsEscape(i, b) | ||
if got != want { | ||
t.Errorf("needsEscape(%d, %c) returned %t, want %t", i, b, got, want) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 0-9 in position 0 should be escaped | ||
for want, vals := range map[bool][]byte{ | ||
false: []byte{'A', 'a', 'e', 'x', 'Q', 'Z'}, | ||
true: []byte{'0', '4', '5', '9'}, | ||
} { | ||
for _, b := range vals { | ||
got := needsEscape(0, b) | ||
if got != want { | ||
t.Errorf("needsEscape(0, %c) returned %t, want %t", b, got, want) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
func TestPathBusEscape(t *testing.T) { | ||
for in, want := range map[string]string{ | ||
"": "_", | ||
"foo.service": "foo_2eservice", | ||
"foobar": "foobar", | ||
"woof@woof.service": "woof_40woof_2eservice", | ||
"0123456": "_30123456", | ||
"account_db.service": "account_5fdb_2eservice", | ||
"got-dashes": "got_2ddashes", | ||
} { | ||
got := PathBusEscape(in) | ||
if got != want { | ||
t.Errorf("bad result for PathBusEscape(%s): got %q, want %q", in, got, want) | ||
} | ||
} | ||
|
||
} | ||
|
||
// TestNew ensures that New() works without errors. | ||
func TestNew(t *testing.T) { | ||
_, err := New() | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.