Skip to content

Commit

Permalink
Add collector for systemd service status.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Harvey committed Oct 6, 2015
1 parent 2d0aead commit 81e6264
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 11 deletions.
5 changes: 5 additions & 0 deletions cmd/scollector/collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ var (
}
WatchProcessesDotNet = func() {}

AddSystemdServiceConfig = func(params conf.ServiceParams) error {
return fmt.Errorf("systemd service watching not implemented on this platform")
}
WatchSystemd = func() {}

KeepalivedCommunity = ""
)

Expand Down
89 changes: 89 additions & 0 deletions cmd/scollector/collectors/systemd_linux.go
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"
)
23 changes: 12 additions & 11 deletions cmd/scollector/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ type Conf struct {
// the specified community.
KeepalivedCommunity string

HAProxy []HAProxy
SNMP []SNMP
MIBS map[string]MIB
ICMP []ICMP
Vsphere []Vsphere
AWS []AWS
Process []ProcessParams
ProcessDotNet []ProcessDotNet
HTTPUnit []HTTPUnit
Riak []Riak
Github []Github
HAProxy []HAProxy
SNMP []SNMP
MIBS map[string]MIB
ICMP []ICMP
Vsphere []Vsphere
AWS []AWS
Process []ProcessParams
SystemdService []ServiceParams
ProcessDotNet []ProcessDotNet
HTTPUnit []HTTPUnit
Riak []Riak
Github []Github
// ElasticIndexFilters takes regular expressions and excludes indicies that
// match those filters from being monitored for metrics in the elastic.indices
// namespace
Expand Down
4 changes: 4 additions & 0 deletions cmd/scollector/conf/conf_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package conf

type ProcessParams struct{}

type ServiceParams struct {
Name string
}
4 changes: 4 additions & 0 deletions cmd/scollector/conf/conf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ type ProcessParams struct {
Args string
IncludeCount bool
}

type ServiceParams struct {
Name string
}
4 changes: 4 additions & 0 deletions cmd/scollector/conf/conf_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package conf
type ProcessParams struct {
Name string
}

type ServiceParams struct {
Name string
}
5 changes: 5 additions & 0 deletions cmd/scollector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func main() {
for _, p := range conf.Process {
check(collectors.AddProcessConfig(p))
}
for _, p := range conf.SystemdService {
check(collectors.AddSystemdServiceConfig(p))
}
for _, p := range conf.ProcessDotNet {
check(collectors.AddProcessDotNetConfig(p))
}
Expand All @@ -142,6 +145,8 @@ func main() {
collectors.WatchProcesses()
collectors.WatchProcessesDotNet()

collectors.WatchSystemd()

if *flagFake > 0 {
collectors.InitFake(*flagFake)
}
Expand Down

0 comments on commit 81e6264

Please sign in to comment.