Skip to content

Commit

Permalink
Created disttools packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
cavaliercoder committed Jun 17, 2017
1 parent 38c66dd commit 8c0d679
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 149 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.xml
*.egg-info/
build/
dist/
File renamed without changes.
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
all: dist

dist:
python setup.py sdist

install:
pip install --upgrade .

clean:
rm -vrf \
zabbix_template_converter.egg-info \
build \
dist

upload:
python setup.py sdist upload

.PHONY: all dist install clean upload
105 changes: 0 additions & 105 deletions README.md

This file was deleted.

87 changes: 87 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
zabbix-template-converter
=========================

This Python script aims to resolve compatability issues when migrating Zabbix
template XML files between versions of Zabbix. For example, you may wish to
import a Zabbix v3.2 template into Zabbix v2.0.

The script works by applying conversion rules to a template, which manipulate
the template XML to match the desired Zabbix version template format.

__WARNING__: This project is still under active development and not ready for
release.

.. code-block:: shell
$ zabbix-template-convertor -h
usage: zabbix-template-convertor [-h] [-v] -o X.Y.Z [-s] file
Migrate Zabbix templates between versions
positional arguments:
file Zabbix template XML file
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-o X.Y.Z, --output-version X.Y.Z
target Zabbix version
-s, --squash-value-maps
remove references to value maps for versions older
than 3.0.0
Examples
--------
To convert a Zabbix 3.2 template for import into v2.0:
.. code-block:: shell
$ zabbix-template-convertor -o 2.0 my_template.xml > my_template-2.0.xml
A number of transformations will take place. For example, Discovery Rule
filters will be downgraded from the multiple-filter format introduced in Zabbix 2.4, to a single filter expression as follows:
.. code-block:: xml
<filter>
<evaltype>0</evaltype>
<formula/>
<conditions>
<condition>
<macro>{#IFNAME}</macro>
<value>@Network interfaces for discovery</value>
<operator>8</operator>
<formulaid>A</formulaid>
</condition>
</conditions>
</filter>
Becomes:
.. code-block:: xml
<filter>{#IFNAME}:@Network interfaces for discovery</filter>
Coverage
--------
This project relies heavily on the community to report incompatability problems
when importing templates.
__Please raise an issue__ if you find a template that won't import after being
converted. Be sure to include the error messages and template file.

Over time, as conversion rules are added, the script should become more comprehensive, and more reliable.


Installation
------------

Copy the script file to your bin directory and add the executable flag:

.. code-block:: shell
$ curl -Lo /bin/zabbix-template-convertor \
https://raw.githubusercontent.com/cavaliercoder/zabbix-template-convertor/master/zabbix-template-convertor
$ chmod +x /bin/zabbix-template-convertor
96 changes: 52 additions & 44 deletions zabbix-template-convertor → bin/zabbix-template-converter
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,6 @@ import re

__version__ = '1.0.0'

# parse cmdline args
parser = argparse.ArgumentParser(description='Migrate Zabbix templates between versions')
parser.add_argument('-v', '--version',
action='version',
version='%%(prog)s %s' % __version__)

parser.add_argument('-o', '--output-version',
help='target Zabbix version',
dest='output_version',
type=str,
metavar='X.Y.Z',
required=True)

parser.add_argument('-s', '--squash-value-maps',
help='remove references to value maps for versions older than 3.0.0',
dest='squash_value_maps',
action='store_true')

parser.add_argument('file',
help='Zabbix template XML file',
type=file)

args = parser.parse_args()

def debug(msg):
from sys import stderr
stderr.write('%s\n' % msg)
Expand Down Expand Up @@ -302,23 +278,55 @@ class FixLastTriggerFunction(ConversionRule):
):
node.text = re.sub(r"\.last\(\)", '.last(0)', node.text)

# read xml template
doc = ET.parse(args.file)
root = doc.getroot()

# load rules
rules = []
for c in ConversionRule.__subclasses__():
rules.append(c(root, args.output_version))

# apply rules
for rule in rules:
try:
rule.apply()
debug('Applied: %s' % rule)
except NotApplicableError:
pass

# print modified template
print('<?xml version="1.0" encoding="UTF-8"?>')
ET.dump(doc)
def __main__():
"""
Script entry point.
"""

# parse cmdline args
parser = argparse.ArgumentParser(description='Migrate Zabbix templates between versions')
parser.add_argument('-v', '--version',
action='version',
version='%%(prog)s %s' % __version__)

parser.add_argument('-o', '--output-version',
help='target Zabbix version',
dest='output_version',
type=str,
metavar='X.Y.Z',
required=True)

parser.add_argument('-s', '--squash-value-maps',
help='remove references to value maps for versions older than 3.0.0',
dest='squash_value_maps',
action='store_true')

parser.add_argument('file',
help='Zabbix template XML file',
type=file)

args = parser.parse_args()

# read xml template
doc = ET.parse(args.file)
root = doc.getroot()

# load rules
rules = []
for c in ConversionRule.__subclasses__():
rules.append(c(root, args.output_version))

# apply rules
for rule in rules:
try:
rule.apply()
debug('Applied: %s' % rule)
except NotApplicableError:
pass

# print modified template
print('<?xml version="1.0" encoding="UTF-8"?>')
ET.dump(doc)

if __name__ == '__main__':
__main__()
31 changes: 31 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

"""
distutils/setuptools install script.
"""

import os
import re

from setuptools import setup

ROOT = os.path.dirname(__file__)
VERSION_RE = re.compile(r'''__version__ = ['"]([0-9.]+)['"]''')

def get_version():
"""
get_version reads the version info from bin/vpc-free:__version__
"""

init = open(os.path.join(ROOT, 'bin', 'zabbix-template-converter')).read()
return VERSION_RE.search(init).group(1)

setup(name='zabbix-template-converter',
version=get_version(),
description='Convert Zabbix templates to older versions.',
long_description=open('README.rst').read(),
url='http://github.com/cavaliercoder/zabbix-template-converter',
author='Ryan Armstrong',
author_email='[email protected]',
license='GNU Public License v3.0',
scripts=['bin/zabbix-template-converter'])

0 comments on commit 8c0d679

Please sign in to comment.