/** * Logs in the user using the given username and password in the model. * @return boolean whether login is successful */ public function searchResult() { $result = array(); $hosts = Host::model()->findAllByAttributes(array('id' => $this->hosts)); switch ($this->type) { case "camtable_mac": $search_table = 'camtable'; $search_field = 'mac'; break; case "camtable_vlan": $search_table = 'camtable'; $search_field = 'vlan_tag'; break; } if (count($hosts) && $search_table == 'camtable') { foreach ($hosts as $host) { $host->loadCamTable(); foreach ($host->cam_table as $row) { if ($this->exact_match) { $found = $row[$search_field] == $this->query; } else { $found = strpos($row[$search_field], $this->query) !== FALSE; } if ($found) { $hostDst = $host->getHostOnPort($row['port']); if ($this->exclude_link_ports && !empty($hostDst)) { continue; } $row['host'] = $host; $row['hostDst'] = $hostDst; $row['vlan'] = Vlan::model()->findByAttributes(array('tag' => $row['vlan_tag'])); $result[] = $row; } } } } return $result; }
/** * Displays connections of Host. */ public function actionCamTable() { try { if (isset($_GET['id'])) { $model = $this->loadModel((int) $_GET['id']); } else { if (isset($_GET['name'])) { $model = $this->loadModelByName((string) $_GET['name']); } } $model->loadCamTable(); // TODO: associar roteadores aos switches para pegar a // tabela ARP dos gateways da rede // $gateway = Host::model()->findByPk(Yii::app()->params['hostGatewayId']); $cam_table = array(); foreach ($model->cam_table as $ctItem) { $mac = $ctItem['mac']; $ctItem['host'] = $ctItem['mac'] ? Host::model()->findByAttributes(array('mac' => $mac)) : null; if (!$ctItem['host'] instanceof Host) { $ip = $gateway instanceof Host ? $gateway->getIpInArpTable($mac) : null; if ($ip) { $ctItem['host'] = new Host(); $ctItem['host']->mac = $mac; $ctItem['host']->ip = $ip; $ctItem['host']->name = $ip ? $ip : $mac; } } $ctItem['host_dst'] = $ctItem['host'] instanceof Host ? $model->getHostOnPort($ctItem['port']) : null; $ctItem['vlan'] = Vlan::model()->findByAttributes(array('tag' => $ctItem['vlan_tag'])); if (!$ctItem['vlan'] instanceof Vlan) { $ctItem['vlan'] = new Vlan(); $ctItem['vlan']->tag = $ctItem['vlan_tag']; } $cam_table[] = $ctItem; } $this->render('camTable', array('model' => $model, 'cam_table' => $cam_table)); } catch (CHttpException $e) { if (isset($_GET['name'])) { $name = (string) trim($_GET['name']); $this->render('addHostNotFound', array('name' => $name)); } else { throw new CHttpException(404, 'The requested page does not exist.'); } } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $tag the Tag of the VLAN to be loaded * @return Vlan the loaded model * @throws CHttpException */ public function loadModelByTag($tag) { $model = Vlan::model()->findByAttributes(array('tag' => $tag)); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionListHosts() { $this->layout = '//layouts/json'; try { if (isset($_GET['hostId'])) { $hostId = (int) $_GET['hostId']; // get root host // $rootHost = $hosts[] = Host::model()->findbyPk($hostId); // get connections $hostConnections = $rootHost->getConnections(); // get host from connections foreach ($hostConnections as $hosts_conn) { array_push($hosts, $hosts_conn->hostDst); } // in switches, get hosts by CAM table if ($rootHost->type == Host::TYPE_SWITCH) { $rootHost->loadCamTable(); // TODO: associar roteadores aos switches para pegar a // tabela ARP dos gateways da rede // $gateway = Host::model()->findByPk(Yii::app()->params['hostGatewayId']); // $arpTable = array(); foreach ($rootHost->cam_table as $k => $camHost) { // se existir conexao cadastrada para a porta, ignore o host // TODO: usar tipo do link==backbone $hostOnPort = $rootHost->getHostOnPort($camHost['port']); if ($hostOnPort instanceof Host && $hostOnPort->type != Host::TYPE_UNKNOWN) { continue; } // havendo 2 ou + hosts numa porta sem conexao cadastrada, criar 'hub virtual' $port = $camHost['port']; $portLabelPrefix = 'port_'; if ($port == $rootHost->cam_table[$k + 1]['port'] || $sourcePort == $portLabelPrefix . $port) { $sourcePort = $portLabelPrefix . $port; if (!isset($virtualHost[$sourcePort])) { $virtualHost[$sourcePort] = new Host(); $virtualHost[$sourcePort]->name = $sourcePort; $virtualHost[$sourcePort]->type = Host::TYPE_SUPPOSED_HUB; array_push($hosts, $virtualHost[$sourcePort]); $virtualConn[$sourcePort] = new Connection(); $virtualConn[$sourcePort]->hostSrc = $rootHost; $virtualConn[$sourcePort]->hostDst = $virtualHost[$sourcePort]; $virtualConn[$sourcePort]->host_src_port = $camHost['port']; $virtualConn[$sourcePort]->type = Connection::TYPE_SUPPOSED_LINK; // TODO: especificar tipo do link array_push($hostConnections, $virtualConn[$sourcePort]); } } else { unset($sourcePort); } // host $host = Host::model()->findByAttributes(array('mac' => $camHost['mac'])); if ($host instanceof Host) { $h = $host; } else { $h = new Host(); $h->mac = $camHost['mac']; $h->ip = $gateway instanceof Host ? $gateway->getIpInArpTable($camHost['mac']) : null; $h->name = $h->ip ? $h->ip : $h->mac; $h->setTypeByMAC(); } array_push($hosts, $h); // connection $c = new Connection(); $c->hostSrc = $virtualHost[$sourcePort] ? $virtualHost[$sourcePort] : $rootHost; $c->hostDst = $h; $c->host_src_port = $camHost['port']; $c->type = Connection::TYPE_UNKNOWN; // vlan $c->vlan = Vlan::model()->findByAttributes(array('tag' => $camHost['vlan_tag'])); if (!$c->vlan instanceof Vlan) { $c->vlan = new Vlan(); $c->vlan->tag = $camHost['vlan_tag']; } array_push($hostConnections, $c); } } } else { $hosts = Host::model()->findAll(array('order' => 'id')); $hostConnections = Connection::model()->findAll(); } $this->render('listHosts', array('hosts' => $hosts, 'hostConnections' => $hostConnections)); } catch (Exception $exc) { $this->render('error', array('error' => "listHosts error:" . $exc->getMessage())); } }