forked from DataDog/gohai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gohai.go
114 lines (91 loc) · 1.9 KB
/
gohai.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/DataDog/gohai/cpu"
"github.com/DataDog/gohai/filesystem"
"github.com/DataDog/gohai/memory"
"github.com/DataDog/gohai/network"
"github.com/DataDog/gohai/platform"
)
type Collector interface {
Name() string
Collect() (interface{}, error)
}
var collectors = []Collector{
&cpu.Cpu{},
&filesystem.FileSystem{},
&memory.Memory{},
&network.Network{},
&platform.Platform{},
}
var options struct {
version bool
}
// version information filled in at build time
var (
buildDate string
gitCommit string
gitBranch string
goVersion string
)
func Collect() (result map[string]interface{}, err error) {
result = make(map[string]interface{})
for _, collector := range collectors {
c, err := collector.Collect()
if err != nil {
log.Printf("[%s] %s", collector.Name(), err)
continue
}
result[collector.Name()] = c
}
result["gohai"] = versionMap()
return
}
func versionMap() (result map[string]interface{}) {
result = make(map[string]interface{})
result["git_hash"] = gitCommit
result["git_branch"] = gitBranch
result["build_date"] = buildDate
result["go_version"] = goVersion
return
}
func versionString() string {
var buf bytes.Buffer
if gitCommit != "" {
fmt.Fprintf(&buf, "Git hash: %s\n", gitCommit)
}
if gitBranch != "" {
fmt.Fprintf(&buf, "Git branch: %s\n", gitBranch)
}
if buildDate != "" {
fmt.Fprintf(&buf, "Build date: %s\n", buildDate)
}
if goVersion != "" {
fmt.Fprintf(&buf, "Go Version: %s\n", goVersion)
}
return buf.String()
}
func init() {
flag.BoolVar(&options.version, "version", false, "Show version information and exit")
flag.Parse()
}
func main() {
if options.version {
fmt.Printf("%s", versionString())
os.Exit(0)
}
gohai, err := Collect()
if err != nil {
panic(err)
}
buf, err := json.Marshal(gohai)
if err != nil {
panic(err)
}
os.Stdout.Write(buf)
}