summaryrefslogtreecommitdiffstats
path: root/frontends/php/include/export.inc.php
blob: a4e3649c05654cfbd16207d7419cf0a6bb2d7d35 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php 
/* 
** ZABBIX
** Copyright (C) 2000-2005 SIA Zabbix
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
	
	require_once "include/defines.inc.php";
?>
<?php
	global $ZBX_EXPORT_MAP;
	
	$ZBX_EXPORT_MAP = array(
		XML_TAG_HOST => array(
			'attribures'	=> array(
				'host'		=> 'name'),
			'elements'	=> array(
				'useip'		=> '',
				'dns'		=> '',
				'ip'		=> '',
				'port'		=> '',
				'status'	=> '')
			),
		XML_TAG_DEPENDENCY => array(
			'attribures'	=> array(
				'dependency'	=> 'description'),
			'elements'	=> array(
				'depends'		=> '')
			),
// based on mod by scricca
		XML_TAG_HOSTPROFILE => array(
			'attribures'	=> array(),
			'elements'	=> array(
				'devicetype'	=> '',
				'name'		=> '',
				'os'			=> '',
				'serialno' 	=> '',
				'tag' 		=> '',
				'macaddress' 	=> '',
				'hardware' 	=> '',
				'software' 	=> '',
				'contact' 	=> '',
				'location' 	=> '',
				'notes' 		=> '')
			),
		XML_TAG_ITEM => array(
			'attribures'	=> array(
				'type'			=> '',
				'key_'			=> 'key',
				'value_type'		=> ''),
			'elements'	=> array(
				'description'		=> '',
				'delay'			=> '',
				'history'		=> '',
				'trends'		=> '',
				'status'		=> '',
				'units'			=> '',
				'multiplier'		=> '',
				'delta'			=> '',
				'formula'		=> '',
				'lastlogsize'		=> '',
				'logtimefmt'		=> '',
				'delay_flex'		=> '',
				'params'		=> '',
				'trapper_hosts'		=> '',
				'snmp_community'	=> '',
				'snmp_oid'		=> '',
				'snmp_port'		=> '',
				'snmpv3_securityname'	=> '',
				'snmpv3_securitylevel'	=> '',
				'snmpv3_authpassphrase'	=> '',
				'snmpv3_privpassphrase'	=> '')
			),
		XML_TAG_TRIGGER => array(
			'attribures'	=> array(),
			'elements'	=> array(
				'description'		=> '',
				'type'				=> '',
				'expression'		=> '',
				'url'			=> '',
				'status'		=> '',
				'priority'		=> '',
				'comments'		=> '')
			),
		XML_TAG_GRAPH => array(
			'attribures'	=> array(
				'name'			=> '',
				'width'			=> '',
				'height'		=> ''),
			'elements'	=> array(
				'yaxistype'		=> '',
				'show_work_period'	=> '',
				'show_triggers'		=> '',
				'graphtype'		=> '',
				'yaxismin'		=> '',
				'yaxismax'		=> '',
				'show_legend'	=> '',
				'show_3d'		=> '')
			),
		XML_TAG_GRAPH_ELEMENT => array(
			'attribures'	=> array(
				'item'	=> ''),
			'elements'	=> array(
				'drawtype'	=> '',
				'sortorder'	=> '',
				'color'		=> '',
				'yaxisside'	=> '',
				'calc_fnc'	=> '',
				'type'		=> '',
				'periods_cnt'	=> '')
			)
		);

	function zbx_xmlwriter_open_memory(){
		return array('tabs' => 0, 'tag'=>array());
	}
	
	function zbx_xmlwriter_set_indent($mem, $val){
		return true;
	}
	
	function zbx_xmlwriter_start_document(&$mem, $ver){
		echo '<?xml version="'.$ver.'"?';
		return true;
	}
	
	function zbx_xmlwriter_start_element(&$mem, $tag){
		array_push($mem['tag'], $tag);
		echo '>'."\n".str_repeat("\t",$mem['tabs']).'<'.$tag;
		$mem['tabs']++;
		return true;
	}
	
	function zbx_xmlwriter_write_attribute(&$mem, $name, $val){
		echo ' '.$name.'="'.htmlspecialchars($val).'"';
		return true;
	}
	
	function zbx_xmlwriter_end_element(&$mem){
		$teg = array_pop($mem['tag']);
		$mem['tabs']--;
		echo '>'."\n".str_repeat("\t",$mem['tabs']).'</'.$teg;
	}
	
	function zbx_xmlwriter_output_memory(&$mem, $val){ 
/* NOTE: use this function only in the end of xml file creation */
		echo '>';
	}
	
	function zbx_xmlwriter_write_element(&$mem, $name, $val){
		echo '>'."\n".str_repeat("\t",$mem['tabs']).'<'.$name.'>'.htmlspecialchars($val).'</'.$name;
	}
	
	class CZabbixXMLExport{
		function CZabbixXMLExport(){}
		
		function export_item(&$memory, $itemid){
			global $ZBX_EXPORT_MAP;

			$data = DBfetch(DBselect('select * from items where itemid='.$itemid));
			if(!$data) return false;
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_ITEM);

			$map =& $ZBX_EXPORT_MAP[XML_TAG_ITEM];
			
			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			foreach($map['elements'] as $db_name => $xml_name){
				if(!isset($data[$db_name])) continue;
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
			}

			if( !empty($data['valuemapid']) && $valuemap = DBfetch(DBselect('select name from valuemaps where valuemapid='.$data['valuemapid']))){
				zbx_xmlwriter_write_element($memory, 'valuemap', $valuemap['name']);
			}

			$db_applications=DBselect('select distinct a.name from applications a,items_applications ia '.
				       ' where ia.applicationid=a.applicationid and ia.itemid='.$itemid);
			if($application = DBfetch($db_applications)){
				zbx_xmlwriter_start_element ($memory,XML_TAG_APPLICATIONS);
				do {
					zbx_xmlwriter_write_element ($memory, XML_TAG_APPLICATION, $application['name']);
				} while($application = DBfetch($db_applications));
				zbx_xmlwriter_end_element($memory); // XML_TAG_APPLICATIONS
			}

			zbx_xmlwriter_end_element($memory); // XML_TAG_ITEM
		}

		
		function export_trigger(&$memory, $triggerid){
			global $ZBX_EXPORT_MAP;

			$data = DBfetch(DBselect('select * from triggers where triggerid='.$triggerid));
			if(!$data) return false;

			$data['expression'] = explode_exp($data["expression"],0,true);
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_TRIGGER);
			
			$map =& $ZBX_EXPORT_MAP[XML_TAG_TRIGGER];
			
			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			foreach($map['elements'] as $db_name => $xml_name){
				if(!isset($data[$db_name])) continue;
				if(empty($xml_name)) $xml_name = $db_name;
				
				zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
			}
			zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGER
		}
		
		function export_graph_element(&$memory, $gitemid){
			global $ZBX_EXPORT_MAP;

			$sql = 'select gi.*,i.key_,h.host '.
					' from graphs_items gi, items i, hosts h'.
					' where h.hostid=i.hostid '.
						' and i.itemid=gi.itemid '.
						' and gi.gitemid='.$gitemid;
						
			$data = DBfetch(DBselect($sql));
			if(!$data) return false;

			$data['item'] = '{HOSTNAME}:'.$data['key_'];
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH_ELEMENT);
			
			$map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH_ELEMENT];
			
			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			foreach($map['elements'] as $db_name => $xml_name){
				if(!isset($data[$db_name])) continue;
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
			}
			zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH_ELEMENT
		}

		function export_graph(&$memory, $graphid){
			global $ZBX_EXPORT_MAP;

			$data = DBfetch(DBselect('select * from graphs where graphid='.$graphid));
			if(!$data) return false;
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH);
			
			$map =& $ZBX_EXPORT_MAP[XML_TAG_GRAPH];
			
			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			foreach($map['elements'] as $db_name => $xml_name){
				if(!isset($data[$db_name])) continue;
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
			}
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_GRAPH_ELEMENTS);
			
			$db_elements = DBselect('select gitemid from graphs_items where graphid='.$graphid);
			while($element = DBfetch($db_elements)){
				$this->export_graph_element($memory, $element['gitemid']);
			}
				
			zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH_ELEMENTS
			zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPH
		}
		
		function export_host(&$memory, $hostid, $export_templates, $export_items, $export_triggers, $export_graphs){
			global $ZBX_EXPORT_MAP;

			$data = DBfetch(DBselect('select * from hosts where hostid='.$hostid));
			if(!$data) return false;
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_HOST);
			
			$map =& $ZBX_EXPORT_MAP[XML_TAG_HOST];

			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			foreach($map['elements'] as $db_name => $xml_name){
				if(!isset($data[$db_name])) continue;
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
			}
			
// based on  mod by scricca
			$data = DBfetch(DBselect('SELECT hp.* FROM hosts_profiles hp WHERE hp.hostid='.$hostid));
//			if(!$data) return false;
			if($data){
				zbx_xmlwriter_start_element ($memory,XML_TAG_HOSTPROFILE);
				
				$map =& $ZBX_EXPORT_MAP[XML_TAG_HOSTPROFILE];
	
				foreach($map['attribures'] as $db_name => $xml_name){
					if(empty($xml_name)) $xml_name = $db_name;
					zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
				}
				
				foreach($map['elements'] as $db_name => $xml_name){
					if(empty($data[$db_name])) continue;
					if(empty($xml_name)) $xml_name = $db_name;
					zbx_xmlwriter_write_element($memory, $xml_name, $data[$db_name]);
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTPROFILE
			}
//--
			$sql = 'select g.name '.
					' from groups g, hosts_groups hg '.
					' where g.groupid=hg.groupid '.
						' and hg.hostid='.$hostid;
			if($db_groups = DBselect()){
				zbx_xmlwriter_start_element ($memory,XML_TAG_GROUPS);
				
				while($group = DBfetch($db_groups)){
					zbx_xmlwriter_write_element ($memory, XML_TAG_GROUP, $group['name']);
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_GROUP
			}
			
			if($export_templates){
				$sql = 'SELECT t.host '.
					' FROM hosts t, hosts_templates ht '.
					' WHERE t.hostid=ht.templateid '.
						' AND ht.hostid='.$hostid;
							
				if($db_templates = DBselect($sql)){
					zbx_xmlwriter_start_element ($memory,XML_TAG_TEMPLATES);

					while($template = DBfetch($db_templates)){
						zbx_xmlwriter_write_element ($memory, XML_TAG_TEMPLATE, $template['host']);
					}
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_TEMPLATES
			}

			if($export_items){
				zbx_xmlwriter_start_element ($memory,XML_TAG_ITEMS);
				
				$db_items=DBselect('select itemid from items where hostid='.$hostid);
				while($item_id = DBfetch($db_items)){
					$this->export_item($memory, $item_id['itemid']);
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_ITEMS
			}
			
			
			if($export_triggers){
				zbx_xmlwriter_start_element ($memory,XML_TAG_TRIGGERS);
				
				$db_triggers = DBselect('select f.triggerid, i.hostid, count(distinct i.hostid) as cnt '.
					' from functions f, items i '.
					' where f.itemid=i.itemid '.
					' group by f.triggerid, i.hostid');
				while($trigger = DBfetch($db_triggers)){
					if((bccomp($trigger['hostid'] , $hostid) != 0) || $trigger['cnt']!=1) continue;
					$this->export_trigger($memory, $trigger['triggerid']);
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_TRIGGERS
			}
			if($export_graphs){
				zbx_xmlwriter_start_element ($memory, XML_TAG_GRAPHS);
				
				$db_graphs = DBselect('select gi.graphid, i.hostid, count(distinct i.hostid) as cnt '.
					' from graphs_items gi, items i '.
					' where gi.itemid=i.itemid '.
					' group by gi.graphid, i.hostid');
				while($graph = DBfetch($db_graphs)){
					if((bccomp($graph['hostid'] , $hostid) != 0) || $graph['cnt']!=1) continue;
					$this->export_graph($memory, $graph['graphid']);
				}
				
				zbx_xmlwriter_end_element($memory); // XML_TAG_GRAPHS
			}
			/* export screens */
			zbx_xmlwriter_end_element($memory); // XML_TAG_HOST
			return true;
		}
		
		function export_dependency(&$memory, $triggerid, $host, $description){
			global $ZBX_EXPORT_MAP;
			if(!$triggerid) return false;			
			
			$data['dependency'] = $host.':'.$description;
			
			zbx_xmlwriter_start_element ($memory,XML_TAG_DEPENDENCY);
			
			$map =& $ZBX_EXPORT_MAP[XML_TAG_DEPENDENCY];

			foreach($map['attribures'] as $db_name => $xml_name){
				if(empty($xml_name)) $xml_name = $db_name;
				zbx_xmlwriter_write_attribute($memory, $xml_name, $data[$db_name]);
			}
			
			$sql = 'SELECT t.triggerid, t.description, h.host'.
					' FROM trigger_depends td, triggers t, items i, hosts h, functions f '.
					' WHERE td.triggerid_down='.$triggerid.
						' AND t.triggerid=td.triggerid_up '.
						' AND f.triggerid=t.triggerid '.
						' AND i.itemid=f.itemid '.
						' AND h.hostid=i.hostid '.
					' GROUP BY t.triggerid, t.description';			

			$res = DBselect($sql);
			while($data = DBfetch($res)){
				$data['depends'] = $data['host'].':'.$data['description'];
				foreach($map['elements'] as $db_name => $xml_name){
					if(!isset($data[$db_name])) continue;
					if(empty($xml_name)) $xml_name = $db_name;
					zbx_xmlwriter_write_element ($memory, $xml_name, $data[$db_name]);
				}
			}
//--
			/* export screens */
			zbx_xmlwriter_end_element($memory); // XML_TAG_DEPENDENCY
			return true;
		}

		function Export(&$hosts, &$templates, &$items, &$triggers, &$graphs){
		
			$memory = zbx_xmlwriter_open_memory();
			zbx_xmlwriter_set_indent($memory, true);
			zbx_xmlwriter_start_document($memory,'1.0');
			zbx_xmlwriter_start_element ($memory,XML_TAG_ZABBIX_EXPORT);
			zbx_xmlwriter_write_attribute($memory, 'version', '1.0');
			zbx_xmlwriter_write_attribute($memory, 'date', date('d.m.y'));
			zbx_xmlwriter_write_attribute($memory, 'time', date('h.i'));
			
				zbx_xmlwriter_start_element ($memory,XML_TAG_HOSTS);
				foreach($hosts as $hostid => $val){
					$this->export_host(
						$memory,
						$hostid,
						isset($templates[$hostid]),
						isset($items[$hostid]),
						isset($triggers[$hostid]),
						isset($graphs[$hostid])
						);
				}
				zbx_xmlwriter_end_element($memory); // XML_TAG_HOSTS
				
				if(!empty($triggers)){

					zbx_xmlwriter_start_element ($memory,XML_TAG_DEPENDENCIES);
					foreach($triggers as $hostid => $val){
						
						$sql = 'SELECT h.host, t.description, t.triggerid '.
								' FROM trigger_depends td, triggers t, functions f, items i, hosts h '.
								' WHERE h.hostid='.$hostid.
									' AND i.hostid=h.hostid '.
									' AND f.itemid=i.itemid '.
									' AND t.triggerid=f.triggerid '.
									' AND td.triggerid_down=t.triggerid '.
								' GROUP BY t.triggerid, t.description';

						$dependent_triggers = DBselect($sql);
						while($deps = DBfetch($dependent_triggers)){
							$this->export_dependency(
								$memory,
								$deps['triggerid'],
								$deps['host'],
								$deps['description']
								);						
						}
					}
					zbx_xmlwriter_end_element($memory); // XML_TAG_DEPENDENCIES
				}
				
			zbx_xmlwriter_end_element($memory); // XML_TAG_ZABBIX_EXPORT
			die(zbx_xmlwriter_output_memory($memory,true));
		}
	}
?>