-
Notifications
You must be signed in to change notification settings - Fork 90
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
Showing
4 changed files
with
170 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,120 @@ | ||
// auto-generated file | ||
//go:generate go run _tools/gen_commands.go | ||
|
||
package main | ||
|
||
import "github.com/motemen/go-cli" | ||
|
||
func init() { | ||
cli.Use( | ||
&cli.Command{ | ||
Name: "", | ||
Action: doMain, | ||
Short: "mackerel-agent", | ||
Long: "main process of mackerel-agent", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "version", | ||
Action: doVersion, | ||
Short: "display version of mackerel-agent", | ||
Long: "version\n\ndisplay the version of mackerel-agent", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "configtest", | ||
Action: doConfigtest, | ||
Short: "configtest", | ||
Long: "configtest\n\ndo configtest", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "retire", | ||
Action: doRetire, | ||
Short: "retire the host", | ||
Long: "retire [-force]\n\nretire the host", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "once", | ||
Action: doOnce, | ||
Short: "output onetime", | ||
Long: "once\n\noutput metrics and meta data of the host one time.\nThese data are only displayed and not posted to Mackerel.", | ||
}, | ||
) | ||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/Songmu/prompter" | ||
"github.com/mackerelio/mackerel-agent/command" | ||
"github.com/mackerelio/mackerel-agent/logging" | ||
"github.com/mackerelio/mackerel-agent/mackerel" | ||
"github.com/mackerelio/mackerel-agent/version" | ||
"github.com/stf-storage/go-stf-server/config" | ||
) | ||
|
||
/* +command - mackerel-agent | ||
main process of mackerel-agent | ||
*/ | ||
func doMain(fs *flag.FlagSet, argv []string) error { | ||
conf, err := resolveConfig(fs, argv) | ||
if err != nil { | ||
logger.Criticalf("faild to load config: %s", err) | ||
return fmt.Errorf("failed to load config: %s", err) | ||
} | ||
if conf.Verbose { | ||
logging.SetLogLevel(logging.DEBUG) | ||
} | ||
logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s", version.VERSION, version.GITCOMMIT, conf.Apibase) | ||
|
||
exitCode := start(conf) | ||
if exitCode != exitStatusOK { | ||
return fmt.Errorf("failed to exit normally. exitCode: %d", exitCode) | ||
} | ||
return nil | ||
} | ||
|
||
/* +command version - display version of mackerel-agent | ||
version | ||
display the version of mackerel-agent | ||
*/ | ||
func doVersion(_ *flag.FlagSet, _ []string) error { | ||
fmt.Printf("mackerel-agent version %s (rev %s) [%s %s %s] \n", | ||
version.VERSION, version.GITCOMMIT, runtime.GOOS, runtime.GOARCH, runtime.Version()) | ||
return nil | ||
} | ||
|
||
/* +command configtest - configtest | ||
configtest | ||
do configtest | ||
*/ | ||
func doConfigtest(fs *flag.FlagSet, argv []string) error { | ||
conf, err := resolveConfig(fs, argv) | ||
if err != nil { | ||
logger.Criticalf("faild to test config: %s", err) | ||
return fmt.Errorf("failed to test config: %s", err) | ||
} | ||
fmt.Fprintf(os.Stderr, "%s Syntax OK\n", conf.Conffile) | ||
return nil | ||
} | ||
|
||
/* +command retire - retire the host | ||
retire [-force] | ||
retire the host | ||
*/ | ||
func doRetire(fs *flag.FlagSet, argv []string) error { | ||
conf, force := resolveConfigForRetire(fs, argv) | ||
|
||
hostID, err := conf.LoadHostID() | ||
if err != nil { | ||
return fmt.Errorf("HostID file is not found") | ||
} | ||
|
||
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose) | ||
if err != nil { | ||
return fmt.Errorf("faild to create api client: %s", err) | ||
} | ||
|
||
if !force && !prompter.YN(fmt.Sprintf("retire this host? (hostID: %s)", hostID), false) { | ||
return fmt.Errorf("Retirement is canceled.") | ||
} | ||
|
||
err = api.RetireHost(hostID) | ||
if err != nil { | ||
return fmt.Errorf("faild to retire the host: %s", err) | ||
} | ||
logger.Infof("This host (hostID: %s) has been retired.", hostID) | ||
// just to try to remove hostID file. | ||
err = conf.DeleteSavedHostID() | ||
if err != nil { | ||
logger.Warningf("Failed to remove HostID file: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
/* +command once - output onetime | ||
once | ||
output metrics and meta data of the host one time. | ||
These data are only displayed and not posted to Mackerel. | ||
*/ | ||
func doOnce(fs *flag.FlagSet, argv []string) error { | ||
conf, err := resolveConfig(fs, argv) | ||
if err != nil { | ||
logger.Warningf("failed to load config (but `once` must not required conf): %s", err) | ||
conf = &config.Config{} | ||
} | ||
command.RunOnce(conf) | ||
return nil | ||
} |
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,52 @@ | ||
// auto-generated file | ||
|
||
package main | ||
|
||
import "github.com/motemen/go-cli" | ||
|
||
func init() { | ||
cli.Use( | ||
&cli.Command{ | ||
Name: "", | ||
Action: doMain, | ||
Short: "mackerel-agent", | ||
Long: "main process of mackerel-agent", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "version", | ||
Action: doVersion, | ||
Short: "display version of mackerel-agent", | ||
Long: "version\n\ndisplay the version of mackerel-agent", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "configtest", | ||
Action: doConfigtest, | ||
Short: "configtest", | ||
Long: "configtest\n\ndo configtest", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "retire", | ||
Action: doRetire, | ||
Short: "retire the host", | ||
Long: "retire [-force]\n\nretire the host", | ||
}, | ||
) | ||
|
||
cli.Use( | ||
&cli.Command{ | ||
Name: "once", | ||
Action: doOnce, | ||
Short: "output onetime", | ||
Long: "once\n\noutput metrics and meta data of the host one time.\nThese data are only displayed and not posted to Mackerel.", | ||
}, | ||
) | ||
} |
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