This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsweet.go
344 lines (314 loc) · 8.16 KB
/
sweet.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package sweet
// sweet.go: network device backups and change alerts for the 21st century - inspired by RANCID.
import (
"fmt"
"github.com/kr/pty"
"io"
"io/ioutil"
"log/syslog"
"os"
"os/exec"
"sync"
"time"
)
const (
recentHours = 12
)
// DeviceAccess stores host access info
type DeviceConfig struct {
Hostname string
Method string
Target string
Timeout time.Duration
CommandTimeout time.Duration
Config map[string]string
}
type DeviceStatusState int
const (
StatePending DeviceStatusState = iota
StateError
StateTimeout
StateSuccess
)
type ConfigDiff struct {
Diff string
Added int
Removed int
NewFile bool
}
type DeviceStatus struct {
Device DeviceConfig
State DeviceStatusState
When time.Time
Configs map[string]string
Diffs map[string]ConfigDiff
ErrorMessage string
}
type Status struct {
Status map[string]DeviceStatus
Lock sync.Mutex
}
// ReportWebData options for formatting web status page.
type WebReport struct {
DeviceStatus
Class string
CSSID string
EnableDiffLink bool
EnableConfLink bool
}
type SSHCollector struct {
Receive chan string
Send chan string
}
type SweetOptions struct {
Interval time.Duration
Timeout time.Duration
GitPush bool
Insecure bool
Concurrency int
HttpListen string
HttpEnabled bool
SmtpString string
Workspace string
ExecutableDir string
ToEmail string
FromEmail string
UseSyslog bool
DefaultUser string
DefaultPass string
DefaultMethod string
Syslog *syslog.Writer
Devices []DeviceConfig
Status *Status
}
type Collector interface {
Collect(device DeviceConfig) (map[string]string, error)
}
//// Kickoff collector runs
func RunCollectors(Opts *SweetOptions) {
collectorSlots := make(chan bool, Opts.Concurrency)
for {
end := time.Now().Add(Opts.Interval)
Opts.LogInfo(fmt.Sprintf("Starting %d collectors. [concurrency=%d]", len(Opts.Devices), Opts.Concurrency))
go func() {
for _, device := range Opts.Devices {
collectorSlots <- true
status := DeviceStatus{}
status.Device = device
status.When = time.Now()
status.State = StatePending
Opts.Status.Set(status)
Opts.LogInfo(fmt.Sprintf("Starting collector: %s", device.Hostname))
status = collectDevice(device, Opts)
Opts.LogInfo(fmt.Sprintf("Finished collector: %s", device.Hostname))
Opts.Status.Set(status)
}
Opts.LogInfo(fmt.Sprintf("All %d collectors finished.", len(Opts.Devices)))
if err := updateDiffs(Opts); err != nil {
Opts.LogFatal(err.Error())
}
if err := commitChanges(Opts); err != nil {
Opts.LogFatal(err.Error())
}
if err := runReporter(Opts); err != nil {
Opts.LogFatal(err.Error())
}
if Opts.Interval == 0 {
Opts.LogInfo("Interval set to 0 - exiting.")
os.Exit(0)
}
}()
Opts.LogInfo(fmt.Sprintf("Waiting for %d collectors.", len(Opts.Devices)))
for i := 0; i < len(Opts.Devices); i++ {
_ = <-collectorSlots
//Opts.LogInfo(fmt.Sprintf("Collector returned."))
}
Opts.LogInfo(fmt.Sprintf("Started all %d collectors.", len(Opts.Devices)))
if end.After(time.Now()) {
time.Sleep(end.Sub(time.Now()))
}
}
}
//// Get and save config from a single device
func collectDevice(device DeviceConfig, Opts *SweetOptions) DeviceStatus {
var err error
status := DeviceStatus{}
status.Device = device
status.When = time.Now()
if len(device.Method) == 0 {
if len(Opts.DefaultMethod) == 0 {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("No method specified for %s and default-method not defined.", device.Hostname)
return status
}
device.Method = Opts.DefaultMethod
}
// override timeouts in device configs
device.Timeout = Opts.Timeout
_, ok := device.Config["timeout"]
if ok {
device.Timeout, err = time.ParseDuration(device.Config["timeout"] + "s")
if err != nil {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("Bad timeout setting %s for host %s", device.Config["timeout"], device.Hostname)
return status
}
}
device.CommandTimeout = Opts.Timeout
_, ok = device.Config["commandtimeout"]
if ok {
device.CommandTimeout, err = time.ParseDuration(device.Config["commandtimeout"] + "s")
if err != nil {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("Bad command timeout setting %s for host %s", device.Config["commandtimeout"], device.Hostname)
return status
}
}
// setup collection options
_, ok = device.Config["user"]
if !ok {
if len(Opts.DefaultUser) == 0 {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("No user specified for %s and default-user not defined.", device.Hostname)
return status
}
device.Config["user"] = Opts.DefaultUser
}
_, ok = device.Config["pass"]
if !ok {
if len(Opts.DefaultPass) == 0 {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("No pass specified for %s and default-pass not defined.", device.Hostname)
return status
}
device.Config["pass"] = Opts.DefaultPass
}
_, ok = device.Config["enable"]
if !ok {
device.Config["enable"] = device.Config["pass"]
}
device.Target = device.Hostname
_, ok = device.Config["ip"]
if ok {
device.Target = device.Config["ip"]
}
if Opts.Insecure {
device.Config["insecure"] = "true"
}
var c Collector
if device.Method == "cisco" {
c = newCiscoCollector()
} else if device.Method == "external" {
// handle absolute and relative script paths
device.Config["scriptPath"] = device.Config["script"]
if device.Config["script"][0] != os.PathSeparator {
device.Config["scriptPath"] = Opts.ExecutableDir + string(os.PathSeparator) + device.Config["script"]
}
c = newExternalCollector()
} else if device.Method == "junos" {
c = newJunOSCollector()
} else {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("Unknown access method: %s", device.Method)
return status
}
var collectionResults map[string]string
r := make(chan map[string]string)
e := make(chan error)
go func() {
result, err := c.Collect(device)
if err != nil {
e <- err
} else {
r <- result
}
}()
select {
case collectionResults = <-r:
case <-time.After(Opts.Timeout):
status.State = StateError
status.ErrorMessage = fmt.Sprintf("collection timeout after %d seconds", int(device.Timeout.Seconds()))
return status
case err := <-e:
status.State = StateError
status.ErrorMessage = fmt.Sprintf("collection error: %s", err.Error())
return status
}
// save the collectionResults to the workspace
for name, val := range collectionResults {
Opts.LogInfo(fmt.Sprintf("Saving result: %s %s", device.Hostname, name))
err = ioutil.WriteFile(device.Hostname+"-"+cleanName(name), []byte(val), 0644)
if err != nil {
status.State = StateError
status.ErrorMessage = fmt.Sprintf("Error saving %s result to workspace: %s", name, err.Error())
return status
}
}
status.State = StateSuccess
status.Configs = collectionResults
return status
}
func newSSHCollector(device DeviceConfig) (*SSHCollector, error) {
c := new(SSHCollector)
c.Receive = make(chan string)
c.Send = make(chan string)
var cmd *exec.Cmd
_, ok := device.Config["insecure"]
if ok && device.Config["insecure"] == "true" {
cmd = exec.Command("ssh", "-oStrictHostKeyChecking=no", device.Config["user"]+"@"+device.Target)
} else {
cmd = exec.Command("ssh", device.Config["user"]+"@"+device.Target)
}
f, err := pty.Start(cmd)
if err != nil {
return c, err
}
go func() {
for {
str, err := readChunk(f)
if err != nil {
close(c.Receive)
return
}
c.Receive <- str
}
}()
go func() {
for {
select {
case command, exists := <-c.Send:
{
if !exists {
return
}
_, err := io.WriteString(f, command)
if err != nil {
panic("send error")
}
}
}
}
}()
return c, nil
}
func (s *Status) Get(device string) DeviceStatus {
defer func() {
s.Lock.Unlock()
}()
s.Lock.Lock()
return s.Status[device]
}
func (s *Status) GetAll() map[string]DeviceStatus {
defer func() {
s.Lock.Unlock()
}()
s.Lock.Lock()
return s.Status
}
func (s *Status) Set(stat DeviceStatus) {
defer func() {
s.Lock.Unlock()
}()
s.Lock.Lock()
s.Status[stat.Device.Hostname] = stat
}