$actual_crawl_cycle = Crawling::getActualCrawlCycle(); if (empty($actual_crawl_cycle) or strtotime($actual_crawl_cycle['crawl_date']) + ($GLOBALS['crawl_cycle'] - 1) * 60 <= time()) { echo "Create crawl data for offline routers\n"; //Set all routers in old crawl cycle that have not been crawled yet to status offline try { $stmt = DB::getInstance()->prepare("SELECT *\n\t\t\t\t\t\t\t\t\t\t\t\tFROM routers\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE id not in (SELECT router_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM crawl_routers\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE crawl_cycle_id={$actual_crawl_cycle['id']})"); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { echo $e->getMessage(); echo $e->getTraceAsString(); } foreach ($result as $router) { echo "\tInserting offline data for router " . $router['hostname'] . "\n"; //store offline crawl for offline router $router_status = new RouterStatus(false, (int) $actual_crawl_cycle['id'], (int) $router['id'], "offline"); $router_status->store(); } echo "Create crawl data for offline interfaces\n"; //store offline crawl for all interfaces of offline router try { $stmt = DB::getInstance()->prepare("SELECT *\n\t\t\t\t\t\t\t\t\t\t\t\tFROM interfaces\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE id not in (SELECT interface_id\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t FROM crawl_interfaces\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t WHERE crawl_cycle_id={$actual_crawl_cycle['id']})"); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { echo $e->getMessage(); echo $e->getTraceAsString(); } foreach ($result as $interface) { echo "\tInserting offline data for interface " . $interface['name'] . "\n"; $networkinterface_status = new NetworkinterfaceStatus(false, (int) $actual_crawl_cycle['id'], (int) $interface['id'], (int) $interface['router_id']);
echo "\t\t\tCraw was successfull, online\n"; try { $xml = new SimpleXMLElement($return_string); $data = simplexml2array($xml); $data['router_id'] = $router->getRouterId(); $data['system_data']['status'] = "online"; } catch (Exception $e) { echo nl2br($e->getMessage()); echo "\t\t\tThere was an error parsing the crawled XML\n"; $data = array(); $data['router_id'] = $router->getRouterId(); $data['system_data']['status'] = "unknown"; } /**Insert Router System Data*/ echo "\t\t\tInserting RouterStatus into DB\n"; $router_status = new RouterStatus(false, (int) $actual_crawl_cycle, $router->getRouterId(), $data['system_data']['status'], false, $data['system_data']['hostname'], (int) $data['client_count'], $data['system_data']['chipset'], $data['system_data']['cpu'], (int) $data['system_data']['memory_total'], (int) $data['system_data']['memory_caching'], (int) $data['system_data']['memory_buffering'], (int) $data['system_data']['memory_free'], $data['system_data']['loadavg'], $data['system_data']['processes'], $data['system_data']['uptime'], $data['system_data']['idletime'], $data['system_data']['local_time'], $data['system_data']['distname'], $data['system_data']['distversion'], $data['system_data']['openwrt_core_revision'], $data['system_data']['openwrt_feeds_packages_revision'], $data['system_data']['firmware_version'], $data['system_data']['firmware_revision'], $data['system_data']['kernel_version'], $data['system_data']['configurator_version'], $data['system_data']['nodewatcher_version'], $data['system_data']['fastd_version'], $data['system_data']['batman_advanced_version']); if ($router_status->store()) { echo "\t\t\tInserting Batman advanced interfaces into DB\n"; /**Insert Batman advanced Interfaces*/ foreach ($data['batman_adv_interfaces'] as $bat_adv_int) { try { DB::getInstance()->exec("INSERT INTO crawl_batman_advanced_interfaces (router_id, crawl_cycle_id, name, status, crawl_date)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t VALUES ('{$data['router_id']}', '{$actual_crawl_cycle}', '{$bat_adv_int['name']}', '{$bat_adv_int['status']}', NOW());"); } catch (PDOException $e) { echo $e->getMessage(); } } echo "\t\t\tInserting Batman advanced originators into DB\n"; /**Insert Batman Advanced Originators*/ $originator_count = count($data['batman_adv_originators']); RrdTool::updateRouterBatmanAdvOriginatorsCountHistory($data['router_id'], $originator_count); $average_link_quality = 0;
public function store() { if ($this->getStatusId() != 0 and $this->getCrawlCycleId() != 0 and $this->getRouterId() != 0 and $this->getStatus() != "") { echo "UPDATE NOT IMPLEMENTED NOW (and will possibly never be implemented because it is not needed..."; } elseif ($this->getCrawlCycleId() != 0 and $this->getRouterId() != 0 and $this->getStatus() != "") { //check if there already exists an crawl item in this crawl cycle $tmp = new RouterStatus(false, $this->getCrawlCycleId(), $this->getRouterId()); if (!$tmp->fetch()) { try { $stmt = DB::getInstance()->prepare("INSERT INTO crawl_routers (router_id, crawl_cycle_id, crawl_date, status,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t hostname, distname, distversion, chipset,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t cpu, memory_total, memory_caching, memory_buffering,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t memory_free, loadavg, processes, uptime, idletime,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t local_time, batman_advanced_version, fastd_version,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t kernel_version, configurator_version,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t nodewatcher_version, firmware_version,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t firmware_revision, openwrt_core_revision,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t openwrt_feeds_packages_revision, client_count)\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tVALUES (?, ?, NOW(), ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?, ?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?, ?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?, ?, ?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t?, ?)"); $stmt->execute(array($this->getRouterId(), $this->getCrawlCycleId(), $this->getStatus(), $this->getHostname(), $this->getDistname(), $this->getDistversion(), $this->getChipset(), $this->getCpu(), $this->getMemoryTotal(), $this->getMemoryCaching(), $this->getMemoryBuffering(), $this->getMemoryFree(), $this->getLoadavg(), $this->getProcesses(), $this->getUptime(), $this->getIdletime(), $this->getLocaltime(), $this->getBatmanAdvancedVersion(), $this->getFastdVersion(), $this->getKernelVersion(), $this->getConfiguratorVersion(), $this->getNodewatcherVersion(), $this->getFirmwareVersion(), $this->getFirmwareRevision(), $this->getOpenwrtCoreRevision(), $this->getOpenwrtFeedsPackagesRevision(), $this->getClientCount())); } catch (PDOException $e) { echo $e->getMessage(); echo $e->getTraceAsString(); } //Update RRD-Files for graphs RrdTool::updateRouterMemoryHistory($this->getRouterId(), $this->getMemoryFree(), $this->getMemoryCaching(), $this->getMemoryBuffering()); $processes = explode("/", $this->getProcesses()); $processes[0] = isset($processes[0]) ? $processes[0] : 0; $processes[1] = isset($processes[1]) ? $processes[1] : 1; RrdTool::updateRouterProcessHistory($this->getRouterId(), $processes[0], $processes[1]); //create events $eventlist = new Eventlist(); $router = new Router($this->getRouterId()); $router->fetch(); $last_router_status = new RouterStatus(false, (int) Crawling::getLastEndedCrawlCycle()['id'], $this->getRouterId()); if ($last_router_status->fetch()) { if ($this->getStatus() and $last_router_status->getStatus() and $this->getStatus() != $last_router_status->getStatus()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "status", array('from' => $last_router_status->getStatus(), 'to' => $this->getStatus(), 'hostname' => $router->getHostname()))); } if ($this->getUptime() and $last_router_status->getUptime() and $this->getUptime() < $last_router_status->getUptime()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "reboot", array('hostname' => $router->getHostname()))); } if ($this->getHostname() and $last_router_status->getHostname() and $this->getHostname() != $last_router_status->getHostname()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "hostname", array('from' => $last_router_status->getHostname(), 'to' => $this->getHostname(), 'hostname' => $router->getHostname()))); } if ($this->getChipset() and $last_router_status->getChipset() and $this->getChipset() != $last_router_status->getChipset()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "chipset", array('from' => $last_router_status->getChipset(), 'to' => $this->getChipset(), 'hostname' => $router->getHostname()))); } if ($this->getNodewatcherVersion() and $last_router_status->getNodewatcherVersion() and $this->getNodewatcherVersion() != $last_router_status->getNodewatcherVersion()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "nodewatcher_version", array('from' => $last_router_status->getNodewatcherVersion(), 'to' => $this->getNodewatcherVersion(), 'hostname' => $router->getHostname()))); } if ($this->getFirmwareVersion() and $last_router_status->getFirmwareVersion() and $this->getFirmwareVersion() != $last_router_status->getFirmwareVersion()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "firmware_version", array('from' => $last_router_status->getFirmwareVersion(), 'to' => $this->getFirmwareVersion(), 'hostname' => $router->getHostname()))); } if ($this->getBatmanAdvancedVersion() and $last_router_status->getBatmanAdvancedVersion() and $this->getBatmanAdvancedVersion() != $last_router_status->getBatmanAdvancedVersion()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "batman_advanced_version", array('from' => $last_router_status->getBatmanAdvancedVersion(), 'to' => $this->getBatmanAdvancedVersion(), 'hostname' => $router->getHostname()))); } if ($this->getDistversion() and $last_router_status->getDistversion() and $this->getDistversion() != $last_router_status->getDistversion()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "distversion", array('from' => $last_router_status->getDistversion(), 'to' => $this->getDistversion(), 'hostname' => $router->getHostname()))); } if ($this->getDistname() and $last_router_status->getDistname() and $this->getDistname() != $last_router_status->getDistname()) { $eventlist->add(new Event(false, false, "router", $this->getRouterId(), "distname", array('from' => $last_router_status->getDistname(), 'to' => $this->getDistname(), 'hostname' => $router->getHostname()))); } } $eventlist->store(); //Check if netmon already knows the given chipset and if //the router assigned to this status has the right chipset set if ($this->getStatus() == "online" and $router->getChipset()->getName() != $this->getChipset()) { $chipset = new Chipset(false, $this->getChipset()); if (!$chipset->fetch()) { $chipset->store(); } $router->setChipsetId($chipset->getChipsetId()); $router->store(); } //Return the ID of the inserted RouterStatus return DB::getInstance()->lastInsertId(); } } return false; }
public function setStatusdata($routerstatus) { if ($routerstatus instanceof RouterStatus) { $this->statusdata = $routerstatus; return true; } elseif (is_int($routerstatus)) { $router_status = new RouterStatus(false, false, $routerstatus); if ($router_status->fetch()) { $this->statusdata = $router_status; return true; } } return false; }
public function insertNewRouter() { $check_router_hostname_exist = Router_old::getRouterByHostname($_POST['hostname']); if (!isset($_POST['allow_router_auto_assign'])) { $_POST['allow_router_auto_assign'] = 0; $_POST['router_auto_assign_login_string'] = ''; } if ($_POST['allow_router_auto_assign'] == '1' and !empty($_POST['router_auto_assign_login_string'])) { $check_router_auto_assign_login_string = Router_old::getRouterByAutoAssignLoginString($_POST['router_auto_assign_login_string']); } if (empty($_POST['hostname'])) { $message[] = array("Bitte geben Sie einen Hostname an.", 2); Message::setMessage($message); return array("result" => false, "router_id" => $router_id); } elseif (!empty($check_router_hostname_exist)) { $message[] = array("Ein Router mit dem Hostnamen {$_POST['hostname']} existiert bereits, bitte wählen Sie einen anderen Hostnamen.", 2); Message::setMessage($message); return array("result" => false, "router_id" => $router_id); } elseif (!(is_string($_POST['hostname']) and strlen($_POST['hostname']) <= 255 and preg_match("/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*\$/", $_POST['hostname']))) { //check for valid hostname as specified in rfc 1123 //see http://stackoverflow.com/a/3824105 $message[] = array("Der Hostname ist ungültig. Erlaubt sind Hostnames nach RFC 1123.", 2); Message::setMessage($message); return array("result" => false, "router_id" => $router_id); } elseif (!empty($check_router_auto_assign_login_string)) { $message[] = array("Der Router Auto Assign Login String wird bereits verwendet.", 2); Message::setMessage($message); return array("result" => false, "router_id" => $router_id); } elseif ($_POST['allow_router_auto_assign'] == '1' and ($_POST['router_auto_assign_login_string'] == "Mac-Adresse..." or empty($_POST['router_auto_assign_login_string']) or ctype_space($_POST['router_auto_assign_login_string']))) { $message[] = array("Wenn Automatische Routerzuweisung aktiviert ist, muss eine Mac-Adresse gesetzt werden.", 2); $message[] = array("Du findest die Mac-Adresse oft auf der Rückseite des Routers.", 0); Message::setMessage($message); return array("result" => false, "router_id" => $router_id); } else { if (!is_numeric($_POST['latitude']) or !is_numeric($_POST['longitude'])) { $_POST['latitude'] = 0; $_POST['longitude'] = 0; } try { $stmt = DB::getInstance()->prepare("INSERT INTO routers (user_id, create_date, update_date, crawl_method, hostname, allow_router_auto_assign, router_auto_assign_login_string, description, location, latitude, longitude, chipset_id)\n\t\t\t\t\t\t\t\t VALUES (?, NOW(), NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->execute(array($_SESSION['user_id'], $_POST['crawl_method'], $_POST['hostname'], $_POST['allow_router_auto_assign'], $_POST['router_auto_assign_login_string'], $_POST['description'], $_POST['location'], $_POST['latitude'], $_POST['longitude'], $_POST['chipset_id'])); $router_id = DB::getInstance()->lastInsertId(); } catch (PDOException $e) { echo $e->getMessage(); echo $e->getTraceAsString(); } $crawl_cycle_id = Crawling::getLastEndedCrawlCycle(); $router_status = new RouterStatus(false, (int) $crawl_cycle_id['id'], (int) $router_id, "offline"); $router_status->store(); //add new api key do { $api_key = new ApiKey(false, ApiKey::generateApiKey(), (int) $router_id, "router", "Initial key"); $api_key_id = $api_key->store(); } while (!$api_key_id); if ($_POST['allow_router_auto_assign'] == '1' and !empty($_POST['router_auto_assign_login_string'])) { RoutersNotAssigned::deleteByAutoAssignLoginString($_POST['router_auto_assign_login_string']); } $message[] = array("Der Router {$_POST['hostname']} wurde angelegt.", 1); //Add event for new router //TODO: add Router Object to data array $event = new Event(false, 'router', (int) $router_id, 'new', array()); $event->store(); //Send Message to twitter if ($_POST['twitter_notification'] == '1') { Message::postTwitterMessage(Config::getConfigValueByName('community_name') . " hat einen neuen #Freifunk Knoten! Wo? Schau nach: " . Config::getConfigValueByName('url_to_netmon') . "/router.php?router_id={$router_id}"); } Message::setMessage($message); return array("result" => true, "router_id" => $router_id); } }