diff --git a/README.md b/README.md index 39d8684..990d230 100644 --- a/README.md +++ b/README.md @@ -20,18 +20,37 @@ CRITICAL (or WARNING) alert is issued if no metric has been posted since the min ## Setting for mackerel-agent You can install the plugin by `mkr` command. + +##### Linux ``` sudo mkr plugin install check-mackerel-metric ``` +##### Windows +(by Administrator) + +``` +"C:\Program Files\Mackerel\mackerel-agent\mkr.exe" plugin install check-mackerel-metric +``` + Add plugin configuration into mackerel-agent.conf. +##### Linux +``` +[plugin.checks.metric-myhost] +command = ["/opt/mackerel-agent/plugins/bin/check-mackerel-metric", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] + +[plugin.checks.metric-myservice] +command = ["/opt/mackerel-agent/plugins/bin/check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +``` + +##### Windows ``` [plugin.checks.metric-myhost] -command = ["check-mackerel-metric", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +command = ["plugins\\bin\\check-mackerel-metric.exe", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] [plugin.checks.metric-myservice] -command = ["check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +command = ["plugins\\bin\\check-mackerel-metric.exe", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] ``` ## Usage @@ -75,18 +94,36 @@ check-mackerel-metric -s SERVICE_NAME -n METRIC_NAME -w WARNING_MINUTE -c CRITIC ## mackerel-agentでの設定 プラグインは `mkr` コマンドでインストールできます。 +##### Linux ``` sudo mkr plugin install check-mackerel-metric ``` +##### Windows +(管理者権限) + +``` +"C:\Program Files\Mackerel\mackerel-agent\mkr.exe" plugin install check-mackerel-metric +``` + mackerel-agent.conf にプラグインの設定を記述してください。 +##### Linux +``` +[plugin.checks.metric-myhost] +command = ["/opt/mackerel-agent/plugins/bin/check-mackerel-metric", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] + +[plugin.checks.metric-myservice] +command = ["/opt/mackerel-agent/plugins/bin/check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +``` + +##### Windows ``` [plugin.checks.metric-myhost] -command = ["check-mackerel-metric", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +command = ["plugins\\bin\\check-mackerel-metric.exe", "-H", "HOST_ID", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] [plugin.checks.metric-myservice] -command = ["check-mackerel-metric", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] +command = ["plugins\\bin\\check-mackerel-metric.exe", "-s", "SERVICE_NAME", "-n", "METRIC_NAME", "-w", "WARNING_MINUTE", "-c", "CRITICAL_MINUTE"] ``` ## 使い方 diff --git a/checkmackerelmetric/checkmackerelmetric.go b/checkmackerelmetric/checkmackerelmetric.go index db0b092..81f87f2 100644 --- a/checkmackerelmetric/checkmackerelmetric.go +++ b/checkmackerelmetric/checkmackerelmetric.go @@ -3,6 +3,8 @@ package checkmackerelmetric import ( "fmt" "os" + "path/filepath" + "runtime" "time" "github.com/alexflint/go-arg" @@ -85,7 +87,17 @@ func (opts *mackerelMetricOpts) run() *checkers.Checker { conf, err := config.LoadConfig(config.DefaultConfig.Conffile) if err != nil { - return checkers.Unknown(fmt.Sprintf("%v", err)) + if runtime.GOOS == "windows" { + newpath := filepath.Join(config.DefaultConfig.Conffile, "../../../mackerel-agent.conf") + conf, err = config.LoadConfig(newpath) + if err != nil { + return checkers.Unknown(err.Error()) + } + conf.Conffile = newpath + conf.Root = filepath.Dir(newpath) + } else { + return checkers.Unknown(err.Error()) + } } apibase := conf.Apibase if apikey == "" { @@ -101,7 +113,7 @@ func (opts *mackerelMetricOpts) run() *checkers.Checker { client, err := mackerel.NewClientWithOptions(apikey, apibase, false) if err != nil { - return checkers.Unknown(fmt.Sprintf("%v", err)) + return checkers.Unknown(err.Error()) } return checkMetric(client, opts, criticalFrom, warningFrom, to) @@ -113,7 +125,7 @@ func checkMetric(client *mackerel.Client, opts *mackerelMetricOpts, criticalFrom // CRITICAL check metricValue, err := fetchMetricValues(client, opts.Host, opts.Service, opts.Metric, criticalFrom, to) if err != nil { - return checkers.Unknown(fmt.Sprintf("%v", err)) + return checkers.Unknown(err.Error()) } if len(metricValue) == 0 { return checkers.Critical(fmt.Sprintf("no metric for %s has been posted since at least %d minutes ago", opts.Metric, opts.Critical))