Skip to content

Commit 9e43c2d

Browse files
committed
save unicode
1 parent 002ecca commit 9e43c2d

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

README.md

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Zabbix Review and Export
2-
Zabbix review and export (backup) can create review mechanism to zabbix configuration or only export (backup) all hosts templates and other object.
2+
With Zabbix review and export (backup) you can create review mechanism and save zabbix configuration as code (Monitoring as Code)
3+
4+
You can only export (backup) all hosts templates and other object with `zabbix-export.py` script.
35

46
- [Requirements](#requirements)
57
- [Make export and backup](#make-export-and-backup)
@@ -14,47 +16,48 @@ Zabbix review and export (backup) can create review mechanism to zabbix configur
1416

1517

1618
If you want use [review (or Monitoring as Code](#make-review):
17-
- `git`
1819
- [GitLab](https://gitlab.com/) - you own instance with configured [GitLab CI](https://docs.gitlab.com/ee/ci/) or cloud account
20+
- `git`
1921

2022

2123
## Make export and backup
2224
It's simple to start use this script as backup mechanism:
2325
```bash
24-
# git clone THIS_REPO or download archive
26+
# git clone THIS_REPO or download and unpack archive
2527

2628
python -mpip install -r requirements.xml
2729

30+
# smoke test :)
2831
python ./zabbix-export.py --help
2932

30-
# backup to current folder XML and JSON
33+
# backup to current folder, save XML and JSON
3134
python ./zabbix-export.py --zabbix-url https://zabbix.example.com --zabbix-username user --zabbix-password password
3235

33-
# backup YAML
36+
# backup zabbix configuration in YAML format
3437
python ./zabbix-export.py --save-yaml --zabbix-url https://zabbix.example.com --zabbix-username user --zabbix-password password
3538

36-
# backup to custom folder as YAML
39+
# backup to custom folder in YAML format
3740
python ./zabbix-export.py --save-yaml --directory /home/username/path/to/zabbix-yaml --zabbix-url https://zabbix.example.com --zabbix-username user --zabbix-password password
3841
```
3942

4043
## Make review
41-
If you want to make review (Moniroting as Code). Read more on habr.com: [RU](#), [EN translated](#)
44+
You want to make review (Moniroting as Code). Read more on habr.com: [RU](#), [EN translated](#)
4245
1. Fork this repository to you GitLab account or instance (e.g. `groupname/zabbix-review-export`)
43-
2. Create repository where will be saved XML and YAML (e.g. two repository `groupname/zabbix-xml` and `groupname/zabbix-yaml`. And do first commit (create empty `README.md`)
44-
3. Create two branches in this repos: `master` and `develop`. In repository `groupname/zabbix-xml` set `develop` [default branch](https://docs.gitlab.com/ee/user/project/repository/branches/#default-branch).
45-
4. Specify [Project Variables](https://docs.gitlab.com/ee/ci/variables/#variables) for all variables, specified on top in [.gitlab-ci.yml](./.gitlab-ci.yml)
46-
5. Change jobs in `.gitlab-ci.yml` and Leave the ones you need job in `.gitlab-ci.yml` and change to you environment (see commented examples block).
46+
2. Create repository where will be saved XML and YAML (e.g. two repositories `groupname/zabbix-xml` and `groupname/zabbix-yaml`. Do first (init) commit (create empty `README.md`).
47+
3. Create two branches in this repos: `master` and `develop`. In repository `groupname/zabbix-xml` set `develop` as a [default branch](https://docs.gitlab.com/ee/user/project/repository/branches/#default-branch).
48+
4. Specify [Project Variables](https://docs.gitlab.com/ee/ci/variables/#variables) for all variables, specified on top of [.gitlab-ci.yml](./.gitlab-ci.yml)
49+
5. Change jobs in `.gitlab-ci.yml` and leave the ones you need job in `.gitlab-ci.yml` and change to you environment (see commented examples block).
4750
6. Try to run manual job `YAML zabbix`
4851
7. Create merge request `develop=>master` in `zabbix-yaml`. For first time you can merge without review, it's too hard :)
4952
8. Configure [Schedule](https://docs.gitlab.com/ee/user/project/pipelines/schedules.html) (eg. every week)
5053
9. Change some host, template or other [supported objects](#supported-objects) in zabbix, run manual job and create merge request again. Enjoy!
5154

5255
### Notes
53-
Use two different repositories for XML+JSON (raw-format) and readable YAML format:
56+
Use two repositories for XML+JSON (raw-format) and readable YAML format:
5457
- `XML` + `JSON` will be useful if you want restore some object after remove or alarge number of changes.
55-
- `YAML` format is more suitable for people to read and review changes. Also the script removes all empty values.
58+
- `YAML` format is more suitable for people to read and review changes. The script removes all empty values.
5659

57-
Also, after merge we create empty merge request `develop=>master` and receive all notifications at changes (schedule or manual jobs run).
60+
Create empty merge request `develop=>master` after merge and receive notifications at changes (schedule or manual jobs run) on your email.
5861

5962
To answer for the question "Who make this changes?" you need use [Zabbix Audit](https://www.zabbix.com/documentation/4.0/manual/web_interface/frontend_sections/reports/audit). It's difficult but possible.
6063

@@ -80,5 +83,8 @@ YAML change action:
8083
YAML change trigger expression:
8184
![yaml-change-trigger-expression.png](./docs/yaml-change-trigger-expression.png)
8285

86+
YAML link template
87+
![yaml-link-template.jpg](./docs/yaml-link-template.jpg)
88+
8389
XML change templates (but we recommend use YAML for review and XML only for backup):
8490
![xml-change-templates.jpg](./docs/xml-change-templates.jpg)

docs/yaml-link-template.jpg

38.1 KB
Loading

zabbix-export.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ def dumps_json(object, data, directory, key='name', save_yaml=False):
9393
logging.debug("Write to file '{}'".format(filename))
9494

9595
if save_yaml:
96-
txt = convert_to_object_without_none(txt)
96+
txt = convert_to_yaml_without_none(txt)
9797

9898
with open(filename, mode="w", encoding='utf-8', newline='\n') as file:
9999
file.write(txt)
100100

101101

102-
def convert_to_object_without_none(txt):
102+
def convert_to_yaml_without_none(txt):
103103
"""
104104
Convert any object to OrderDict without None value
105105
"""
@@ -108,7 +108,7 @@ def convert_to_object_without_none(txt):
108108
raw = remove_none(raw)
109109
represent_dict_order = lambda self, data: self.represent_mapping('tag:yaml.org,2002:map', data.items()) # noqa
110110
yaml.add_representer(OrderedDict, represent_dict_order)
111-
txt = yaml.dump(raw, default_flow_style=False, width=10000)
111+
txt = yaml.dump(raw, default_flow_style=False, width=10000, allow_unicode=True)
112112
return txt
113113

114114

@@ -140,7 +140,7 @@ def dump_xml(object, txt, name, directory, save_yaml=False):
140140
txt = txt.replace('"', '"')
141141

142142
if save_yaml:
143-
txt = convert_to_object_without_none(txt)
143+
txt = convert_to_yaml_without_none(txt)
144144

145145
logging.debug("Write to file '{}'".format(filename))
146146
with open(filename, mode="w", encoding='utf-8', newline='\n') as file:

0 commit comments

Comments
 (0)