Skip to content

Commit

Permalink
Merge pull request #1 from monicasarbu/sigar
Browse files Browse the repository at this point in the history
Use gosigar library to fetch system wide and per process stats
  • Loading branch information
monicasarbu committed Aug 6, 2015
2 parents c2d4437 + 32f6f45 commit 8b2dc84
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 19 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type TopConfig struct {
Period *int64
Procs *[]string
}

type ConfigSettings struct {
Expand Down
61 changes: 61 additions & 0 deletions etc/topbeat.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
################### Topbeat Configuration Example #########################

############################# Input ############################################
input:
period: 1

procs: [".*"]


############################# Shipper ############################################
shipper:

# The name of the shipper that publishes the network data. It can be used to group
# all the transactions sent by a single shipper in the web interface.
# If this options is not defined, the hostname is used.
name:

# The tags of the shipper are included in their own field with each
# transaction published. Tags make it easy to group transactions by different
# logical properties.
#tags: ["service1"]

############################# Output ############################################

# Configure what outputs to use when sending the data collected by topbeat.
# You can enable one or multiple outputs by setting enabled option to true.
output:

# Elasticsearch as output
# Options:
# host, port: where Elasticsearch is listening on
# save_topology: specify if the topology is saved in Elasticsearch
elasticsearch:
enabled: true
hosts: ["localhost:9200"]
save_topology: false
index: "topbeat"

# Redis as output
# Options:
# host, port: where Redis is listening on
# save_topology: specify if the topology is saved in Redis
#redis:
# enabled: true
# host: localhost
# port: 6379
# save_topology: true

# File as output
# Options:
# path: where to save the files
# filename: name of the files
# rotate_every_kb: maximum size of the files in path
# number of files: maximum number of files in path
#file:
# enabled: true
# path: "/tmp/topbeat"
# filename: topbeat
# rotate_every_kb: 1000
# number_of_files: 7

75 changes: 56 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"regexp"
"runtime"
"time"

Expand All @@ -12,10 +13,6 @@ import (
"github.com/elastic/libbeat/logp"
"github.com/elastic/libbeat/publisher"
"github.com/elastic/libbeat/service"
"github.com/monicasarbu/gotop/cpu"
"github.com/monicasarbu/gotop/load"
"github.com/monicasarbu/gotop/mem"
"github.com/monicasarbu/gotop/proc"
)

// You can overwrite these, e.g.: go build -ldflags "-X main.Version 1.0.0-beta3"
Expand All @@ -25,6 +22,7 @@ var Name = "topbeat"
type Topbeat struct {
isAlive bool
period time.Duration
procs []string

events chan common.MapStr
}
Expand All @@ -36,56 +34,95 @@ func (t *Topbeat) Init(config TopConfig, events chan common.MapStr) error {
} else {
t.period = 1 * time.Second
}
if config.Procs != nil {
t.procs = *config.Procs
} else {
t.procs = []string{".*"} //all processes
}

logp.Debug("topbeat", "Init toppbeat")
logp.Debug("topbeat", "Follow processes %q\n", t.procs)
logp.Debug("topbeat", "Period %v\n", t.period)
t.events = events
return nil
}

func (t *Topbeat) MatchProcess(name string) bool {

for _, reg := range t.procs {
matched, _ := regexp.MatchString(reg, name)
if matched {
return true
}
}
return false
}

func (t *Topbeat) Run() error {

_, _ = cpu.Cpu_times_percent(0)
t.isAlive = true

for t.isAlive {
time.Sleep(1 * time.Second)

load_stat, err := load.Load()
load_stat, err := GetSystemLoad()
if err != nil {
logp.Err("Error reading load statistics: %v", err)
logp.Warn("Getting load statistics: %v", err)
continue
}

cpu_stat, err := cpu.Cpu_times_percent(0)
cpu_stat, err := GetCpuTimes()
if err != nil {
logp.Err("Error reading cpu times: %v", err)
logp.Warn("Getting cpu times: %v", err)
continue
}

mem_stat, err := mem.Virtual_memory()
mem_stat, err := GetMemory()
if err != nil {
logp.Err("Error reading memory statistics: %v", err)
logp.Warn("Getting memory details: %v", err)
continue
}
swap_stat, err := GetSwap()
if err != nil {
logp.Warn("Getting swap details: %v", err)
}

pids := proc.Pids()
procs := []proc.Process{}
pids, err := Pids()
if err != nil {
logp.Warn("Getting the list of pids: %v", err)
}

for _, pid := range pids {
process, err := proc.GetProcess(pid)
process, err := GetProcess(pid)
if err != nil {
logp.Err("Error geting the process %d: %v", pid, err)
logp.Debug("topbeat", "Skip process %d: %v", pid, err)
continue
}
procs = append(procs, *process)

if t.MatchProcess(process.Name) {

logp.Debug("topbeat", "Process: %s", process)

event := common.MapStr{
"timestamp": common.Time(time.Now()),
"type": "proc",
"proc.pid": process.Pid,
"proc.ppid": process.Ppid,
"proc.name": process.Name,
"proc.state": process.State,
"proc.mem": process.Mem,
"proc.cpu": process.Cpu,
}
t.events <- event
}
}

event := common.MapStr{
"timestamp": common.Time(time.Now()),
"type": "top",
"type": "system",
"load": load_stat,
"cpu": cpu_stat,
"mem": mem_stat,
"procs": procs,
"swap": swap_stat,
}

t.events <- event
Expand Down
Loading

0 comments on commit 8b2dc84

Please sign in to comment.