<div class="pHeader"><?php print _('Ping check result'); ?> </div> <!-- content --> <div class="pContent"> <?php # online if ($pingRes == 0) { $Result->show("success", _("IP address") . " {$address['ip']} " . _("is alive"), false); } elseif ($pingRes == 1 || $pingRes == 2) { $Result->show("danger", _("IP address") . " {$address['ip']} " . _("is not alive"), false); } else { # fetch error code $ecode = $Ping->ping_exit_explain($pingRes); $Result->show("danger", _("Error") . ": {$ecode} ({$pingRes})", false); } # hr print "<hr>"; $Result->show("muted pull-right", "(" . $Ping->settings->scanPingType . ")", false); # additional notes if (isset($Ping->rtt)) { $Result->show("muted pull-right", $Ping->rtt . " ms", false); } ?> </div> <!-- footer --> <div class="pFooter"> <div class="btn-group">
/** * Read address functions * * identifiers can be: * - {id} * - {id}/ping/ // pings address * - /search/{ip_address}/ // searches for addresses in database, returns multiple if found * - custom_fields * - tags // all tags * - tags/{id}/ // specific tag * - tags/{id}/addresses // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip_addr); // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); return array("code" => 200, "data" => $result); } else { $this->Response->throw_exception(404, "Address offline. Exit code: " . $result['exit_code'] . "( " . $Scan->ping_exit_explain($result['exit_code']) . " )"); } } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }
/** * Read address functions * * identifiers can be: * - /addresses/{id}/ * - /addresses/{id}/ping/ // pings address * - /addresses/{ip}/{subnetId}/ // Returns address from subnet * - /addresses/search/{ip_address}/ // searches for addresses in database, returns multiple if found * - /addresses/search_hostname/{hostname}/ // searches for addresses in database by hostname, returns multiple if found * - /addresses/search_hostbase/{hostbase}/ // searches for addresses by leading substring (base) of hostname, returns ordered multiple * - /addresses/first_free/{subnetId}/ // returns first available address (subnetId can be provided with parameters) * - /addresses/custom_fields/ // custom fields * - /addresses/tags/ // all tags * - /addresses/tags/{id}/ // specific tag * - /addresses/tags/{id}/addresses/ // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "first_free") { // check for isFull if (isset($this->_params->subnetId)) { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->subnetId); } else { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->id2); } if ($subnet->isFull == 1) { $this->Response->throw_exception(404, "No free addresses found"); } $this->_params->ip_addr = $this->Addresses->get_first_available_address($subnet->id, $this->Subnets); // null if ($this->_params->ip_addr == false) { $this->Response->throw_exception(404, 'No free addresses found'); } else { return array("code" => 200, "data" => $this->Addresses->transform_address($this->_params->ip_addr, "dotted")); } } elseif ($this->Tools->validate_ip($this->_params->id) !== false && isset($this->_params->id2)) { // fetch all in subnet $result = $this->Tools->fetch_multiple_objects("ipaddresses", "subnetId", $this->_params->id2); if ($result !== false) { foreach ($result as $k => $r) { if ($r->ip !== $this->_params->id) { unset($result[$k]); } else { $result_filtered = $r; } } if (sizeof($result) == 0) { $result = false; } else { $result = $result_filtered; } } if ($result == false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $result); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result = array(); $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip); $result['result_code'] = $Scan->ping_exit_explain($result['exit_code']); $result['message'] = $result['exit_code'] == 0 ? "Address online" : "Address offline"; // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); } return array("code" => 200, "data" => $result); } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } elseif (@$this->_params->id == "search_hostname") { $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $this->_params->id2); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } elseif (@$this->_params->id == "search_hostbase") { $target = $this->_params->id2 . "%"; $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $target, "dns_name", true, true); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }