-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsystestmon.py
1330 lines (1214 loc) · 66 KB
/
systestmon.py
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
import argparse
import json
import logging
import re
from datetime import datetime, timedelta
from threading import Thread
import httplib2
import traceback
import socket
import time
import base64
import os
import ast
import zipfile
from couchbase.cluster import Cluster, PasswordAuthenticator
import paramiko
from collections import Mapping, Sequence, Set, deque
from scp import SCPClient
class Globals(object):
logger = logging.getLogger("systestmon")
timestamp = str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))
sdk_client = None
class SDKClient(object):
def __init__(self, cb_host):
try:
self.cluster = self.get_cluster(cb_host)
self.bucket = self.get_bucket("system_test_dashboard")
except Exception as e:
print("SDK WARNING: %s" % e)
def append_list(self, key, value):
try:
self.bucket.list_append(key, value, create=True)
except Exception:
self.bucket.default_collection().list_append(key, value, create=True)
def store_results(self, msg_sub, msg_content):
build_id = os.getenv("BUILD_NUMBER")
if build_id is not None:
key = "log_parser_results_" + build_id
self.append_list(key, msg_sub + "\n" + msg_content)
def get_cluster(self, cb_host):
try:
cluster = Cluster('couchbase://{}'.format(cb_host))
authenticator = PasswordAuthenticator('Administrator', 'password')
cluster.authenticate(authenticator)
return cluster
except Exception:
from couchbase.cluster import ClusterOptions
cluster = Cluster(
'couchbase://{}'.format(cb_host),
ClusterOptions(PasswordAuthenticator('Administrator',
'password')))
return cluster
def get_bucket(self, name):
try:
return self.cluster.open_bucket(name)
except Exception:
return self.cluster.bucket(name)
class Configuration(object):
# Input map of keywords to be mined for in the logs
configuration = [
{
"component": "memcached",
"logfiles": "babysitter.log*",
"services": "all",
"keywords": ["exception occurred in runloop",
"failover exited with reason",
"Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\["],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "memcached",
"logfiles": "memcached.log.*",
"services": "all",
"keywords": ["CRITICAL", "Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\[",
"exception occurred in runloop",
"Invalid packet header detected"],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "index",
"logfiles": "indexer.log*",
"services": "index",
"keywords": ["panic", "fatal", "Error parsing XATTR", "zero", "protobuf.Error", "Encounter planner error",
"corruption", "processFlushAbort", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\[",
"Failed to initialize metadata provider", "found missing page", "invalid last page",
"Storage corrupted and unrecoverable", "ensureMonotonicTs Align seqno smaller than lastFlushTs",
"invalid length of composite element filters in scan request",
"Internal error while creating new scan request", "StorageMgr::handleCreateSnapshot Disk commit timestamp is not snapshot aligned",
"ReplicaViolation", "ExcludeNodeViolation", "EquivIndexViolation", "ServerGroupViolation", "DeleteNodeViolation",
"NoViolation", "MemoryViolation", "NumVbs out of valid range", "Skipped disk snapshot cleanup",
"Unable to find recovery point. Resetting store"],
"ignore_keywords": ["fatal remote"],
"check_stats_api": True,
"stats_api_list": ["stats/storage", "stats"],
"port": "9102",
"collect_dumps": True
},
{
"component": "analytics",
"logfiles": "analytics_error*",
"services": "cbas",
"keywords": ["fata", "Analytics Service is temporarily unavailable", "Failed during startup task", "HYR0",
"ASX", "IllegalStateException", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\[", "panic", "LEAK: ByteBuf.release() was not called",
"failed to migrate metadata partition", "Internal error", "CBAS\[number\]", "IllegalReferenceCountException", "ArrayIndexOutOfBoundsException",
"ACIDException", "MetadataException", "OverflowException", "UnderflowException", "NullPointerException", "StackOverflowException", "Failure closing a closeable resource"],
"ignore_keywords": ["HYR0010","HYR0115","ASX3110","HYR0114"],
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "eventing",
"logfiles": "eventing.log*",
"services": "eventing",
"keywords": ["panic", "fatal", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\[", "Minidump"],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "fts",
"logfiles": "fts.log*",
"services": "fts",
"keywords": ["panic", "fatal", "authPassword", "\[ERRO\]", "Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\["],
"ignore_keywords": ["Fatal:false", "use of closed network connection",
"Reschedule failed, failing request", "TLS handshake error", "cannot unmarshal object",
"bleve.Index is not copyable"],
"check_stats_api": True,
"stats_api_list": ["api/stats"],
"port": "8094",
"collect_dumps": True
},
{
"component": "xdcr",
"logfiles": "*xdcr*.log*",
"services": "kv",
"keywords": ["Failed on calling", "panic", "fatal", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\[",
"non-recoverable error from xmem client", "Unable to respond to caller",
"Unable to generate req or resp", "error when making rest call or unmarshalling data",
"unable to find last known target manifest version", "net/http: request canceled",
"has payloadCompressed but no payload after deserialization",
"Error converting VBTask to DCP Nozzle Task",
"Xmem is stuck"],
"ignore_keywords": ["Errors\": \"[]"],
"check_stats_api": False,
"collect_dumps": False,
"outgoing_mutations_threshold": 1000000
},
{
"component": "projector",
"logfiles": "projector.log*",
"services": "kv",
"keywords": ["panic", "Error parsing XATTR", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\[", "seq order violation"],
#"keywords": ["panic", "Error parsing XATTR", "Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\["],
"ignore_keywords": None,
"check_stats_api": False,
"port": "9999",
"collect_dumps": True
},
{
"component": "rebalance",
"logfiles": "error.log*",
"services": "all",
"keywords": ["rebalance exited", "failover exited with reason", "Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\[","Join completion call failed"],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "crash",
"logfiles": "info.log*",
"services": "all",
"keywords": ["exited with status", "failover exited with reason", "Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\["],
"ignore_keywords": ["exited with status 0"],
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "query",
"logfiles": "query.log*",
"services": "n1ql",
"keywords": ["panic", "fatal", "Encounter planner error", "Basic\s[a-zA-Z]\{10,\}",
"Menelaus-Auth-User:\[", "invalid byte in chunk length", "Minidump"],
"ignore_keywords": ["not available"],
"check_stats_api": False,
"collect_dumps": True,
"port": "8093"
},
{
"component": "autofailover",
"logfiles": "info.log*",
"services": "all",
"keywords": ["due to operation being unsafe for service index"],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": False
},
{
"component": "backup",
"logfiles": "backup_service.log*",
"services": "backup",
"keywords": ["panic", "fatal", "warn", "Failed Task",
"Basic\s[a-zA-Z]\{10,\}", "Menelaus-Auth-User:\["],
"ignore_keywords": None,
"check_stats_api": False,
"collect_dumps": True,
"port": "8097"
}
]
# Frequency of scanning the logs in seconds
scan_interval = 3600
# Level of memory usage after which alert should be raised
mem_threshold = 90
# Level of CPU usage after which alert should be raised
cpu_threshold = 90
ignore_list = ["Port exited with status 0", "Fatal:false",
"HyracksDataException: HYR0115: Local network error",
"No such file or directory"]
class ScriptConfig(object):
print_all_logs = False
should_collect_dumps = False
cbcollect_on_high_mem_cpu_usage = False
scan_xdcr_destination = False
docker_host = None
email_recipients = ""
state_file_dir = ""
class CBCluster(object):
def __init__(self, cluster_name, master_node, rest_username, rest_password,
ssh_username, ssh_password):
self.cluster_name = cluster_name
self.master_node = master_node
self.rest_username = rest_username
self.rest_password = rest_password
self.ssh_username = ssh_username
self.ssh_password = ssh_password
class SysTestMon(object):
# 1. Get service map for cluster
# 2. For each component, get nodes for the requested service
# 3. SSH to those nodes, grep for specified keywords in specified files
# 4. Reporting
def __init__(self, cluster, run_infinite, start_itr=1):
self.logger = Globals.logger
self.cluster = cluster
self.cb_version = ""
self.run_infinite = run_infinite
self.state_file = "{}/eagle-eye_{}.state" \
.format(ScriptConfig.state_file_dir, cluster.master_node)
self.keyword_counts = dict()
self.keyword_counts["timestamp"] = Globals.timestamp
self.dump_dir_name = ""
self.docker_logs_dump = ""
self.iter_count = start_itr
self.token = None
@staticmethod
def append_msg_string(msg_text, file_text, msg_to_append):
truncated = False
file_text = file_text + msg_to_append
msg_to_append = msg_to_append.split("\n")
if len(msg_to_append) > 11:
truncated = True
msg_to_append = msg_to_append[:10]
msg_to_append = "\n".join(msg_to_append)
msg_to_append += "...[truncated]\n" if truncated else "\n"
msg_text = msg_text + msg_to_append
return msg_text, file_text
def run(self):
self.wait_for_cluster_init(self.cluster.master_node)
last_scan_timestamp = ""
xdcr_monitor_threads = list()
try:
cmd = "/opt/couchbase/bin/couchbase-server --version"
_, self.cb_version, std_err = self.execute_command(
cmd, self.cluster.master_node, self.cluster.ssh_username, self.cluster.ssh_password)
except Exception:
pass
while True:
msg_sub = ""
msg_content = ""
file_content = ""
should_cbcollect = False
prev_keyword_counts = None
self.dump_dir_name = "dump_collected_{}".format(self.iter_count)
# Used to identify the current itr uniquely
self.token = int(time.time())
if os.path.exists(self.state_file):
s = open(self.state_file, 'r').read()
prev_keyword_counts = ast.literal_eval(s)
last_scan_timestamp = datetime.strptime(
prev_keyword_counts["last_scan_timestamp"],
"%Y-%m-%d %H:%M:%S.%f")
if not os.path.isdir(self.dump_dir_name):
os.mkdir(self.dump_dir_name)
node_map = self.get_services_map(self.cluster.master_node,
self.cluster.rest_username,
self.cluster.rest_password)
if not node_map:
continue
if ScriptConfig.scan_xdcr_destination:
for x_index, xdcr_ip in enumerate(self.get_xdcr_dest(self.cluster.master_node)):
self.logger.info("Starting XDCR log collection for {}"
.format(xdcr_ip))
xdcr_cluster = CBCluster(
"xdcr_cluster_%s" % x_index, xdcr_ip,
self.cluster.rest_username, self.cluster.rest_password,
self.cluster.ssh_username, self.cluster.ssh_password)
t_sysmon_obj = SysTestMon(xdcr_cluster, False,
start_itr=self.iter_count)
t_thread = Thread(name="XDCR_{}_thread".format(xdcr_ip),
target=t_sysmon_obj.run)
xdcr_monitor_threads.append(t_thread)
t_thread.start()
for component in Configuration.configuration:
nodes = self.find_nodes_with_service(node_map,
component["services"])
self.logger.info("{} ({}) - Nodes with {} service : {}"
.format(self.cluster.master_node,
self.cluster.cluster_name,
component["services"], str(nodes)))
for keyword in component["keywords"]:
key = component["component"] + "_" + keyword
self.logger.info(
"{} ({}) - Parsing for component: {}, looking for '{}'"
.format(self.cluster.master_node,
self.cluster.cluster_name,
component["component"], keyword))
total_occurrences = 0
for node in nodes:
if component["ignore_keywords"]:
command = "zgrep -i \"{0}\" /opt/couchbase/var/lib/couchbase/logs/{1} | grep -vE \"{2}\"".format(
keyword, component["logfiles"], "|".join(component["ignore_keywords"]))
else:
command = "zgrep -i \"{0}\" /opt/couchbase/var/lib/couchbase/logs/{1}".format(
keyword, component["logfiles"])
occurences = 0
try:
occurences, output, std_err = self.execute_command(
command, node, self.cluster.ssh_username,
self.cluster.ssh_password)
except Exception as e:
self.logger.info("Exception {0}".format(e))
txt = '\n\n%s: %s\n\n ' \
'Found an exception: %s' \
% (node, component["component"], e)
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
if occurences > 0:
self.logger.warning(
"{} - {} occurrences of keyword '{}' found"
.format(node, occurences, keyword))
txt = "\n\n%s: %s" % (node, component["component"])
if ScriptConfig.print_all_logs \
or last_scan_timestamp == "":
try:
self.logger.debug('\n'.join(output))
txt += "\n%s" % ('\n'.join(output))
except UnicodeDecodeError as e:
self.logger.warning(str(e))
txt += "\n%s" % ('\n'.join(output)
.decode("utf-8"))
else:
txt = self.print_output(output, last_scan_timestamp, txt)
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
total_occurrences += occurences
self.keyword_counts[key] = total_occurrences
if prev_keyword_counts is not None \
and key in prev_keyword_counts.keys():
if total_occurrences > int(prev_keyword_counts[key]):
self.logger.warning(
"There have been more occurences of keyword {0} "
"in the logs since the last iteration. Hence "
"performing a cbcollect.".format(keyword))
should_cbcollect = True
else:
if total_occurrences > 0:
should_cbcollect = True
for node in nodes:
if component["check_stats_api"]:
txt = '\n\n%s: %s' % (node, component["component"])
try:
fin_neg_stat, txt = self.check_stats_api(node, component, txt)
if fin_neg_stat.__len__() != 0:
should_cbcollect = True
except Exception as e:
self.logger.info("Found an exception {0}".format(e))
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
if component["collect_dumps"] and ScriptConfig.should_collect_dumps:
txt = '\n\n%s: %s collecting dumps' % (node, component["component"])
try:
txt = self.collect_dumps(node, component, txt)
except Exception as e:
self.logger.info("Found an exception {0}".format(e))
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
# Check if all n1ql nodes are healthy
if component["component"] == "query":
n1ql_nodes = self.find_nodes_with_service(node_map, "n1ql")
if n1ql_nodes:
# Check to make sure all nodes are healthy
self.logger.info("Checking query nodes for health")
should_collect, message = self.check_nodes_healthy(
nodes=nodes, component=component,
rest_username=self.cluster.rest_username,
rest_password=self.cluster.rest_password,
ssh_username=self.cluster.ssh_username,
ssh_password=self.cluster.ssh_password)
if should_collect:
should_cbcollect = True
if not message == '':
txt = '\n\n%s: %s\n\n%s\n' \
% (node, component["component"], message)
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
# Check system:completed_requests for errors
self.logger.info("Checking system:completed requests for errors")
should_collect, message = self.check_completed_requests(
nodes=nodes, component=component,
rest_username=self.cluster.rest_username,
rest_password=self.cluster.rest_password,
ssh_username=self.cluster.ssh_username,
ssh_password=self.cluster.ssh_password)
if should_collect:
should_cbcollect = True
if not message == '':
txt = '\n\n%s: %s\n\n%s\n' \
% (node, component["component"], message)
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
# Check active_requests to make sure that are no more than 1k active requests at a single time
self.logger.info("Checking system:active requests for too many requests")
should_collect, message = self.check_active_requests(
nodes=nodes, component=component,
rest_username=self.cluster.rest_username,
rest_password=self.cluster.rest_password,
ssh_username=self.cluster.ssh_username,
ssh_password=self.cluster.ssh_password)
if should_collect:
should_cbcollect = True
if not message == '':
txt = '\n\n%s: %s\n\n%s\n' \
% (node, component["component"], message)
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
# # Check if XDCR outgoing mutations in the past hour > threshold
# if component["component"] == "xdcr":
# threshold = component["outgoing_mutations_threshold"]
# src_buckets = self.get_xdcr_src_buckets(master_node)
# for src_bucket in src_buckets:
# bucket_stats = self.fetch_bucket_xdcr_stats(master_node, src_bucket)['op']['samples'][
# 'replication_changes_left'][-60:]
# if all(stat > threshold for stat in bucket_stats):
# self.logger.warn(
# "XDCR outgoing mutations in the past hour on {0}\n{1} > {2}".format(
# src_bucket,
# bucket_stats,
# threshold))
# should_cbcollect = True
# Check for health of all nodes
for node in node_map:
if node["memUsage"] > Configuration.mem_threshold:
self.logger.warning(
"***** ALERT : Memory usage on {0} is very high : {1}%"
.format(node["hostname"], node["memUsage"]))
# if cbcollect_on_high_mem_cpu_usage:
# should_cbcollect = True
if node["cpuUsage"] > Configuration.cpu_threshold:
self.logger.warning(
"***** ALERT : CPU usage on {0} is very high : {1}%"
.format(node["hostname"], node["cpuUsage"]))
# if cbcollect_on_high_mem_cpu_usage:
# should_cbcollect = True
if node["status"] != "healthy":
self.logger.warning(
"***** ALERT: {0} is not healthy. Current status: {1}%"
.format(node["hostname"], node["status"]))
if ScriptConfig.cbcollect_on_high_mem_cpu_usage:
should_cbcollect = True
# Check NTP status
# command = "timedatectl status | grep NTP"
# self.record_command_output(node["hostname"], command)
# Check disk usage
command = "df -kh /data"
self.record_command_output(node["hostname"], command)
last_scan_timestamp = datetime.now() - timedelta(minutes=10.0)
self.logger.info("Last scan timestamp:" + str(last_scan_timestamp))
self.keyword_counts["last_scan_timestamp"] = str(last_scan_timestamp)
if ScriptConfig.docker_host is not None:
try:
command = "docker ps -q | xargs docker inspect --format {{.LogPath}}"
occurences, output, std_err = self.execute_command(
command, ScriptConfig.docker_host,
self.cluster.ssh_username, self.cluster.ssh_password)
self.docker_logs_dump = "docker_dump_collected_" + str(self.iter_count) \
+ "_" + str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))
os.mkdir(self.docker_logs_dump)
ssh_client = self.get_ssh_client(
ScriptConfig.docker_host, self.cluster.ssh_username,
self.cluster.ssh_password)
for file in output:
with SCPClient(ssh_client.get_transport()) as scp:
scp.get(file, local_path=self.docker_logs_dump)
docker_logs_location = "{0}/{1}".format(os.getcwd(), self.docker_logs_dump)
txt = "\n\nDocker logs collected at: %s" % docker_logs_location
msg_content, file_content = \
self.append_msg_string(msg_content,
file_content, txt)
self.logger.info("Collecting all docker logs completed. "
"Docker logs at : {0}"
.format(docker_logs_location))
except Exception as e:
self.logger.info("Could not collect docker logs: %s" % e)
collected = False
cbcollect_urls = ""
start_time = time.time()
time_out_val = start_time + 3600
while should_cbcollect and not collected \
and time.time() < time_out_val:
self.logger.info("{} ({}) :: Running cbcollect_info"
.format(self.cluster.master_node,
self.cluster.cluster_name))
command = \
"/opt/couchbase/bin/couchbase-cli collect-logs-start " \
"-c {0} -u {1} -p {2} --all-nodes --upload " \
"--upload-host cb-jira.s3.us-east-2.amazonaws.com/logs " \
"--customer systestmon-iteration-{3}-{4}"\
.format(self.cluster.master_node,
self.cluster.rest_username,
self.cluster.rest_password, self.iter_count, self.token)
_, cbcollect_output, std_err = self.execute_command(
command, self.cluster.master_node,
self.cluster.ssh_username, self.cluster.ssh_password)
if std_err:
self.logger.error("Error while running cbcollect_info")
self.logger.info(std_err)
else:
for i in range(len(cbcollect_output)):
self.logger.info(cbcollect_output[i])
while True:
command = "/opt/couchbase/bin/couchbase-cli " \
"collect-logs-status -c {0} -u {1} -p {2}"\
.format(self.cluster.master_node,
self.cluster.rest_username,
self.cluster.rest_password)
_, cbcollect_output, std_err = self.execute_command(
command, self.cluster.master_node,
self.cluster.ssh_username, self.cluster.ssh_password)
if std_err:
self.logger.error("cbcollect_info error: %s" % std_err)
break
else:
if cbcollect_output[0] == "Status: running":
time.sleep(60)
elif cbcollect_output[0] == "Status: completed":
collected = True
cbcollect_upload_paths = list()
for line in cbcollect_output:
if "url :" in line:
cbcollect_upload_paths.append(line)
cbcollect_urls = "\nCbcollect logs:\n\n%s" \
% ('\n'.join(cbcollect_upload_paths))
self.logger.info(cbcollect_urls)
break
elif cbcollect_output[0] == "Status: cancelled":
collected = False
cbcollect_upload_paths = list()
for line in cbcollect_output:
if "url :" in line:
cbcollect_upload_paths.append(line)
cbcollect_urls = "\nCbcollect logs:\n\n%s" \
% ('\n'.join(cbcollect_upload_paths))
self.logger.info(cbcollect_urls)
break
else:
self.logger.error("Issue with cbcollect: %s"
% cbcollect_output)
break
self.update_state_file()
txt = "{} ({}) : {} Log scan iteration number {} complete" \
.format(self.cluster.master_node,
self.cluster.cluster_name, self.cb_version,
self.iter_count)
msg_sub = msg_sub.join(txt)
if should_cbcollect:
try:
msg_content += cbcollect_urls
file_content += cbcollect_urls
self.send_email(msg_sub, ScriptConfig.email_recipients,
msg_content, file_content)
except Exception as e:
self.logger.critical("Send mail exception: %s" % e)
try:
Globals.sdk_client.store_results(msg_sub, msg_content)
except Exception:
pass
self.logger.info("========== %s ==========" % txt)
if ScriptConfig.scan_xdcr_destination:
for t_thread in xdcr_monitor_threads:
t_thread.join(1800)
if not self.run_infinite:
break
self.iter_count = self.iter_count + 1
if time.time() - start_time >= Configuration.scan_interval:
continue
else:
sleep_time = Configuration.scan_interval \
- int(time.time() - start_time)
self.logger.info("====== Sleeping for {0} seconds ======"
.format(sleep_time))
time.sleep(sleep_time)
def get_ssh_client(self, host, username, password):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username=username, password=password,
timeout=120, banner_timeout=120)
return client
def record_command_output(self, node, command):
self.logger.info("{} :: Running '{}'".format(node, command))
try:
_, df_output, std_err = self.execute_command(
command, node, self.cluster.ssh_username,
self.cluster.ssh_password)
if std_err:
self.logger.error("std error while running '%s': %s"
% (command, std_err))
else:
for i in range(len(df_output)):
self.logger.info(df_output[i])
except Exception as e:
self.logger.error(e)
def send_email(self, msg_sub, email_recipients, msg_content, file_content):
attachment = ""
mail_from = "Eagle-Eye<[email protected]>"
t_file_prefix = "/tmp/iter_{}_{}_{}"\
.format(self.iter_count, self.cluster.master_node, self.token)
# Dump the email body to file
with open("{}_body.log".format(t_file_prefix), "w") as fp:
fp.write(msg_content.strip())
if file_content:
# Dump full logs into file for attachment
with open("{}.log".format(t_file_prefix), "w") as fp:
fp.write(file_content.strip())
# Zip the logs to compress
p = os.popen("zip -jFSr {0}.zip {0}.log".format(t_file_prefix))
status = p.close()
if status:
self.logger.info("Zip status: {}".format(status))
attachment = "-a \"{0}.zip\"".format(t_file_prefix)
# Send mail
cmd = "mailx -s \"{}\" {} -r \"{}\" \"{}\" < \"{}_body.log\"" \
.format(msg_sub, attachment, mail_from,
email_recipients, t_file_prefix)
self.logger.info("Sending mail: %s" % cmd)
p = os.popen(cmd)
status = p.close()
if status:
self.logger.info("Sendmail exit status: {}".format(status))
# Cleanup tmp files only if mail sent
if status is None:
p = os.popen("rm -f {}*".format(t_file_prefix))
status = p.close()
if status:
self.logger.info("Del .log file status: {}".format(status))
def update_state_file(self):
target = open(self.state_file, 'w')
target.write(str(self.keyword_counts))
def collect_dumps(self, node, component, msg_content):
goroutine_dump_name, cpupprof_dump_name, heappprof_dump_name = \
self.get_dumps(node, component["port"])
msg_content = msg_content + '\n' + "Dump Location : " + '\n' \
+ goroutine_dump_name + '\n' + cpupprof_dump_name \
+ '\n' + heappprof_dump_name
return msg_content
def check_stats_api(self, node, component, msg_content):
fin_neg_stat = []
for stat in component["stats_api_list"]:
stat_json = self.get_stats(stat, node, component["port"])
neg_stat = self.check_for_negative_stat(stat_json)
if neg_stat.__len__() != 0:
fin_neg_stat.append(neg_stat)
else:
neg_stat = None
self.logger.info(str(stat) + " : " + str(neg_stat))
msg_content = msg_content + '\n' + str(stat) + " : " + str(neg_stat)
return fin_neg_stat, msg_content
def check_for_negative_stat(self, stat_json):
queue = deque([stat_json])
neg_stats = []
while queue:
node = queue.popleft()
nodevalue = node
if type(node) is tuple:
nodekey = node[0]
nodevalue = node[1]
if isinstance(nodevalue, Mapping):
for k, v in nodevalue.items():
queue.extend([(k, v)])
elif isinstance(nodevalue, (Sequence, Set)) and not isinstance(nodevalue, basestring):
queue.extend(nodevalue)
else:
if isinstance(nodevalue, (int, long)) and nodevalue < 0 and "mutation_queue_size" not in nodekey:
neg_stats.append(node)
return neg_stats
def get_dumps(self, node, port):
goroutine_dump_name = "{0}/{1}/goroutine_{2}".format(os.getcwd(), self.dump_dir_name, node)
cpupprof_dump_name = "{0}/{1}/cpupprof_{2}".format(os.getcwd(), self.dump_dir_name, node)
heappprof_dump_name = "{0}/{1}/heappprof_{2}".format(os.getcwd(), self.dump_dir_name, node)
heap_dump_cmd = "curl http://{0}:{1}/debug/pprof/heap?debug=1 " \
"-u Administrator:password > {2}"\
.format(node, port, heappprof_dump_name)
cpu_dump_cmd = "curl http://{0}:{1}/debug/pprof/profile?debug=1 " \
"-u Administrator:password > {2}"\
.format(node, port, cpupprof_dump_name)
goroutine_dump_cmd = "curl http://{0}:{1}/debug/pprof/goroutine?debug=1 " \
"-u Administrator:password > {2}"\
.format(node, port, goroutine_dump_name)
self.logger.info(heap_dump_cmd)
os.system(heap_dump_cmd)
self.logger.info(cpu_dump_cmd)
os.system(cpu_dump_cmd)
self.logger.info(goroutine_dump_cmd)
os.system(goroutine_dump_cmd)
return goroutine_dump_name, cpupprof_dump_name, heappprof_dump_name
def get_stats(self, stat, node, port):
api = "http://{0}:{1}/{2}".format(node, port, stat)
json_parsed = {}
status, content, _ = self._http_request(api)
if status:
json_parsed = json.loads(content)
return json_parsed
def convert_output_to_json(self, output):
list_to_string = ''
list_to_string = list_to_string.join(output)
json_output = json.loads(list_to_string)
return json_output
def check_nodes_healthy(self, nodes, component, rest_username, rest_password, ssh_username, ssh_password):
msg_content = ''
should_cbcollect = False
for node in nodes:
command = "curl http://{0}:{1}/query/service -u {2}:{3} " \
"-d 'statement=select 1'"\
.format(node, component["port"], rest_username,
rest_password)
self.logger.info("Running curl: {0}".format(command))
try:
occurences, output, std_err = self.execute_command(
command, node, ssh_username, ssh_password)
self.logger.info("Node:{0} Results:{1}".format(node, str(output)))
if "Empty reply from server" in str(output) \
or "failed to connect" in str(output) \
or "timeout" in str(output):
self.logger.error(
"The n1ql service appears to be unhealthy! "
"Select 1 from node {0} failed! {1}"
.format(node, output))
should_cbcollect = True
except Exception as e:
self.logger.info("Found an exception {0}".format(e))
msg_content = msg_content + '\n\n' + node + " : " + str(component["component"])
msg_content = msg_content + '\n\n' + "Found an exception {0}".format(e) + "\n"
return should_cbcollect, msg_content
def check_completed_requests(self, nodes, component, rest_username,
rest_password, ssh_username, ssh_password):
msg_content = ''
should_cbcollect = False
collection_timestamp = time.time()
collection_timestamp = datetime.fromtimestamp(collection_timestamp).strftime('%Y-%m-%dT%H:%M:%S')
path = os.getcwd()
file = "query_completed_requests_errors_{0}.txt".format(collection_timestamp)
# Group the errors by the number of occurences of each distinct error message
command = "curl http://{0}:{1}/query/service -u {2}:{3} " \
"-d 'statement=select count(errors[0].message) " \
"as errorCount, errors[0].message from system:completed_requests " \
"where errorCount > 0 group by errors[0].message'"\
.format(nodes[0], component["port"], rest_username,
rest_password)
self.logger.info("Running curl: {0}".format(command))
try:
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
# Convert the output to a json dict that we can parse
results = self.convert_output_to_json(output)
if results['metrics']['resultCount'] > 0:
for result in results['results']:
if 'message' in result:
# Filter out known errors
# Some errors can be filtered out based on errors[0].message, add those error messages here
if "Timeout" in result['message'] and "exceeded" in result["message"]:
try:
self.logger.info("Error message {0} is a known error, we will skip it and remove it from completed_requests".format(result['message']))
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=delete from system:completed_requests where errors[0].message = \"{4}\"'".format(
nodes[0], component["port"], rest_username, rest_password, result['message'])
self.logger.info("Running curl: {0}".format(command))
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
results = self.convert_output_to_json(output)
except Exception as e:
if "errors" in str(e):
continue
else:
self.logger.info("There was an exception {0}".format(str(e)))
# Some errors need to be checked out further in order to see if they need to be filtered
elif "Commit Transaction statement error" in result['message']:
try:
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=select * from system:completed_requests where errors[0].message = \"{4}\"'".format(
nodes[0], component["port"], rest_username, rest_password, result['message'])
self.logger.info("{} - Running curl: {0}".format(nodes[0], command))
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
# Convert the output to a json dict that we can parse
results = self.convert_output_to_json(output)
# Check the causes field for known errors, if we encounter one, remove them from completed_requests
for result in results['results']:
if "cause" in result['errors'][0]:
if "deadline expired before WWC" in result['errors'][0]['cause']['cause']['cause']:
self.logger.info(
"Error message {0} is a known error, we will skip it and remove it from completed_requests".format(
result['errors'][0]['cause']['cause']['cause']))
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=delete from system:completed_requests where errors[0].cause.cause.cause = \"{4}\"'".format(
nodes[0], component["port"], rest_username, rest_password,
result['errors'][0]['cause']['cause']['cause'])
self.logger.info("{} - Running curl: {}".format(nodes[0], command))
_, _, _ = self.execute_command(
command, nodes[0],
ssh_username, ssh_password)
except Exception as e:
if "errors" in str(e):
continue
else:
self.logger.info("There was an exception {0}".format(str(e)))
# add elifs here to mimic the above to filter more known causes of error messages
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=select count(errors[0].message) as errorCount, errors[0].message from system:completed_requests where errorCount > 0 group by errors[0].message'".format(
nodes[0], component["port"], rest_username, rest_password)
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
# Convert the output to a json dict that we can parse
results = self.convert_output_to_json(output)
if results['metrics']['resultCount'] > 0:
self.logger.info("Errors found: {0}".format(results['results']))
for result in results['results']:
if 'message' in result:
self.logger.info(
"Number of occurences of message '{0}':{1}".format(result['message'], result['errorCount']))
# Look for an example of each error message, and print it out timestamped
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=select * from system:completed_requests where errors[0].message = \"{4}\" limit 1'".format(
nodes[0], component["port"], rest_username, rest_password, result['message'])
self.logger.info("Running curl: {0}".format(command))
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
# Convert the output to a json dict that we can parse
results = self.convert_output_to_json(output)
self.logger.info(
"Sample result for error message '{0}' at time {1}: {2}"
.format(result['message'],
results['results'][0]['completed_requests']['requestTime'],
results['results']))
# Update msg_content to show errors were found
msg_content = msg_content + '\n\n' + nodes[0] + " : " + str(component["component"])
msg_content = msg_content + '\n\n' + "Sample result for error message '{0}' at time {1}: {2}".format(result['message'],
results['results'][0][
'completed_requests'][
'requestTime'],
results['results']) + "\n"
# Get the entire completed_requests errors and dump them to a file
command = "curl http://{0}:{1}/query/service -u {2}:{3} " \
"-d 'statement=select * from system:completed_requests " \
"where errorCount > 0 order by requestTime desc'"\
.format(nodes[0], component["port"], rest_username,
rest_password)
self.logger.info("{} = Running curl: {0}".format(nodes[0], command))
try:
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
# Convert the output to a json dict that we can parse
results = self.convert_output_to_json(output)
# If there are results store the results in a file
if results['metrics']['resultCount'] > 0:
self.logger.info("We found errors in completed requests, storing errors to a file")
with open(os.path.join(path, file), 'w') as fp:
json.dump(results, fp, indent=4)
fp.close()
zipname = path + "/query_completed_requests_errors_{0}.zip".format(
collection_timestamp)
zipfile.ZipFile(zipname, mode='w').write(file)
os.remove(file)
file = file.replace(".txt", ".zip")
self.logger.info(
"Errors from completed_requests stored at {0}/{1}".format(path, file))
self.logger.info(
"After storing competed_requests_errors we will delete them from the server")
command = "curl http://{0}:{1}/query/service -u {2}:{3} -d 'statement=delete from system:completed_requests where errorCount > 0'".format(
nodes[0], component["port"], rest_username, rest_password)
self.logger.info("Running curl: {0}".format(command))
occurences, output, std_err = self.execute_command(
command, nodes[0], ssh_username, ssh_password)
should_cbcollect = True
except Exception as e:
self.logger.info("Found an exception {0}".format(e))
msg_content = msg_content + '\n\n' + nodes[0] + " : " + str(component["component"])
msg_content = msg_content + '\n\n' + "Found an exception {0}".format(e) + "\n"
except Exception as e: