File: utility_functions.php

package info (click to toggle)
cacti 0.6.7-2.2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,000 kB
  • ctags: 1,120
  • sloc: php: 5,059; sql: 922; sh: 302; perl: 81; makefile: 56
file content (392 lines) | stat: -rw-r--r-- 15,437 bytes parent folder | download
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
<?/*
+-------------------------------------------------------------------------+
| Copyright (C) 2002 Ian Berry                                            |
|                                                                         |
| 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.                            |
+-------------------------------------------------------------------------+
| cacti: the rrdtool frontend [php-auth, php-tree, php-form]              |
+-------------------------------------------------------------------------+
| This code is currently maintained and debugged by Ian Berry, any        |
| questions or comments regarding this code should be directed to:        |
| - iberry@raxnet.net                                                     |
+-------------------------------------------------------------------------+
| - raXnet - http://www.raxnet.net/                                       |
+-------------------------------------------------------------------------+
*/?>
<?

function DuplicateGraph($graph_id) {
	include ("include/database.php");
	
	$sql_id = mysql_query("select * from rrd_graph where id=$graph_id", $cnn_id);
	$name = mysql_result($sql_id, 0, "title") . " (Copy)";
	
	/* duplicate graph defs */
	mysql_query("insert into rrd_graph (id,imageformatid,title,height,width,upperlimit,
	lowerlimit,verticallabel,autoscale,autopadding,autoscaleopts,rigid,basevalue,grouping,export) 
	values (0,
		" . mysql_result($sql_id, 0, "imageformatid") . ",\"$name\""
		. "," . mysql_result($sql_id, 0, "height") 
		. "," . mysql_result($sql_id, 0, "width")
		. "," . mysql_result($sql_id, 0, "upperlimit") 
		. "," . mysql_result($sql_id, 0, "lowerlimit")
		. ",\"" . mysql_result($sql_id, 0, "verticallabel") . "\""
		. ",\"" . mysql_result($sql_id, 0, "autoscale") . "\""
		. ",\"" . mysql_result($sql_id, 0, "autopadding") . "\""
		. ",\"" . mysql_result($sql_id, 0, "autoscaleopts") . "\""
		. ",\"" . mysql_result($sql_id, 0, "rigid") . "\""
		. ",\"" . mysql_result($sql_id, 0, "basevalue") . "\""
		. ",\"" . mysql_result($sql_id, 0, "grouping") . "\""
		. ",\"" . mysql_result($sql_id, 0, "export") . "\")", $cnn_id);
	
	$graph_grouping = mysql_result($sql_id, 0, "grouping");
	
	/* duplicate graph items */
	$sql_id = mysql_query("select LAST_INSERT_ID() as id", $cnn_id);
	$last_id = mysql_result($sql_id, 0, "id");
	
	$sql_id = mysql_query("select * from rrd_graph_item where graphid=$graph_id", $cnn_id);
	$rows = mysql_num_rows($sql_id); $i = 0;
	
	while ($i < $rows) {
		mysql_query("insert into rrd_graph_item (id,dsid,colorid,textformat,value,sequence,
			graphid,graphtypeid,consolidationfunction,hardreturn,cdefid,sequenceparent,parent) 
			values (0" 
			. ",\"" . mysql_result($sql_id, $i, "dsid") . "\""
			. ",\"" . mysql_result($sql_id, $i, "colorid") . "\""
			. ",\"" . mysql_result($sql_id, $i, "textformat") . "\""
			. ",\"" . mysql_result($sql_id, $i, "value") . "\""
			. ",\"" . mysql_result($sql_id, $i, "sequence") . "\""
			. ",\"" . $last_id . "\""
			. ",\"" . mysql_result($sql_id, $i, "graphtypeid") . "\""
			. ",\"" . mysql_result($sql_id, $i, "consolidationfunction") . "\""
			. ",\"" . mysql_result($sql_id, $i, "hardreturn") . "\""
			. ",\"" . mysql_result($sql_id, $i, "cdefid") . "\""
			. ",\"" . mysql_result($sql_id, $i, "sequenceparent") . "\""
			. ",\"" . mysql_result($sql_id, $i, "parent") . "\")", $cnn_id);
		$i++;
	}
	
	/* if graph grouping is turned on; we are going to "reset" it by undoing and redoing
	the graph grouping. This will force each item to get valid parent items instead of
	pointing to the old graph items */
	if ($graph_grouping == "on") {
		ungroup_graph_items($last_id);
		group_graph_items($last_id);
	}
}

function DuplicateDataSource($datasource_id) {
	include ("include/database.php");
	
	$sql_id = mysql_query("select * from rrd_ds where id=$datasource_id", $cnn_id);
	
	$name = mysql_result($sql_id, 0, "name") . "_copy";
	/* duplicate data source defs */
	mysql_query("insert into rrd_ds (id,name,datasourcetypeid,heartbeat,
		minvalue,maxvalue,srcid,active,dsname,dspath,step) values (0"
		. ",\"" . $name . "\""
		. ",\"" . mysql_result($sql_id, $i, "datasourcetypeid") . "\""
		. ",\"" . mysql_result($sql_id, $i, "heartbeat") . "\""
		. ",\"" . mysql_result($sql_id, $i, "minvalue") . "\""
		. ",\"" . mysql_result($sql_id, $i, "maxvalue") . "\""
		. ",\"" . mysql_result($sql_id, $i, "srcid") . "\""
		. ",\"" . mysql_result($sql_id, $i, "active") . "\""
		. ",\"" . mysql_result($sql_id, $i, "dsname") . "\""
		. ",\"\""
		. ",\"" . mysql_result($sql_id, $i, "step") . "\""
		. ")", $cnn_id);
	
	/* duplicate data source data */
	$sql_id = mysql_query("select LAST_INSERT_ID() as id", $cnn_id);
	$last_id = mysql_result($sql_id, 0, "id");
	
	$sql_id = mysql_query("select * from src_data where dsid=$datasource_id", $cnn_id);
	$rows = mysql_num_rows($sql_id); $i = 0;
	
	while ($i < $rows) {
		mysql_query("insert into src_data (id,fieldid,dsid,value) values (0" 
			. ",\"" . mysql_result($sql_id, $i, "fieldid") . "\""
			. ",\"" . $last_id . "\""
			. ",\"" . mysql_result($sql_id, $i, "value") . "\""
			. ")", $cnn_id);
		$i++;
	}
	
	/* duplicate data source -> rra link data */
	$sql_id = mysql_query("select * from lnk_ds_rra where dsid=$datasource_id", $cnn_id);
	$rows = mysql_num_rows($sql_id); $i = 0;
	
	while ($i < $rows) {
		mysql_query("insert into lnk_ds_rra (dsid,rraid) values ($last_id"
			. ",\"" . mysql_result($sql_id, $i, "rraid") . "\""
			. ")", $cnn_id);
		$i++;
	}
	
	return $last_id;
}

function group_graph_items($graph_id) {
	include ("include/database.php");
	
	$sql_id = mysql_query("select 
		i.consolidationfunction,i.sequence,i.id,i.dsid,
		t.name 
		from rrd_graph_item i left join 
		def_graph_type t on 
		i.graphtypeid=t.id 
		where i.graphid=$graph_id 
		order by i.sequenceparent, i.sequence", $cnn_id);
	$rows = mysql_num_rows($sql_id); $i = 0;
	
	while ($i < $rows) {
		$child_counter[mysql_result($sql_id, $i, "dsid")]++;
		
		if ((mysql_result($sql_id, $i, "name") == "AREA") || (mysql_result($sql_id, $i, "name") == "STACK") || (mysql_result($sql_id, $i, "name") == "LINE1") || (mysql_result($sql_id, $i, "name") == "LINE2") || (mysql_result($sql_id, $i, "name") == "LINE3")) {
			if (isset($has_been_used[mysql_result($sql_id, $i, "dsid")][mysql_result($sql_id, $i, "consolidationfunction")]) == false) {
				$parent_counter++;
			}
			
			$current_parent_id = mysql_result($sql_id, $i, "id");
			$current_sequence = 0;
			
			$has_been_used[mysql_result($sql_id, $i, "dsid")][mysql_result($sql_id, $i, "consolidationfunction")] = true;
		}else{
			$current_sequence = $child_counter[mysql_result($sql_id, $i, "dsid")];
		}
		
		mysql_query("update rrd_graph_item set sequence=$current_sequence
			, sequenceparent=$parent_counter, parent=$current_parent_id
			  where id=" . mysql_result($sql_id, $i, "id"), $cnn_id);
		$i++;
	}
}

function ungroup_graph_items($graph_id) {
	include ("include/database.php");
	
	$sql_id = mysql_query("select sequence,id,dsid from rrd_graph_item where graphid=$graph_id
		order by sequenceparent,sequence", $cnn_id);
	$rows = mysql_num_rows($sql_id); $i = 0;
	
	while ($i < $rows) {
		mysql_query("update rrd_graph_item set sequence=" . ($i+1) . " where id=" . mysql_result($sql_id, $i, "id"), $cnn_id);
		$i++;
	}
	
	mysql_query("update rrd_graph_item set sequenceparent=0 where graphid=$graph_id", $cnn_id);
	mysql_query("update rrd_graph_item set parent=0 where graphid=$graph_id", $cnn_id);
	
	$i++;
}

function CreateGraphDataFromSNMPData($graph_parameters) {
	include ("include/database.php");
	
	$sql_id = mysql_query("select * from snmp_hosts_interfaces where id=" . $graph_parameters["snmp_interface_id"], $cnn_id);
	
	/* ---- Data Source ---- */
	
	/* defaults */
	$ds_datasourcetype = "COUNTER";
	$ds_heartbeat = "600";
	$ds_minvalue = "0";
	$ds_maxvalue = mysql_result($sql_id, 0, "speed");
	$ds_srctype = "snmp_net";
	$snmp_interface_index = mysql_result($sql_id, 0, "interfacenumber");
	$snmp_interface_description = mysql_result($sql_id, 0, "description");
	
	/* host info */
	$sql_id = mysql_query("select * from snmp_hosts where id=" . mysql_result($sql_id, 0, "hostid"), $cnn_id);
	$snmp_host_hostname = mysql_result($sql_id, 0, "hostname");
	$snmp_host_community = mysql_result($sql_id, 0, "community");
	
	$sql_id = mysql_query("select id from def_ds where name=\"$ds_datasourcetype\"", $cnn_id);
	$ds_datasourcetype = mysql_result($sql_id, 0, "id");
	
	/* get the src_id given the src_type */
	$sql_id = mysql_query("select id from src where type=\"$ds_srctype\"", $cnn_id);
	
	if (mysql_num_rows($sql_id) != 0) {
		$ds_srcid = mysql_result($sql_id, 0, "id");
	}
	
	$o = 0;
	while ($o <= 1) {
		switch ($o) {
			case 0:
				$ds_direction = "in";
				break;
			case 1:
				$ds_direction = "out";
				break;
		}
		
		/* set a path for this new data source [RRDPATH/FullDSName_[in|out].rrd] */
		$data_source_path = "<path_rra>/" . strtolower(CleanUpName($graph_parameters["data_source_name"])) . "_" . "$ds_direction.rrd";
		
		$sql_id = mysql_query("insert into rrd_ds (name,datasourcetypeid,heartbeat,minvalue,
			maxvalue,srcid,active,dsname,dspath) values (\"" . $graph_parameters["data_source_name"] . "_" . "$ds_direction\",
			$ds_datasourcetype,$ds_heartbeat,$ds_minvalue,$ds_maxvalue,$ds_srcid,\"on\",\"\",\"$data_source_path\")"
			,$cnn_id);
		
		/* get newly saved id */
		$sql_id = mysql_query("select LAST_INSERT_ID()",$cnn_id);
		$datasource_id[$o] = mysql_result($sql_id,"",0); /* store both dsid's */
		
		/* this will do any of the cleanup that is required on the data source name to make
		sure rrdtool is ok with it. */
		SyncDataSourceName($datasource_id[$o], "", $data_source_path);
		
		/* save rra data: use all rra's */
		$sql_id = mysql_query("select id from rrd_rra", $cnn_id);
		$rows = mysql_num_rows($sql_id); $i = 0;
		
		while ($i < $rows) {
			mysql_query("insert into lnk_ds_rra (dsid,rraid) values ($datasource_id[$o]," . mysql_result($sql_id, $i, "id") . ")",$cnn_id);
			
			$i++;
		}
		
		/* now we have to write the acual data for these ds's to start working, this will
		only work correctly if the user did not tamper with the data names of the
		"Gather SNMP Network Data" data input source! */
		$sql_id = mysql_query("select id,dataname from src_fields where srcid=$ds_srcid and 
			inputoutput=\"in\"", $cnn_id);
		$rows = mysql_num_rows($sql_id); $i = 0;
		
		while ($i < $rows) {
			$ds_input_value = "";
			/* get values for the input fields that need them */
			switch (mysql_result($sql_id, $i, "dataname")) {
				case 'inout':
					$ds_input_value = $ds_direction;
					break;
				case 'ip':
					$ds_input_value = $snmp_host_hostname;
					break;
				case 'community':
					$ds_input_value = $snmp_host_community;
					break;
				case 'ifnum':
					$ds_input_value = $snmp_interface_index;
					break;
			}
			
			mysql_query("insert into src_data (fieldid,dsid,value) values (" . mysql_result($sql_id, $i, "id") . ",$datasource_id[$o],\"$ds_input_value\")",$cnn_id);
			
			$i++;
		}
		
		$o++;
	}
	
	/* ---- Graph Data ---- */
	
	/* Graph Title */
	$graph_title = BuildGraphTitleFromSNMP($graph_parameters);
	
	if ($graph_title == "") {
		$graph_title = "Traffic Analysis for $snmp_interface_description";
	}
	
	/* defaults */
	$graph_image_format = "PNG";
	$graph_height = "120";
	$graph_width = "500";
	$graph_vertical_label = "Bytes Per Second";
	$graph_color_in = "00CF00";
	$graph_color_out = "002A97";
	
	$sql_id = mysql_query("select id from def_colors where hex=\"$graph_color_in\"", $cnn_id);
	$graph_color_in = mysql_result($sql_id, 0, "id");
	
	$sql_id = mysql_query("select id from def_colors where hex=\"$graph_color_out\"", $cnn_id);
	$graph_color_out = mysql_result($sql_id, 0, "id");
	
	$sql_id = mysql_query("select id from def_image_type where name=\"$graph_image_format\"", $cnn_id);
	$graph_image_format = mysql_result($sql_id, 0, "id");
	
	$sql_id = mysql_query("insert into rrd_graph (imageformatid,title,height,width,upperlimit,
		lowerlimit,verticallabel,autoscale,autopadding,autoscaleopts,rigid,basevalue,grouping,export) 
		values ($graph_image_format,\"$graph_title\",$graph_height,$graph_width,0,0,
		\"$graph_vertical_label\",\"on\",\"on\",2,\"on\",1000,\"on\",\"on\")",$cnn_id);
	
	/* get newly saved id */
	$sql_id = mysql_query("select LAST_INSERT_ID()",$cnn_id);
	$graph_id = mysql_result($sql_id,"",0);
	
	/* this would be a great place to put some code for graph templates, however it is not
	written yet, so its all going to be manual for now */
	
	$graph_hard_return_array[0] = "";
	$graph_hard_return_array[1] = "";
	$graph_hard_return_array[2] = "";
	
	$graph_color_array[1] = "0";
	$graph_color_array[2] = "0";
	$graph_color_array[3] = "0";
	
	$graph_text_format_array[1] = "Current:";
	$graph_text_format_array[2] = "Average:";
	$graph_text_format_array[3] = "Maximum:";
	
	$graph_graph_type_array[1] = "GPRINT";
	$graph_graph_type_array[2] = "GPRINT";
	$graph_graph_type_array[3] = "GPRINT";
	
	$graph_cf_function_array[0] = "AVERAGE";
	$graph_cf_function_array[1] = "LAST";
	$graph_cf_function_array[2] = "AVERAGE";
	$graph_cf_function_array[3] = "MAX";
	
	$o = 0;
	while ($o <= 1) {
		switch ($o) {
			case 0:
				$graph_text_format_array[0] = "Inbound";
				$graph_graph_type_array[0] = "AREA";
				$graph_color_array[0] = $graph_color_in;
				$graph_hard_return_array[3] = "on";
				break;
			case 1:
				$graph_text_format_array[0] = "Outbound";
				$graph_graph_type_array[0] = "LINE1";
				$graph_color_array[0] = $graph_color_out;
				$graph_hard_return_array[3] = "";
				break;
		}
		
		$s = 0;
		while ($s < count($graph_text_format_array)) {
			$sql_id = mysql_query("select id from def_graph_type where name=\"$graph_graph_type_array[$s]\"", $cnn_id);
			$graph_type = mysql_result($sql_id, 0, "id");
			
			$sql_id = mysql_query("select id from def_cf where name=\"$graph_cf_function_array[$s]\"", $cnn_id);
			$graph_cf_function = mysql_result($sql_id, 0, "id");
			
			$sql_id = mysql_query("insert into rrd_graph_item (dsid,colorid,textformat,
				sequence,graphid,graphtypeid,consolidationfunction,hardreturn) values ($datasource_id[$o],
				$graph_color_array[$s],\"$graph_text_format_array[$s]\",((($o*4)+$s)+1),$graph_id,$graph_type,$graph_cf_function,
				\"$graph_hard_return_array[$s]\")",$cnn_id);
			
			$s++;
		}
		
		$o++;
	}
	
	/* turn on graph grouping */
	group_graph_items($graph_id);
}

?>