------------------------------------------------------------------------

  @package   Plugin Monitoring for GLPI
  @author    David Durieux
  @co-author 
  @comment   
  @copyright Copyright (c) 2011-2012 Plugin Monitoring for GLPI team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      https://forge.indepnet.net/projects/monitoring/
  @since     2011

  ------------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
    define('GLPI_ROOT', '../../..');
}
include GLPI_ROOT . "/inc/includes.php";
PluginMonitoringProfile::checkRight("config", "w");
commonHeader($LANG['plugin_monitoring']['title'][0], $_SERVER["PHP_SELF"], "plugins", "monitoring", "rrdtemplates");
if (isset($_FILES['filename'])) {
    if (strstr($_FILES['filename']['name'], ".json")) {
        if (Document::renameForce($_FILES['filename']['tmp_name'], GLPI_PLUGIN_DOC_DIR . "/monitoring/templates/" . $_FILES['filename']['name'])) {
            addMessageAfterRedirect($LANG['document'][26]);
        }
    }
    glpi_header($_SERVER['HTTP_REFERER']);
}
$pmRrdtool = new PluginMonitoringRrdtool();
$pmRrdtool->addTemplate();
commonFooter();
  Plugin Monitoring for GLPI is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

  ------------------------------------------------------------------------

  @package   Plugin Monitoring for GLPI
  @author    David Durieux
  @co-author 
  @comment   
  @copyright Copyright (c) 2011-2012 Plugin Monitoring for GLPI team
  @license   AGPL License 3.0 or (at your option) any later version
             http://www.gnu.org/licenses/agpl-3.0-standalone.html
  @link      https://forge.indepnet.net/projects/monitoring/
  @since     2011

  ------------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
    define('GLPI_ROOT', '../../..');
}
include GLPI_ROOT . "/inc/includes.php";
PluginMonitoringProfile::checkRight("config", "r");
commonHeader($LANG['plugin_monitoring']['title'][0], $_SERVER["PHP_SELF"], "plugins", "monitoring", "rrdtemplates");
$pmRrdtool = new PluginMonitoringRrdtool();
$pmRrdtool->showRRDTemplates();
commonFooter();
 function showWidget($id, $time)
 {
     global $LANG, $DB, $CFG_GLPI;
     $pmRrdtool = new PluginMonitoringRrdtool();
     $pmComponent = new PluginMonitoringComponent();
     $this->getFromDB($id);
     $pmComponent->getFromDB($this->fields['plugin_monitoring_components_id']);
     $pmRrdtool->displayGLPIGraph($pmComponent->fields['graph_template'], "PluginMonitoringService", $id, "0", $time);
     return '<img src="' . $CFG_GLPI['root_doc'] . '/plugins/monitoring/front/send.php?file=PluginMonitoringService-' . $id . '-' . $time . '0.png"/>';
 }
 function parseToRrdtool($plugin_monitoring_services_id)
 {
     global $DB;
     $pmRrdtool = new PluginMonitoringRrdtool();
     $pmCommand = new PluginMonitoringCommand();
     $pmService = new PluginMonitoringService();
     $pmComponent = new PluginMonitoringComponent();
     if ($pmService->getFromDB($plugin_monitoring_services_id)) {
         $pmComponent->getFromDB($pmService->fields['plugin_monitoring_components_id']);
         if (!isset($pmComponent->fields['plugin_monitoring_commands_id'])) {
             return;
         }
         if (is_null($pmComponent->fields['graph_template'])) {
             return;
         }
         $pmCommand->getFromDB($pmComponent->fields['plugin_monitoring_commands_id']);
         $pmUnavaibility = new PluginMonitoringUnavaibility();
         $pmUnavaibility->runUnavaibility($plugin_monitoring_services_id);
         $query = "SELECT * FROM `" . $this->getTable() . "`\n            WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n            ORDER BY `date`";
         $result = $DB->query($query);
         $i = 0;
         $nb_rows = $DB->numrows($result);
         $rrdtool_value = '';
         $last_date = '';
         while ($edata = $DB->fetch_array($result)) {
             $i++;
             if ($edata['unavailability'] == '0') {
                 if ($last_date != '') {
                     $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, 0, '', $rrdtool_value, 1);
                 }
                 break;
             }
             $perf_data = $edata['perf_data'];
             if ($edata['perf_data'] == '') {
                 $perf_data = $edata['output'];
             }
             if ($edata['unavailability'] != '2' and $i < $nb_rows) {
                 $rrdtool_value = $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, $this->convert_datetime_timestamp($edata['date']), $perf_data, $rrdtool_value, 0);
             }
             $last_date = $edata['date'];
             if ($i == $nb_rows) {
                 if ($edata['unavailability'] != '2') {
                     $input = array();
                     $input['id'] = $edata['id'];
                     $input['unavailability'] = 2;
                     $this->update($input);
                     $pmRrdtool->addData($pmComponent->fields['graph_template'], $plugin_monitoring_services_id, $this->convert_datetime_timestamp($edata['date']), $perf_data, $rrdtool_value, 1);
                     $queryd = "DELETE FROM `" . $this->getTable() . "`\n                     WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'\n                        AND `date`<'" . $edata['date'] . "'";
                     $DB->query($queryd);
                 }
             }
         }
         $a_list = array();
         $a_list[] = "2h";
         $a_list[] = "12h";
         $a_list[] = "1d";
         $a_list[] = "1w";
         $a_list[] = "1m";
         $a_list[] = "0y6m";
         $a_list[] = "1y";
         $pmConfig = new PluginMonitoringConfig();
         $pmConfig->getFromDB(1);
         $a_timezones = importArrayFromDB($pmConfig->fields['timezones']);
         foreach ($a_list as $time) {
             foreach ($a_timezones as $timezone) {
                 $pmRrdtool->displayGLPIGraph($pmComponent->fields['graph_template'], "PluginMonitoringService", $plugin_monitoring_services_id, $timezone, $time);
             }
         }
     } else {
         $query = "DELETE FROM `" . $this->getTable() . "`\n            WHERE `plugin_monitoring_services_id`='" . $plugin_monitoring_services_id . "'";
         $DB->query($query);
     }
 }