Skip to content

Commit

Permalink
Merge branch 'master' into build-k
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Jul 16, 2015
2 parents a6c1315 + 8a7aa18 commit bf32996
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -22,8 +22,8 @@ deploy:
api_key:
secure: NtpNjquqjnwpeVQQM1GTHTTU7YOo8fEIyoBtMf3Vf1ayZjuWVZxwNfM77E596TG52a8pnZtpapXyHT0M4e1zms7F5KVCrOEfOB0OrA4IDzoATelVqdONnN3lbRJeVJVdSmK8/FNKwjI24tQZTaTQcIOioNqh7ZRcrEYlatGCuAw=
file:
- /home/travis/rpmbuild/RPMS/noarch/mackerel-agent-0.18.0-1.noarch.rpm
- packaging/mackerel-agent_0.18.0-1_all.deb
- /home/travis/rpmbuild/RPMS/noarch/mackerel-agent-0.18.1-1.noarch.rpm
- packaging/mackerel-agent_0.18.1-1_all.deb
- snapshot/mackerel-agent_darwin_386.zip
- snapshot/mackerel-agent_darwin_amd64.zip
- snapshot/mackerel-agent_freebsd_386.zip
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog

## 0.18.1 (2015-07-16)

* s/ami_id/ami-id/ in spec/cloud.go #112 (Songmu)
* remove `UpdateHost()` process from `prepareHost()` for simplicity #116 (Songmu)
* filter invalid roleFullNames with warning logs #117 (Songmu)
* allow using spaces as delimiter for custom metric values #119 (Songmu)


## 0.18.0 (2015-07-08)

* Retry in prepare #108 (Songmu)
Expand Down
27 changes: 9 additions & 18 deletions command/command.go
Expand Up @@ -108,29 +108,14 @@ func prepareHost(root string, api *mackerel.API, roleFullnames []string, checks
if lastErr != nil {
return nil, fmt.Errorf("Failed to find this host on mackerel: %s", lastErr.Error())
}
} else { // update
} else { // check the hostID is valid or not
doRetry(func() error {
result, lastErr = api.FindHost(hostID)
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to find this host on mackerel (You may want to delete file \"%s\" to register this host to an another organization): %s", idFilePath(root), lastErr.Error())
}

doRetry(func() error {
lastErr = api.UpdateHost(hostID, mackerel.HostSpec{
Name: hostname,
Meta: meta,
Interfaces: interfaces,
RoleFullnames: roleFullnames,
Checks: checks,
DisplayName: displayName,
})
return filterErrorForRetry(lastErr)
})
if lastErr != nil {
return nil, fmt.Errorf("Failed to update this host: %s", lastErr.Error())
}
}

if hostSt != "" && hostSt != result.Status {
Expand Down Expand Up @@ -321,11 +306,12 @@ func loop(c *context, termCh chan struct{}) int {

func updateHostSpecsLoop(c *context, quit chan struct{}) {
for {
UpdateHostSpecs(c.conf, c.api, c.host)
select {
case <-quit:
return
case <-time.After(specsUpdateInterval):
UpdateHostSpecs(c.conf, c.api, c.host)
// nop
}
}
}
Expand Down Expand Up @@ -475,7 +461,12 @@ func collectHostSpecs() (string, map[string]interface{}, []map[string]interface{
return "", nil, nil, fmt.Errorf("failed to obtain hostname: %s", err.Error())
}

meta := spec.Collect(specGenerators())
specGens := specGenerators()
cGen := spec.SuggestCloudGenerator()
if cGen != nil {
specGens = append(specGens, cGen)
}
meta := spec.Collect(specGens)

interfacesSpec, err := interfaceGenerator().Generate()
if err != nil {
Expand Down
9 changes: 1 addition & 8 deletions command/command_linux.go
Expand Up @@ -9,20 +9,13 @@ import (
)

func specGenerators() []spec.Generator {
specs := []spec.Generator{
return []spec.Generator{
&specLinux.KernelGenerator{},
&specLinux.CPUGenerator{},
&specLinux.MemoryGenerator{},
&specLinux.BlockDeviceGenerator{},
&specLinux.FilesystemGenerator{},
}
cloudGenerator, err := specLinux.NewCloudGenerator("")
if err != nil {
logger.Errorf("Failed to create cloudGenerator: %s", err.Error())
} else {
specs = append(specs, cloudGenerator)
}
return specs
}

func interfaceGenerator() spec.Generator {
Expand Down
5 changes: 5 additions & 0 deletions command/command_test.go
Expand Up @@ -284,6 +284,11 @@ func TestLoop(t *testing.T) {
"success": true,
}
}
mockHandlers["PUT /api/v0/hosts/xyzabc12345"] = func(req *http.Request) (int, jsonObject) {
return 200, jsonObject{
"result": "OK",
}
}

// Prepare required objects...
ag := &agent.Agent{
Expand Down
35 changes: 18 additions & 17 deletions main.go
Expand Up @@ -22,23 +22,15 @@ import (
// allow options like -role=... -role=...
type roleFullnamesFlag []string

var roleFullnamePattern = regexp.MustCompile(`^[\w-]+:\s*[\w-]+$`)
var roleFullnamePattern = regexp.MustCompile(`^[a-zA-Z0-9][-_a-zA-Z0-9]*:\s*[a-zA-Z0-9][-_a-zA-Z0-9]*$`)

func (r *roleFullnamesFlag) String() string {
return fmt.Sprint(*r)
}

func (r *roleFullnamesFlag) Set(input string) error {
inputRoles := strings.Split(input, ",")

for _, inputRole := range inputRoles {
if roleFullnamePattern.MatchString(inputRole) == false {
return fmt.Errorf("Bad format for role fullname (expecting <service>:<role>): %s", inputRole)
}
}

*r = append(*r, inputRoles...)

return nil
}

Expand Down Expand Up @@ -88,14 +80,14 @@ func resolveConfig() (*config.Config, *otherOptions) {
otherOptions := &otherOptions{}

var (
conffile = flag.String("conf", config.DefaultConfig.Conffile, "Config file path (Configs in this file are over-written by command line options)")
apibase = flag.String("apibase", config.DefaultConfig.Apibase, "API base")
pidfile = flag.String("pidfile", config.DefaultConfig.Pidfile, "File containing PID")
root = flag.String("root", config.DefaultConfig.Root, "Directory containing variable state information")
apikey = flag.String("apikey", "", "API key from mackerel.io web site")
diagnostic = flag.Bool("diagnostic", false, "Enables diagnostic features")
runOnce = flag.Bool("once", false, "Show spec and metrics to stdout once")
printVersion = flag.Bool("version", false, "Prints version and exit")
conffile = flag.String("conf", config.DefaultConfig.Conffile, "Config file path (Configs in this file are over-written by command line options)")
apibase = flag.String("apibase", config.DefaultConfig.Apibase, "API base")
pidfile = flag.String("pidfile", config.DefaultConfig.Pidfile, "File containing PID")
root = flag.String("root", config.DefaultConfig.Root, "Directory containing variable state information")
apikey = flag.String("apikey", "", "API key from mackerel.io web site")
diagnostic = flag.Bool("diagnostic", false, "Enables diagnostic features")
runOnce = flag.Bool("once", false, "Show spec and metrics to stdout once")
printVersion = flag.Bool("version", false, "Prints version and exit")
)

var verbose bool
Expand Down Expand Up @@ -145,6 +137,15 @@ func resolveConfig() (*config.Config, *otherOptions) {
}
})

r := []string{}
for _, roleFullName := range conf.Roles {
if !roleFullnamePattern.MatchString(roleFullName) {
logger.Errorf("Bad format for role fullname (expecting <service>:<role>. Alphabet, numbers, hyphens and underscores are acceptable, but the first character must not be a hyphen or an underscore.): '%s'", roleFullName)
} else {
r = append(r, roleFullName)
}
}
conf.Roles = r
return conf, nil
}

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Expand Up @@ -23,7 +23,7 @@ diagnostic=false
confFile.Close()
defer os.Remove(confFile.Name())

os.Args = []string{"mackerel-agent", "-conf=" + confFile.Name(), "-role=My-Service:default", "-verbose", "-diagnostic"}
os.Args = []string{"mackerel-agent", "-conf=" + confFile.Name(), "-role=My-Service:default,INVALID#SERVICE", "-verbose", "-diagnostic"}
// Overrides Args from go test command
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.PanicOnError)

Expand Down
4 changes: 3 additions & 1 deletion metrics/plugin.go
Expand Up @@ -212,6 +212,8 @@ func (g *pluginGenerator) makeCreateGraphDefsPayload() []mackerel.CreateGraphDef
return payloads
}

var delimReg = regexp.MustCompile(`[\s\t]+`)

func (g *pluginGenerator) collectValues() (Values, error) {
command := g.Config.Command
pluginLogger.Debugf("Executing plugin: command = \"%s\"", command)
Expand All @@ -228,7 +230,7 @@ func (g *pluginGenerator) collectValues() (Values, error) {
for _, line := range strings.Split(stdout, "\n") {
// Key, value, timestamp
// ex.) tcp.CLOSING 0 1397031808
items := strings.Split(line, "\t")
items := delimReg.Split(line, 3)
if len(items) != 3 {
continue
}
Expand Down
26 changes: 25 additions & 1 deletion metrics/plugin_linux_test.go → metrics/plugin_test.go
@@ -1,4 +1,4 @@
// +build linux
// +build linux darwin freebsd

package metrics

Expand Down Expand Up @@ -73,6 +73,30 @@ func TestPluginCollectValuesCommand(t *testing.T) {
}
}

func TestPluginCollectValuesCommandWithSpaces(t *testing.T) {
g := &pluginGenerator{Config: config.PluginConfig{
Command: `echo "just.echo.2 2 1397822016"`,
}}

values, err := g.collectValues()
if err != nil {
t.Errorf("should not raise error: %v", err)
}

if len(values) != 1 {
t.Error("Only 1 value shoud be generated")
}

for name, value := range values {
if name != "custom.just.echo.2" {
t.Errorf("Wrong name: %s", name)
}
if value != 2.0 {
t.Errorf("Wrong value: %+v", value)
}
}
}

func TestPluginLoadPluginMeta(t *testing.T) {
g := &pluginGenerator{
Config: config.PluginConfig{
Expand Down
5 changes: 4 additions & 1 deletion metrics/windows/plugin.go
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"errors"
"os/exec"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -43,6 +44,8 @@ func (g *PluginGenerator) Generate() (metrics.Values, error) {
return results, nil
}

var delimReg = regexp.MustCompile(`[\s\t]+`)

func (g *PluginGenerator) collectValues(command string) (metrics.Values, error) {
pluginLogger.Debugf("Executing plugin: command = \"%s\"", command)

Expand All @@ -63,7 +66,7 @@ func (g *PluginGenerator) collectValues(command string) (metrics.Values, error)
for _, line := range strings.Split(string(outBuffer.Bytes()), "\n") {
// Key, value, timestamp
// ex.) localhost.localdomain.tcp.CLOSING 0 1397031808
items := strings.Split(line, "\t")
items := delimReg.Split(line, 3)
if len(items) != 3 {
continue
}
Expand Down
13 changes: 13 additions & 0 deletions packaging/deb/debian/changelog
@@ -1,3 +1,16 @@
mackerel-agent (0.18.1-1) stable; urgency=low

* s/ami_id/ami-id/ in spec/cloud.go (by Songmu)
<https://github.com/mackerelio/mackerel-agent/pull/112>
* remove `UpdateHost()` process from `prepareHost()` for simplicity (by Songmu)
<https://github.com/mackerelio/mackerel-agent/pull/116>
* filter invalid roleFullNames with warning logs (by Songmu)
<https://github.com/mackerelio/mackerel-agent/pull/117>
* allow using spaces as delimiter for custom metric values (by Songmu)
<https://github.com/mackerelio/mackerel-agent/pull/119>

-- Songmu <y.songmu@gmail.com> Thu, 16 Jul 2015 17:05:43 +0900

mackerel-agent (0.18.0-1) stable; urgency=low

* Retry in prepare (by Songmu)
Expand Down
8 changes: 7 additions & 1 deletion packaging/rpm/mackerel-agent.spec
Expand Up @@ -5,7 +5,7 @@
%define _localbindir /usr/local/bin

Name: mackerel-agent
Version: 0.18.0
Version: 0.18.1
Release: 1
License: Commercial
Summary: macekrel.io agent
Expand Down Expand Up @@ -73,6 +73,12 @@ fi
%{_sysconfdir}/logrotate.d/%{name}

%changelog
* Thu Jul 16 2015 <y.songmu@gmail.com> - 0.18.1-1
- s/ami_id/ami-id/ in spec/cloud.go (by Songmu)
- remove `UpdateHost()` process from `prepareHost()` for simplicity (by Songmu)
- filter invalid roleFullNames with warning logs (by Songmu)
- allow using spaces as delimiter for custom metric values (by Songmu)

* Wed Jul 08 2015 <tomohiro68@gmail.com> - 0.18.0-1
- Retry in prepare (by Songmu)
- [WORKAROUND] downgrade golang version for windows (by Sixeight)
Expand Down

0 comments on commit bf32996

Please sign in to comment.