Replace godep with dep
This commit is contained in:
parent
1e7489927c
commit
bf5616c65b
14883 changed files with 3937406 additions and 361781 deletions
13
vendor/github.com/ncabatoff/process-exporter/config/base_test.go
generated
vendored
Normal file
13
vendor/github.com/ncabatoff/process-exporter/config/base_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
. "gopkg.in/check.v1"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Hook up gocheck into the "go test" runner.
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type MySuite struct{}
|
||||
|
||||
var _ = Suite(&MySuite{})
|
||||
268
vendor/github.com/ncabatoff/process-exporter/config/config.go
generated
vendored
Normal file
268
vendor/github.com/ncabatoff/process-exporter/config/config.go
generated
vendored
Normal file
|
|
@ -0,0 +1,268 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
common "github.com/ncabatoff/process-exporter"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type (
|
||||
Matcher interface {
|
||||
// Match returns empty string for no match, or the group name on success.
|
||||
Match(common.NameAndCmdline) bool
|
||||
}
|
||||
|
||||
FirstMatcher []common.MatchNamer
|
||||
Config struct {
|
||||
MatchNamers FirstMatcher
|
||||
}
|
||||
|
||||
commMatcher struct {
|
||||
comms map[string]struct{}
|
||||
}
|
||||
|
||||
exeMatcher struct {
|
||||
exes map[string]string
|
||||
}
|
||||
|
||||
cmdlineMatcher struct {
|
||||
regexes []*regexp.Regexp
|
||||
captures map[string]string
|
||||
}
|
||||
|
||||
andMatcher []Matcher
|
||||
|
||||
templateNamer struct {
|
||||
template *template.Template
|
||||
}
|
||||
|
||||
matchNamer struct {
|
||||
andMatcher
|
||||
templateNamer
|
||||
}
|
||||
|
||||
templateParams struct {
|
||||
Comm string
|
||||
ExeBase string
|
||||
ExeFull string
|
||||
Matches map[string]string
|
||||
}
|
||||
)
|
||||
|
||||
func (f FirstMatcher) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
|
||||
for _, m := range f {
|
||||
if matched, name := m.MatchAndName(nacl); matched {
|
||||
return true, name
|
||||
}
|
||||
}
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (m *matchNamer) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
|
||||
if !m.Match(nacl) {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
matches := make(map[string]string)
|
||||
for _, m := range m.andMatcher {
|
||||
if mc, ok := m.(*cmdlineMatcher); ok {
|
||||
for k, v := range mc.captures {
|
||||
matches[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exebase, exefull := nacl.Name, nacl.Name
|
||||
if len(nacl.Cmdline) > 0 {
|
||||
exefull = nacl.Cmdline[0]
|
||||
exebase = filepath.Base(exefull)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
m.template.Execute(&buf, &templateParams{
|
||||
Comm: nacl.Name,
|
||||
ExeBase: exebase,
|
||||
ExeFull: exefull,
|
||||
Matches: matches,
|
||||
})
|
||||
return true, buf.String()
|
||||
}
|
||||
|
||||
func (m *commMatcher) Match(nacl common.NameAndCmdline) bool {
|
||||
_, found := m.comms[nacl.Name]
|
||||
return found
|
||||
}
|
||||
|
||||
func (m *exeMatcher) Match(nacl common.NameAndCmdline) bool {
|
||||
if len(nacl.Cmdline) == 0 {
|
||||
return false
|
||||
}
|
||||
thisbase := filepath.Base(nacl.Cmdline[0])
|
||||
fqpath, found := m.exes[thisbase]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if fqpath == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return fqpath == nacl.Cmdline[0]
|
||||
}
|
||||
|
||||
func (m *cmdlineMatcher) Match(nacl common.NameAndCmdline) bool {
|
||||
for _, regex := range m.regexes {
|
||||
captures := regex.FindStringSubmatch(strings.Join(nacl.Cmdline, " "))
|
||||
if m.captures == nil {
|
||||
return false
|
||||
}
|
||||
subexpNames := regex.SubexpNames()
|
||||
if len(subexpNames) != len(captures) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, name := range subexpNames {
|
||||
m.captures[name] = captures[i]
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m andMatcher) Match(nacl common.NameAndCmdline) bool {
|
||||
for _, matcher := range m {
|
||||
if !matcher.Match(nacl) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ReadRecipesFile opens the named file and extracts recipes from it.
|
||||
func ReadFile(cfgpath string) (*Config, error) {
|
||||
content, err := ioutil.ReadFile(cfgpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetConfig(string(content))
|
||||
}
|
||||
|
||||
// GetConfig extracts Config from content by parsing it as YAML.
|
||||
func GetConfig(content string) (*Config, error) {
|
||||
var yamldata map[string]interface{}
|
||||
|
||||
err := yaml.Unmarshal([]byte(content), &yamldata)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
yamlProcnames, ok := yamldata["process_names"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("error parsing YAML config: no top-level 'process_names' key")
|
||||
}
|
||||
procnames, ok := yamlProcnames.([]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("error parsing YAML config: 'process_names' is not a list")
|
||||
}
|
||||
|
||||
var cfg Config
|
||||
for i, procname := range procnames {
|
||||
mn, err := getMatchNamer(procname)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse process_name entry %d: %v", i, err)
|
||||
}
|
||||
cfg.MatchNamers = append(cfg.MatchNamers, mn)
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func getMatchNamer(yamlmn interface{}) (common.MatchNamer, error) {
|
||||
nm, ok := yamlmn.(map[interface{}]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("not a map")
|
||||
}
|
||||
|
||||
var smap = make(map[string][]string)
|
||||
var nametmpl string
|
||||
for k, v := range nm {
|
||||
key, ok := k.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("non-string key %v", k)
|
||||
}
|
||||
|
||||
if key == "name" {
|
||||
value, ok := v.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("non-string value %v for key %q", v, key)
|
||||
}
|
||||
nametmpl = value
|
||||
} else {
|
||||
vals, ok := v.([]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("non-string array value %v for key %q", v, key)
|
||||
}
|
||||
var strs []string
|
||||
for i, si := range vals {
|
||||
s, ok := si.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("non-string value %v in list[%d] for key %q", v, i, key)
|
||||
}
|
||||
strs = append(strs, s)
|
||||
}
|
||||
smap[key] = strs
|
||||
}
|
||||
}
|
||||
|
||||
var matchers andMatcher
|
||||
if comm, ok := smap["comm"]; ok {
|
||||
comms := make(map[string]struct{})
|
||||
for _, c := range comm {
|
||||
comms[c] = struct{}{}
|
||||
}
|
||||
matchers = append(matchers, &commMatcher{comms})
|
||||
}
|
||||
if exe, ok := smap["exe"]; ok {
|
||||
exes := make(map[string]string)
|
||||
for _, e := range exe {
|
||||
if strings.Contains(e, "/") {
|
||||
exes[filepath.Base(e)] = e
|
||||
} else {
|
||||
exes[e] = ""
|
||||
}
|
||||
}
|
||||
matchers = append(matchers, &exeMatcher{exes})
|
||||
}
|
||||
if cmdline, ok := smap["cmdline"]; ok {
|
||||
var rs []*regexp.Regexp
|
||||
for _, c := range cmdline {
|
||||
r, err := regexp.Compile(c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad cmdline regex %q: %v", c, err)
|
||||
}
|
||||
rs = append(rs, r)
|
||||
}
|
||||
matchers = append(matchers, &cmdlineMatcher{
|
||||
regexes: rs,
|
||||
captures: make(map[string]string),
|
||||
})
|
||||
}
|
||||
if len(matchers) == 0 {
|
||||
return nil, fmt.Errorf("no matchers provided")
|
||||
}
|
||||
|
||||
if nametmpl == "" {
|
||||
nametmpl = "{{.ExeBase}}"
|
||||
}
|
||||
tmpl := template.New("cmdname")
|
||||
tmpl, err := tmpl.Parse(nametmpl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad name template %q: %v", nametmpl, err)
|
||||
}
|
||||
|
||||
return &matchNamer{matchers, templateNamer{tmpl}}, nil
|
||||
}
|
||||
77
vendor/github.com/ncabatoff/process-exporter/config/config_test.go
generated
vendored
Normal file
77
vendor/github.com/ncabatoff/process-exporter/config/config_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
// "github.com/kylelemons/godebug/pretty"
|
||||
common "github.com/ncabatoff/process-exporter"
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func (s MySuite) TestConfigBasic(c *C) {
|
||||
yml := `
|
||||
process_names:
|
||||
- exe:
|
||||
- bash
|
||||
- exe:
|
||||
- sh
|
||||
- exe:
|
||||
- /bin/ksh
|
||||
`
|
||||
cfg, err := GetConfig(yml)
|
||||
c.Assert(err, IsNil)
|
||||
c.Check(cfg.MatchNamers, HasLen, 3)
|
||||
|
||||
bash := common.NameAndCmdline{"bash", []string{"/bin/bash"}}
|
||||
sh := common.NameAndCmdline{"sh", []string{"sh"}}
|
||||
ksh := common.NameAndCmdline{"ksh", []string{"/bin/ksh"}}
|
||||
|
||||
found, name := cfg.MatchNamers[0].MatchAndName(bash)
|
||||
c.Check(found, Equals, true)
|
||||
c.Check(name, Equals, "bash")
|
||||
found, name = cfg.MatchNamers[0].MatchAndName(sh)
|
||||
c.Check(found, Equals, false)
|
||||
found, name = cfg.MatchNamers[0].MatchAndName(ksh)
|
||||
c.Check(found, Equals, false)
|
||||
|
||||
found, name = cfg.MatchNamers[1].MatchAndName(bash)
|
||||
c.Check(found, Equals, false)
|
||||
found, name = cfg.MatchNamers[1].MatchAndName(sh)
|
||||
c.Check(found, Equals, true)
|
||||
c.Check(name, Equals, "sh")
|
||||
found, name = cfg.MatchNamers[1].MatchAndName(ksh)
|
||||
c.Check(found, Equals, false)
|
||||
|
||||
found, name = cfg.MatchNamers[2].MatchAndName(bash)
|
||||
c.Check(found, Equals, false)
|
||||
found, name = cfg.MatchNamers[2].MatchAndName(sh)
|
||||
c.Check(found, Equals, false)
|
||||
found, name = cfg.MatchNamers[2].MatchAndName(ksh)
|
||||
c.Check(found, Equals, true)
|
||||
c.Check(name, Equals, "ksh")
|
||||
}
|
||||
|
||||
func (s MySuite) TestConfigTemplates(c *C) {
|
||||
yml := `
|
||||
process_names:
|
||||
- exe:
|
||||
- postmaster
|
||||
cmdline:
|
||||
- "-D\\s+.+?(?P<Path>[^/]+)(?:$|\\s)"
|
||||
name: "{{.ExeBase}}:{{.Matches.Path}}"
|
||||
- exe:
|
||||
- prometheus
|
||||
name: "{{.ExeFull}}"
|
||||
`
|
||||
cfg, err := GetConfig(yml)
|
||||
c.Assert(err, IsNil)
|
||||
c.Check(cfg.MatchNamers, HasLen, 2)
|
||||
|
||||
postgres := common.NameAndCmdline{"postmaster", []string{"/usr/bin/postmaster", "-D", "/data/pg"}}
|
||||
found, name := cfg.MatchNamers[0].MatchAndName(postgres)
|
||||
c.Check(found, Equals, true)
|
||||
c.Check(name, Equals, "postmaster:pg")
|
||||
|
||||
pm := common.NameAndCmdline{"prometheus", []string{"/usr/local/bin/prometheus"}}
|
||||
found, name = cfg.MatchNamers[1].MatchAndName(pm)
|
||||
c.Check(found, Equals, true)
|
||||
c.Check(name, Equals, "/usr/local/bin/prometheus")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue