function ReadData($targetString, &$map, &$mapItem)
 {
     $dsnames[IN] = "traffic_in";
     $dsnames[OUT] = "traffic_out";
     $data[IN] = null;
     $data[OUT] = null;
     $data_time = 0;
     $keyfield = "rrd_name";
     $datatype = "";
     if (preg_match('/^dsstats:(\\d+):([\\-a-zA-Z0-9_]+):([\\-a-zA-Z0-9_]+)$/', $targetString, $matches)) {
         $local_data_id = $matches[1];
         $dsnames[IN] = $matches[2];
         $dsnames[OUT] = $matches[3];
         $datatype = $this->owner->get_hint("dsstats_default_type", "last");
         wm_debug("Default datatype changed to " . $datatype . ".\n");
     } elseif (preg_match('/^dsstats:([a-z]+):(\\d+):([\\-a-zA-Z0-9_]+):([\\-a-zA-Z0-9_]+)$/', $targetString, $matches)) {
         $dsnames[IN] = $matches[3];
         $dsnames[OUT] = $matches[4];
         $datatype = $matches[1];
         $local_data_id = $matches[2];
     }
     $table = $this->determineTable($datatype);
     $field = $this->determineField($datatype);
     if ($datatype == "last") {
         $field = "calculated";
         $table = "data_source_stats_hourly_last";
     }
     if ($datatype == "wm") {
         $field = "last_calc";
         $table = "weathermap_data";
         $keyfield = "data_source_name";
     }
     if ($table != "" and $field != "") {
         $data = $this->fetchDSStatsData($keyfield, $field, $table, $local_data_id, $dsnames);
         if ($datatype == 'wm' && ($data[IN] == null || $data[OUT] == null)) {
             $this->registerWMDataRequirement($local_data_id, $data, $dsnames);
         }
     }
     // fill all that other information (ifSpeed, etc)
     // (but only if it's not switched off!)
     if ($this->owner->get_hint("dsstats_no_cacti_extras") === null && $local_data_id > 0) {
         UpdateCactiData($mapItem, $local_data_id);
     }
     wm_debug("DSStats ReadData: Returning (" . ($data[IN] === null ? 'null' : $data[IN]) . "," . ($data[OUT] === null ? 'null' : $data[OUT]) . ",{$data_time})\n");
     return array($data[IN], $data[OUT], $data_time);
 }
 function wmrrd_read_from_poller_output($rrdfile, $cf, $start, $end, $dsnames, &$data, &$map, &$data_time, &$item)
 {
     global $config;
     wm_debug("RRD ReadData: poller_output style\n");
     if (isset($config)) {
         // take away the cacti bit, to get the appropriate path for the table
         // $db_rrdname = realpath($rrdfile);
         $path_rra = $config["rra_path"];
         $db_rrdname = $rrdfile;
         $db_rrdname = str_replace($path_rra, "<path_rra>", $db_rrdname);
         wm_debug("******************************************************************\nChecking weathermap_data\n");
         foreach (array(IN, OUT) as $dir) {
             wm_debug("RRD ReadData: poller_output - looking for {$dir} value\n");
             if ($dsnames[$dir] != '-') {
                 wm_debug("RRD ReadData: poller_output - DS name is " . $dsnames[$dir] . "\n");
                 $SQL = "select * from weathermap_data where rrdfile='" . mysql_real_escape_string($db_rrdname) . "' and data_source_name='" . mysql_real_escape_string($dsnames[$dir]) . "'";
                 $SQLcheck = "select data_template_data.local_data_id from data_template_data,data_template_rrd where data_template_data.local_data_id=data_template_rrd.local_data_id and data_template_data.data_source_path='" . mysql_real_escape_string($db_rrdname) . "' and data_template_rrd.data_source_name='" . mysql_real_escape_string($dsnames[$dir]) . "'";
                 $SQLvalid = "select data_template_rrd.data_source_name from data_template_data,data_template_rrd where data_template_data.local_data_id=data_template_rrd.local_data_id and data_template_data.data_source_path='" . mysql_real_escape_string($db_rrdname) . "'";
                 $worst_time = time() - 8 * 60;
                 $result = db_fetch_row($SQL);
                 // OK, the straightforward query for data failed, let's work out why, and add the new data source if necessary
                 if (!isset($result['id'])) {
                     wm_debug("RRD ReadData: poller_output - Adding new weathermap_data row for {$db_rrdname}:" . $dsnames[$dir] . "\n");
                     $result = db_fetch_row($SQLcheck);
                     if (!isset($result['local_data_id'])) {
                         $fields = array();
                         $results = db_fetch_assoc($SQLvalid);
                         foreach ($results as $result) {
                             $fields[] = $result['data_source_name'];
                         }
                         if (count($fields) > 0) {
                             wm_warn("RRD ReadData: poller_output: " . $dsnames[$dir] . " is not a valid DS name for {$db_rrdname} - valid names are: " . join(", ", $fields) . " [WMRRD07]\n");
                         } else {
                             wm_warn("RRD ReadData: poller_output: {$db_rrdname} is not a valid RRD filename within this Cacti install. <path_rra> is {$path_rra} [WMRRD08]\n");
                         }
                     } else {
                         // add the new data source (which we just checked exists) to the table.
                         // Include the local_data_id as well, to make life easier in poller_output
                         // (and to allow the cacti: DS plugin to use the same table, too)
                         $SQLins = "insert into weathermap_data (rrdfile, data_source_name, sequence, local_data_id) values ('" . mysql_real_escape_string($db_rrdname) . "','" . mysql_real_escape_string($dsnames[$dir]) . "', 0," . $result['local_data_id'] . ")";
                         wm_debug("RRD ReadData: poller_output - Adding new weathermap_data row for data source ID " . $result['local_data_id'] . "\n");
                         db_execute($SQLins);
                     }
                 } else {
                     // the data table line already exists
                     wm_debug("RRD ReadData: poller_output - found weathermap_data row\n");
                     // if the result is valid, then use it
                     if ($result['sequence'] > 2 && $result['last_time'] > $worst_time) {
                         $data[$dir] = $result['last_calc'];
                         $data_time = $result['last_time'];
                         wm_debug("RRD ReadData: poller_output - data looks valid\n");
                     } else {
                         $data[$dir] = 0;
                         wm_debug("RRD ReadData: poller_output - data is either too old, or too new\n");
                     }
                     // now, we can use the local_data_id to get some other useful info
                     // first, see if the weathermap_data entry *has* a local_data_id. If not, we need to update this entry.
                     $ldi = 0;
                     if (!isset($result['local_data_id']) || $result['local_data_id'] == 0) {
                         $r2 = db_fetch_row($SQLcheck);
                         if (isset($r2['local_data_id'])) {
                             $ldi = $r2['local_data_id'];
                             wm_debug("RRD ReadData: updated  local_data_id for wmdata.id=" . $result['id'] . "to {$ldi}\n");
                             // put that in now, so that we can skip this step next time
                             db_execute("update weathermap_data set local_data_id=" . $r2['local_data_id'] . " where id=" . $result['id']);
                         }
                     } else {
                         $ldi = $result['local_data_id'];
                     }
                     if ($ldi > 0) {
                         UpdateCactiData($item, $ldi);
                     }
                 }
             } else {
                 wm_debug("RRD ReadData: poller_output - DS name is '-'\n");
             }
         }
     } else {
         wm_warn("RRD ReadData: poller_output - Cacti environment is not right [WMRRD12]\n");
     }
     wm_debug("RRD ReadData: poller_output - result is " . ($data[IN] === NULL ? 'NULL' : $data[IN]) . "," . ($data[OUT] === NULL ? 'NULL' : $data[OUT]) . "\n");
     wm_debug("RRD ReadData: poller_output - ended\n");
 }
 function ReadData($targetstring, &$map, &$item)
 {
     global $config;
     $dsnames[IN] = "traffic_in";
     $dsnames[OUT] = "traffic_out";
     $data[IN] = NULL;
     $data[OUT] = NULL;
     $inbw = NULL;
     $outbw = NULL;
     $data_time = 0;
     $table = "";
     $keyfield = "rrd_name";
     $datatype = "";
     $field = "";
     if (preg_match("/^dsstats:(\\d+):([\\-a-zA-Z0-9_]+):([\\-a-zA-Z0-9_]+)\$/", $targetstring, $matches)) {
         $local_data_id = $matches[1];
         $dsnames[IN] = $matches[2];
         $dsnames[OUT] = $matches[3];
         $datatype = "last";
         if ($map->get_hint("dsstats_default_type") != '') {
             $datatype = $map->get_hint("dsstats_default_type");
             debug("Default datatype changed to " . $datatype . ".\n");
         }
     } elseif (preg_match("/^dsstats:([a-z]+):(\\d+):([\\-a-zA-Z0-9_]+):([\\-a-zA-Z0-9_]+)\$/", $targetstring, $matches)) {
         $dsnames[IN] = $matches[3];
         $dsnames[OUT] = $matches[4];
         $datatype = $matches[1];
         $local_data_id = $matches[2];
     }
     if (substr($datatype, 0, 5) == "daily") {
         $table = "data_source_stats_daily";
     }
     if (substr($datatype, 0, 6) == "weekly") {
         $table = "data_source_stats_weekly";
     }
     if (substr($datatype, 0, 7) == "monthly") {
         $table = "data_source_stats_monthly";
     }
     if (substr($datatype, 0, 6) == "hourly") {
         $table = "data_source_stats_hourly";
     }
     if (substr($datatype, 0, 6) == "yearly") {
         $table = "data_source_stats_yearly";
     }
     if (substr($datatype, -7) == "average") {
         $field = "average";
     }
     if (substr($datatype, -4) == "peak") {
         $field = "peak";
     }
     if ($datatype == "last") {
         $field = "calculated";
         $table = "data_source_stats_hourly_last";
     }
     if ($datatype == "wm") {
         $field = "last_calc";
         $table = "weathermap_data";
         $keyfield = "data_source_name";
     }
     if ($table != "" and $field != "") {
         $SQL = sprintf("select %s as name, %s as result from %s where local_data_id=%d and (%s='%s' or %s='%s')", $keyfield, $field, $table, $local_data_id, $keyfield, mysql_escape_string($dsnames[IN]), $keyfield, mysql_escape_string($dsnames[OUT]));
         $results = db_fetch_assoc($SQL);
         if (sizeof($results) > 0) {
             foreach ($results as $result) {
                 foreach (array(IN, OUT) as $dir) {
                     if ($dsnames[$dir] == $result['name'] && $result['result'] != -90909090909 && $result['result'] != 'U') {
                         $data[$dir] = $result['result'];
                     }
                 }
             }
         }
         if ($datatype == 'wm' && ($data[IN] == NULL || $data[OUT] == NULL)) {
             debug("Didn't get data for 'wm' source. Inserting new tasks.");
             // insert the required details into weathermap_data, so it will be picked up next time
             $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", $local_data_id);
             $result = db_fetch_row($SQL);
             if (sizeof($result) > 0) {
                 $db_rrdname = $result['path'];
                 debug("Filename is {$db_rrdname}");
                 foreach (array(IN, OUT) as $dir) {
                     if ($data[$dir] === NULL) {
                         $SQLins = "insert into weathermap_data (rrdfile, data_source_name, sequence, local_data_id) values ('" . mysql_real_escape_string($db_rrdname) . "','" . mysql_real_escape_string($dsnames[$dir]) . "', 0," . $local_data_id . ")";
                         // warn($SQLins);
                         db_execute($SQLins);
                     }
                 }
             } else {
                 warn("DSStats ReadData: Failed to find a filename for DS id {$local_data_id} [WMDSTATS01]");
             }
         }
     }
     // fill all that other information (ifSpeed, etc)
     if ($local_data_id > 0) {
         UpdateCactiData($item, $local_data_id);
     }
     debug("DSStats ReadData: Returning (" . ($data[IN] === NULL ? 'NULL' : $data[IN]) . "," . ($data[OUT] === NULL ? 'NULL' : $data[OUT]) . ",{$data_time})\n");
     return array($data[IN], $data[OUT], $data_time);
 }