jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [lib/] [datasources/] [WeatherMapDataSource_dsstats.php] - Blame information for rev 95

 

Line No. Rev Author Line
185simandl<?php
2 
3include_once(dirname(__FILE__)."/../ds-common.php");
4 
5class WeatherMapDataSource_dsstats extends WeatherMapDataSource {
6 
7 function Init(&$map)
8 {
9 global $config;
10 if($map->context=='cacti')
11 {
12 if( !function_exists('db_fetch_row') )
13 {
14 debug("ReadData DSStats: Cacti database library not found. [DSSTATS001]\n");
15 return(FALSE);
16 }
17 if(function_exists("api_plugin_is_enabled"))
18 {
19 if(! api_plugin_is_enabled('dsstats'))
20 {
21 debug("ReadData DSStats: DSStats plugin not enabled (new-style). [DSSTATS002B]\n");
22 return(FALSE);
23 }
24 }
25 else
26 {
27 if( !isset($plugins) || !in_array('dsstats',$plugins))
28 {
29 debug("ReadData DSStats: DSStats plugin not enabled (old-style). [DSSTATS002A]\n");
30 return(FALSE);
31 }
32 }
33 
34 $sql = "show tables";
35 $result = db_fetch_assoc($sql) or die (mysql_error());
36 $tables = array();
37 
38 foreach($result as $index => $arr) {
39 foreach ($arr as $t) {
40 $tables[] = $t;
41 }
42 }
43 
44 if( !in_array('data_source_stats_hourly_last', $tables) )
45 {
46 debug('ReadData DSStats: data_source_stats_hourly_last database table not found. [DSSTATS003]\n');
47 return(FALSE);
48 }
49 
50 return(TRUE);
51 }
52 
53 return(FALSE);
54 }
55 
56# dsstats:<datatype>:<local_data_id>:<rrd_name_in>:<rrd_name_out>
57 
58 function Recognise($targetstring)
59 {
60 if(preg_match("/^dsstats:([a-z]+):(\d+):([\-a-zA-Z0-9_]+):([\-a-zA-Z0-9_]+)$/",$targetstring,$matches))
61 {
62 return TRUE;
63 }
64 elseif(preg_match("/^dsstats:(\d+):([\-a-zA-Z0-9_]+):([\-a-zA-Z0-9_]+)$/",$targetstring,$matches))
65 {
66 return TRUE;
67 }
68 else
69 {
70 return FALSE;
71 }
72 }
73 
74 
75 
76 // Actually read data from a data source, and return it
77 // returns a 3-part array (invalue, outvalue and datavalid time_t)
78 // invalue and outvalue should be -1,-1 if there is no valid data
79 // data_time is intended to allow more informed graphing in the future
80 function ReadData($targetstring, &$map, &$item)
81 {
82 global $config;
83 
84 $dsnames[IN] = "traffic_in";
85 $dsnames[OUT] = "traffic_out";
86 $data[IN] = NULL;
87 $data[OUT] = NULL;
88 
89 $inbw = NULL;
90 $outbw = NULL;
91 $data_time = 0;
92 
93 $table = "";
94 $keyfield = "rrd_name";
95 $datatype = "";
96 $field = "";
97 
98 if(preg_match("/^dsstats:(\d+):([\-a-zA-Z0-9_]+):([\-a-zA-Z0-9_]+)$/",$targetstring,$matches))
99 {
100 $local_data_id = $matches[1];
101 $dsnames[IN] = $matches[2];
102 $dsnames[OUT] = $matches[3];
103 
104 $datatype = "last";
105 
106 if($map->get_hint("dsstats_default_type") != '') {
107 $datatype = $map->get_hint("dsstats_default_type");
108 debug("Default datatype changed to ".$datatype.".\n");
109 }
110 }elseif(preg_match("/^dsstats:([a-z]+):(\d+):([\-a-zA-Z0-9_]+):([\-a-zA-Z0-9_]+)$/",$targetstring,$matches))
111 {
112 $dsnames[IN] = $matches[3];
113 $dsnames[OUT] = $matches[4];
114 $datatype = $matches[1];
115 $local_data_id = $matches[2];
116 }
117 
118 if( substr($datatype,0,5) == "daily") $table = "data_source_stats_daily";
119 if( substr($datatype,0,6) == "weekly") $table = "data_source_stats_weekly";
120 if( substr($datatype,0,7) == "monthly") $table = "data_source_stats_monthly";
121 if( substr($datatype,0,6) == "hourly") $table = "data_source_stats_hourly";
122 if( substr($datatype,0,6) == "yearly") $table = "data_source_stats_yearly";
123 
124 if( substr($datatype,-7) == "average" ) $field = "average";
125 if( substr($datatype,-4) == "peak" ) $field = "peak";
126 
127 if($datatype == "last")
128 {
129 $field = "calculated";
130 $table = "data_source_stats_hourly_last";
131 }
132 
133 if($datatype == "wm")
134 {
135 $field = "last_calc";
136 $table = "weathermap_data";
137 $keyfield = "data_source_name";
138 }
139 
140 if($table != "" and $field != "")
141 {
142 $SQL = sprintf("select %s as name, %s as result from %s where local_data_id=%d and (%s='%s' or %s='%s')",
143 $keyfield, $field,
144 $table, $local_data_id, $keyfield,
145 mysql_escape_string($dsnames[IN]), $keyfield, mysql_escape_string($dsnames[OUT])
146 );
147 
148 $results = db_fetch_assoc($SQL);
149 if(sizeof($results)>0)
150 {
151 foreach ($results as $result)
152 {
153 foreach ( array(IN,OUT) as $dir)
154 {
155 if( ($dsnames[$dir] == $result['name']) && ($result['result'] != -90909090909) && ($result['result'] !='U') )
156 {
157 $data[$dir] = $result['result'];
158 }
159 }
160 }
161 }
162 
163 if($datatype=='wm' && ($data[IN] == NULL || $data[OUT] == NULL) )
164 {
165 debug("Didn't get data for 'wm' source. Inserting new tasks.");
166 // insert the required details into weathermap_data, so it will be picked up next time
167 $SQL = sprintf("select data_template_data.data_source_path as path from data_template_data,data_template_rrd where data_template_data.local_data_id=data_template_rrd.local_data_id and data_template_rrd.local_data_id=%d",
168 $local_data_id
169 );
170 $result = db_fetch_row($SQL);
171 if(sizeof($result)>0)
172 {
173 $db_rrdname = $result['path'];
174 debug("Filename is $db_rrdname");
175 foreach (array(IN,OUT) as $dir)
176 {
177 if($data[$dir] === NULL)
178 {
179 $SQLins = "insert into weathermap_data (rrdfile, data_source_name, sequence, local_data_id) values ('" .
180 mysql_real_escape_string($db_rrdname) . "','" .
181 mysql_real_escape_string($dsnames[$dir]) . "', 0," .
182 $local_data_id.")";
183 // warn($SQLins);
184 db_execute($SQLins);
185 }
186 }
187 }
188 else
189 {
190 warn("DSStats ReadData: Failed to find a filename for DS id $local_data_id [WMDSTATS01]");
191 }
192 }
193 }
194 
195 // fill all that other information (ifSpeed, etc)
196 if($local_data_id>0) UpdateCactiData($item, $local_data_id);
197 
198 debug ("DSStats ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n");
199 
200 return( array($data[IN], $data[OUT], $data_time) );
201 }
202}
203 
204// vim:ts=4:sw=4:
205?>

Powered by WebSVN 2.2.1