Skip to content

Commit

Permalink
Merge pull request #32 from mackerelio-labs/winpath
Browse files Browse the repository at this point in the history
workaround for Windows path and update README about plugin path
  • Loading branch information
kmuto authored Dec 21, 2023
2 parents 8e7575e + 2735985 commit ee7b7e3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
```

## 使い方
Expand Down
18 changes: 15 additions & 3 deletions checkmackerelmetric/checkmackerelmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package checkmackerelmetric
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"

"github.com/alexflint/go-arg"
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit ee7b7e3

Please sign in to comment.