Example #1
0
 /**
  * Process screen.
  *
  * @return CDiv (screen inside container)
  */
 public function get()
 {
     $this->dataId = 'graph_' . $this->screenitem['screenitemid'] . '_' . $this->screenitem['screenid'];
     $resourceId = isset($this->screenitem['real_resourceid']) ? $this->screenitem['real_resourceid'] : $this->screenitem['resourceid'];
     $containerId = 'graph_container_' . $this->screenitem['screenitemid'] . '_' . $this->screenitem['screenid'];
     $graphDims = getGraphDims($resourceId);
     $graphDims['graphHeight'] = $this->screenitem['height'];
     $graphDims['width'] = $this->screenitem['width'];
     $graph = getGraphByGraphId($resourceId);
     $graphId = $graph['graphid'];
     $legend = $graph['show_legend'];
     $graph3d = $graph['show_3d'];
     if ($this->screenitem['dynamic'] == SCREEN_DYNAMIC_ITEM && $this->hostid) {
         // get host
         $hosts = API::Host()->get(['hostids' => $this->hostid, 'output' => ['hostid', 'name']]);
         $host = reset($hosts);
         // get graph
         $graph = API::Graph()->get(['graphids' => $resourceId, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => ['hostid'], 'selectGraphItems' => API_OUTPUT_EXTEND]);
         $graph = reset($graph);
         // if items from one host we change them, or set calculated if not exist on that host
         if (count($graph['hosts']) == 1) {
             if ($graph['ymax_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymax_itemid']) {
                 $newDynamic = getSameGraphItemsForHost([['itemid' => $graph['ymax_itemid']]], $this->hostid, false);
                 $newDynamic = reset($newDynamic);
                 if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
                     $graph['ymax_itemid'] = $newDynamic['itemid'];
                 } else {
                     $graph['ymax_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                 }
             }
             if ($graph['ymin_type'] == GRAPH_YAXIS_TYPE_ITEM_VALUE && $graph['ymin_itemid']) {
                 $newDynamic = getSameGraphItemsForHost([['itemid' => $graph['ymin_itemid']]], $this->hostid, false);
                 $newDynamic = reset($newDynamic);
                 if (isset($newDynamic['itemid']) && $newDynamic['itemid'] > 0) {
                     $graph['ymin_itemid'] = $newDynamic['itemid'];
                 } else {
                     $graph['ymin_type'] = GRAPH_YAXIS_TYPE_CALCULATED;
                 }
             }
         }
         // get url
         $this->screenitem['url'] = $graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED ? 'chart7.php' : 'chart3.php';
         $this->screenitem['url'] = new CUrl($this->screenitem['url']);
         foreach ($graph as $name => $value) {
             if ($name == 'width' || $name == 'height') {
                 continue;
             }
             $this->screenitem['url']->setArgument($name, $value);
         }
         $newGraphItems = getSameGraphItemsForHost($graph['gitems'], $this->hostid, false);
         foreach ($newGraphItems as $newGraphItem) {
             unset($newGraphItem['gitemid'], $newGraphItem['graphid']);
             foreach ($newGraphItem as $name => $value) {
                 $this->screenitem['url']->setArgument('items[' . $newGraphItem['itemid'] . '][' . $name . ']', $value);
             }
         }
         $this->screenitem['url']->setArgument('name', $host['name'] . NAME_DELIMITER . $graph['name']);
         $this->screenitem['url'] = $this->screenitem['url']->getUrl();
     }
     // get time control
     $timeControlData = ['id' => $this->getDataId(), 'containerid' => $containerId, 'objDims' => $graphDims, 'loadSBox' => 0, 'loadImage' => 1, 'periodFixed' => CProfile::get('web.screens.timelinefixed', 1), 'sliderMaximumTimePeriod' => ZBX_MAX_PERIOD];
     $isDefault = false;
     if ($graphDims['graphtype'] == GRAPH_TYPE_PIE || $graphDims['graphtype'] == GRAPH_TYPE_EXPLODED) {
         if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $this->screenitem['url'] === '') {
             $this->screenitem['url'] = 'chart6.php?graphid=' . $resourceId . '&screenid=' . $this->screenitem['screenid'];
             $isDefault = true;
         }
         $this->timeline['starttime'] = date(TIMESTAMP_FORMAT, get_min_itemclock_by_graphid($resourceId));
         $timeControlData['src'] = $this->screenitem['url'] . '&width=' . $this->screenitem['width'] . '&height=' . $this->screenitem['height'] . '&legend=' . $legend . '&graph3d=' . $graph3d . $this->getProfileUrlParams();
         $timeControlData['src'] .= $this->mode == SCREEN_MODE_EDIT ? '&period=3600&stime=' . date(TIMESTAMP_FORMAT, time()) : '&period=' . $this->timeline['period'] . '&stime=' . $this->timeline['stimeNow'];
     } else {
         if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || $this->screenitem['url'] === '') {
             $this->screenitem['url'] = 'chart2.php?graphid=' . $resourceId . '&screenid=' . $this->screenitem['screenid'];
             $isDefault = true;
         }
         if ($this->mode != SCREEN_MODE_EDIT && $graphId) {
             if ($this->mode == SCREEN_MODE_PREVIEW) {
                 $timeControlData['loadSBox'] = 1;
             }
         }
         $timeControlData['src'] = $this->screenitem['url'] . '&width=' . $this->screenitem['width'] . '&height=' . $this->screenitem['height'] . '&legend=' . $legend . $this->getProfileUrlParams();
         $timeControlData['src'] .= $this->mode == SCREEN_MODE_EDIT ? '&period=3600&stime=' . date(TIMESTAMP_FORMAT, time()) : '&period=' . $this->timeline['period'] . '&stime=' . $this->timeline['stimeNow'];
     }
     // output
     if ($this->mode == SCREEN_MODE_JS) {
         return 'timeControl.addObject("' . $this->getDataId() . '", ' . CJs::encodeJson($this->timeline) . ', ' . CJs::encodeJson($timeControlData) . ')';
     } else {
         if ($this->mode == SCREEN_MODE_SLIDESHOW) {
             insert_js('timeControl.addObject("' . $this->getDataId() . '", ' . CJs::encodeJson($this->timeline) . ', ' . CJs::encodeJson($timeControlData) . ');');
         } else {
             zbx_add_post_js('timeControl.addObject("' . $this->getDataId() . '", ' . CJs::encodeJson($this->timeline) . ', ' . CJs::encodeJson($timeControlData) . ');');
         }
         if ($this->mode == SCREEN_MODE_EDIT || $this->mode == SCREEN_MODE_SLIDESHOW || !$isDefault) {
             $item = new CDiv();
         } elseif ($this->mode == SCREEN_MODE_PREVIEW) {
             $item = new CLink(null, 'charts.php?graphid=' . $resourceId . '&period=' . $this->timeline['period'] . '&stime=' . $this->timeline['stimeNow']);
         }
         $item->setId($containerId);
         return $this->getOutput($item);
     }
 }
Example #2
0
 protected function inherit($graph, $hostids = null)
 {
     $graphTemplates = API::Template()->get(array('itemids' => zbx_objectValues($graph['gitems'], 'itemid'), 'output' => array('templateid'), 'nopermissions' => true));
     if (empty($graphTemplates)) {
         return true;
     }
     $graphTemplate = reset($graphTemplates);
     $chdHosts = API::Host()->get(array('templateids' => $graphTemplate['templateid'], 'output' => array('hostid', 'host'), 'preservekeys' => true, 'hostids' => $hostids, 'nopermissions' => true, 'templated_hosts' => true));
     $graph = $this->get(array('graphids' => $graph['graphid'], 'nopermissions' => true, 'filter' => array('flags' => null), 'selectGraphItems' => API_OUTPUT_EXTEND, 'output' => API_OUTPUT_EXTEND));
     $graph = reset($graph);
     foreach ($chdHosts as $chdHost) {
         $tmpGraph = $graph;
         $tmpGraph['templateid'] = $graph['graphid'];
         $tmpGraph['gitems'] = getSameGraphItemsForHost($tmpGraph['gitems'], $chdHost['hostid']);
         if (!$tmpGraph['gitems']) {
             self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s".', $tmpGraph['name'], $chdHost['host']));
         }
         if ($tmpGraph['ymax_itemid'] > 0) {
             $ymaxItemid = getSameGraphItemsForHost(array(array('itemid' => $tmpGraph['ymax_itemid'])), $chdHost['hostid']);
             if (!$ymaxItemid) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s" (Ymax value item).', $tmpGraph['name'], $chdHost['host']));
             }
             $ymaxItemid = reset($ymaxItemid);
             $tmpGraph['ymax_itemid'] = $ymaxItemid['itemid'];
         }
         if ($tmpGraph['ymin_itemid'] > 0) {
             $yminItemid = getSameGraphItemsForHost(array(array('itemid' => $tmpGraph['ymin_itemid'])), $chdHost['hostid']);
             if (!$yminItemid) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" cannot inherit. No required items on "%2$s" (Ymin value item).', $tmpGraph['name'], $chdHost['host']));
             }
             $yminItemid = reset($yminItemid);
             $tmpGraph['ymin_itemid'] = $yminItemid['itemid'];
         }
         // check if templated graph exists
         $chdGraphs = $this->get(array('filter' => array('templateid' => $tmpGraph['graphid'], 'flags' => array(ZBX_FLAG_DISCOVERY_PROTOTYPE, ZBX_FLAG_DISCOVERY_NORMAL)), 'output' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'hostids' => $chdHost['hostid']));
         if ($chdGraph = reset($chdGraphs)) {
             if ($tmpGraph['name'] !== $chdGraph['name']) {
                 $graphExists = $this->get(array('output' => array('graphid'), 'hostids' => $chdHost['hostid'], 'filter' => array('name' => $tmpGraph['name'], 'flags' => null), 'nopermissions' => true, 'limit' => 1));
                 if ($graphExists) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s".', $tmpGraph['name'], $chdHost['host']));
                 }
             } elseif ($chdGraph['flags'] != $tmpGraph['flags']) {
                 self::exception(ZBX_API_ERROR_PARAMETERS, _('Graph with same name but other type exist.'));
             }
             $tmpGraph['graphid'] = $chdGraph['graphid'];
             $this->updateReal($tmpGraph, $chdGraph);
         } else {
             $chdGraph = $this->get(array('filter' => array('name' => $tmpGraph['name'], 'flags' => null), 'output' => API_OUTPUT_EXTEND, 'selectGraphItems' => API_OUTPUT_EXTEND, 'preservekeys' => true, 'nopermissions' => true, 'hostids' => $chdHost['hostid']));
             if ($chdGraph = reset($chdGraph)) {
                 if ($chdGraph['templateid'] != 0) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (inherited from another template).', $tmpGraph['name'], $chdHost['host']));
                 } elseif ($chdGraph['flags'] != $tmpGraph['flags']) {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _('Graph with same name but other type exist.'));
                 }
                 $chdGraphItemItems = array();
                 foreach (array(API::Item(), API::ItemPrototype()) as $api) {
                     $chdGraphItemItems += $api->get(array('output' => array('key_', 'hostid', 'itemid'), 'itemids' => zbx_objectValues($chdGraph['gitems'], 'itemid'), 'preservekeys' => true));
                 }
                 if (count($chdGraph['gitems']) == count($tmpGraph['gitems'])) {
                     foreach ($tmpGraph['gitems'] as $gitem) {
                         foreach ($chdGraph['gitems'] as $chdGraphItem) {
                             $chdGraphItemItem = $chdGraphItemItems[$chdGraphItem['itemid']];
                             if ($gitem['key_'] == $chdGraphItemItem['key_'] && bccomp($chdHost['hostid'], $chdGraphItemItem['hostid']) == 0) {
                                 continue 2;
                             }
                         }
                         self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (items are not identical).', $tmpGraph['name'], $chdHost['host']));
                     }
                     $tmpGraph['graphid'] = $chdGraph['graphid'];
                     $this->updateReal($tmpGraph, $chdGraph);
                 } else {
                     self::exception(ZBX_API_ERROR_PARAMETERS, _s('Graph "%1$s" already exists on "%2$s" (items are not identical).', $tmpGraph['name'], $chdHost['host']));
                 }
             } else {
                 $graphid = $this->createReal($tmpGraph);
                 $tmpGraph['graphid'] = $graphid;
             }
         }
         $this->inherit($tmpGraph);
     }
 }
Example #3
0
/**
 * Copy specified graph to specified host.
 *
 * @param string $graphId
 * @param string $hostId
 *
 * @return array
 */
function copyGraphToHost($graphId, $hostId)
{
    $graphs = API::Graph()->get(array('graphids' => $graphId, 'output' => API_OUTPUT_EXTEND, 'selectHosts' => array('hostid', 'name'), 'selectGraphItems' => API_OUTPUT_EXTEND));
    $graph = reset($graphs);
    $graphHost = reset($graph['hosts']);
    if ($graphHost['hostid'] == $hostId) {
        error(_s('Graph "%1$s" already exists on "%2$s".', $graph['name'], $graphHost['name']));
        return false;
    }
    $graph['gitems'] = getSameGraphItemsForHost($graph['gitems'], $hostId, true, array(ZBX_FLAG_DISCOVERY_NORMAL, ZBX_FLAG_DISCOVERY_CREATED));
    if (!$graph['gitems']) {
        $host = get_host_by_hostid($hostId);
        info(_s('Skipped copying of graph "%1$s" to host "%2$s".', $graph['name'], $host['host']));
        return false;
    }
    // retrieve actual ymax_itemid and ymin_itemid
    if ($graph['ymax_itemid'] && ($itemId = get_same_item_for_host($graph['ymax_itemid'], $hostId))) {
        $graph['ymax_itemid'] = $itemId;
    }
    if ($graph['ymin_itemid'] && ($itemId = get_same_item_for_host($graph['ymin_itemid'], $hostId))) {
        $graph['ymin_itemid'] = $itemId;
    }
    unset($graph['templateid']);
    return API::Graph()->create($graph);
}