-
Notifications
You must be signed in to change notification settings - Fork 151
/
mackerel-plugin.go
77 lines (64 loc) · 1.45 KB
/
mackerel-plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)
func main() {
os.Exit(run(os.Args))
}
const (
exitOK = iota
exitError
)
var helpReg = regexp.MustCompile(`--?h(?:elp)?`)
//go:generate sh -c "perl tool/gen_mackerel_plugin.pl > mackerel-plugin_gen.go"
func run(args []string) int {
var plug string
f, err := exec.LookPath(args[0])
if err != nil {
log.Println(err)
return exitError
}
fi, err := os.Lstat(f)
if err != nil {
log.Println(err)
return exitError
}
base := filepath.Base(f)
if fi.Mode()&os.ModeSymlink == os.ModeSymlink && strings.HasPrefix(base, "mackerel-plugin-") {
// if mackerel-plugin is symbolic linked from mackerel-plugin-memcached, run the memcached plugin
plug = strings.TrimPrefix(base, "mackerel-plugin-")
} else {
if len(args) < 2 {
printHelp()
return exitError
}
plug = args[1]
if helpReg.MatchString(plug) {
printHelp()
return exitOK
}
os.Args = append([]string{f}, args[2:]...)
}
err = runPlugin(plug)
if err != nil {
return exitError
}
return exitOK
}
const version = "0.86.0"
var gitcommit string
func printHelp() {
fmt.Printf(`mackerel-plugin %s (rev %s) [%s %s %s]
Usage: mackerel-plugin <plugin> [<args>]
Following plugins are available:
%s
See `+"`mackerel-plugin <plugin> -h` "+`for more information on a specific plugin
`, version, gitcommit, runtime.GOOS, runtime.GOARCH, runtime.Version(), strings.Join(plugins, "\n "))
}