-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
70 lines (60 loc) · 1.35 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/fujiwara/go-zabbix-get/zabbix"
)
var (
Version string
)
func main() {
var (
port int
server string
key string
timeout int
showVersion bool
format string
outputKey string
)
flag.IntVar(&port, "p", 10050, "port")
flag.StringVar(&server, "s", "127.0.0.1", "hostname or IP")
flag.StringVar(&key, "k", "", "key")
flag.IntVar(&timeout, "t", 30, "timeout")
flag.BoolVar(&showVersion, "V", false, "show Version")
flag.StringVar(&format, "f", "zabbix", "output format (zabbix or sensu)")
flag.StringVar(&outputKey, "o", "", "output key string (format=sensu only)")
flag.Parse()
if showVersion {
fmt.Printf("go-zabbix-get version %s\n", Version)
os.Exit(255)
}
if key == "" {
flag.PrintDefaults()
os.Exit(255)
}
address := fmt.Sprintf("%s:%d", server, port)
value, err := zabbix.Get(address, key, time.Duration(timeout)*time.Second)
if err != nil {
log.Fatalf("error: %s", err)
}
switch format {
case "sensu":
if outputKey == "" {
printFormatSensu(key, value)
} else {
printFormatSensu(outputKey, value)
}
default:
printFormatZabbix(value)
}
}
func printFormatZabbix(value string) {
fmt.Printf("%s\n", value)
}
func printFormatSensu(key string, value string) {
fmt.Printf("%s\t%s\t%d\n", key, value, time.Now().Unix())
}