File tree Expand file tree Collapse file tree 4 files changed +648
-0
lines changed Expand file tree Collapse file tree 4 files changed +648
-0
lines changed Original file line number Diff line number Diff line change
1
+ FROM python:2.7
2
+
3
+ ENV ZBX_API_URL=http://zabbix-web:8080
4
+ ENV ZBX_API_USER="Admin"
5
+ ENV ZBX_API_PASSWORD="zabbix"
6
+ ENV ZBX_CONFIG="zbx_export_hosts.xml"
7
+ ENV ZBX_BOOTSTRAP_SCRIPT="bootstrap_config.py"
8
+
9
+ RUN pip install pyzabbix
10
+
11
+ ADD ./bootstrap_config.py /bootstrap_config.py
12
+ ADD ${ZBX_CONFIG} /${ZBX_CONFIG}
13
+
14
+ WORKDIR /
15
+
16
+ # Run bootstrap_config.py when the container launches
17
+ CMD ["python" , "/bootstrap_config.py" ]
Original file line number Diff line number Diff line change
1
+ import os
2
+ from time import sleep
3
+ from pyzabbix import ZabbixAPI , ZabbixAPIException
4
+
5
+ zabbix_url = os .environ ['ZBX_API_URL' ]
6
+ zabbix_user = os .environ ['ZBX_API_USER' ]
7
+ zabbix_password = os .environ ['ZBX_API_PASSWORD' ]
8
+ print (zabbix_url , zabbix_user , zabbix_password )
9
+
10
+ zapi = ZabbixAPI (zabbix_url , timeout = 5 )
11
+
12
+ for i in range (10 ):
13
+ print ("Trying to connected to Zabbix API %s" % zabbix_url )
14
+ try :
15
+ zapi .login (zabbix_user , zabbix_password )
16
+ print ("Connected to Zabbix API Version %s" % zapi .api_version ())
17
+ break
18
+ except ZabbixAPIException as e :
19
+ print e
20
+ sleep (5 )
21
+ except :
22
+ print ("Waiting" )
23
+ sleep (5 )
24
+
25
+
26
+ config_path = os .environ ['ZBX_CONFIG' ]
27
+ import_rules = {
28
+ 'discoveryRules' : {
29
+ 'createMissing' : True ,
30
+ 'updateExisting' : True
31
+ },
32
+ 'graphs' : {
33
+ 'createMissing' : True ,
34
+ 'updateExisting' : True
35
+ },
36
+ 'host_groups' : {
37
+ 'createMissing' : True
38
+ },
39
+ 'hosts' : {
40
+ 'createMissing' : True ,
41
+ 'updateExisting' : True
42
+ },
43
+ 'images' : {
44
+ 'createMissing' : True ,
45
+ 'updateExisting' : True
46
+ },
47
+ 'items' : {
48
+ 'createMissing' : True ,
49
+ 'updateExisting' : True
50
+ },
51
+ 'maps' : {
52
+ 'createMissing' : True ,
53
+ 'updateExisting' : True
54
+ },
55
+ 'templateLinkage' : {
56
+ 'createMissing' : True ,
57
+ },
58
+ 'templates' : {
59
+ 'createMissing' : True ,
60
+ 'updateExisting' : True
61
+ },
62
+ 'triggers' : {
63
+ 'createMissing' : True ,
64
+ 'updateExisting' : True
65
+ },
66
+ }
67
+
68
+ print ("Importing Zabbix config from %s" % config_path )
69
+ with open (config_path , 'r' ) as f :
70
+ config = f .read ()
71
+
72
+ try :
73
+ # https://github.com/lukecyca/pyzabbix/issues/62
74
+ import_result = zapi .confimport ("xml" , config , import_rules )
75
+ print (import_result )
76
+ except ZabbixAPIException as e :
77
+ print e
78
+
79
+ for h in zapi .host .get (output = "extend" ):
80
+ print (h ['name' ])
You can’t perform that action at this time.
0 commit comments