Skip to content

Commit a2acbf5

Browse files
author
alexander popov
committed
Merge branch 'send_metrics_zabbix' into dev
2 parents 7f85b13 + 0d16840 commit a2acbf5

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

mamonsu/lib/parser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@
123123
{prog} zabbix item error <host name>
124124
{prog} zabbix item lastvalue <host name>
125125
{prog} zabbix item lastclock <host name>
126+
127+
Export metrics to zabbix server
128+
Command: --send-data-zabbix
129+
Example:
130+
{prog} --send-data-zabbix --zabbix-file=localhost.log --zabbix-address=localhost
131+
Options:
132+
--zabbix-address <name of the Zabbix host to send metrics>
133+
--zabbix-port <port of Zabbix server to send metrics> by default 10051
134+
--zabbix-file <path to file with metrics to send metrics>
135+
--zabbix-client <name of the Zabbix host to send metrics> by default localhost
136+
--zabbix-log-level <log level to send metrics> (INFO|DEBUG|WARN) by default INFO
126137
"""
127138

128139
if platform.LINUX:
@@ -271,4 +282,14 @@ def parse_args():
271282
'--old-zabbix',
272283
dest='old_zabbix', action='store_true',
273284
help='Create special template for currently unsupported zabbix versions')
285+
# Zabbix server to send metrics
286+
parser.add_option('--zabbix-address', dest='zabbix_address', default=None)
287+
# port of Zabbix server to send metrics
288+
parser.add_option('--zabbix-port', dest='zabbix_port', default='10051')
289+
# name of the Zabbix host to send metrics
290+
parser.add_option('--zabbix-client', dest='zabbix_client', default='localhost')
291+
# path to file with metrics to send metrics
292+
parser.add_option('--zabbix-file', dest='zabbix_file', default=None)
293+
# log level to send metrics
294+
parser.add_option('--zabbix-log-level', dest='zabbix_log_level', default='INFO')
274295
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: 35 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,40 @@ 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+
try:
77+
split_line = line.rstrip('\n').split('\t')
78+
if len(split_line) == 3:
79+
metric = {
80+
'host': zabbix_client,
81+
'key': split_line[2],
82+
'value': split_line[1],
83+
'clock': int(split_line[0])}
84+
metrics.append(metric)
85+
else:
86+
self.log.error('Can\'t load metric in line: "{0}". The line must have the format: time <tab> value <tab> metric\'s name.'.format(line.rstrip('\n')))
87+
except Exception as e:
88+
self.log.error('Can\'t load metric in line: "{0}". Error : {1} '.format(line.rstrip('\n'),e,))
89+
90+
data = json.dumps({
91+
'request': 'sender data',
92+
'data': metrics,
93+
'clock': int(time.time())
94+
})
95+
self._send_data(data)
96+
self.log.info('sended {0} metrics'.format(str(len(metrics))))
97+
metrics = []
98+
if not lines:
99+
break
100+
67101
def _send_data(self, data):
68102
data_len = struct.pack('<Q', len(data))
69103
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)