Skip to content

Commit a4f2a27

Browse files
author
alexander popov
committed
feat: re send metrics with error one by one PGPRO-4577
1 parent af63d40 commit a4f2a27

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

mamonsu/lib/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __init__(self, cfg_file=None, plugin_directories=None):
5252
config.set('zabbix', 'client', socket.gethostname())
5353
config.set('zabbix', 'address', '127.0.0.1')
5454
config.set('zabbix', 'port', str(10051))
55+
config.set('zabbix', 're_send', str(False))
5556

5657
config.add_section('metric_log')
5758
config.set('metric_log', 'enabled', str(False))

mamonsu/lib/senders/zbx.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self, config):
2727
self.port = config.fetch('zabbix', 'port', int)
2828
self.max_queue_size = config.fetch('sender', 'queue', int)
2929
self.fqdn = config.fetch('zabbix', 'client')
30+
self.re_send = config.fetch('zabbix', 're_send', bool)
3031
self.queue = Queue()
3132
self.log = logging.getLogger(
3233
'ZBX-{0}:{1}'.format(self.host, self.port))
@@ -55,13 +56,22 @@ def _flush(self):
5556
metrics = self.queue.flush()
5657
if len(metrics) == 0:
5758
return
59+
clock = int(time.time())
5860
data = json.dumps({
5961
'request': 'sender data',
6062
'data': metrics,
61-
'clock': int(time.time())
63+
'clock': clock
6264
})
63-
self._send_data(data)
64-
65+
sent_all = self._send_data(data)
66+
if not sent_all and self.re_send:
67+
for metric in metrics:
68+
data = json.dumps({
69+
'request': 'sender data',
70+
'data': [metric],
71+
'clock': clock
72+
})
73+
self._send_data(data)
74+
6575
def send_file_to_zabbix(self, path):
6676
zabbix_client = self.config.fetch('zabbix', 'client')
6777
self.log.setLevel((self.config.fetch('log', 'level')).upper())
@@ -82,7 +92,8 @@ def send_file_to_zabbix(self, path):
8292
metrics.append(metric)
8393
else:
8494
self.log.error(
85-
'Can\'t load metric in line: "{0}". The line must have the format: time <tab> value <tab> metric\'s name.'.format(
95+
'Can\'t load metric in line: "{0}". The line must have the format: '
96+
'time <tab> value <tab> metric\'s name.'.format(
8697
line.rstrip('\n')))
8798
except Exception as e:
8899
self.log.error('Can\'t load metric in line: "{0}". Error : {1} '.format(line.rstrip('\n'), e, ))
@@ -99,6 +110,7 @@ def send_file_to_zabbix(self, path):
99110
break
100111

101112
def _send_data(self, data):
113+
sent_all = True
102114
data_len = struct.pack('<Q', len(data))
103115
packet = b'ZBXD\x01' + data_len + str.encode(data)
104116
try:
@@ -111,13 +123,15 @@ def _send_data(self, data):
111123
resp_body = self._receive(sock, resp_body_len)
112124
self.log.debug('response: {0}'.format(resp_body))
113125
if 'failed: 0' not in str(resp_body):
126+
sent_all = False
114127
self.log.error(
115128
'On request:\n{0}\nget response'
116129
' with failed items:\n{1}'.format(
117130
data,
118131
resp_body))
119132
finally:
120133
sock.close()
134+
return sent_all
121135

122136
def _receive(self, sock, count):
123137
buf = str.encode('')

packaging/conf/example.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ query_timeout = 10
2121
# the address field must point to the running Zabbix server, while the client field must provide the name of
2222
# the Zabbix host. You can find the list of hosts available for your account in the Zabbix web
2323
# interface under Configuration > Hosts.
24+
# re_send - True - in case of transmission error, mamonsu repeats sending metrics one by one to look in log metrics with error
2425

2526
[zabbix]
2627
enabled = True
2728
client = localhost
2829
address = 127.0.0.1
2930
port = 10051
31+
re_send = False
3032

3133
######### General parameters sections ############
3234

packaging/conf/example_win32.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ query_timeout = 10
2121
# the address field must point to the running Zabbix server, while the client field must provide the name of
2222
# the Zabbix host. You can find the list of hosts available for your account in the Zabbix web
2323
# interface under Configuration > Hosts.
24+
# re_send - True - in case of transmission error, mamonsu repeats sending metrics one by one to look in log metrics with error
2425

2526
[zabbix]
2627
enabled = True
2728
client = localhost
2829
address = 127.0.0.1
2930
port = 10051
31+
re_send = False
3032

3133
######### General parameters sections ############
3234

0 commit comments

Comments
 (0)