/** * Returns a device_id when given an entity_id and an entity_type. Returns FALSE if the device isn't found. * * @param $entity_id * @param $entity_type * * @return bool|integer */ function get_device_id_by_entity_id($entity_id, $entity_type) { // $entity = get_entity_by_id_cache($entity_type, $entity_id); $translate = entity_type_translate_array($entity_type); if (is_numeric($entity_id) && $entity_type) { $device_id = dbFetchCell('SELECT `device_id` FROM `' . $translate['table'] . '` WHERE `' . $translate['id_field'] . '` = ?', array($entity_id)); } if (is_numeric($device_id)) { return $device_id; } else { return FALSE; } }
function entity_permitted_array(&$entities, $entity_type) { $entity_type_data = entity_type_translate_array($entity_type); // Strip out the entities the user isn't allowed to see, if they don't have global view rights if (!isset($_SESSION['user_limited']) || $_SESSION['user_limited']) { foreach ($entities as $key => $entity) { if (!is_entity_permitted($entity[$entity_type_data['id_field']], $entity_type, $entity['device_id'])) { unset($entities[$key]); } } } }
function generate_entity_popup_header($entity, $vars) { $translate = entity_type_translate_array($vars['entity_type']); $vars['popup'] = TRUE; $vars['entity_icon'] = TRUE; switch ($vars['entity_type']) { case "sensor": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_sensor_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "toner": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_toner_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "bgp_peer": if ($entity['peer_device_id']) { $peer_dev = device_by_id_cache($entity['peer_device_id']); $peer_name = '<br /><a class="entity" style="font-weight: bold;">' . $peer_dev['hostname'] . '</a>'; } else { if ($entity['reverse_dns']) { $peer_name = '<br /><span style="font-weight: bold;">' . $entity['reverse_dns'] . '</span>'; } } $astext = '<span>AS' . $entity['bgpPeerRemoteAs']; if ($entity['astext']) { $astext .= '<br />' . $entity['astext'] . '</span>'; } $astext .= '</span>'; $contents .= generate_box_open(); $contents .= ' <table class="' . OBS_CLASS_TABLE . '"> <tr class="' . $entity['row_class'] . ' vertical-align" style="font-size: 10pt;"> <td class="state-marker"></td> <td style="width: 10px;"></td> <td style="width: 10px;"><i class="' . $translate['icon'] . '"></i></td> <td><a class="entity-popup" style="font-size: 15px; font-weight: bold;">' . $entity['entity_shortname'] . '</a>' . $peer_name . '</td> <td class="text-nowrap" style="width: 20%;">' . $astext . '</td> <td></td> </tr> </table>'; $contents .= generate_box_close(); break; case "sla": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_sla_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "processor": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_processor_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "mempool": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_mempool_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "p2pradio": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_p2pradio_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "status": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_status_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "storage": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_storage_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "netscalervsvr": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_netscalervsvr_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "netscalersvc": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_netscalersvc_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; case "netscalersvcgrpmem": $contents .= generate_box_open(); $contents .= '<table class="' . OBS_CLASS_TABLE . '">'; $contents .= generate_netscalersvcmem_row($entity, $vars); $contents .= '</table>'; $contents .= generate_box_close(); break; default: entity_rewrite($vars['entity_type'], $entity); $contents = generate_box_open() . ' <table class="' . OBS_CLASS_TABLE_STRIPED . '"> <tr class="' . $entity['row_class'] . '" style="font-size: 10pt;"> <td class="state-marker"></td> <td style="width: 10px;"></td> <td width="400"><i class="' . $translate['icon'] . '" style="margin-right: 10px;"></i> <a class="entity-popup" style="font-size: 15px; font-weight: bold;">' . $entity['entity_name'] . '</a></td> <td width="100"></td> <td></td> </tr> </table>' . generate_box_close(); } return $contents; }
function match_device_entities($device_id, $attributes, $entity_type) { $param = array(); list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($entity_type); $entity_type = entity_type_translate_array($entity_type); $sql = "SELECT * FROM `" . mres($entity_table) . "`"; $sql .= " WHERE device_id = ?"; $param[] = $device_id; if (isset($entity_type['deleted_field'])) { $sql .= " AND `" . $entity_type['deleted_field'] . "` != ?"; $param[] = '1'; } foreach ($attributes as $attrib) { switch ($attrib['condition']) { case '==': case 'is': case 'eq': case 'equals': case '=': $sql .= " AND `" . $attrib['attrib'] . "` = ?"; $param[] = $attrib['value']; break; case 'ne': case 'notequals': case 'isnot': case '!=': $sql .= " AND `" . $attrib['attrib'] . "` != ?"; $param[] = $attrib['value']; break; case 'le': case '<=': $sql .= " AND `" . $attrib['attrib'] . "` < ?"; $param[] = $attrib['value']; break; case 'ge': case '>=': $sql .= " AND `" . $attrib['attrib'] . "` > ?"; $param[] = $attrib['value']; break; case 'lt': case 'less': case '<': $sql .= " AND `" . $attrib['attrib'] . "` < ?"; $param[] = $attrib['value']; break; case 'gt': case 'greater': case '>': $sql .= " AND `" . $attrib['attrib'] . "` > ?"; $param[] = $attrib['value']; break; case 'match': $attrib['value'] = str_replace("*", "%", $attrib['value']); $sql .= " AND `" . $attrib['attrib'] . "` LIKE ?"; $param[] = $attrib['value']; break; case 'regexp': $sql .= " AND `" . $attrib['attrib'] . "` REGEXP ?"; $param[] = $attrib['value']; break; case 'notmatch': $attrib['value'] = str_replace("*", "%", $attrib['value']); $sql .= " AND `" . $attrib['attrib'] . "` NOT LIKE ?"; $param[] = $attrib['value']; break; case 'notregexp': $sql .= " AND `" . $attrib['attrib'] . "` NOT REGEXP ?"; $param[] = $attrib['value']; break; } } $entities = dbFetchRows($sql, $param); return $entities; }
function humanize_pseudowire(&$pw) { if (isset($pw['humanized'])) { return; } if ($pw['pwRowStatus'] != 'active') { $pw['row_class'] = 'ignore'; } else { if ($pw['event'] == 'alert') { $pw['row_class'] = 'error'; } else { if ($pw['event'] != 'ok') { $pw['row_class'] = 'warning'; } } } $device =& $GLOBALS['cache']['devices']['id'][$pw['device_id']]; if (isset($device['status']) && !$device['status'] || isset($device['disabled']) && $device['disabled']) { $pw['row_class'] = 'error'; } if ($pw['event'] == 'ok') { $pw['pw_class'] = 'label label-success'; } else { if ($pw['event'] == 'alert') { $pw['pw_class'] = 'label label-error'; } else { $pw['pw_class'] = 'label label-warning'; } } $translate = entity_type_translate_array('pseudowire'); $pw['graph'] = $translate['graph']['type']; // Default graph $pw['humanized'] = TRUE; }
function match_device_entities($device_id, $entity_attribs, $entity_type) { // FIXME - this is going to be horribly slow. list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($entity_type); $entity_type = entity_type_translate_array($entity_type); if (!is_array($entity_type)) { return NULL; } // Do nothing if entity type unknown $param = array(); $sql = "SELECT * FROM `" . mysql_real_escape_string($entity_table) . "`"; $sql .= " WHERE device_id = ?"; #print_vars($entity_type); if (isset($entity_type['where'])) { $sql .= ' AND ' . $entity_type['where']; } $param[] = $device_id; if (isset($entity_type['deleted_field'])) { $sql .= " AND `" . $entity_type['deleted_field'] . "` != ?"; $param[] = '1'; } foreach ($entity_attribs as $attrib) { switch ($attrib['condition']) { case '==': case 'is': case 'eq': case 'equals': case '=': $sql .= " AND `" . $attrib['attrib'] . "` = ?"; $param[] = $attrib['value']; break; case 'ne': case 'notequals': case 'isnot': case '!=': $sql .= " AND `" . $attrib['attrib'] . "` != ?"; $param[] = $attrib['value']; break; case 'le': case '<=': $sql .= " AND `" . $attrib['attrib'] . "` < ?"; $param[] = $attrib['value']; break; case 'ge': case '>=': $sql .= " AND `" . $attrib['attrib'] . "` > ?"; $param[] = $attrib['value']; break; case 'lt': case 'less': case '<': $sql .= " AND `" . $attrib['attrib'] . "` < ?"; $param[] = $attrib['value']; break; case 'gt': case 'greater': case '>': $sql .= " AND `" . $attrib['attrib'] . "` > ?"; $param[] = $attrib['value']; break; case 'match': $attrib['value'] = str_replace("*", "%", $attrib['value']); $sql .= 'AND ifnull(`' . $attrib['attrib'] . '`,"") LIKE ?'; $param[] = $attrib['value']; break; case 'regexp': $sql .= 'AND ifnull(`' . $attrib['attrib'] . '`,"") REGEXP ?'; $param[] = $attrib['value']; break; case 'notmatch': $attrib['value'] = str_replace("*", "%", $attrib['value']); $sql .= 'AND ifnull(`' . $attrib['attrib'] . '`,"") NOT LIKE ?'; $param[] = $attrib['value']; break; case 'notregexp': $sql .= 'AND ifnull(`' . $attrib['attrib'] . '`,"") NOT REGEXP ?'; $param[] = $attrib['value']; break; } } // print_vars(array($sql, $param)); $entities = dbFetchRows($sql, $param); return $entities; }
function entity_rewrite($entity_type, &$entity) { $translate = entity_type_translate_array($entity_type); // By default, fill $entity['entity_name'] with name_field contents. if (isset($translate['name_field'])) { $entity['entity_name'] = $entity[$translate['name_field']]; } // By default, fill $entity['entity_shortname'] with shortname_field contents. Fallback to entity_name when field name is not set. if (isset($translate['shortname_field'])) { $entity['entity_shortname'] = $entity[$translate['name_field']]; } else { $entity['entity_shortname'] = $entity['entity_name']; } // By default, fill $entity['entity_descr'] with descr_field contents. if (isset($translate['descr_field'])) { $entity['entity_descr'] = $entity[$translate['descr_field']]; } // By default, fill $entity['entity_id'] with id_field contents. if (isset($translate['id_field'])) { $entity['entity_id'] = $entity[$translate['id_field']]; } switch ($entity_type) { case "bgp_peer": // Special handling of name/shortname/descr for bgp_peer, since it combines multiple elements. if (Net_IPv6::checkIPv6($entity['bgpPeerRemoteAddr'])) { $addr = Net_IPv6::compress($entity['bgpPeerRemoteAddr']); } else { $addr = $entity['bgpPeerRemoteAddr']; } $entity['entity_name'] = "AS" . $entity['bgpPeerRemoteAs'] . " " . $addr; $entity['entity_shortname'] = $addr; $entity['entity_descr'] = $entity['astext']; break; case "sla": $entity['entity_name'] = "SLA #" . $entity['sla_index'] . " (" . $entity['sla_tag'] . ")"; $entity['entity_shortname'] = "#" . $entity['sla_index'] . " (" . $entity['sla_tag'] . ")"; break; case "pseudowire": $entity['entity_name'] = $entity['pwID'] . ($entity['pwDescr'] ? " (" . $entity['pwDescr'] . ")" : ''); $entity['entity_shortname'] = $entity['pwID']; break; } }
$sensors_db[$sensor_type][$entry['sensor_id']] = $entry; } } } //r($sensors_db['measured']); // Now print founded bundle (measured_class+sensor) if (isset($sensors_db['measured'])) { foreach ($sensors_db['measured'] as $measured_class => $measured_entity) { $box_args = array('title' => nicecase($measured_class) . ' sensors', 'icon' => 'oicon-node'); echo generate_box_open($box_args); echo ' <table class="table table-condensed table-striped">'; foreach ($measured_entity as $entity_id => $entry) { $entity = get_entity_by_id_cache($measured_class, $entity_id); $entity_name = entity_name($measured_class, $entity); $entity_link = generate_entity_link($measured_class, $entity); $entity_type = entity_type_translate_array($measured_class); //echo(' <tr class="'.$port['row_class'].'"> // <td class="state-marker"></td> echo ' <tr> <td colspan="6" class="entity"><i class="' . $entity_type['icon'] . '"></i> ' . $entity_link . '</td></tr>'; foreach ($entry as $sensor) { // Remove port name from sensor description $sensor['sensor_descr'] = trim(str_ireplace($entity_name, '', $sensor['sensor_descr'])); if (empty($sensor['sensor_descr'])) { // Some time sensor descriptions equals to entity name $sensor['sensor_descr'] = nicecase($sensor['sensor_class']); } print_sensor_row($sensor, $vars); } } ?>
function entity_rewrite($type, &$entity) { $translate = entity_type_translate_array($type); switch ($type) { case "bgp_peer": if (Net_IPv6::checkIPv6($entity['bgpPeerRemoteAddr'])) { $addr = Net_IPv6::compress($entity['bgpPeerRemoteAddr']); } else { $addr = $entity['bgpPeerRemoteAddr']; } $entity['entity_name'] = "AS" . $entity['bgpPeerRemoteAs'] . " " . $addr; $entity['entity_shortname'] = $addr; $entity['entity_descr'] = $entity['astext']; break; default: // By default, fill $entity['entity_name'] with name_field contents. if (isset($translate['name_field'])) { $entity['entity_name'] = $entity[$translate['name_field']]; } // By default, fill $entity['entity_shortname'] with shortname_field contents. Fallback to entity_name when field name is not set. if (isset($translate['shortname_field'])) { $entity['entity_shortname'] = $entity[$translate['name_field']]; } else { $entity['entity_shortname'] = $entity['entity_name']; } // By default, fill $entity['entity_descr'] with descr_field contents. if (isset($translate['descr_field'])) { $entity['entity_descr'] = $entity[$translate['descr_field']]; } break; } }
function match_device_entities($device_id, $entity_attribs, $entity_type) { // FIXME - this is going to be horribly slow. list($entity_table, $entity_id_field, $entity_name_field) = entity_type_translate($entity_type); $entity_type = entity_type_translate_array($entity_type); if (!is_array($entity_type)) { return NULL; } // Do nothing if entity type unknown $param = array(); $sql = "SELECT * FROM `" . dbEscape($entity_table) . "`"; // FIXME. Not sure why these required escape table name $sql .= " WHERE device_id = ?"; #print_vars($entity_type); if (isset($entity_type['where'])) { $sql .= ' AND ' . $entity_type['where']; } $param[] = $device_id; if (isset($entity_type['deleted_field'])) { $sql .= " AND `" . $entity_type['deleted_field'] . "` != ?"; $param[] = '1'; } foreach ($entity_attribs as $attrib) { switch ($attrib['condition']) { case 'ge': case '>=': $sql .= ' AND `' . $attrib['attrib'] . '` >= ?'; $param[] = $attrib['value']; break; case 'le': case '<=': $sql .= ' AND `' . $attrib['attrib'] . '` <= ?'; $param[] = $attrib['value']; break; case 'gt': case 'greater': case '>': $sql .= ' AND `' . $attrib['attrib'] . '` > ?'; $param[] = $attrib['value']; break; case 'lt': case 'less': case '<': $sql .= ' AND `' . $attrib['attrib'] . '` < ?'; $param[] = $attrib['value']; break; case 'notequals': case 'isnot': case 'ne': case '!=': $sql .= ' AND `' . $attrib['attrib'] . '` != ?'; $param[] = $attrib['value']; break; case 'equals': case 'eq': case 'is': case '==': case '=': $sql .= ' AND `' . $attrib['attrib'] . '` = ?'; $param[] = $attrib['value']; break; case 'match': case 'matches': $attrib['value'] = str_replace('*', '%', $attrib['value']); $attrib['value'] = str_replace('?', '_', $attrib['value']); $sql .= ' AND IFNULL(`' . $attrib['attrib'] . '`, "") LIKE ?'; $param[] = $attrib['value']; break; case 'notmatches': case 'notmatch': case '!match': $attrib['value'] = str_replace('*', '%', $attrib['value']); $attrib['value'] = str_replace('?', '_', $attrib['value']); $sql .= ' AND IFNULL(`' . $attrib['attrib'] . '`, "") NOT LIKE ?'; $param[] = $attrib['value']; break; case 'regexp': case 'regex': $sql .= ' AND IFNULL(`' . $attrib['attrib'] . '`, "") REGEXP ?'; $param[] = $attrib['value']; break; case 'notregexp': case 'notregex': case '!regexp': case '!regex': $sql .= ' AND IFNULL(`' . $attrib['attrib'] . '`, "") NOT REGEXP ?'; $param[] = $attrib['value']; break; case 'in': case 'list': $sql .= generate_query_values(explode(',', $attrib['value']), $attrib['attrib']); break; case '!in': case '!list': case 'notin': case 'notlist': $sql .= generate_query_values(explode(',', $attrib['value']), $attrib['attrib'], '!='); break; } } // print_vars(array($sql, $param)); $entities = dbFetchRows($sql, $param); return $entities; }
function generate_entity_link($type, $entity, $text = NULL, $graph_type = NULL, $escape = TRUE) { global $config, $entity_cache; if (is_numeric($entity)) { $entity = get_entity_by_id_cache($type, $entity); } // Rewrite sensor subtypes to 'sensor' $translate = entity_type_translate_array($type); if (isset($translate['parent_type'])) { $type = $translate['parent_type']; } switch ($type) { case "device": if (empty($text)) { $text = $entity['hostname']; } // FIXME use name_field property for all of these? Like entity_rewrite above does. $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id']), array(), $escape); break; case "mempool": if (empty($text)) { $text = $entity['mempool_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'mempool'), array(), $escape); break; case "processor": if (empty($text)) { $text = $entity['processor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'processor'), array(), $escape); break; case "sensor": if (empty($text)) { $text = $entity['sensor_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => $entity['sensor_class']), array(), $escape); break; case "toner": if (empty($text)) { $text = $entity['toner_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'printing'), array(), $escape); break; case "port": $link = generate_port_link($entity, $text, $graph_type, $escape); break; case "storage": if (empty($text)) { $text = $entity['storage_descr']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'health', 'metric' => 'storage'), array(), $escape); break; case "bgp_peer": if (Net_IPv6::checkIPv6($entity['bgpPeerRemoteAddr'])) { $addr = Net_IPv6::compress($entity['bgpPeerRemoteAddr']); } else { $addr = $entity['bgpPeerRemoteAddr']; } if (empty($text)) { $text = $addr . " (AS" . $entity['bgpPeerRemoteAs'] . ")"; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'routing', 'proto' => 'bgp'), array(), $escape); break; case "netscaler_vsvr": if (empty($text)) { $text = $entity['vsvr_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_vsvr', 'vsvr' => $entity['vsvr_id']), array(), $escape); break; case "netscaler_svc": if (empty($text)) { $text = $entity['svc_label']; } $link = generate_link($text, array('page' => 'device', 'device' => $entity['device_id'], 'tab' => 'loadbalancer', 'type' => 'netscaler_services', 'svc' => $entity['svc_id']), array(), $escape); break; default: $link = $entity[$type . '_id']; } return $link; }