jablonka.czprosek.czf

weathermap

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

 

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 {
3785simandl $data[IN] = NULL;
38 $data[OUT] = NULL;
391simandl $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 {
4985simandl warn("ExternalScript ReadData: Failed to run external script. [WMEXT01]\n");
501simandl }
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 {
6285simandl $data[IN] = floatval($lines[0]);
63 $data[OUT] = floatval($lines[1]);
64 
65 $item->add_hint("external_line1",$lines[0]);
66 $item->add_hint("external_line2",$lines[1]);
67 $item->add_hint("external_line3",$lines[2]);
68 $item->add_hint("external_line4",$lines[3]);
691simandl $data_time = time();
70 }
71 else
72 {
7385simandl warn("ExternalScript ReadData: Not enough lines read from external script ($i read, 4 expected) [WMEXT02]\n");
741simandl }
75 }
76 }
77 
7885simandl debug ("ExternalScript ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[OUT]).",$data_time)\n");
79 
80 return( array($data[IN], $data[OUT], $data_time) );
811simandl }
82}
83 
84// vim:ts=4:sw=4:
85 
86?>

Powered by WebSVN 2.2.1