Skip to content

Commit 86c7d26

Browse files
author
alexander popov
committed
added export metrics to zabbix server
1 parent 93c71de commit 86c7d26

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

mamonsu/lib/parser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@
117117
{prog} zabbix item error <host name>
118118
{prog} zabbix item lastvalue <host name>
119119
{prog} zabbix item lastclock <host name>
120+
121+
Export metrics to zabbix server
122+
Command: --send-data-zabbix
123+
Example:
124+
{prog} --send-data-zabbix --zabbix-file=localhost.log --zabbix-addres=licalhost
125+
Options:
126+
--zabbix-address <name of the Zabbix host to send metrics>
127+
--zabbix-port <port of Zabbix server to send metrics> by default 10051
128+
--zabbix-file <path to file with metrics to send metrics>
129+
--zabbix-client <name of the Zabbix host to send metrics> by default localhost
130+
--zabbix-log-level <log level to send metrics> (INFO|DEBUG|WARN) by default INFO
120131
"""
121132

122133
if platform.LINUX:
@@ -261,4 +272,15 @@ def parse_args():
261272
parser.add_option(
262273
'--application', dest='application',
263274
default='App-PostgresPro-{0}'.format(sys.platform.title()))
275+
# Zabbix server to send metrics
276+
parser.add_option('--zabbix-address', dest='zabbix_address', default=None)
277+
# port of Zabbix server to send metrics
278+
parser.add_option('--zabbix-port', dest='zabbix_port', default='10051')
279+
# name of the Zabbix host to send metrics
280+
parser.add_option('--zabbix-client', dest='zabbix_client', default='localhost')
281+
# path to file with metrics to send metrics
282+
parser.add_option('--zabbix-file', dest='zabbix_file', default=None)
283+
# log level to send metrics
284+
parser.add_option('--zabbix-log-level', dest='zabbix_log_level', default='INFO')
285+
264286
return parser.parse_args()

mamonsu/lib/runner.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ def quit_handler(_signo=None, _stack_frame=None):
5151
from mamonsu.tools.agent.start import run_agent
5252
sys.argv.remove('agent')
5353
return run_agent()
54+
elif tool == '--send-data-zabbix':
55+
args, _ = parse_args()
56+
if not args.zabbix_address:
57+
print('To send metrics to zabbix, please, add option --zabbix-addres')
58+
exit(125)
59+
60+
if not args.zabbix_file:
61+
print ('To send metrics to zabbix, please, add option --zabbix_file')
62+
exit(123)
63+
64+
cfg = Config(args.config_file, args.plugins_dirs)
65+
cfg.config.set('zabbix','address',args.zabbix_address)
66+
cfg.config.set('zabbix','port',args.zabbix_port)
67+
cfg.config.set('zabbix', 'client', args.zabbix_client)
68+
cfg.config.set('log', 'level', args.zabbix_log_level)
69+
70+
supervisor = Supervisor(cfg)
71+
supervisor.send_file_zabbix(cfg,args.zabbix_file)
72+
exit(0)
73+
5474
elif tool == 'export':
5575
args, commands = parse_args()
5676
# get PG version

mamonsu/lib/senders/zbx.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import mamonsu.lib.platform as platform
1313
from mamonsu.lib.plugin import Plugin
1414
from mamonsu.lib.queue import Queue
15-
15+
from itertools import islice
1616

1717
class ZbxSender(Plugin):
1818

@@ -64,6 +64,34 @@ def _flush(self):
6464
})
6565
self._send_data(data)
6666

67+
def send_file_to_zabbix (self,path):
68+
zabbix_client = self.config.fetch('zabbix', 'client')
69+
self.log.setLevel((self.config.fetch('log', 'level')).upper())
70+
71+
metrics = []
72+
with open(path, 'r') as f:
73+
while True:
74+
lines=list(islice(f, 100))
75+
for line in lines:
76+
split_line = line.rstrip('\n').split('\t')
77+
metric = {
78+
'host': zabbix_client,
79+
'key': split_line[2],
80+
'value': split_line[1],
81+
'clock': int(split_line[0])}
82+
metrics.append(metric)
83+
84+
data = json.dumps({
85+
'request': 'sender data',
86+
'data': metrics,
87+
'clock': int(time.time())
88+
})
89+
self._send_data(data)
90+
self.log.info('sended {0} metrics'.format(str(len(metrics))))
91+
metrics = []
92+
if not lines:
93+
break
94+
6795
def _send_data(self, data):
6896
data_len = struct.pack('<Q', len(data))
6997
if platform.PY3:

mamonsu/lib/supervisor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mamonsu.lib.senders import *
77
from mamonsu.tools.agent import *
88
from mamonsu.plugins import *
9+
from mamonsu.lib.senders.zbx import ZbxSender
910

1011

1112
class Supervisor(object):
@@ -62,3 +63,7 @@ def _loop(self):
6263
else:
6364
self._sender.send('mamonsu.plugin.errors[]', '')
6465
plugin_errors, plugin_probes = 0, 0
66+
67+
def send_file_zabbix(self,cfg,path):
68+
zbxSender = ZbxSender (cfg)
69+
zbxSender.send_file_to_zabbix(path)

0 commit comments

Comments
 (0)