/
support.go
186 lines (157 loc) · 5.98 KB
/
support.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Package support provides methods for collecting and printing support information
about system.
By default, it collects information about the application and environment:
- Name
- Version
- Go version used
- Binary SHA
- Git commit SHA
There are also some sub-packages to collect/parse additional information:
- apps: Package for extracting apps versions info
- deps: Package for extracting dependency information from gomod data
- pkgs: Package for collecting information about installed packages
- services: Package for collecting information about services
- fs: Package for collecting information about the file system
- network: Package to collect information about the network
Example of collecting maximum information about the application and system:
support.Collect("TestApp", "12.3.4").
WithRevision("fc8d81e").
WithDeps(deps.Extract(gomodData)).
WithApps(apps.Golang(), apps.GCC()).
WithPackages(pkgs.Collect("rpm", "go,golang", "java,jre,jdk", "nano")).
WithServices(services.Collect("firewalld", "nginx")).
WithChecks(myAppAvailabilityCheck()).
WithEnvVars("LANG", "PAGER", "SSH_CLIENT").
WithNetwork(network.Collect("https://domain.com/ip-echo")).
WithFS(fs.Collect()).
Print()
Also, you can't encode data to JSON/GOB and send it to your server instead of printing
it to the console.
info := support.Collect("TestApp", "12.3.4").
WithRevision("fc8d81e").
WithDeps(deps.Extract(gomodData)).
WithApps(apps.Golang(), apps.GCC()).
WithPackages(pkgs.Collect("rpm", "go,golang", "java,jre,jdk", "nano")).
WithServices(services.Collect("firewalld", "nginx")).
WithChecks(myAppAvailabilityCheck()).
WithEnvVars("LANG", "PAGER", "SSH_CLIENT").
WithNetwork(network.Collect("https://domain.com/ip-echo")).
WithFS(fs.Collect())
b, _ := json.Marshal(info)
fmt.Println(string(b))
*/
package support
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
type CheckStatus string
const (
CHECK_OK CheckStatus = "ok"
CHECK_ERROR CheckStatus = "error"
CHECK_WARN CheckStatus = "warn"
CHECK_SKIP CheckStatus = "skip"
)
type ServiceStatus string
const (
STATUS_WORKS ServiceStatus = "works"
STATUS_STOPPED ServiceStatus = "stopped"
STATUS_UNKNOWN ServiceStatus = "unknown"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Info contains all support information (can be encoded in JSON/GOB)
type Info struct {
Name string `json:"name"`
Version string `json:"version"`
Binary string `json:"binary"`
Build *BuildInfo `json:"build,omitempty"`
OS *OSInfo `json:"os,omitempty"`
System *SystemInfo `json:"system,omitempty"`
Network *NetworkInfo `json:"network,omitempty"`
FS []FSInfo `json:"fs,omitempty"`
Pkgs []Pkg `json:"pkgs,omitempty"`
Services []Service `json:"services,omitempty"`
Deps []Dep `json:"deps,omitempty"`
Apps []App `json:"apps,omitempty"`
Checks []Check `json:"checks,omitempty"`
Env []EnvVar `json:"env,omitempty"`
}
// BuildInfo contains information about binary
type BuildInfo struct {
GoVersion string `json:"go_version"`
GoArch string `json:"go_arch"`
GoOS string `json:"go_os"`
GitSHA string `json:"git_sha,omitempty"`
BinSHA string `json:"bin_sha,omitempty"`
}
// OSInfo contains extended information about OS
type OSInfo struct {
Name string `json:"name,omitempty"`
PrettyName string `json:"pretty_name,omitempty"`
Version string `json:"version,omitempty"`
Build string `json:"build,omitempty"`
ID string `json:"id,omitempty"`
IDLike string `json:"id_like,omitempty"`
VersionID string `json:"version_id,omitempty"`
VersionCode string `json:"version_code,omitempty"`
PlatformID string `json:"platform_id,omitempty"`
CPE string `json:"cpe,omitempty"`
coloredName string
coloredPrettyName string
}
// SystemInfo contains basic information about system
type SystemInfo struct {
Name string `json:"name"`
Arch string `json:"arch"`
Kernel string `json:"kernel"`
ContainerEngine string `json:"container_engine,omitempty"`
}
// NetworkInfo contains basic information about network
type NetworkInfo struct {
Hostname string `json:"hostname"`
PublicIP string `json:"public_ip,omitempty"`
IPv4 []string `json:"ipv4"`
IPv6 []string `json:"ipv6,omitempty"`
}
// FSInfo contains basic information about file system mount
type FSInfo struct {
Path string `json:"path,omitempty"`
Device string `json:"device,omitempty"`
Type string `json:"type,omitempty"`
Used uint64 `json:"used,omitempty"`
Free uint64 `json:"free,omitempty"`
}
// Service contains basic info about service
type Service struct {
Name string `json:"name"`
Status ServiceStatus `json:"status"`
IsPresent bool `json:"is_present"`
IsEnabled bool `json:"is_enabled"`
}
// App contains basic information about app
type App struct {
Name string `json:"name"`
Version string `json:"version"`
}
// Pkg contains basic information about package
type Pkg = App
// Dep contains dependency information
type Dep struct {
Path string `json:"path"`
Version string `json:"version"`
Extra string `json:"extra"`
}
// EnvVar contains information about environment variable
type EnvVar struct {
Key string `json:"key"`
Value string `json:"value"`
}
// Check contains info about custom check
type Check struct {
Status CheckStatus `json:"status"`
Title string `json:"title"`
Message string `json:"message,omitempty"`
}