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 | 83 | simandl | |
5 | 85 | simandl | // TARGET dbplug:databasename:username:pass:hostkey |
6 | 83 | simandl | |
7 | 85 | simandl | class WeatherMapDataSource_mrtg extends WeatherMapDataSource { |
8 | 83 | simandl | |
9 | 85 | simandl | function Recognise($targetstring) |
10 | | | { |
11 | | | if(preg_match("/\.(htm|html)$/",$targetstring,$matches)) |
12 | | | { |
13 | | | return TRUE; |
14 | | | } |
15 | | | else |
16 | | | { |
17 | | | return FALSE; |
18 | | | } |
19 | | | } |
20 | | | |
21 | | | function ReadData($targetstring, &$map, &$item) |
22 | | | { |
23 | | | $data[IN] = NULL; |
24 | | | $data[OUT] = NULL; |
25 | | | $data_time = 0; |
26 | | | |
27 | | | $matchvalue= $item->get_hint('mrtg_value'); |
28 | | | $matchperiod = $item->get_hint('mrtg_period'); |
29 | | | $swap = intval($item->get_hint('mrtg_swap')); |
30 | | | $negate = intval($item->get_hint('mrtg_negate')); |
31 | | | |
32 | | | if($matchvalue =='') $matchvalue = "cu"; |
33 | | | if($matchperiod =='') $matchperiod = "d"; |
34 | | | |
35 | | | $fd=fopen($targetstring, "r"); |
36 | | | |
37 | | | if ($fd) |
38 | | | { |
39 | | | while (!feof($fd)) |
40 | | | { |
41 | | | $buffer=fgets($fd, 4096); |
42 | | | debug("MRTG ReadData: Matching on '${matchvalue}in $matchperiod' and '${matchvalue}out $matchperiod'\n"); |
43 | | | |
44 | | | if (preg_match("/<\!-- ${matchvalue}in $matchperiod ([-+]?\d+\.?\d*) -->/", $buffer, $matches)) { $data[IN] = $matches[1] * 8; } |
45 | | | if (preg_match("/<\!-- ${matchvalue}out $matchperiod ([-+]?\d+\.?\d*) -->/", $buffer, $matches)) { $data[OUT] = $matches[1] * 8; } |
46 | | | } |
47 | | | fclose($fd); |
48 | | | # don't bother with the modified time if the target is a URL |
49 | | | if(! preg_match('/^[a-z]+:\/\//',$targetstring) ) |
50 | | | { |
51 | | | $data_time = filemtime($targetstring); |
52 | | | } |
53 | | | } |
54 | | | else |
55 | | | { |
56 | | | // some error code to go in here |
57 | | | debug ("MRTG ReadData: Couldn't open ($targetstring). \n"); |
58 | | | } |
59 | | | |
60 | | | if($swap==1) |
61 | | | { |
62 | | | debug("MRTG ReadData: Swapping IN and OUT\n"); |
63 | | | $t = $data[OUT]; |
64 | | | $data[OUT] = $data[IN]; |
65 | | | $data[IN] = $t; |
66 | | | } |
67 | | | |
68 | | | if($negate) |
69 | | | { |
70 | | | debug("MRTG ReadData: Negating values\n"); |
71 | | | $data[OUT] = -$data[OUT]; |
72 | | | $data[IN] = -$data[IN]; |
73 | | | } |
74 | | | |
75 | | | debug ("MRTG ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n"); |
76 | | | |
77 | | | return( array($data[IN], $data[OUT], $data_time) ); |
78 | | | } |
79 | | | } |
80 | | | |
81 | | | // vim:ts=4:sw=4: |
82 | | | ?> |