-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathmackerel-plugin.go
More file actions
92 lines (80 loc) · 1.72 KB
/
mackerel-plugin.go
File metadata and controls
92 lines (80 loc) · 1.72 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"runtime/debug"
"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
}
func fromVCS() (version, rev string) {
version = "unknown"
rev = "unknown"
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
version = info.Main.Version
for _, s := range info.Settings {
if s.Key == "vcs.revision" {
rev = s.Value
return
}
}
return
}
func printHelp() {
version, gitcommit := fromVCS()
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 "))
}