forked from erwen/zabbix_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplete.py
132 lines (120 loc) · 3.3 KB
/
templete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from xml.etree.ElementTree import Element, SubElement, Comment, tostring, ElementTree
from xml.etree import ElementTree
from xml.dom import minidom
import os
import sys
help='''python template.py $text_file $xml_file
$text_file formart
#######################################
~Customer - test
srv-test-lb1
srv-test-web1
srv-test-db1
######################################'''
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
def check_arg():
if len(sys.argv) != 3:
print help
sys.exit()
def check_xml(file):
if os.path.isfile(file):
print 'The xml file is already there'
sys.exit()
def check_text(file):
try:
os.path.isfile(file)
except IOError:
print 'The text file is not found'
sys.exit()
pass
def get_ioformart():
answer=raw_input('please defind the io graph,default for aliyun(IOStat hda-xvdb %util):')
if answer == '':
io_graph='IOStat hda-xvdb %util'
else:
io_graph=answer
z_list=['CPU Load Average', 'Memory Usage', io_graph, 'Eth0 Network Traffic', 'Eth1 Network Traffic']
return z_list
def get_server(X, Y, NAME, HOST):
item = SubElement(items, 'screen_item')
resourcetype=SubElement(item, 'resourcetype')
resourcetype.text='0'
width=SubElement(item, 'width')
width.text='200'
height=SubElement(item, 'height')
height.text='100'
x=SubElement(item, 'x')
x.text=str(X)
y=SubElement(item, 'y')
y.text=str(Y)
colspan=SubElement(item, 'colspan')
colspan.text='1'
rowspan=SubElement(item, 'rowspan')
rowspan.text='1'
elements=SubElement(item, 'elements')
elements.text='0'
valign=SubElement(item, 'valign')
valign.text='0'
halign=SubElement(item, 'halign')
halign.text='0'
style=SubElement(item, 'style')
style.text='0'
url=SubElement(item, 'url')
dynamic=SubElement(item, 'dynamic')
dynamic.text='0'
sort_triggers=SubElement(item, 'sort_triggers')
sort_triggers.text='0'
resource=SubElement(item, 'resource')
name=SubElement(resource, 'name')
name.text=HOST
host=SubElement(resource, 'host')
host.text=NAME
max_columns=SubElement(item, 'max_columns')
max_columns.text='1'
application=SubElement(item, 'application')
check_arg()
t_file = sys.argv[1]
x_file = sys.argv[2]
s_list=[ ]
f_list=[ ]
check_text(t_file)
check_xml(x_file)
# defined graphs list(z_list)
z_list = get_ioformart()
f = open(t_file, 'r')
for f_line in f.readlines():
f_line=f_line.strip('\n')
f_list.append(f_line)
f.close()
Vsize = len(f_list) - 1
cus_name = f_list[0]
cus=f_list[0]
del f_list[0]
zabbix_import = Element('zabbix_export')
version = SubElement(zabbix_import, 'version')
version.text = '2.0'
date = SubElement(zabbix_import, 'date')
date.text = '11:53'
screens = SubElement(zabbix_import, 'screens')
screen = SubElement(screens, 'screen')
name = SubElement(screen, 'name')
name.text = cus_name
hsize = SubElement(screen, 'hsize')
hsize.text = '5'
vsize = SubElement(screen, 'vsize')
vsize.text=str(Vsize)
items = SubElement(screen, 'screen_items')
for Host in f_list:
Y=f_list.index(Host)
for Zabbix in z_list:
X=z_list.index(Zabbix)
get_server(X, Y, Host, Zabbix)
xml_output = prettify(zabbix_import)
f=open(x_file, 'w')
f.write(xml_output)
f.close()