jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [convert-to-dsstats.php] - Blame information for rev 131

 

Line No. Rev Author Line
185simandl<?php
2 
3 #
4 # Change the uncommented line to point to your Cacti installation
5 #
6 $cacti_base = dirname(__FILE__)."/../../";
7 # $cacti_base = "C:/xampp/htdocs/cacti/";
8 # $cacti_base = "/var/www/html/cacti/";
9 # $cacti_base = "/Applications/XAMPP/htdocs/cacti/";
10 
11 // check if the goalposts have moved
12 if( is_dir($cacti_base) && file_exists($cacti_base."/include/global.php") )
13 {
14 // include the cacti-config, so we know about the database
15 require_once($cacti_base."/include/global.php");
16 }
17 elseif( is_dir($cacti_base) && file_exists($cacti_base."/include/config.php") )
18 {
19 // include the cacti-config, so we know about the database
20 require_once($cacti_base."/include/config.php");
21 }
22 else
23 {
24 die("Couldn't find a usable Cacti config - check the first few lines of ".__FILE__."\n");
25 }
26 
27 require_once 'Weathermap.class.php';
28 require_once 'Console/Getopt.php';
29 
30 $reverse = 0;
31 $inputfile = "";
32 $outputfile = "";
33 $converted = 0;
34 $candidates = 0;
35 $totaltargets = 0;
36 
37 $cg=new Console_Getopt();
38 $short_opts='';
39 $long_opts=array
40 (
41 "help",
42 "input=",
43 "output=",
44 "debug",
45 "reverse",
46 );
47 
48 $args=$cg->readPHPArgv();
49 $ret=$cg->getopt($args, $short_opts, $long_opts);
50 
51 if (PEAR::isError($ret)) { die ("Error in command line: " . $ret->getMessage() . "\n (try --help)\n"); }
52 
53 $gopts=$ret[0];
54 
55 if (sizeof($gopts) > 0)
56 {
57 foreach ($gopts as $o)
58 {
59 switch ($o[0])
60 {
61 case '--debug':
62 $weathermap_debugging=TRUE;
63 break;
64 case '--input':
65 $inputfile=$o[1];
66 break;
67 case '--output':
68 $outputfile=$o[1];
69 break;
70 case '--reverse':
71 $reverse = 1;
72 break;
73 case 'help':
74 default:
75 print "Weathermap DSStats converter. Converts rrd targets to DSStats\n";
76 print "-------------------------------------------------------------\n";
77 print "Usage: php convert-to-dstats.php [options]\n\n";
78 print " --input {filename} - File to read from\n";
79 print " --output {filename} - File to write to\n";
80 # print " --reverse - Convert from DSStats to RRDtool instead\n";
81 print " --debug - Enable debugging output\n";
82 print " --help - Show this message\n";
83 exit();
84 }
85 }
86 }
87 
88 if($inputfile == "" || $outputfile == "")
89 {
90 print "You must specify an input and output file. See --help.\n";
91 exit();
92 }
93 
94 $map = new WeatherMap;
95 
96 $map->context = 'cacti';
97 $map->rrdtool = read_config_option("path_rrdtool");
98 
99 print "Reading config from $inputfile\n";
100 
101 $map->ReadConfig($inputfile);
102 
103 $map->DatasourceInit();
104 $map->ProcessTargets();
105 
106 $allitems = array(&$map->links, &$map->nodes);
107 reset($allitems);
108 
109 while( list($kk,) = each($allitems))
110 {
111 unset($objects);
112 $objects = &$allitems[$kk];
113 
114 reset($objects);
115 while (list($k,) = each($objects))
116 {
117 unset($myobj);
118 $myobj = &$objects[$k];
119 
120 $type = $myobj->my_type();
121 
122 $name=$myobj->name;
123 debug ("ReadData for $type $name: \n");
124 
125 if( ($type=='LINK' && isset($myobj->a)) || ($type=='NODE' && !is_null($myobj->x) ) )
126 {
127 if (count($myobj->targets)>0)
128 {
129 $totaltargets++;
130 $tindex = 0;
131 foreach ($myobj->targets as $target)
132 {
133 debug ("ReadData: New Target: $target[4]\n");
134 
135 $targetstring = $target[0];
136 $multiply = $target[1];
137 
138 if($reverse == 0 && $target[5] == "WeatherMapDataSource_rrd")
139 {
140 $candidates++;
141 # list($in,$out,$datatime) = $map->plugins['data'][ $target[5] ]->ReadData($targetstring, $map, $myobj);
142 debug("ConvertDS: $targetstring is a candidate for conversion.");
143 $rrdfile = $targetstring;
144 $multiplier = 8;
145 $dsnames[IN] = "traffic_in";
146 $dsnames[OUT] = "traffic_out";
147 
148 if(preg_match("/^(.*\.rrd):([\-a-zA-Z0-9_]+):([\-a-zA-Z0-9_]+)$/",$targetstring,$matches))
149 {
150 $rrdfile = $matches[1];
151 
152 $dsnames[IN] = $matches[2];
153 $dsnames[OUT] = $matches[3];
154 
155 debug("ConvertDS: Special DS names seen (".$dsnames[IN]." and ".$dsnames[OUT].").\n");
156 }
157 if(preg_match("/^rrd:(.*)/",$rrdfile,$matches))
158 {
159 $rrdfile = $matches[1];
160 }
161 if(preg_match("/^gauge:(.*)/",$rrdfile,$matches))
162 {
163 $rrdfile = $matches[1];
164 $multiplier = 1;
165 }
166 if(preg_match("/^scale:([+-]?\d*\.?\d*):(.*)/",$rrdfile,$matches))
167 {
168 $rrdfile = $matches[2];
169 $multiplier = $matches[1];
170 }
171 
172 $path_rra = $config["rra_path"];
173 $db_rrdname = $rrdfile;
174 $db_rrdname = str_replace($path_rra,"<path_rra>",$db_rrdname);
175 # special case for relative paths
176 $db_rrdname = str_replace("../../rra","<path_rra>",$db_rrdname);
177 
178 if($db_rrdname != $rrdfile)
179 {
180 debug("ConvertDS: Looking for $db_rrdname in the database.");
181 
182 $SQLcheck = "select data_template_data.local_data_id from data_template_data,data_template_rrd where data_template_data.local_data_id=data_template_rrd.local_data_id and data_template_data.data_source_path='".mysql_real_escape_string($db_rrdname)."'";
183 debug("ConvertDS: ".$SQLcheck);
184 $results = db_fetch_assoc($SQLcheck);
185 
186 if( (sizeof($results) > 0) && (isset($results[0]['local_data_id']) ) )
187 {
188 $new_target = sprintf("dsstats:%d:%s:%s", $results[0]['local_data_id'], $dsnames[IN], $dsnames[OUT]);
189 $m = $multiply * $multiplier;
190 if( $m != 1)
191 {
192 if($m == -1) $new_target = "-".$new_target;
193 if($m == intval($m))
194 {
195 $new_target = sprintf("%d*%s",$m,$new_target);
196 }
197 else
198 {
199 $new_target = sprintf("%f*%s",$m,$new_target);
200 }
201 
202 }
203 
204 debug("ConvertDS: Converting to $new_target");
205 $converted++;
206 
207 if($type == 'NODE')
208 {
209 $map->nodes[$name]->targets[$tindex][4] = $new_target;
210 }
211 if($type == 'LINK')
212 {
213 $map->links[$name]->targets[$tindex][4] = $new_target;
214 }
215 
216 }
217 else
218 {
219 warn("ConvertDS: Failed to find a match for $db_rrdname - can't convert to DSStats.");
220 }
221 }
222 else
223 {
224 warn("ConvertDS: $rrdfile doesn't match with $path_rra - not bothering to look in the database.");
225 }
226 }
227 
228 // XXX - not implemented yet!
229 if($reverse == 1 && $target[5] == "WeatherMapDataSource_dsstats" && 1==0)
230 {
231 $candidates++;
232 # list($in,$out,$datatime) = $map->plugins['data'][ $target[5] ]->ReadData($targetstring, $map, $myobj);
233 debug("ConvertDS: $targetstring is a candidate for conversion.");
234 
235 $multiplier = 1;
236 $dsnames[IN] = "traffic_in";
237 $dsnames[OUT] = "traffic_out";
238 
239 $path_rra = $config["rra_path"];
240 $db_rrdname = $rrdfile;
241 $db_rrdname = str_replace($path_rra,"<path_rra>",$db_rrdname);
242 # special case for relative paths
243 $db_rrdname = str_replace("../../rra","<path_rra>",$db_rrdname);
244 
245 
246 debug("ConvertDS: Looking for $db_rrdname in the database.");
247 
248 $SQLcheck = "select data_template_data.local_data_id from data_template_data,data_template_rrd where data_template_data.local_data_id=data_template_rrd.local_data_id and data_template_data.data_source_path='".mysql_real_escape_string($db_rrdname)."'";
249 debug("ConvertDS: ".$SQLcheck);
250 $results = db_fetch_assoc($SQLcheck);
251 
252 if( (sizeof($results) > 0) && (isset($results[0]['local_data_id']) ) )
253 {
254 $new_target = sprintf("dsstats:%d:%s:%s", $results[0]['local_data_id'], $dsnames[IN], $dsnames[OUT]);
255 $m = $multiply * $multiplier;
256 if( $m != 1)
257 {
258 if($m == -1) $new_target = "-".$new_target;
259 if($m == intval($m))
260 {
261 $new_target = sprintf("%d*%s",$m,$new_target);
262 }
263 else
264 {
265 $new_target = sprintf("%f*%s",$m,$new_target);
266 }
267 
268 }
269 
270 debug("ConvertDS: Converting to $new_target");
271 $converted++;
272 
273 if($type == 'NODE')
274 {
275 $map->nodes[$name]->targets[$tindex][4] = $new_target;
276 }
277 if($type == 'LINK')
278 {
279 $map->links[$name]->targets[$tindex][4] = $new_target;
280 }
281 
282 }
283 else
284 {
285 warn("ConvertDS: Failed to find a match for $db_rrdname - can't convert back to rrdfile.");
286 }
287 
288 }
289 
290 $tindex++;
291 }
292 
293 debug ("ReadData complete for $type $name\n");
294 }
295 else
296 {
297 debug("ReadData: No targets for $type $name\n");
298 }
299 }
300 else
301 {
302 debug("ReadData: Skipping $type $name that looks like a template\n.");
303 }
304 
305 unset($myobj);
306 }
307 }
308 
309 $map->WriteConfig($outputfile);
310 
311 print "Wrote new config to $outputfile\n";
312 
313 print "$totaltargets targets, $candidates rrd-based targets, $converted were actually converted.\n";
314 
315 // vim:ts=4:sw=4:
316?>

Powered by WebSVN 2.2.1