-
Notifications
You must be signed in to change notification settings - Fork 2
/
TemplateExport.cs
294 lines (256 loc) · 16.3 KB
/
TemplateExport.cs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
namespace ZabbixTemplates
{
using System;
using System.IO;
using System.Text;
using System.Xml;
public class TemplateExport
{
public TemplateExport()
{
Templates = new TemplateCollection();
Groups = new GroupSet();
}
private string _version = "3.0";
public string Version
{
get { return _version; }
set { _version = value; }
}
public string Date {
get { return DateTime.UtcNow.ToString(@"yyyy-MM-dd\THH:mm:ss\Z"); }
}
public TemplateCollection Templates { get; protected set; }
public GroupSet Groups { get; protected set; }
#region Serialization
public XmlDocument ToXmlDocument()
{
// create document
var doc = new XmlDocument();
var dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.InsertBefore(dec, doc.DocumentElement);
// create root
var root = doc.AppendChild(doc.CreateElement("zabbix_export"));
root.AppendChild(doc.CreateElement("version")).InnerText = Version;
root.AppendChild(doc.CreateElement("date")).InnerText = Date;
// append groups
var groupsNode = root.AppendChild(doc.CreateElement("groups"));
foreach (var group in Groups)
{
groupsNode.AppendChild(doc.CreateElement("group")).AppendChild(doc.CreateElement("name")).InnerText = group;
}
// append templates
var templatesNode = root.AppendChild(doc.CreateElement("templates"));
foreach (var template in Templates)
{
var templateNode = templatesNode.AppendChild(doc.CreateElement("template"));
templateNode.AppendChild(doc.CreateElement("template")).InnerText = template.Name;
templateNode.AppendChild(doc.CreateElement("name")).InnerText = template.Name;
templateNode.AppendChild(doc.CreateElement("description")).InnerText = template.Description;
// append template groups
var templateGroupsNode = templateNode.AppendChild(doc.CreateElement("groups"));
foreach (var group in template.Groups)
{
templateGroupsNode.AppendChild(doc.CreateElement("group")).AppendChild(doc.CreateElement("name")).InnerText = group;
}
// append template applications
var applicationsNode = templateNode.AppendChild(doc.CreateElement("applications"));
foreach (var app in template.Applications)
{
applicationsNode.AppendChild(doc.CreateElement("application")).AppendChild(doc.CreateElement("name")).InnerText = app;
}
// append template items
var itemsNode = templateNode.AppendChild(doc.CreateElement("items"));
foreach (var item in template.Items)
{
var itemNode = itemsNode.AppendChild(doc.CreateElement("item"));
itemNode.AppendChild(doc.CreateElement("name")).InnerText = item.Name;
itemNode.AppendChild(doc.CreateElement("description")).InnerText = item.Description;
itemNode.AppendChild(doc.CreateElement("type")).InnerText = ((int)item.ItemType).ToString();
itemNode.AppendChild(doc.CreateElement("key")).InnerText = item.Key;
itemNode.AppendChild(doc.CreateElement("delay")).InnerText = item.Delay.ToString();
itemNode.AppendChild(doc.CreateElement("history")).InnerText = item.History.ToString();
itemNode.AppendChild(doc.CreateElement("trends")).InnerText = item.Trends.ToString();
itemNode.AppendChild(doc.CreateElement("status")).InnerText = ((int)item.Status).ToString();
// append item applications
var itemApplicationsNode = itemNode.AppendChild(doc.CreateElement("applications"));
foreach (var app in item.Applications)
{
itemApplicationsNode.AppendChild(doc.CreateElement("application")).AppendChild(doc.CreateElement("name")).InnerText = app;
}
// dummy values
itemNode.AppendChild(doc.CreateElement("allowed_hosts"));
itemNode.AppendChild(doc.CreateElement("authtype")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("data_type")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("delay_flex"));
itemNode.AppendChild(doc.CreateElement("delta")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("formula")).InnerText = "1";
itemNode.AppendChild(doc.CreateElement("inventory_link")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("ipmi_sensor"));
itemNode.AppendChild(doc.CreateElement("logtimefmt"));
itemNode.AppendChild(doc.CreateElement("multiplier")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("params"));
itemNode.AppendChild(doc.CreateElement("password"));
itemNode.AppendChild(doc.CreateElement("port"));
itemNode.AppendChild(doc.CreateElement("privatekey"));
itemNode.AppendChild(doc.CreateElement("publickey"));
itemNode.AppendChild(doc.CreateElement("snmp_community"));
itemNode.AppendChild(doc.CreateElement("snmp_oid"));
itemNode.AppendChild(doc.CreateElement("snmpv3_authpassphrase"));
itemNode.AppendChild(doc.CreateElement("snmpv3_authprotocol")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("snmpv3_contextname"));
itemNode.AppendChild(doc.CreateElement("snmpv3_privpassphrase"));
itemNode.AppendChild(doc.CreateElement("snmpv3_privprotocol")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("snmpv3_securitylevel")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("snmpv3_securityname"));
itemNode.AppendChild(doc.CreateElement("units"));
itemNode.AppendChild(doc.CreateElement("username"));
itemNode.AppendChild(doc.CreateElement("value_type")).InnerText = "0";
itemNode.AppendChild(doc.CreateElement("valuemap"));
}
// append Discovery rules
var discoveryRulesNode = templateNode.AppendChild(doc.CreateElement("discovery_rules"));
foreach (var discoveryRule in template.DiscoveryRules)
{
var discoveryRuleNode = discoveryRulesNode.AppendChild(doc.CreateElement("discovery_rule"));
discoveryRuleNode.AppendChild(doc.CreateElement("name")).InnerText = discoveryRule.Name;
discoveryRuleNode.AppendChild(doc.CreateElement("description")).InnerText = discoveryRule.Description;
discoveryRuleNode.AppendChild(doc.CreateElement("type")).InnerText = ((int)discoveryRule.ItemType).ToString();
discoveryRuleNode.AppendChild(doc.CreateElement("key")).InnerText = discoveryRule.Key;
discoveryRuleNode.AppendChild(doc.CreateElement("delay")).InnerText = discoveryRule.Delay.ToString();
discoveryRuleNode.AppendChild(doc.CreateElement("status")).InnerText = ((int)discoveryRule.Status).ToString();
discoveryRuleNode.AppendChild(doc.CreateElement("lifetime")).InnerText = ((int)discoveryRule.Lifetime).ToString();
discoveryRuleNode.AppendChild(doc.CreateElement("trigger_prototypes"));
discoveryRuleNode.AppendChild(doc.CreateElement("graph_prototypes"));
discoveryRuleNode.AppendChild(doc.CreateElement("host_prototypes"));
// dummy values
discoveryRuleNode.AppendChild(doc.CreateElement("allowed_hosts"));
discoveryRuleNode.AppendChild(doc.CreateElement("authtype")).InnerText = "0";
discoveryRuleNode.AppendChild(doc.CreateElement("delay_flex"));
discoveryRuleNode.AppendChild(doc.CreateElement("ipmi_sensor"));
discoveryRuleNode.AppendChild(doc.CreateElement("params"));
discoveryRuleNode.AppendChild(doc.CreateElement("password"));
discoveryRuleNode.AppendChild(doc.CreateElement("port"));
discoveryRuleNode.AppendChild(doc.CreateElement("privatekey"));
discoveryRuleNode.AppendChild(doc.CreateElement("publickey"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmp_community"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmp_oid"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_authpassphrase"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_authprotocol")).InnerText = "0";
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_contextname"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_privpassphrase"));
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_privprotocol")).InnerText = "0";
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_securitylevel")).InnerText = "0";
discoveryRuleNode.AppendChild(doc.CreateElement("snmpv3_securityname"));
discoveryRuleNode.AppendChild(doc.CreateElement("username"));
var filterNode = discoveryRuleNode.AppendChild(doc.CreateElement("filter"));
filterNode.AppendChild(doc.CreateElement("evaltype")).InnerText = "0";
filterNode.AppendChild(doc.CreateElement("formula"));
filterNode.AppendChild(doc.CreateElement("conditions"));
var itemPrototypesNode = discoveryRuleNode.AppendChild(doc.CreateElement("item_prototypes"));
foreach (var itemPrototype in discoveryRule.ItemPrototypes)
{
var itemPrototypeNode = itemPrototypesNode.AppendChild(doc.CreateElement("item_prototype"));
itemPrototypeNode.AppendChild(doc.CreateElement("name")).InnerText = itemPrototype.Name;
itemPrototypeNode.AppendChild(doc.CreateElement("description")).InnerText = itemPrototype.Description;
itemPrototypeNode.AppendChild(doc.CreateElement("type")).InnerText = ((int)itemPrototype.ItemType).ToString();
itemPrototypeNode.AppendChild(doc.CreateElement("key")).InnerText = itemPrototype.Key;
itemPrototypeNode.AppendChild(doc.CreateElement("delay")).InnerText = itemPrototype.Delay.ToString();
itemPrototypeNode.AppendChild(doc.CreateElement("history")).InnerText = itemPrototype.History.ToString();
itemPrototypeNode.AppendChild(doc.CreateElement("trends")).InnerText = itemPrototype.Trends.ToString();
itemPrototypeNode.AppendChild(doc.CreateElement("status")).InnerText = ((int)itemPrototype.Status).ToString();
// append item applications
var itemApplicationsNode = itemPrototypeNode.AppendChild(doc.CreateElement("applications"));
foreach (var app in itemPrototype.Applications)
{
itemApplicationsNode.AppendChild(doc.CreateElement("application")).AppendChild(doc.CreateElement("name")).InnerText = app;
}
// dummy values
itemPrototypeNode.AppendChild(doc.CreateElement("allowed_hosts"));
itemPrototypeNode.AppendChild(doc.CreateElement("application_prototypes"));
itemPrototypeNode.AppendChild(doc.CreateElement("authtype")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("data_type")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("delay_flex"));
itemPrototypeNode.AppendChild(doc.CreateElement("delta")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("formula")).InnerText = "1";
itemPrototypeNode.AppendChild(doc.CreateElement("inventory_link")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("ipmi_sensor"));
itemPrototypeNode.AppendChild(doc.CreateElement("logtimefmt"));
itemPrototypeNode.AppendChild(doc.CreateElement("multiplier")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("params"));
itemPrototypeNode.AppendChild(doc.CreateElement("password"));
itemPrototypeNode.AppendChild(doc.CreateElement("port"));
itemPrototypeNode.AppendChild(doc.CreateElement("privatekey"));
itemPrototypeNode.AppendChild(doc.CreateElement("publickey"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmp_community"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmp_oid"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_authpassphrase"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_authprotocol")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_contextname"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_privpassphrase"));
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_privprotocol")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_securitylevel")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("snmpv3_securityname"));
itemPrototypeNode.AppendChild(doc.CreateElement("units"));
itemPrototypeNode.AppendChild(doc.CreateElement("username"));
itemPrototypeNode.AppendChild(doc.CreateElement("value_type")).InnerText = "0";
itemPrototypeNode.AppendChild(doc.CreateElement("valuemap"));
}
}
}
return doc;
}
public void ToXmlStream(StreamWriter w)
{
// write doc header
w.WriteLine(String.Format(
@"<?xml version=""1.0"" encoding=""{0}""?>
<zabbix_export>
<version>{1}</version>
<date>{2}</date>
<groups />
<templates>", w.Encoding.WebName, Version, Date));
// write each template
foreach(var template in Templates)
{
w.WriteLine(String.Format(
@" <template>
<template>{0}</template>
<name>{0}</name>
<description>{1}</description>
</template>", template.Name, template.Description));
}
// close templates section
w.WriteLine(" </templates>");
// close document
w.WriteLine("</zabbix_export>");
}
public string ToXmlString()
{
string result;
/*
// write XML data to memory
using (var m = new MemoryStream()) {
using (var w = new StreamWriter(m, Encoding.UTF8))
{
ToXmlStream(w);
w.Flush();
m.Position = 0;
using (var r = new StreamReader(m, Encoding.UTF8))
{
result = r.ReadToEnd();
}
}
}
return result;
* */
using (var w = new StringWriter()) {
var doc = ToXmlDocument();
doc.Save(w);
result = w.ToString();
}
return result;
}
#endregion
}
}