-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tobias Schwab
committed
Aug 25, 2013
1 parent
d6ab57d
commit d4fdeda
Showing
6 changed files
with
3,124 additions
and
3 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
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,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
const ( | ||
FILES = "files" | ||
FILES_OPEN = "Open" | ||
) | ||
|
||
var LSOF_CMD = `lsof | tail -n +2 | tr -s " " "` + "\t" + `" | cut -f 1,2` | ||
|
||
func init() { | ||
parser.Add(FILES, "true", "Collect open files") | ||
} | ||
|
||
type Files struct { | ||
RawStatus []byte | ||
} | ||
|
||
func (files *Files) fetch() (b []byte, e error) { | ||
if len(files.RawStatus) == 0 { | ||
args := []string { "-c" } | ||
args = append(args, LSOF_CMD) | ||
files.RawStatus, _ = exec.Command("bash", args...).Output() | ||
if len(files.RawStatus) == 0 { | ||
return b, fmt.Errorf("lsof returned empty result") | ||
} | ||
} | ||
return files.RawStatus, nil | ||
} | ||
|
||
func (f *Files) Collect(col *MetricsCollection) error { | ||
b, e := f.fetch() | ||
if e != nil { | ||
return e | ||
} | ||
stats := map[string]int64{} | ||
for _, line := range strings.Split(string(b), "\n") { | ||
stats[line]++ | ||
} | ||
for k, v := range stats { | ||
chunks := strings.SplitN(k, "\t", 2) | ||
if len(chunks) == 2 { | ||
name, pid := chunks[0], chunks[1] | ||
tags := map[string]string{"name": NormalizeProcessName(name), "pid": pid} | ||
col.AddWithTags(FILES_OPEN, v, tags) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (f *Files) Keys() []string { | ||
return []string{FILES_OPEN} | ||
} | ||
|
||
func (f *Files) Prefix() string { | ||
return FILES | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestFiles(t *testing.T) { | ||
files := &Files{RawStatus: readFile("fixtures/lsof.txt")} | ||
assert.Equal(t, files.Prefix(), "files") | ||
assert.NotEmpty(t, files.Keys()) | ||
|
||
mh := new(MetricHandler) | ||
stats, _ := mh.Collect(files) | ||
assert.True(t, len(stats) > 4) | ||
assert.Equal(t, len(stats), 74) | ||
|
||
names := map[string]int{} | ||
for _, s := range stats { | ||
names[s.Tags["name"]]++ | ||
} | ||
assert.Equal(t, names["kworker/0"], 0) | ||
assert.Equal(t, names["kworker"], 8) | ||
} | ||
|
||
func TestParseLsofOutput(t *testing.T) { | ||
return | ||
} |
Oops, something went wrong.