forked from cmaster11/overseer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_worker.go
1231 lines (1016 loc) · 32.9 KB
/
cmd_worker.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Worker
//
// The worker sub-command executes tests pulled from a central redis queue.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/cmaster11/overseer/parser"
"github.com/cmaster11/overseer/protocols"
"github.com/cmaster11/overseer/test"
"github.com/cmaster11/overseer/utils"
"github.com/go-redis/redis"
"github.com/google/subcommands"
"github.com/marpaia/graphite-golang"
_ "github.com/skx/golang-metrics"
)
// This is our structure, largely populated by command-line arguments
type workerCmd struct {
// How many parallel checks can we execute?
Parallel uint
// Should we run tests against IPv4 addresses?
IPv4 bool
// Should we run tests against IPv6 addresses?
IPv6 bool
// Should we retry failed tests a number of times to smooth failures?
Retry bool
// If we should retry failed tests, how many times before we give up?
RetryCount uint
// Prior to retrying a failed test how long should we pause?
RetryDelay time.Duration
// Default min duration
MinDuration time.Duration
// Default min duration cache lifetime factor
MinDurationCacheFactor uint
// Default deduplication duration
DedupDuration time.Duration
// The redis-host we're going to connect to for our queues.
RedisHost string
// The redis-database we're going to use.
RedisDB int
// The (optional) redis-password we'll use.
RedisPassword string
// The redis-sockt we're going to use. (If used, we ignore the specified host / port)
RedisSocket string
// Redis connection timeout
RedisDialTimeout time.Duration
// Tag applied to all results
Tag string
// How long should tests run for?
Timeout time.Duration
// Should the testing, and the tests, be verbose?
Verbose bool
// Default period test sleep, if not overridden by specific test setting
PeriodTestSleep time.Duration
// Default period test threshold percentage, if not overridden by specific test setting
PeriodTestThreshold float32
// The handle to our redis-server
_r *redis.Client
// The handle to our graphite-server
_g *graphite.Graphite
}
//
// Glue
//
func (*workerCmd) Name() string { return "worker" }
func (*workerCmd) Synopsis() string { return "Fetch jobs from the central queue and execute them" }
func (*workerCmd) Usage() string {
return `worker :
Execute tests pulled from the central redis queue, until terminated.
`
}
// MetricsFromEnvironment sets up a carbon connection from the environment
// if suitable values are found
func (p *workerCmd) MetricsFromEnvironment() {
//
// Get the hostname to connect to.
//
host := os.Getenv("METRICS_HOST")
if host == "" {
host = os.Getenv("METRICS")
}
// No host then we'll return
if host == "" {
return
}
// Split the into Host + Port
ho, pr, err := net.SplitHostPort(host)
if err != nil {
// If that failed we assume the port was missing
ho = host
pr = "2003"
}
// Setup the protocol to use
protocol := os.Getenv("METRICS_PROTOCOL")
if protocol == "" {
protocol = "udp"
}
// Ensure that the port is an integer
port, err := strconv.Atoi(pr)
if err == nil {
p._g, err = graphite.GraphiteFactory(protocol, ho, port, "")
if err != nil {
fmt.Printf("Error setting up metrics - skipping - %s\n", err.Error())
}
} else {
fmt.Printf("Error setting up metrics - failed to convert port to number - %s\n", err.Error())
}
}
// verbose shows a message only if we're running verbosely
func (p *workerCmd) verbose(txt string) {
if p.Verbose {
fmt.Print(txt)
}
}
//
// Flag setup.
//
func (p *workerCmd) SetFlags(f *flag.FlagSet) {
//
// Setup the default options here, these can be loaded/replaced
// via a configuration-file if it is present.
//
var defaults workerCmd
defaults.Parallel = uint(runtime.NumCPU())
defaults.IPv4 = true
defaults.IPv6 = true
defaults.Retry = true
defaults.RetryCount = 5
defaults.RetryDelay = 5 * time.Second
defaults.MinDuration = 0
defaults.MinDurationCacheFactor = 10
defaults.DedupDuration = 0
defaults.Tag = ""
defaults.Timeout = 10 * time.Second
defaults.Verbose = false
defaults.RedisHost = "localhost:6379"
defaults.RedisDB = 0
defaults.RedisPassword = ""
defaults.RedisDialTimeout = 5 * time.Second
defaults.PeriodTestSleep = 5 * time.Second
defaults.PeriodTestThreshold = 0
//
// If we have a configuration file then load it
//
if len(os.Getenv("OVERSEER")) > 0 {
cfg, err := ioutil.ReadFile(os.Getenv("OVERSEER"))
if err == nil {
err = json.Unmarshal(cfg, &defaults)
if err != nil {
fmt.Printf("WARNING: Error loading overseer.json - %s\n",
err.Error())
}
} else {
fmt.Printf("WARNING: Failed to read configuration-file - %s\n",
err.Error())
}
}
//
// Allow these defaults to be changed by command-line flags
//
// Worker
f.UintVar(&p.Parallel, "parallel", defaults.Parallel, "Number of parallel tests the worker can be handled at the same time.")
// Verbose
f.BoolVar(&p.Verbose, "verbose", defaults.Verbose, "Show more output.")
// Protocols
f.BoolVar(&p.IPv4, "4", defaults.IPv4, "Enable IPv4 tests.")
f.BoolVar(&p.IPv6, "6", defaults.IPv6, "Enable IPv6 tests.")
// Timeout
f.DurationVar(&p.Timeout, "timeout", defaults.Timeout, "The global timeout for all tests, in seconds.")
// Retry
f.BoolVar(&p.Retry, "retry", defaults.Retry, "Should failing tests be retried a few times before raising a notification.")
f.UintVar(&p.RetryCount, "retry-count", defaults.RetryCount, "How many times to retry a test, before regarding it as a failure.")
f.DurationVar(&p.RetryDelay, "retry-delay", defaults.RetryDelay, "The time to sleep between failing tests.")
f.DurationVar(&p.DedupDuration, "dedup", defaults.DedupDuration, "The maximum duration of a deduplication.")
f.DurationVar(&p.MinDuration, "min-duration", defaults.MinDuration, "The minimum duration of an error, for it to generate an alert.")
f.UintVar(&p.MinDurationCacheFactor, "min-duration-cache-factor", defaults.MinDurationCacheFactor,
"The lifetime factor for a min-duration error, for it to be reset (e.g. min-duration=2sec, min-duration-cache-factor=10 -> if an error is thrown after 20sec, it will be again considered like a first-time error).")
// Redis
f.StringVar(&p.RedisHost, "redis-host", defaults.RedisHost, "Specify the address of the redis queue.")
f.IntVar(&p.RedisDB, "redis-db", defaults.RedisDB, "Specify the database-number for redis.")
f.StringVar(&p.RedisPassword, "redis-pass", defaults.RedisPassword, "Specify the password for the redis queue.")
f.StringVar(&p.RedisSocket, "redis-socket", defaults.RedisSocket, "If set, will be used for the redis connections.")
f.DurationVar(&p.RedisDialTimeout, "redis-timeout", defaults.RedisDialTimeout, "Redis connection timeout.")
// Tag
f.StringVar(&p.Tag, "tag", defaults.Tag, "Specify the tag to add to all test-results.")
// Period test
f.DurationVar(&p.PeriodTestSleep, "period-test-sleep", defaults.PeriodTestSleep, "The sleeping interval between subsequent tests in a period-test.")
f.Var(utils.NewPercentageValue(defaults.PeriodTestThreshold, &p.PeriodTestThreshold), "period-test-threshold", "The percentage of failures need to trigger an alert in a period-test.")
}
// notify is used to store the result of a test in our redis queue.
func (p *workerCmd) notify(testDefinition test.Test, uniqueHash *string, resultError error, details *string) error {
//
// If we don't have a redis-server then return immediately.
//
// (This shouldn't happen, as without a redis-handle we can't
// fetch jobs to execute.)
//
if p._r == nil {
return nil
}
//
// The message we'll publish will be a JSON hash
//
testResult := &test.Result{
Input: testDefinition.Input,
Target: testDefinition.Target,
Time: time.Now().Unix(),
Type: testDefinition.Type,
Tag: p.Tag,
Details: details,
UniqueHash: uniqueHash,
TestLabel: testDefinition.TestLabel,
}
//
// Was the test result a failure? If so update the object
// to contain the failure-message, and record that it was
// a failure rather than the default pass.
//
if resultError != nil {
errorString := resultError.Error()
testResult.Error = &errorString
}
now := time.Now()
// If test has a min duration rule, avoid triggering a notification if not needed, or clean the min duration cache if needed.
if testDefinition.MinDuration != nil {
minDurationSeconds := int64(*testDefinition.MinDuration / time.Second)
// We need a minimum cache duration, otherwise the min duration test cannot work
if testDefinition.MinDurationCacheFactor == 0 {
testDefinition.MinDurationCacheFactor = 2
}
hash := testResult.Hash()
firstErrorTime := p.getMinDurationFirstErrorTime(hash)
testResult.FirstErrorTime = firstErrorTime
expireDuration := *testDefinition.MinDuration * time.Duration(testDefinition.MinDurationCacheFactor)
if testResult.Error != nil {
// With minDuration, we don't want to trigger the notification, unless the test has failed for a long enough amount of time
if firstErrorTime != nil {
diffFirstError := now.Unix() - *firstErrorTime
// We want to keep the ORIGINAL error time as alert shown, just extend the expiry time
p.setMinDurationFirstErrorTime(hash, *firstErrorTime, expireDuration)
if diffFirstError < minDurationSeconds {
// There is no need to trigger the notification, because not enough time has passed since the error got fired
p.verbose(fmt.Sprintf("Skipping notification (minDuration, alert shown %s ago) for test `%s` (%s)\n",
time.Duration(diffFirstError)*time.Second,
testDefinition.Input, testDefinition.Target))
return nil
}
// Let's show the alert!
// Mark it as shown
p.setMinDurationAlertShown(hash, true, expireDuration)
} else {
p.setMinDurationFirstErrorTime(hash, now.Unix(), expireDuration)
// Do not throw alert here, because this is the first generated alert
return nil
}
} else {
// Check if there was any min duration cache
if firstErrorTime != nil {
diffFirstError := now.Unix() - *firstErrorTime
alertShown := p.getMinDurationAlertShown(hash)
// Clear the cache because test got resolved
p.clearMinDurationFirstErrorTime(hash)
p.clearMinDurationAlertShown(hash)
testResult.Recovered = true
// If the error has never been triggered, because min duration did not expire, do not trigger a recovered message
if diffFirstError < minDurationSeconds {
p.verbose(fmt.Sprintf("Test recovered (min duration not met, skipping recovered message): `%s` (%s)\n",
testDefinition.Input, testDefinition.Target))
return nil
}
// Check if we want to show the recovered message
if !alertShown {
p.verbose(fmt.Sprintf("Test recovered (alert not shown, skipping recovered message): `%s` (%s)\n",
testDefinition.Input, testDefinition.Target))
return nil
}
p.verbose(fmt.Sprintf("Test recovered (min duration cache cleared, showing recovered message): `%s` (%s)\n",
testDefinition.Input, testDefinition.Target))
} else {
/*
If we're here, a recovered message got generated for an error that has never been seen.
Skip it, as it is probably a broken test.
*/
p.verbose(fmt.Sprintf("Test recovered (min duration not met, original error never seen): `%s` (%s)\n",
testDefinition.Input, testDefinition.Target))
return nil
}
}
}
// If test has a deduplication rule, avoid re-triggering a notification if not needed, or clean the dedup cache if needed.
if testDefinition.DedupDuration != nil {
hash := testResult.Hash()
if testResult.Error != nil {
// Save the current notification time, this keeps alive the deduplication. *10 so that it's not going to expire
// anytime soon.
p.setDeduplicationCacheTime(hash, *testDefinition.DedupDuration*10)
lastAlertTime := p.getDeduplicationLastAlertTime(hash)
// With dedup, we don't want to trigger same notification, unless we just passed the dedup duration
if lastAlertTime != nil {
diffLastAlert := now.Unix() - *lastAlertTime
dedupDurationSeconds := int64(*testDefinition.DedupDuration / time.Second)
if diffLastAlert < dedupDurationSeconds {
// There is no need to trigger the notification, because not enough time has passed since the last one
p.verbose(fmt.Sprintf("Skipping notification (dedup, last notif %s ago) for test `%s` (%s)\n",
time.Duration(diffLastAlert)*time.Second,
testDefinition.Input, testDefinition.Target))
return nil
}
// Let the user know that the generated notification is a duplicate
testResult.IsDedup = true
}
p.setDeduplicationLastAlertTime(hash, *testDefinition.DedupDuration*10)
} else {
// Check if a dedup was happening
dedupCacheTime := p.getDeduplicationCacheTime(hash)
// If there was a dedup cache time, we can mark this test as recovered
if dedupCacheTime != nil {
// Clear any dedup cache, because the test has passed
p.clearDeduplicationCacheTime(hash)
p.clearDeduplicationLastAlertTime(hash)
testResult.Recovered = true
p.verbose(fmt.Sprintf("Test recovered (dedup cache cleared): `%s` (%s)\n",
testDefinition.Input, testDefinition.Target))
}
}
}
//
// Convert the test result to a JSON string we can notify.
//
j, err := json.Marshal(testResult)
if err != nil {
fmt.Printf("Failed to encode test-result to JSON: %s", err.Error())
return err
}
//
// Publish the message to the queue.
//
_, err = p._r.RPush("overseer.results", j).Result()
if err != nil {
fmt.Printf("Result addition failed: %s\n", err)
return err
}
return nil
}
func (p *workerCmd) getDeduplicationCacheKey(hash string) string {
return fmt.Sprintf("overseer.dedup-cache.%s", hash)
}
func (p *workerCmd) getDeduplicationCacheTime(hash string) *int64 {
if p._r == nil {
return nil
}
cacheKey := p.getDeduplicationCacheKey(hash)
cacheTime, err := p._r.Get(cacheKey).Int64()
if err != nil {
if err == redis.Nil {
// Key just does not exist
return nil
}
fmt.Printf("Failed to get dedup cache key: %s\n", err)
return nil
}
return &cacheTime
}
func (p *workerCmd) setDeduplicationCacheTime(hash string, expiry time.Duration) {
if p._r == nil {
return
}
cacheKey := p.getDeduplicationCacheKey(hash)
_, err := p._r.Set(cacheKey, time.Now().Unix(), expiry).Result()
if err != nil {
fmt.Printf("Failed to set dedup cache key: %s\n", err)
}
}
func (p *workerCmd) clearDeduplicationCacheTime(hash string) {
if p._r == nil {
return
}
cacheKey := p.getDeduplicationCacheKey(hash)
_, err := p._r.Del(cacheKey).Result()
if err != nil {
fmt.Printf("Failed to clear dedup cache key: %s\n", err)
}
}
func (p *workerCmd) getDeduplicationLastAlertKey(hash string) string {
return fmt.Sprintf("overseer.dedup-last-alert.%s", hash)
}
func (p *workerCmd) getDeduplicationLastAlertTime(hash string) *int64 {
if p._r == nil {
return nil
}
cacheKey := p.getDeduplicationLastAlertKey(hash)
cacheTime, err := p._r.Get(cacheKey).Int64()
if err != nil {
if err == redis.Nil {
// Key just does not exist
return nil
}
fmt.Printf("Failed to get dedup last alert key: %s\n", err)
return nil
}
return &cacheTime
}
func (p *workerCmd) setDeduplicationLastAlertTime(hash string, expiry time.Duration) {
if p._r == nil {
return
}
cacheKey := p.getDeduplicationLastAlertKey(hash)
_, err := p._r.Set(cacheKey, time.Now().Unix(), expiry).Result()
if err != nil {
fmt.Printf("Failed to set dedup last alert key: %s\n", err)
}
}
func (p *workerCmd) clearDeduplicationLastAlertTime(hash string) {
if p._r == nil {
return
}
cacheKey := p.getDeduplicationLastAlertKey(hash)
_, err := p._r.Del(cacheKey).Result()
if err != nil {
fmt.Printf("Failed to clear dedup last alert key: %s\n", err)
}
}
func (p *workerCmd) getMinDurationFirstErrorKey(hash string) string {
return fmt.Sprintf("overseer.min-duration-first-error.%s", hash)
}
func (p *workerCmd) getMinDurationFirstErrorTime(hash string) *int64 {
if p._r == nil {
return nil
}
cacheKey := p.getMinDurationFirstErrorKey(hash)
cacheTime, err := p._r.Get(cacheKey).Int64()
if err != nil {
if err == redis.Nil {
// Key just does not exist
return nil
}
fmt.Printf("Failed to get min-duration alert shown key: %s\n", err)
return nil
}
return &cacheTime
}
func (p *workerCmd) setMinDurationFirstErrorTime(hash string, errorTime int64, expiry time.Duration) {
if p._r == nil {
return
}
cacheKey := p.getMinDurationFirstErrorKey(hash)
_, err := p._r.Set(cacheKey, errorTime, expiry).Result()
if err != nil {
fmt.Printf("Failed to set min-duration alert shown key: %s\n", err)
}
}
func (p *workerCmd) clearMinDurationFirstErrorTime(hash string) {
if p._r == nil {
return
}
cacheKey := p.getMinDurationFirstErrorKey(hash)
_, err := p._r.Del(cacheKey).Result()
if err != nil {
fmt.Printf("Failed to clear min-duration alert shown key: %s\n", err)
}
}
func (p *workerCmd) getMinDurationAlertShownKey(hash string) string {
return fmt.Sprintf("overseer.min-duration-alert-shown.%s", hash)
}
func (p *workerCmd) getMinDurationAlertShown(hash string) bool {
if p._r == nil {
return false
}
cacheKey := p.getMinDurationAlertShownKey(hash)
alertShown, err := p._r.Get(cacheKey).Int64()
if err != nil {
if err == redis.Nil {
// Key just does not exist
return false
}
fmt.Printf("Failed to get min-duration alert shown key: %s\n", err)
return false
}
return alertShown > 0
}
func (p *workerCmd) setMinDurationAlertShown(hash string, shown bool, expiry time.Duration) {
if p._r == nil {
return
}
alertShown := 0
if shown {
alertShown = 1
}
cacheKey := p.getMinDurationAlertShownKey(hash)
_, err := p._r.Set(cacheKey, alertShown, expiry).Result()
if err != nil {
fmt.Printf("Failed to set min-duration alert shown key: %s\n", err)
}
}
func (p *workerCmd) clearMinDurationAlertShown(hash string) {
if p._r == nil {
return
}
cacheKey := p.getMinDurationAlertShownKey(hash)
_, err := p._r.Del(cacheKey).Result()
if err != nil {
fmt.Printf("Failed to clear min-duration alert shown key: %s\n", err)
}
}
// alphaNumeric removes all non alpha-numeric characters from the
// given string, and returns it. We replace the characters that
// are invalid with `_`.
func (p *workerCmd) alphaNumeric(input string) string {
//
// Remove non alphanumeric
//
reg, err := regexp.Compile("[^A-Za-z0-9]+")
if err != nil {
panic(err)
}
return reg.ReplaceAllString(input, "_")
}
// formatMetrics Format a test for metrics submission.
//
// This is a little weird because ideally we'd want to submit to the
// metrics-host :
//
// overseer.$testType.$testTarget.$key => value
//
// But of course the target might not be what we think it is for all
// cases - i.e. A DNS test the target is the name of the nameserver rather
// than the thing to lookup, which is the natural target.
//
func (p *workerCmd) formatMetrics(tst test.Test, key string) string {
prefix := "overseer.test."
//
// Special-case for the DNS-test
//
if tst.Type == "dns" {
return prefix + ".dns." + p.alphaNumeric(tst.Arguments["lookup"]) + "." + key
}
//
// Otherwise we have a normal test.
//
return prefix + tst.Type + "." + p.alphaNumeric(tst.Target) + "." + key
}
// runTest is really the core of our application, as it is responsible
// for receiving a test to execute, executing it, and then issuing
// the notification with the result.
func (p *workerCmd) runTest(workerIdx uint, tst test.Test, opts test.Options) error {
workerPrefix := fmt.Sprintf("[W%d] ", workerIdx)
// Create a map for metric-recording.
metricsLock := new(sync.Mutex)
metrics := map[string]string{}
// If there are no deduplication rules, assign the default worker one. Unless the test is a period-test
if tst.DedupDuration == nil && tst.PeriodTestDuration == nil && p.DedupDuration > 0 {
// Assign a default dedup duration
tst.DedupDuration = &p.DedupDuration
}
// If there are no min duration rules, assign the default worker one. Unless the test is a period-test
if tst.MinDuration == nil && tst.PeriodTestDuration == nil && p.MinDuration > 0 {
// Assign a default min duration
tst.MinDuration = &p.MinDuration
}
// If there are no min duration cache rules, assign the default worker one. Unless the test is a period-test
if tst.MinDurationCacheFactor == 0 && tst.PeriodTestDuration == nil && p.MinDurationCacheFactor > 0 {
// Assign a default min duration
tst.MinDurationCacheFactor = p.MinDurationCacheFactor
}
//
// Setup our local state.
//
testType := tst.Type
testTarget := tst.Target
//
// Look for a suitable protocol handler
//
tmp := protocols.ProtocolHandler(testType)
//
// Each test will be executed for each address-family, so we need to
// keep track of the IPs of the real test-target.
//
var targets []string
// If we're dealing with hostname-based testing, then resolve hostnames
if tmp.ShouldResolveHostname() {
//
// If the first argument looks like an URI then get the host
// out of it.
//
if strings.Contains(testTarget, "://") {
u, err := url.Parse(testTarget)
if err != nil {
return err
}
testTarget = u.Hostname()
}
// Record the time before we lookup our targets IPs.
timeA := time.Now()
// Now resolve the target to IPv4 & IPv6 addresses.
ips, err := net.LookupIP(testTarget)
if err != nil {
//
// We failed to resolve the target, so we have to raise
// a failure. But before we do that we need to sanitize
// the test.
//
tst.Input = tst.Sanitize()
//
// Notify the world about our DNS-failure.
//
p.notify(tst, nil, fmt.Errorf("failed to resolve name %s", testTarget), nil)
//
// Otherwise we're done.
//
fmt.Printf(workerPrefix+"WARNING: Failed to resolve %s for %s test!\n", testTarget, testType)
return err
}
// Calculate the time the DNS-resolution took - in milliseconds.
timeB := time.Now()
duration := timeB.Sub(timeA)
diff := fmt.Sprintf("%f", float64(duration)/float64(time.Millisecond))
// Record time in our metric hash
metricsLock.Lock()
metrics["overseer.dns."+p.alphaNumeric(testTarget)+".duration"] = diff
metricsLock.Unlock()
//
// We'll run the test against each of the resulting IPv4 and
// IPv6 addresess - ignoring any IP-protocol which is disabled.
//
// Save the results in our `targets` array, unless disabled.
//
for _, ip := range ips {
if ip.To4() != nil {
if p.IPv4 {
targets = append(targets, ip.String())
}
}
if ip.To16() != nil && ip.To4() == nil {
if p.IPv6 {
targets = append(targets, ip.String())
}
}
}
} else {
// Directly pass the original target
targets = append(targets, testTarget)
}
if tst.MaxTargetsCount > 0 && len(targets) > tst.MaxTargetsCount {
// By sorting we have a higher chance of targeting the same target
sort.Strings(targets)
targets = targets[:tst.MaxTargetsCount]
}
testEndFn := func(startTime time.Time, target string, attempts uint, result error, details *string) {
//
// Now the test is complete we can record the time it
// took to carry out, and the number of attempts it
// took to complete.
//
timeB := time.Now()
duration := timeB.Sub(startTime)
diff := fmt.Sprintf("%f", float64(duration)/float64(time.Millisecond))
metricsLock.Lock()
metrics[p.formatMetrics(tst, "duration")] = diff
metrics[p.formatMetrics(tst, "attempts")] = fmt.Sprintf("%d", attempts)
metricsLock.Unlock()
//
// Post the result of the test to the notifier.
//
// Before we trigger the notification we need to
// update the target to the thing we probed, which might
// not necessarily be that which was originally submitted.
//
// i.e. "mail.steve.org.uk must run ssh" might become
// "1.2.3.4 must run ssh" as a result of the DNS lookup.
//
// However because we might run the same test against
// multiple hosts we need to do this with a copy so that
// we don't lose the original target.
//
tstCopy := tst
tstCopy.Target = target
//
// We also want to filter out any password which was found
// on the input-line.
//
tstCopy.Input = tst.Sanitize()
//
// Now we can trigger the notification with our updated
// copy of the test.
//
p.notify(tstCopy, tmp.GetUniqueHashForTest(tstCopy, opts), result, details)
}
wg := &sync.WaitGroup{}
//
// Now for each target, run the test.
//
for _, target := range targets {
wg.Add(1)
go func() {
// Is this a period test?
if tst.PeriodTestDuration != nil {
periodTestDuration := *tst.PeriodTestDuration
periodTestSleep := tst.PeriodTestSleep
if periodTestSleep == 0 {
periodTestSleep = p.PeriodTestSleep
}
periodTestThreshold := p.PeriodTestThreshold
if tst.PeriodTestThreshold != nil {
periodTestThreshold = *tst.PeriodTestThreshold
}
p.verbose(fmt.Sprintf(workerPrefix+"Running '%s' period-test (duration: %s, sleep: %s, threshold: %.0f%%) against %s (%s)\n", testType, periodTestDuration, periodTestSleep, periodTestThreshold, testTarget, target))
// Start time
timeStart := time.Now()
timeEnd := timeStart.Add(periodTestDuration)
var countSuccess uint = 0
var countFail uint = 0
iteration := 0
var errorStrings []string
for time.Now().Before(timeEnd) {
iteration++
iterationStartTime := time.Now()
// Copy opts
currentOpts := opts
currentOpts.PeriodTestIndex = iteration
currentOpts.PeriodTestStartTime = iterationStartTime.UnixNano() / int64(time.Millisecond)
err := tmp.RunTest(tst, target, currentOpts)
iterationDuration := time.Since(iterationStartTime)
iterationElapsedString := fmt.Sprintf("%.2fms", float64(iterationDuration)/float64(time.Millisecond))
if err != nil {
countFail++
p.verbose(fmt.Sprintf(workerPrefix+"Period-test (test %d failed, took %s): %s\n", iteration, iterationElapsedString, err.Error()))
errString := fmt.Sprintf("test %d failed, took %s: %s", iteration, iterationElapsedString, err.Error())
errorStrings = append(errorStrings, errString)
} else {
countSuccess++
p.verbose(fmt.Sprintf(workerPrefix+"Period-test (test %d success, took %s)\n", iteration, iterationElapsedString))
}
time.Sleep(periodTestSleep)
}
totalAttempts := countFail + countSuccess
errPercentage := float32(countFail) / float32(totalAttempts)
var result error
var failuresString *string
if len(errorStrings) > 0 {
var lines []string
for _, errorString := range errorStrings {
lines = append(lines, fmt.Sprintf("- %s", errorString))
}
output := fmt.Sprintf("Period-test errors:\n%s", strings.Join(lines, "\n"))
failuresString = &output
}
if errPercentage > periodTestThreshold {
result = fmt.Errorf("%d tests failed out of %d (%.2f%%)", countFail, totalAttempts, errPercentage*100)
p.verbose(fmt.Sprintf(workerPrefix+"Test failed: %s\n", result.Error()))
} else {
p.verbose(fmt.Sprintf(workerPrefix+"Test passed: %d tests failed out of %d (%.2f%%)\n", countFail, totalAttempts, errPercentage*100))
}
testEndFn(timeStart, target, totalAttempts, result, failuresString)
wg.Done()
return
}
p.verbose(fmt.Sprintf(workerPrefix+"Running '%s' test against %s (%s)\n", testType, testTarget, target))
//
// We'll repeat failing tests up to five times by default
//
var attempt uint = 0
var maxAttempts uint = p.RetryCount
//
// If retrying is disabled then don't retry.
//
if !p.Retry {
maxAttempts = attempt + 1
}
if tst.MaxRetries != nil {
maxAttempts = *tst.MaxRetries + 1
}
//
// The result of the test.
//
var result error
//
// Record the start-time of the test.
//
timeA := time.Now()
//
// Start the count here for graphing execution attempts.
//
var c uint = 0
//
// Prepare to repeat the test.
//
// We only repeat tests that fail, if the test passes then
// it will only be executed once.
//
// This is designed to cope with transient failures, at a
// cost that flapping services might be missed.
//
for attempt < maxAttempts {
attempt++
c++
//
// Run the test
//
result = tmp.RunTest(tst, target, opts)
//
// If the test passed then we're good.
//
if result == nil {
p.verbose(fmt.Sprintf(workerPrefix+"[%d/%d] - Test passed.\n", attempt, maxAttempts))
// break out of loop