1 | 85 | simandl | <?php |
2 | | | // Sample Pluggable datasource for PHP Weathermap 0.9 |
3 | | | // - read a pair of values from a database, and return it |
4 | | | |
5 | | | // TARGET dbplug:databasename:username:pass:hostkey |
6 | | | |
7 | | | class WeatherMapDataSource_dbsample extends WeatherMapDataSource { |
8 | | | |
9 | | | function Init(&$map) |
10 | | | { |
11 | | | if(! function_exists("mysql_real_escape_string") ) return FALSE; |
12 | | | if(! function_exists("mysql_connect") ) return FALSE; |
13 | | | |
14 | | | return(TRUE); |
15 | | | } |
16 | | | |
17 | | | function Recognise($targetstring) |
18 | | | { |
19 | | | if(preg_match("/^dbplug:([^:]+)$/",$targetstring,$matches)) |
20 | | | { |
21 | | | return TRUE; |
22 | | | } |
23 | | | else |
24 | | | { |
25 | | | return FALSE; |
26 | | | } |
27 | | | } |
28 | | | |
29 | | | function ReadData($targetstring, &$map, &$item) |
30 | | | { |
31 | | | $data[IN] = NULL; |
32 | | | $data[OUT] = NULL; |
33 | | | $data_time = 0; |
34 | | | |
35 | | | if(preg_match("/^dbplug:([^:]+)$/",$targetstring,$matches)) |
36 | | | { |
37 | | | $database_user = $map->get_hint('dbplug_dbuser'); |
38 | | | $database_pass = $map->get_hint('dbplug_dbpass'); |
39 | | | $database_name = $map->get_hint('dbplug_dbname'); |
40 | | | $database_host = $map->get_hint('dbplug_dbhost'); |
41 | | | |
42 | | | $key = mysql_real_escape_string($matches[1]); |
43 | | | |
44 | | | $SQL = "select in,out from table where host=$key LIMIT 1"; |
45 | | | if(mysql_connect($database_host,$database_user,$database_pass)) |
46 | | | { |
47 | | | if(mysql_select_db($database_name)) |
48 | | | { |
49 | | | $result = mysql_query($SQL); |
50 | | | if (!$result) |
51 | | | { |
52 | | | warn("dbsample ReadData: Invalid query: " . mysql_error()."\n"); |
53 | | | } |
54 | | | else |
55 | | | { |
56 | | | $row = mysql_fetch_assoc($result); |
57 | | | $data[IN] = $row['in']; |
58 | | | $data[OUT] = $row['out']; |
59 | | | } |
60 | | | } |
61 | | | else |
62 | | | { |
63 | | | warn("dbsample ReadData: failed to select database: ".mysql_error()."\n"); |
64 | | | } |
65 | | | } |
66 | | | else |
67 | | | { |
68 | | | warn("dbsample ReadData: failed to connect to database server: ".mysql_error()."\n"); |
69 | | | } |
70 | | | |
71 | | | $data_time = now(); |
72 | | | } |
73 | | | |
74 | | | |
75 | | | debug ("RRD ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[IN]).",$data_time)\n"); |
76 | | | |
77 | | | return( array($data[IN], $data[OUT], $data_time) ); |
78 | | | } |
79 | | | } |
80 | | | |
81 | | | // vim:ts=4:sw=4: |
82 | | | ?> |