forked from DataDog/datadog-agent
/
common.go
223 lines (189 loc) · 4.55 KB
/
common.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package util
import (
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/StackVista/stackstate-agent/pkg/autodiscovery/integration"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/version"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// CopyFile atomically copies file path `src`` to file path `dst`.
func CopyFile(src, dst string) error {
fi, err := os.Stat(src)
if err != nil {
return err
}
perm := fi.Mode()
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
tmp, err := ioutil.TempFile(filepath.Dir(dst), "")
if err != nil {
return err
}
tmpName := tmp.Name()
_, err = io.Copy(tmp, in)
if err != nil {
tmp.Close()
os.Remove(tmpName)
return err
}
err = tmp.Close()
if err != nil {
os.Remove(tmpName)
return err
}
err = os.Chmod(tmpName, perm)
if err != nil {
os.Remove(tmpName)
return err
}
err = os.Rename(tmpName, dst)
if err != nil {
os.Remove(tmpName)
return err
}
return nil
}
// CopyFileAll calls CopyFile, but will create necessary directories for `dst`.
func CopyFileAll(src, dst string) error {
err := EnsureParentDirsExist(dst)
if err != nil {
return err
}
return CopyFile(src, dst)
}
// CopyDir copies directory recursively
func CopyDir(src, dst string) error {
var (
err error
fds []os.FileInfo
srcinfo os.FileInfo
)
if srcinfo, err = os.Stat(src); err != nil {
return err
}
if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}
if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
s := path.Join(src, fd.Name())
d := path.Join(dst, fd.Name())
if fd.IsDir() {
err = CopyDir(s, d)
} else {
err = CopyFile(s, d)
}
if err != nil {
return err
}
}
return nil
}
// GetFileSize gets the file size
func GetFileSize(path string) (int64, error) {
stat, err := os.Stat(path)
if err != nil {
return 0, err
}
return stat.Size(), nil
}
// GetFileModTime gets the modification time
func GetFileModTime(path string) (time.Time, error) {
stat, err := os.Stat(path)
if err != nil {
return time.Time{}, err
}
return stat.ModTime(), nil
}
// EnsureParentDirsExist makes a path immediately available for
// writing by creating the necessary parent directories.
func EnsureParentDirsExist(p string) error {
err := os.MkdirAll(filepath.Dir(p), os.ModePerm)
if err != nil {
return err
}
return nil
}
// HTTPHeaders returns a http headers including various basic information (User-Agent, Content-Type...).
func HTTPHeaders() map[string]string {
av, _ := version.Agent()
return map[string]string{
"User-Agent": fmt.Sprintf("Datadog Agent/%s", av.GetNumber()),
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "text/html, */*",
}
}
// GetJSONSerializableMap returns a JSON serializable map from a raw map
func GetJSONSerializableMap(m interface{}) interface{} {
switch x := m.(type) {
// unbelievably I cannot collapse this into the next (identical) case
case map[interface{}]interface{}:
j := integration.JSONMap{}
for k, v := range x {
j[k.(string)] = GetJSONSerializableMap(v)
}
return j
case integration.RawMap:
j := integration.JSONMap{}
for k, v := range x {
j[k.(string)] = GetJSONSerializableMap(v)
}
return j
case integration.JSONMap:
j := integration.JSONMap{}
for k, v := range x {
j[k] = GetJSONSerializableMap(v)
}
return j
case []interface{}:
j := make([]interface{}, len(x))
for i, v := range x {
j[i] = GetJSONSerializableMap(v)
}
return j
}
return m
}
// GetGoRoutinesDump returns the stack trace of every Go routine of a running Agent.
func GetGoRoutinesDump() (string, error) {
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return "", err
}
pprofURL := fmt.Sprintf("http://%v:%s/debug/pprof/goroutine?debug=2",
ipcAddress, config.Datadog.GetString("expvar_port"))
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
client := http.Client{}
req, err := http.NewRequest(http.MethodGet, pprofURL, nil)
if err != nil {
return "", err
}
resp, err := client.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
return string(data), err
}