Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Feat/processes #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Noderig metrics:
- Load
- Disk
- Net
- Processes
- External collectors

## Status
Expand Down Expand Up @@ -40,6 +41,7 @@ Flags:
--mem uint8 memory metrics level (default 1)
--net uint8 network metrics level (default 1)
--load uint8 load metrics level (default 1)
--process uint8 processes metrics level (default 0)
-c --collectors string external collectors directory (default "./collectors")
-k --keep-for uint keep collectors data for the given number of fetch (default 3)
```
Expand Down Expand Up @@ -119,6 +121,12 @@ Noderig have some built-in collectors.
<tr><td>os.net.dropped{direction=out,iface=eth0}</td><td>iface out drop count (drops)</td></tr>
</table>

### Process
<table>
<tr><td>0</td><td></td><td>disabled metrics</td></tr>
<tr><td rowspan="2">1</td><td>os.process.up{name=ntpd}</td><td>is process running (boolean)</td></tr>
</table>

## Configuration

Noderig can read a simple default [config file](config.yaml).
Expand All @@ -140,11 +148,12 @@ Noderig have some built-in collectors. They could be configured by a log level.
You can also defined custom collectors, in an scollector way. (see: http://bosun.org/scollector/external-collectors)

```yaml
cpu: 1 # CPU collector level (Optional, default: 1)
mem: 1 # Memory collector level (Optional, default: 1)
load: 1 # Load collector level (Optional, default: 1)
disk: 1 # Disk collector level (Optional, default: 1)
net: 1 # Network collector level (Optional, default: 1)
cpu: 1 # CPU collector level (Optional, default: 1)
mem: 1 # Memory collector level (Optional, default: 1)
load: 1 # Load collector level (Optional, default: 1)
disk: 1 # Disk collector level (Optional, default: 1)
net: 1 # Network collector level (Optional, default: 1)
process: 0 # Process collector level (Optional, default: 0)
```

#### Collectors Modules
Expand Down Expand Up @@ -175,6 +184,13 @@ disk-opts:
- sda3
```

```yaml
process-opts:
whitelist: # Give a filtering list of processes to watch
- httpd
- noderig
```

#### Parameters

Noderig can be customized through some parameters.
Expand Down
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"net/http"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -29,6 +29,7 @@ func init() {
RootCmd.Flags().Uint8("mem", 1, "memory metrics level")
RootCmd.Flags().Uint8("disk", 1, "disk metrics level")
RootCmd.Flags().Uint8("net", 1, "network metrics level")
RootCmd.Flags().Uint8("process", 0, "process metrics level")
RootCmd.Flags().Uint64("period", 1000, "default collection period")
RootCmd.Flags().StringP("collectors", "c", "./collectors", "external collectors directory")
RootCmd.Flags().Uint64P("keep-for", "k", 3, "keep collectors data for the given number of fetch")
Expand Down Expand Up @@ -101,6 +102,9 @@ var RootCmd = &cobra.Command{
disk := collectors.NewDisk(uint(viper.GetInt("period")), uint8(viper.GetInt("disk")), viper.Get("disk-opts"))
cs = append(cs, disk)

process := collectors.NewProcess(uint(viper.GetInt("period")), uint8(viper.GetInt("process")), viper.Sub("process-opts"))
cs = append(cs, process)

// Load external collectors
cpath := viper.GetString("collectors")
cdir, err := os.Open(cpath)
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
version = "2.3.1"
version = "2.3.2"
githash = "HEAD"
)

Expand Down
2 changes: 1 addition & 1 deletion collectors/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/cpu"
)

Expand Down
2 changes: 1 addition & 1 deletion collectors/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
)
Expand Down
2 changes: 1 addition & 1 deletion collectors/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/disk"
)

Expand Down
2 changes: 1 addition & 1 deletion collectors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/load"
)

Expand Down
2 changes: 1 addition & 1 deletion collectors/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/mem"
)

Expand Down
2 changes: 1 addition & 1 deletion collectors/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/net"
)

Expand Down
101 changes: 101 additions & 0 deletions collectors/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package collectors

import (
"bytes"
"fmt"
"strings"
"sync"
"time"

"github.com/mitchellh/go-ps"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

// Process collector
type Process struct {
whitelist []string
mutex sync.RWMutex
sensision bytes.Buffer
level uint8
}

// NewProcess collector
func NewProcess(period uint, level uint8, opts *viper.Viper) *Process {
p := &Process{
level: level,
}

if opts != nil {
p.whitelist = opts.GetStringSlice("whitelist")
}
log.Debugf("Process whitelist: %+v", p.whitelist)

if p.level == 0 || len(p.whitelist) == 0 {
return p
}

log.Debugf("Process whitelist: %+v", p.whitelist)

tick := time.Tick(time.Duration(period) * time.Millisecond)
go func() {
for range tick {
if err := p.scrape(); err != nil {
log.Error(err)
}
}
}()

return p
}

// Metrics delivers metrics.
func (p *Process) Metrics() *bytes.Buffer {
p.mutex.RLock()
defer p.mutex.RUnlock()

var res bytes.Buffer
res.Write(p.sensision.Bytes())
return &res
}

func (p *Process) scrape() error {
processes, err := ps.Processes()
if err != nil {
return err
}
now := time.Now()

res := map[string]bool{}

for _, processName := range p.whitelist {
res[processName] = false
for _, process := range processes {
if strings.Contains(process.Executable(), processName) {
res[processName] = true
continue
}
}
}

// protect consistency
p.mutex.Lock()
defer p.mutex.Unlock()

p.sensision.Reset()

for pcs, ok := range res {
if _, err := p.sensision.WriteString(processMetric(now, pcs, ok)); err != nil {
return err
}
}

return nil
}

func processMetric(t time.Time, processName string, up bool) string {
if up {
return fmt.Sprintf("%d// os.process.up{name=%s} true\n", t.UnixNano()/1000, processName)
}
return fmt.Sprintf("%d// os.process.up{name=%s} false\n", t.UnixNano()/1000, processName)
}
5 changes: 5 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ mem: 1
load: 1
disk: 1
net: 1
process: 0
disk-opts:
names:
- sda1
process-opts:
whitelist:
- noderig
- ntpd
#flushPath: /tmp/noderig
#listen: none
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.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package: github.com/ovh/noderig
import:
- package: github.com/Sirupsen/logrus
- package: github.com/sirupsen/logrus
version: ^0.11.0
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
Expand Down
2 changes: 1 addition & 1 deletion noderig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package main

import (
log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"

"github.com/ovh/noderig/cmd"
)
Expand Down