Skip to content

Commit 8c0d679

Browse files
committed
Created disttools packaging
1 parent 38c66dd commit 8c0d679

File tree

7 files changed

+191
-149
lines changed

7 files changed

+191
-149
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
*.xml
2+
*.egg-info/
3+
build/
4+
dist/
File renamed without changes.

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
all: dist
2+
3+
dist:
4+
python setup.py sdist
5+
6+
install:
7+
pip install --upgrade .
8+
9+
clean:
10+
rm -vrf \
11+
zabbix_template_converter.egg-info \
12+
build \
13+
dist
14+
15+
upload:
16+
python setup.py sdist upload
17+
18+
.PHONY: all dist install clean upload

README.md

Lines changed: 0 additions & 105 deletions
This file was deleted.

README.rst

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
zabbix-template-converter
2+
=========================
3+
4+
This Python script aims to resolve compatability issues when migrating Zabbix
5+
template XML files between versions of Zabbix. For example, you may wish to
6+
import a Zabbix v3.2 template into Zabbix v2.0.
7+
8+
The script works by applying conversion rules to a template, which manipulate
9+
the template XML to match the desired Zabbix version template format.
10+
11+
__WARNING__: This project is still under active development and not ready for
12+
release.
13+
14+
.. code-block:: shell
15+
16+
$ zabbix-template-convertor -h
17+
usage: zabbix-template-convertor [-h] [-v] -o X.Y.Z [-s] file
18+
19+
Migrate Zabbix templates between versions
20+
21+
positional arguments:
22+
file Zabbix template XML file
23+
24+
optional arguments:
25+
-h, --help show this help message and exit
26+
-v, --version show program's version number and exit
27+
-o X.Y.Z, --output-version X.Y.Z
28+
target Zabbix version
29+
-s, --squash-value-maps
30+
remove references to value maps for versions older
31+
than 3.0.0
32+
33+
Examples
34+
--------
35+
36+
To convert a Zabbix 3.2 template for import into v2.0:
37+
38+
.. code-block:: shell
39+
40+
$ zabbix-template-convertor -o 2.0 my_template.xml > my_template-2.0.xml
41+
42+
A number of transformations will take place. For example, Discovery Rule
43+
filters will be downgraded from the multiple-filter format introduced in Zabbix 2.4, to a single filter expression as follows:
44+
45+
.. code-block:: xml
46+
47+
<filter>
48+
<evaltype>0</evaltype>
49+
<formula/>
50+
<conditions>
51+
<condition>
52+
<macro>{#IFNAME}</macro>
53+
<value>@Network interfaces for discovery</value>
54+
<operator>8</operator>
55+
<formulaid>A</formulaid>
56+
</condition>
57+
</conditions>
58+
</filter>
59+
60+
Becomes:
61+
62+
.. code-block:: xml
63+
64+
<filter>{#IFNAME}:@Network interfaces for discovery</filter>
65+
66+
Coverage
67+
--------
68+
69+
This project relies heavily on the community to report incompatability problems
70+
when importing templates.
71+
72+
__Please raise an issue__ if you find a template that won't import after being
73+
converted. Be sure to include the error messages and template file.
74+
75+
Over time, as conversion rules are added, the script should become more comprehensive, and more reliable.
76+
77+
78+
Installation
79+
------------
80+
81+
Copy the script file to your bin directory and add the executable flag:
82+
83+
.. code-block:: shell
84+
85+
$ curl -Lo /bin/zabbix-template-convertor \
86+
https://raw.githubusercontent.com/cavaliercoder/zabbix-template-convertor/master/zabbix-template-convertor
87+
$ chmod +x /bin/zabbix-template-convertor
Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,6 @@ import re
55

66
__version__ = '1.0.0'
77

8-
# parse cmdline args
9-
parser = argparse.ArgumentParser(description='Migrate Zabbix templates between versions')
10-
parser.add_argument('-v', '--version',
11-
action='version',
12-
version='%%(prog)s %s' % __version__)
13-
14-
parser.add_argument('-o', '--output-version',
15-
help='target Zabbix version',
16-
dest='output_version',
17-
type=str,
18-
metavar='X.Y.Z',
19-
required=True)
20-
21-
parser.add_argument('-s', '--squash-value-maps',
22-
help='remove references to value maps for versions older than 3.0.0',
23-
dest='squash_value_maps',
24-
action='store_true')
25-
26-
parser.add_argument('file',
27-
help='Zabbix template XML file',
28-
type=file)
29-
30-
args = parser.parse_args()
31-
328
def debug(msg):
339
from sys import stderr
3410
stderr.write('%s\n' % msg)
@@ -302,23 +278,55 @@ class FixLastTriggerFunction(ConversionRule):
302278
):
303279
node.text = re.sub(r"\.last\(\)", '.last(0)', node.text)
304280

305-
# read xml template
306-
doc = ET.parse(args.file)
307-
root = doc.getroot()
308-
309-
# load rules
310-
rules = []
311-
for c in ConversionRule.__subclasses__():
312-
rules.append(c(root, args.output_version))
313-
314-
# apply rules
315-
for rule in rules:
316-
try:
317-
rule.apply()
318-
debug('Applied: %s' % rule)
319-
except NotApplicableError:
320-
pass
321-
322-
# print modified template
323-
print('<?xml version="1.0" encoding="UTF-8"?>')
324-
ET.dump(doc)
281+
def __main__():
282+
"""
283+
Script entry point.
284+
"""
285+
286+
# parse cmdline args
287+
parser = argparse.ArgumentParser(description='Migrate Zabbix templates between versions')
288+
parser.add_argument('-v', '--version',
289+
action='version',
290+
version='%%(prog)s %s' % __version__)
291+
292+
parser.add_argument('-o', '--output-version',
293+
help='target Zabbix version',
294+
dest='output_version',
295+
type=str,
296+
metavar='X.Y.Z',
297+
required=True)
298+
299+
parser.add_argument('-s', '--squash-value-maps',
300+
help='remove references to value maps for versions older than 3.0.0',
301+
dest='squash_value_maps',
302+
action='store_true')
303+
304+
parser.add_argument('file',
305+
help='Zabbix template XML file',
306+
type=file)
307+
308+
args = parser.parse_args()
309+
310+
# read xml template
311+
doc = ET.parse(args.file)
312+
root = doc.getroot()
313+
314+
# load rules
315+
rules = []
316+
for c in ConversionRule.__subclasses__():
317+
rules.append(c(root, args.output_version))
318+
319+
# apply rules
320+
for rule in rules:
321+
try:
322+
rule.apply()
323+
debug('Applied: %s' % rule)
324+
except NotApplicableError:
325+
pass
326+
327+
# print modified template
328+
print('<?xml version="1.0" encoding="UTF-8"?>')
329+
ET.dump(doc)
330+
331+
if __name__ == '__main__':
332+
__main__()

setup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
distutils/setuptools install script.
5+
"""
6+
7+
import os
8+
import re
9+
10+
from setuptools import setup
11+
12+
ROOT = os.path.dirname(__file__)
13+
VERSION_RE = re.compile(r'''__version__ = ['"]([0-9.]+)['"]''')
14+
15+
def get_version():
16+
"""
17+
get_version reads the version info from bin/vpc-free:__version__
18+
"""
19+
20+
init = open(os.path.join(ROOT, 'bin', 'zabbix-template-converter')).read()
21+
return VERSION_RE.search(init).group(1)
22+
23+
setup(name='zabbix-template-converter',
24+
version=get_version(),
25+
description='Convert Zabbix templates to older versions.',
26+
long_description=open('README.rst').read(),
27+
url='https://github.com/cavaliercoder/zabbix-template-converter',
28+
author='Ryan Armstrong',
29+
author_email='ryan@cavaliercoder.com',
30+
license='GNU Public License v3.0',
31+
scripts=['bin/zabbix-template-converter'])

0 commit comments

Comments
 (0)