jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [lib/] [datasources/] [WeatherMapDataSource_external.php] - Blame information for rev 18

 

Line No. Rev Author Line
11simandl<?php
2// Run an external 'MRTG-compatible' script, and return it's values
3// TARGET !/usr/local/bin/qmailmrtg7 t /var/log/qmail
4 
5// MRTG Target scripts return 4 lines of text as output:
6// 'Input' value - interpreted as a byte count (so multiplied by 8)
7// 'Output' value - interpreted as a byte count (so multiplied by 8)
8// 'uptime' as a string
9// 'name of targer' as a string
10// we ignore the last two
11 
12// NOTE: Obviously, if you allow anyone to create maps, you are
13// allowing them to run ANY COMMAND as the user that runs
14// weathermap, by using this plugin. This might not be a
15// good thing.
16 
17// If you want to allow only one command, consider making
18// your own datasource plugin which only runs that one command.
19 
20 
21class WeatherMapDataSource_external extends WeatherMapDataSource {
22 
23 function Recognise($targetstring)
24 {
25 if(preg_match("/^!(.*)$/",$targetstring,$matches))
26 {
27 return TRUE;
28 }
29 else
30 {
31 return FALSE;
32 }
33 }
34 
35 function ReadData($targetstring, &$map, &$item)
36 {
37 $inbw=-1;
38 $outbw=-1;
39 $data_time = 0;
40 
41 if(preg_match("/^!(.*)$/",$targetstring,$matches))
42 {
43 $command = $matches[1];
44 
45 debug("ExternalScript ReadData: Running $command\n");
46 // run the command here
47 if( ($pipe = popen($command,"r")) === false)
48 {
49 warn("ExternalScript ReadData: Failed to run external script.\n");
50 }
51 else
52 {
53 $i=0;
54 while( ($i <5) && ! feof($pipe) )
55 {
56 $lines[$i++] = fgets($pipe,1024);
57 }
58 pclose($pipe);
59 
60 if($i==5)
61 {
62 $inbw = floatval($lines[0]);
63 $outbw = floatval($lines[1]);
64 $data_time = time();
65 }
66 else
67 {
68 warn("ExternalScript ReadData: Not enough lines read from external script ($i read, 4 expected)\n");
69 }
70 }
71 }
72 
73 debug ("ExternalScript ReadData: Returning ($inbw,$outbw,$data_time)\n");
74 
75 return( array($inbw, $outbw, $data_time) );
76 }
77}
78 
79// vim:ts=4:sw=4:
80 
81?>

Powered by WebSVN 2.2.1