-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegraf.py
786 lines (762 loc) · 23 KB
/
telegraf.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
import datetime
import json
import logging
from timeit import default_timer as timer
import psycopg2
import psycopg2.sql
log = logging.getLogger(__name__)
measurement_metadata = {
'net': {
'known': {
'host',
'interface',
'err_out',
'err_in',
'bytes_recv',
'bytes_sent',
'drop_out',
'drop_in',
'packets_sent',
'packets_recv',
'ip_forwarding',
'ip_reasmtimeout',
'ip_defaultttl',
'ip_forwdatagrams',
'ip_fragfails',
'ip_outrequests',
'ip_reasmoks',
'ip_inhdrerrors',
'ip_inaddrerrors',
'ip_inreceives',
'ip_reasmfails',
'ip_indelivers',
'ip_outnoroutes',
'ip_indiscards',
'ip_inunknownprotos',
'ip_reasmreqds',
'ip_fragoks',
'ip_fragcreates',
'ip_outdiscards',
'icmp_outechos',
'icmp_outdestunreachs',
'icmp_outtimestampreps',
'icmp_outsrcquenchs',
'icmp_outechoreps',
'icmp_outerrors',
'icmp_outredirects',
'icmp_outparmprobs',
'icmp_outtimestamps',
'icmp_outaddrmasks',
'icmp_outtimeexcds',
'icmp_outaddrmaskreps',
'icmp_outmsgs',
'icmp_intimestampreps',
'icmp_indestunreachs',
'icmp_inredirects',
'icmp_inechos',
'icmp_inerrors',
'icmp_intimestamps',
'icmp_inparmprobs',
'icmp_intimeexcds',
'icmp_inechoreps',
'icmp_insrcquenchs',
'icmp_incsumerrors',
'icmp_inmsgs',
'icmp_inaddrmaskreps',
'icmp_inaddrmasks',
'icmp_indestunreachs',
'icmpmsg_intype0',
'icmpmsg_intype3',
'icmpmsg_intype8',
'icmpmsg_outtype0',
'icmpmsg_outtype3',
'icmpmsg_outtype5',
'icmpmsg_outtype8',
'icmpmsg_outtype11',
'icmpmsg_intype11',
'tcp_outrsts',
'tcp_rtomin',
'tcp_currestab',
'tcp_retranssegs',
'tcp_outsegs',
'tcp_insegs',
'tcp_attemptfails',
'tcp_rtoalgorithm',
'tcp_activeopens',
'tcp_rtomax',
'tcp_incsumerrors',
'tcp_estabresets',
'tcp_passiveopens',
'tcp_maxconn',
'tcp_inerrs',
'udplite_sndbuferrors',
'udplite_incsumerrors',
'udplite_inerrors',
'udplite_noports',
'udplite_ignoredmulti',
'udplite_indatagrams',
'udplite_rcvbuferrors',
'udplite_outdatagrams',
'udp_noports',
'udp_ignoredmulti',
'udp_incsumerrors',
'udp_indatagrams',
'udp_rcvbuferrors',
'udp_sndbuferrors',
'udp_inerrors',
'udp_outdatagrams'
},
'discard': {
'ip_forwarding',
'ip_reasmtimeout',
'ip_defaultttl',
'ip_forwdatagrams',
'ip_fragfails',
'ip_outrequests',
'ip_reasmoks',
'ip_inhdrerrors',
'ip_inaddrerrors',
'ip_inreceives',
'ip_reasmfails',
'ip_indelivers',
'ip_outnoroutes',
'ip_indiscards',
'ip_inunknownprotos',
'ip_reasmreqds',
'ip_fragoks',
'ip_fragcreates',
'ip_outdiscards',
'icmp_intimestampreps',
'icmp_outechos',
'icmp_outdestunreachs',
'icmp_outtimestampreps',
'icmp_outsrcquenchs',
'icmp_inredirects',
'icmp_inechos',
'icmp_inerrors',
'icmp_intimestamps',
'icmp_outechoreps',
'icmp_inparmprobs',
'icmp_outerrors',
'icmp_outredirects',
'icmp_intimeexcds',
'icmp_outparmprobs',
'icmp_inechoreps',
'icmp_outtimestamps',
'icmp_outaddrmasks',
'icmp_insrcquenchs',
'icmp_incsumerrors',
'icmp_outtimeexcds',
'icmp_inmsgs',
'icmp_outaddrmaskreps',
'icmp_inaddrmaskreps',
'icmp_inaddrmasks',
'icmp_outmsgs',
'icmp_indestunreachs',
'icmpmsg_outtype0',
'icmpmsg_intype3',
'icmpmsg_intype8',
'icmpmsg_outtype8',
'icmpmsg_intype0',
'icmpmsg_outtype3',
'icmpmsg_outtype5',
'icmpmsg_outtype11',
'icmpmsg_intype11',
'tcp_outrsts',
'tcp_rtomin',
'tcp_currestab',
'tcp_retranssegs',
'tcp_outsegs',
'tcp_insegs',
'tcp_attemptfails',
'tcp_rtoalgorithm',
'tcp_activeopens',
'tcp_rtomax',
'tcp_incsumerrors',
'tcp_estabresets',
'tcp_passiveopens',
'tcp_maxconn',
'tcp_inerrs',
'udplite_sndbuferrors',
'udplite_incsumerrors',
'udplite_inerrors',
'udplite_noports',
'udplite_ignoredmulti',
'udplite_indatagrams',
'udplite_rcvbuferrors',
'udplite_outdatagrams',
'udp_noports',
'udp_ignoredmulti',
'udp_incsumerrors',
'udp_indatagrams',
'udp_rcvbuferrors',
'udp_sndbuferrors',
'udp_inerrors',
'udp_outdatagrams'
}
},
'cpu': {
'known': {
'cpu',
'host',
'usage_user',
'usage_steal',
'usage_iowait',
'usage_softirq',
'usage_system',
'usage_guest_nice',
'usage_guest',
'usage_idle',
'usage_irq',
'usage_nice'
},
'discard': set()
},
'diskio': {
'known': {
'host',
'name',
'reads',
'write_time',
'write_bytes',
'writes',
'read_time',
'read_bytes',
'io_time',
'weighted_io_time',
'iops_in_progress'
},
'discard': set()
},
'netstat': {
'known': {
'host',
'tcp_close',
'udp_socket',
'tcp_syn_sent',
'tcp_closing',
'tcp_time_wait',
'tcp_fin_wait1',
'tcp_fin_wait2',
'tcp_close_wait',
'tcp_established',
'tcp_listen',
'tcp_syn_recv',
'tcp_last_ack',
'tcp_none'
},
'discard': set()
},
'mem': {
'known': {
'host',
'total',
'inactive',
'active',
'free',
'available',
'available_percent',
'used_percent',
'buffered',
'cached',
'used',
'slab'
},
'discard': {'slab'}
},
'swap': {
'known': {
'host',
'used',
'used_percent',
'free',
'total',
'out',
'in'
},
'discard': {
'used_percent',
'out',
'in'
}
},
'docker': {
'known': {
'host',
'engine_host',
'n_goroutines',
'n_containers',
'n_cpus',
'n_containers_running',
'n_containers_paused',
'n_containers_stopped',
'n_used_file_descriptors',
'n_images',
'n_listener_events',
'memory_total',
'unit'
},
'discard': {
'engine_host',
'n_cpus',
'n_goroutines',
'n_used_file_descriptors',
'n_listener_events',
'memory_total',
'unit'
}
},
'docker_container_cpu': {
'known': {
'container_id',
'usage_total',
'engine_host',
'cpu',
'author',
'maintainer',
'container_version',
'container_image',
'zoe_deployment_name',
'host',
'zoe_execution_name',
'container_name',
'zoe_execution_id',
'zoe_owner',
'zoe_service_id',
'zoe_service_name',
'zoe_type',
'com.docker.swarm.id',
'usage_in_kernelmode',
'throttling_periods',
'usage_in_usermode',
'usage_percent',
'throttling_throttled_time',
'usage_system',
'throttling_throttled_periods'
},
'discard': {
'container_id',
'engine_host',
'author',
'maintainer',
'zoe_type',
'com.docker.swarm.id',
'usage_in_kernelmode',
'throttling_periods',
'usage_in_usermode',
'throttling_throttled_time',
'usage_system',
'throttling_throttled_periods'
}
},
'docker_container_net': {
'known': {
'container_image',
'zoe_type',
'engine_host',
'zoe_execution_id',
'zoe_owner',
'zoe_service_name',
'zoe_service_id',
'zoe_deployment_name',
'maintainer',
'author',
'container_name',
'network',
'host',
'zoe_execution_name',
'container_version',
'tx_errors',
'tx_packets',
'rx_dropped',
'rx_errors',
'container_id',
'tx_bytes',
'rx_packets',
'rx_bytes',
'tx_dropped',
'com.docker.swarm.id'
},
'discard': {
'container_id',
'engine_host',
'author',
'maintainer',
'zoe_type',
'com.docker.swarm.id'
}
},
'docker_container_blkio': {
'known': {
'zoe_deployment_name',
'zoe_execution_name',
'container_image',
'container_version',
'device',
'maintainer',
'author',
'zoe_type',
'zoe_service_id',
'zoe_owner',
'host',
'container_name',
'engine_host',
'zoe_execution_id',
'zoe_service_name',
'io_serviced_recursive_write',
'io_serviced_recursive_total',
'io_service_bytes_recursive_read',
'io_service_bytes_recursive_sync',
'io_service_bytes_recursive_write',
'io_serviced_recursive_read',
'io_service_bytes_recursive_async',
'io_serviced_recursive_async',
'container_id',
'io_serviced_recursive_sync',
'io_service_bytes_recursive_total',
'com.docker.swarm.id'
},
'discard': {
'author',
'maintainer',
'zoe_type',
'engine_host',
'container_id',
'com.docker.swarm.id'
}
},
'docker_container_mem': {
'known': {
'active_file',
'active_anon',
'author',
'maintainer',
'cache',
'host',
'engine_host',
'container_version',
'container_image',
'container_name',
'container_id',
'inactive_anon',
'limit',
'max_usage',
'mapped_file',
'pgpgin',
'pgfault',
'total_rss',
'total_pgpgin',
'total_cache',
'total_active_file',
'total_pgmajfault',
'total_writeback',
'total_active_anon',
'total_rss_huge',
'total_inactive_anon',
'total_pgfault',
'total_pgpgout',
'total_inactive_file',
'total_unevictable',
'total_mapped_file',
'usage_percent',
'zoe_type',
'zoe_service_id',
'zoe_execution_id',
'zoe_service_name',
'zoe_deployment_name',
'zoe_owner',
'zoe_execution_name',
'inactive_file',
'rss',
'pgmajfault',
'rss_huge',
'pgpgout',
'usage',
'hierarchical_memory_limit',
'writeback',
'unevictable',
'com.docker.swarm.id'
},
'discard': {
'zoe_type',
'author',
'maintainer',
'engine_host',
'inactive_anon',
'total_pgpgin',
'pgpgin',
'cache',
'active_file',
'pgfault',
'container_id',
'max_usage',
'mapped_file',
'limit',
'total_rss_huge',
'active_anon',
'inactive_file',
'rss',
'pgmajfault',
'rss_huge',
'pgpgout',
'total_unevictable',
'total_mapped_file',
'total_active_file',
'total_pgmajfault',
'total_writeback',
'total_active_anon',
'total_inactive_anon',
'total_pgpgout',
'total_inactive_file',
'writeback',
'unevictable',
'com.docker.swarm.id'
}
},
'ping': {
'known': {
'average_response_ms',
'minimum_response_ms',
'result_code',
'maximum_response_ms',
'packets_transmitted',
'packets_received',
'percent_packet_loss',
'url',
'host'
},
'discard': set()
}
}
conn = psycopg2.connect(host="bf11", dbname="metrics", user="postgres", password="zoepostgres")
for m in measurement_metadata:
measurement_metadata[m]['keep'] = measurement_metadata[m]['known'] - measurement_metadata[m]['discard']
counters = {
'inserts': 0,
'errors': 0,
'total_lag': 0,
'insert_times': [],
'msg_in': 0
}
def unpack_telegraf_json_protocol(message):
timestamp = datetime.datetime.fromtimestamp(message['timestamp'] / 1000, datetime.timezone.utc)
measurement = message['name']
values = {}
for k, v in message['tags'].items():
v = v.replace(':', '_')
v = v.replace('==', '-eq-')
v = v.replace('=', '-')
v = v.replace(' ', '_')
values[k] = v
for k, v in message['fields'].items():
values[k] = v
return measurement, timestamp, values
def router(message, kafka_producer):
counters['msg_in'] += 1
try:
measurement, timestamp, values = unpack_telegraf_json_protocol(message)
except Exception as e:
log.error(e)
counters['errors'] += 1
return True
if measurement not in measurement_metadata:
log.warning('Skipping unknown measurement: {}'.format(message))
counters['errors'] += 1
return False
if 'docker' in measurement:
if 'zoe_deployment_name' not in values:
values['zoe_deployment_name'] = None
if 'zoe_service_id' not in values:
values['zoe_service_id'] = None
if 'zoe_execution_id' not in values:
values['zoe_execution_id'] = None
if 'zoe_owner' not in values:
values['zoe_owner'] = None
if 'zoe_execution_name' not in values:
values['zoe_execution_name'] = None
if 'zoe_service_name' not in values:
values['zoe_service_name'] = None
values_names = set(values.keys())
if message_filter(measurement, values):
return True
values_to_insert = values_names & measurement_metadata[measurement]['keep']
if values_to_insert != measurement_metadata[measurement]['keep']:
error_message = {
'measurement': measurement,
'timestamp': timestamp.timestamp(),
'tags': values,
'unknown_tags': list(values_names - measurement_metadata[measurement]['known']),
'missing_tags': list(measurement_metadata[measurement]['keep'] - values_names)
}
kafka_producer.produce('metrics_errors', json.dumps(error_message))
counters['errors'] += 1
if len(error_message['unknown_tags']) > 0:
log.error('Error: {}'.format(error_message))
return True
values = {k: v for k, v in values.items() if k in values_to_insert}
return insert(measurement, timestamp, values)
def insert(table, timestamp, values):
values["timestamp"] = timestamp
time_start = timer()
q = psycopg2.sql.SQL("INSERT INTO {} ({}) VALUES ({})").format(
psycopg2.sql.Identifier(table),
psycopg2.sql.SQL(', ').join(map(psycopg2.sql.Identifier, values.keys())),
psycopg2.sql.SQL(', ').join(map(psycopg2.sql.Placeholder, values.keys())))
with conn:
with conn.cursor() as cur:
try:
cur.execute(q, values)
counters['inserts'] += 1
except (psycopg2.OperationalError, psycopg2.ProgrammingError) as e:
log.error(e)
counters['errors'] += 1
return False
counters['insert_times'].append(timer() - time_start)
return True
def flush_stats(time_interval):
if time_interval == 0:
return
timestamp = datetime.datetime.now(datetime.timezone.utc)
q = 'INSERT INTO kafkapost (timestamp, host, kafka_lag, messages_in_sec, inserts_sec, parse_errors_sec, avg_insert_time) VALUES (%s, %s, %s, %s, %s, %s, %s)'
if len(counters['insert_times']) == 0:
avg_insert_time = 0
else:
avg_insert_time = sum(counters['insert_times']) / len(counters['insert_times'])
with conn:
with conn.cursor() as cur:
cur.execute(q, (timestamp, 'bf11', counters['total_lag'], counters['msg_in'] / time_interval, counters['inserts'] / time_interval, counters['errors'] / time_interval, avg_insert_time))
counters['inserts'] = 0
counters['errors'] = 0
counters['total_lag'] = 0
counters['insert_times'] = []
counters['msg_in'] = 0
def message_filter(measurement, values):
if measurement == 'swap' and 'in' in values and 'out' in values:
return True
if measurement == 'net' and 'icmp_inaddrmaskreps' in values:
return True
if measurement == 'docker_container_cpu' and values['cpu'] != 'cpu-total':
return True
if measurement == 'docker' and 'memory_total' in values:
return True
else:
return False
#######################
# CREATE TABLE diskio
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# name TEXT,
# reads BIGINT,
# write_time BIGINT,
# write_bytes BIGINT,
# writes BIGINT,
# read_time BIGINT,
# read_bytes BIGINT,
# io_time BIGINT,
# weighted_io_time BIGINT,
# iops_in_progress BIGINT
# )
#
# CREATE TABLE docker_container_cpu
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# usage_total BIGINT,
# cpu TEXT,
# container_version TEXT,
# container_image TEXT,
# zoe_deployment_name TEXT NULL DEFAULT NULL,
# zoe_execution_name TEXT NULL DEFAULT NULL,
# container_name TEXT,
# zoe_execution_id TEXT NULL DEFAULT NULL,
# zoe_owner TEXT NULL DEFAULT NULL,
# zoe_service_id TEXT NULL DEFAULT NULL,
# zoe_service_name TEXT NULL DEFAULT NULL
# )
#
# CREATE TABLE docker_container_net
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# container_image TEXT,
# zoe_execution_id TEXT NULL DEFAULT NULL,
# zoe_owner TEXT NULL DEFAULT NULL,
# zoe_service_name TEXT NULL DEFAULT NULL,
# zoe_service_id TEXT NULL DEFAULT NULL,
# zoe_deployment_name TEXT NULL DEFAULT NULL,
# container_name TEXT NULL DEFAULT NULL,
# network TEXT,
# zoe_execution_name TEXT NULL DEFAULT NULL,
# container_version TEXT NULL DEFAULT NULL,
# tx_errors BIGINT,
# tx_packets BIGINT,
# rx_dropped BIGINT,
# rx_errors BIGINT,
# tx_bytes BIGINT,
# rx_packets BIGINT,
# rx_bytes BIGINT,
# tx_dropped BIGINT
# )
#
# CREATE TABLE docker_container_blkio
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# container_image TEXT,
# zoe_execution_id TEXT NULL DEFAULT NULL,
# zoe_owner TEXT NULL DEFAULT NULL,
# zoe_service_name TEXT NULL DEFAULT NULL,
# zoe_service_id TEXT NULL DEFAULT NULL,
# zoe_deployment_name TEXT NULL DEFAULT NULL,
# container_name TEXT NULL DEFAULT NULL,
# network TEXT,
# zoe_execution_name TEXT NULL DEFAULT NULL,
# container_version TEXT NULL DEFAULT NULL,
# device TEXT,
# io_serviced_recursive_write BIGINT,
# io_serviced_recursive_total BIGINT,
# io_service_bytes_recursive_read BIGINT,
# io_service_bytes_recursive_sync BIGINT,
# io_service_bytes_recursive_write BIGINT,
# io_serviced_recursive_read BIGINT,
# io_service_bytes_recursive_async BIGINT,
# io_serviced_recursive_async BIGINT,
# io_serviced_recursive_sync BIGINT,
# io_service_bytes_recursive_total BIGINT
# )
#
# CREATE TABLE docker_container_mem
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# container_image TEXT,
# container_version TEXT NULL DEFAULT NULL,
# container_name TEXT NULL DEFAULT NULL,
# zoe_execution_id TEXT NULL DEFAULT NULL,
# zoe_owner TEXT NULL DEFAULT NULL,
# zoe_service_name TEXT NULL DEFAULT NULL,
# zoe_service_id TEXT NULL DEFAULT NULL,
# zoe_deployment_name TEXT NULL DEFAULT NULL,
# zoe_execution_name TEXT NULL DEFAULT NULL,
# total_rss BIGINT,
# total_cache BIGINT,
# total_pgfault BIGINT,
# usage_percent FLOAT,
# usage BIGINT,
# hierarchical_memory_limit BIGINT
# )
#
# CREATE TABLE ping
# (
# timestamp TIMESTAMP WITH TIME ZONE,
# host TEXT,
# average_response_ms FLOAT,
# minimum_response_ms FLOAT,
# result_code INT,
# maximum_response_ms FLOAT,
# packets_transmitted INT,
# packets_received INT,
# percent_packet_loss FLOAT,
# url TEXT
# )