-
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.
Add collector for systemd service status.
- Loading branch information
Jason Harvey
committed
Oct 6, 2015
1 parent
2d0aead
commit 81e6264
Showing
7 changed files
with
123 additions
and
11 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
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,89 @@ | ||
package collectors | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"bosun.org/_third_party/github.com/coreos/go-systemd/dbus" | ||
"bosun.org/cmd/scollector/conf" | ||
"bosun.org/metadata" | ||
"bosun.org/opentsdb" | ||
"bosun.org/util" | ||
) | ||
|
||
var regexesServices = []*regexp.Regexp{} | ||
|
||
func init() { | ||
// The following two func defs have no-op stubs in collectors.go, as they are | ||
// called by main.go, but only functional on Linux environments. | ||
WatchSystemd = func() { | ||
collectors = append(collectors, &IntervalCollector{ | ||
F: func() (opentsdb.MultiDataPoint, error) { | ||
return c_systemd() | ||
}, | ||
name: "c_systemd", | ||
}) | ||
} | ||
|
||
AddSystemdServiceConfig = func(params conf.ServiceParams) error { | ||
if params.Name == "" { | ||
return fmt.Errorf("empty service Name") | ||
} | ||
reg, err := regexp.Compile(params.Name) | ||
if err != nil { | ||
return err | ||
} | ||
regexesServices = append(regexesServices, reg) | ||
return nil | ||
} | ||
} | ||
|
||
func c_systemd() (opentsdb.MultiDataPoint, error) { | ||
conn, err := dbus.New() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
units, err := conn.ListUnits() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var md opentsdb.MultiDataPoint | ||
for _, unit := range units { | ||
if strings.HasSuffix(unit.Name, ".service") { | ||
shortName := strings.TrimSuffix(unit.Name, ".service") | ||
if nameMatches(shortName, regexesServices) { | ||
systemdTags := opentsdb.TagSet{"name": unit.Name} | ||
osTags := opentsdb.TagSet{"name": shortName} | ||
Add(&md, "linux.systemd.unit.activestate", activeState[unit.ActiveState], systemdTags, metadata.Gauge, metadata.StatusCode, descActiveState) | ||
Add(&md, "os.service.running", util.Btoi(unit.ActiveState != "active"), osTags, metadata.Gauge, metadata.Ok, "") | ||
} | ||
} | ||
} | ||
|
||
return md, err | ||
} | ||
|
||
func nameMatches(name string, regexes []*regexp.Regexp) bool { | ||
for _, r := range regexes { | ||
if r.MatchString(name) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
var activeState = map[string]int{ | ||
"active": 0, | ||
"reloading": 1, | ||
"inactive": 2, | ||
"failed": 3, | ||
"activating": 4, | ||
"deactivating": 5, | ||
} | ||
|
||
const ( | ||
descActiveState = "0: active, 1: reloading, 2: inactive, 3: failed, 4: activating, 5: deactivating" | ||
) |
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
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package conf | ||
|
||
type ProcessParams struct{} | ||
|
||
type ServiceParams struct { | ||
Name string | ||
} |
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 |
---|---|---|
|
@@ -6,3 +6,7 @@ type ProcessParams struct { | |
Args string | ||
IncludeCount bool | ||
} | ||
|
||
type ServiceParams struct { | ||
Name string | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,7 @@ package conf | |
type ProcessParams struct { | ||
Name string | ||
} | ||
|
||
type ServiceParams struct { | ||
Name string | ||
} |
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