Skip to content

Commit 64803b9

Browse files
author
alexander popov
committed
feat: add command mamonsu zabbix version to show version of zabbix and add test for this (PGPRO-4586)
1 parent 23fa7e3 commit 64803b9

File tree

10 files changed

+51
-42
lines changed

10 files changed

+51
-42
lines changed

Makefile.tests

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ run_local_test_pg13: clean_local_test_pg13
101101
docker rm pg13
102102

103103
run_zabbix: clean_zabbix
104-
docker network create --driver=bridge --subnet=172.18.0.0/16 network-local-tests
105104
$(WORKDIR)/tests/local_start_zabbix.sh
106105

107106
clean_local_test_pg9.5:

mamonsu.py

100755100644
File mode changed.

mamonsu/lib/parser.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@
6464
{prog} agent metric-get <metric key>
6565
Options:
6666
-c, --config <file>
67+
"""
6768

69+
zabbix_msg= """
6870
Zabbix API toolbox:
69-
Command: zabbix
70-
Usage:
71+
Command: %prog zabbix [--url] [--user] [--password] [--log-level] commands
72+
commands:
7173
{prog} zabbix template list
7274
{prog} zabbix template show <template name>
7375
{prog} zabbix template id <template name>
@@ -90,11 +92,16 @@
9092
{prog} zabbix item error <host name>
9193
{prog} zabbix item lastvalue <host name>
9294
{prog} zabbix item lastclock <host name>
95+
{prog} zabbix version
96+
9397
Options:
9498
--url=http://zabbix/web/face
9599
--user=WebUser
96-
--password=WebPassword
97-
100+
--password=WebPassword"""
101+
102+
usage_msg += zabbix_msg + '\n'
103+
104+
usage_msg += """
98105
Export metrics to zabbix server
99106
Command: upload
100107
Example:

mamonsu/tools/bootstrap/start.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ def run_deploy():
149149
args = Args()
150150

151151
if not args.try_configure_connect_to_pg():
152+
sys.stderr.write(str(args.args) + '\n')
152153
sys.exit(1)
153154

154155
if not Pooler.is_superuser():
155156
sys.stderr.write(
156157
"ERROR: Bootstrap must be run by PostgreSQL superuser\n")
158+
sys.stderr.write(str(args.args) + '\n')
157159
sys.exit(1)
158160

159161
try:

mamonsu/tools/zabbix_cli/operations.py

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,19 @@
44
import sys
55
import json
66
from mamonsu.tools.zabbix_cli.request import Request
7+
from mamonsu.lib.parser import zabbix_msg
78

89

910
class Operations(object):
1011

11-
_help_msg = """
12-
Arguments must be:
13-
14-
mamonsu zabbix template list
15-
mamonsu zabbix template show <template name>
16-
mamonsu zabbix template id <template name>
17-
mamonsu zabbix template delete <template id>
18-
mamonsu zabbix template export <file>
19-
20-
mamonsu zabbix host list
21-
mamonsu zabbix host show <host name>
22-
mamonsu zabbix host id <host name>
23-
mamonsu zabbix host delete <host id>
24-
mamonsu zabbix host create <host name> <hostgroup id> <template id> <ip>
25-
mamonsu zabbix host info templates <host id>
26-
mamonsu zabbix host info hostgroups <host id>
27-
mamonsu zabbix host info graphs <host id>
28-
mamonsu zabbix host info items <host id>
29-
30-
mamonsu zabbix hostgroup list
31-
mamonsu zabbix hostgroup show <hostgroup name>
32-
mamonsu zabbix hostgroup id <hostgroup name>
33-
mamonsu zabbix hostgroup delete <hostgroup id>
34-
mamonsu zabbix hostgroup create <hostgroup name>
35-
36-
mamonsu zabbix item error <host name>
37-
mamonsu zabbix item lastvalue <host name>
38-
mamonsu zabbix item lastclock <host name>
39-
"""
12+
_help_msg = zabbix_msg
4013

4114
def __init__(self, arg):
42-
4315
self.arg = arg
16+
4417
if len(self.arg.commands) < 2:
45-
self._print_help()
18+
if len(self.arg.commands) ==0 or self.arg.commands[0] != 'version':
19+
self._print_help()
4620

4721
self.req = Request(
4822
url='{0}/api_jsonrpc.php'.format(arg.zabbix_url),
@@ -57,6 +31,8 @@ def __init__(self, arg):
5731
return self.host(self.arg.commands[1:])
5832
elif self.arg.commands[0] == 'item':
5933
return self.item(self.arg.commands[1:])
34+
elif self.arg.commands[0] == 'version':
35+
return self.version(self.arg.commands[1:])
6036
else:
6137
self._print_help()
6238

@@ -313,3 +289,16 @@ def item(self, args):
313289
except Exception as e:
314290
sys.stderr.write('Error find: {0}\n'.format(e))
315291
sys.exit(3)
292+
293+
def version(self, args):
294+
if len(args) != 0:
295+
return self._print_help()
296+
try:
297+
self.req.set_user(None)
298+
self.req.set_passwd(None)
299+
300+
version = self.req.post(method='apiinfo.version', params=[])
301+
print(str(version))
302+
except Exception as e:
303+
sys.stderr.write('Error find: {0}\n'.format(e))
304+
sys.exit(3)

mamonsu/tools/zabbix_cli/request.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@ class Request(object):
1313
def __init__(self, url, user, passwd):
1414
self._url, self._user, self._passwd = url, user, passwd
1515
self._id, self._auth_tocken = 0, None
16-
16+
17+
def set_user(self, user):
18+
self._user=user
19+
20+
def set_passwd(self, passwd):
21+
self._passwd=passwd
22+
1723
def _auth(self):
1824
if self._auth_tocken is None:
25+
if not self._user:
26+
return None
1927
self._auth_tocken = self.post(
2028
'user.login',
2129
{'user': self._user, 'password': self._passwd})

mamonsu/tools/zabbix_cli/start.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
from mamonsu import __version__
88
from mamonsu.lib.default_config import DefaultConfig
99
from mamonsu.tools.zabbix_cli.operations import Operations
10+
from mamonsu.lib.parser import zabbix_msg
1011

1112

1213
class Args(DefaultConfig):
1314

1415
def __init__(self):
1516

1617
parser = optparse.OptionParser(
17-
usage='%prog zabbix [--url] [--user] '
18-
'[--password] [--log-level] commands',
18+
#usage='%prog zabbix [--url] [--user] [--password] [--log-level] commands',
19+
usage = zabbix_msg,
1920
version='%prog zabbix cli tools {0}'.format(__version__),
2021
description='Zabbix CLI tools')
2122
group = optparse.OptionGroup(

tests/local_start_zabbix.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
if [ ! -v ZABBIX_DOCKER_VERSIONS ];
44
then
5-
export ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.4 3.2 3.0"
5+
#export ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.4 3.2 3.0"
6+
export ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.2 3.0"
67
fi
78
# !!!! zabbix 4.4 имеет порт 80, остальные 8080
89
#ZABBIX_DOCKER_VERSIONS="4.4"

tests/local_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
###### test mamonsu zabbix *
77
mkdir /etc/mamonsu/
88
cp /var/tmp/mamonsu/packaging/conf/example.conf /etc/mamonsu/agent.conf
9-
ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.4 3.2 3.0"
9+
#ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.4 3.2 3.0"
10+
ZABBIX_DOCKER_VERSIONS="5.2 5.0 4.4 4.2 4.0 3.2 3.0"
1011
for VER in $ZABBIX_DOCKER_VERSIONS
1112
do
1213
IFS='.' read -ra VER_ARRAY <<<$VER

tests/zabbix.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ mamonsu zabbix template show PostgresPro-Linux $OPTIONS | grep PostgresPro-Linux
5858
mamonsu zabbix template id PostgresPro-Linux $OPTIONS | grep -x -E "[[:digit:]]+" || exit 11
5959
TEMPLATE_ID=`mamonsu zabbix template id PostgresPro-Linux $OPTIONS`
6060
mamonsu zabbix template delete $TEMPLATE_ID $OPTIONS | grep "templateids.*$TEMPLATE_ID" || exit 11
61-
61+
mamonsu zabbix version $OPTIONS
6262
# test 2 mamonsu zabbix - Environment variables
6363

6464
export ZABBIX_URL=$Z_URL
@@ -73,5 +73,6 @@ mamonsu zabbix template show PostgresPro-Linux | grep PostgresPro-Linux || exit
7373
mamonsu zabbix template id PostgresPro-Linux | grep -x -E "[[:digit:]]+" || exit 11
7474
TEMPLATE_ID=`mamonsu zabbix template id PostgresPro-Linux`
7575
mamonsu zabbix template delete $TEMPLATE_ID | grep "templateids.*$TEMPLATE_ID" || exit 11
76+
mamonsu zabbix version
7677

7778
exit 0

0 commit comments

Comments
 (0)