コード例 #1
0
function common_routeur($conf, $item, $vocables)
{
    $final_results = array();
    $checktimestamp_log = time();
    $item_checked = $item;
    $pdo = connexionbdd($conf);
    //second we take fails checker ip
    $fails = array();
    $sql = "SELECT DISTINCT checker_id, checker_ip FROM results WHERE result!=0 AND item=:item";
    $statement = $pdo->prepare($sql);
    $statement->bindValue(':item', $item);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $checker) {
        $fails[] = $checker;
    }
    $statement->closeCursor();
    $nb_all_fail = $fails;
    if (!!$fails) {
        //third we take no fails checker ip
        $no_fails = array();
        $sql = "SELECT DISTINCT checker_id, checker_ip FROM results WHERE result=0 AND item=:item";
        $statement = $pdo->prepare($sql);
        $statement->bindValue(':item', $item);
        if (!$statement->execute()) {
            throw new PDOException();
        }
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result as $checker) {
            $no_fails[] = $checker;
        }
        $statement->closeCursor();
        //fourth we take lost checker ip
        $lost = array();
        $sql = "SELECT distinct checker_id, checker_ip\n\t\tFROM results \n\t\tWHERE item = :item\n\t\t\t\tand check_timestamp < :dateMinus3000 \n\t\t\t\tand check_timestamp > :dateMinus6000\n\t\t\t\tand checker_ip NOT IN ( select checker_ip from results where item = :item\n\t\t\t\t\t\t\t\t\t\t\tand check_timestamp < :date \n\t\t\t\t\t\t\t\t\t\t\tand  check_timestamp > :dateMinus3000 )";
        $statement = $pdo->prepare($sql);
        $statement->bindValue(':item', $item);
        $statement->bindValue(':dateMinus3000', date('Y-m-d H:i:s', time() - 3000));
        $statement->bindValue(':dateMinus6000', date('Y-m-d H:i:s', time() - 6000));
        $statement->bindValue(':date', date('Y-m-d H:i:s', time()));
        if (!$statement->execute()) {
            throw new PDOException();
        }
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        foreach ($result as $checker) {
            $lost[] = $checker;
        }
        $statement->closeCursor();
        // Calculate routeur ip for failed checker
        //we initate networks array
        $network_for_failed_checker = array();
        foreach ($fails as $checker) {
            //we try to take route for network from db
            $route = "";
            $network = netmask($checker['checker_ip'], $conf['minimum_netmask_for_groups']);
            $network = $network . "/" . $conf['minimum_netmask_for_groups'];
            $sql = "select route from traceroute where check_net = :net";
            $statement = $pdo->prepare($sql);
            $statement->bindValue(':net', $network);
            if (!$statement->execute()) {
                throw new PDOException();
            }
            $result = $statement->fetch(PDO::FETCH_ASSOC);
            $route = $result['route'];
            //if we have the traceroute for ip checker
            if (!!$result) {
                //we first check if network has no no fail ip checkers
                $i = 0;
                while ($i < count($no_fails) and $network != netmask($no_fails[$i]['checker_ip'], $conf['minimum_netmask_for_groups'])) {
                    $i++;
                }
                if ($i == count($no_fails)) {
                    //if the networks exists in the array, we only add the ip checker to compare to the network key
                    if (!array_key_exists($network, $network_for_failed_checker)) {
                        $network_for_failed_checker[$network] = array();
                        $route = explode(';', $result['route']);
                        $ip_routeurs = array();
                        foreach ($route as $routeur) {
                            $ip_routeurs[] = explode(';', $routeur);
                        }
                        $network_for_failed_checker[$network]['route'] = $ip_routeurs;
                        $network_for_failed_checker[$network]['nb_fail'] = 1;
                    } else {
                        $network_for_failed_checker[$network]['nb_fail'] = $network_for_failed_checker[$network]['nb_fail'] + 1;
                    }
                }
            } else {
                //one must add the road
                $routes = shell_exec("traceroute -nI " . $checker['checker_ip']);
                $routes = explode("\n", $routes);
                array_splice($routes, 0, 1);
                $final_route = array();
                foreach ($routes as $route) {
                    $route_explode = explode(' ', $route);
                    if (count($route_explode) > 1) {
                        $final_route[] = $route_explode[3];
                    }
                }
                $net_with_mask = $network;
                $sql = "INSERT INTO traceroute (check_net,route,timestamp) VALUES (:check,:route,:time)";
                $statement = $pdo->prepare($sql);
                $statement->bindValue(':check', $net_with_mask);
                $statement->bindValue(':route', implode(';', $final_route));
                $statement->bindValue(':time', date('Y-m-d H:i:s', time()));
                if (!$statement->execute()) {
                    throw new PDOException();
                }
            }
        }
        $routeurs_failed = array();
        if (count($network_for_failed_checker) > 1) {
            $network_keys = array_keys($network_for_failed_checker);
            foreach ($network_keys as $net) {
                $network_1 = $network_for_failed_checker[$net];
                $networks_left = $network_keys;
                $networks_left = array_slice($networks_left, 0, array_search($net, $networks_left));
                foreach ($networks_left as $net_compare) {
                    $network_2 = $network_for_failed_checker[$net_compare];
                    if ($network_1 != $network_2) {
                        $trouver = false;
                        $i = count($network_1['route']) - 1;
                        while ($i >= 0 and !$trouver) {
                            $j = count($network_2['route']) - 1;
                            while ($j >= 0 and $network_1['route'][$i] != $network_2['route'][$j]) {
                                $j--;
                            }
                            if ($j >= 0) {
                                $trouver = true;
                                if (!array_key_exists($network_1['route'][$i], $routeurs_failed)) {
                                    $routeurs_failed[$network_1['route'][$i]] = array($network_1['nb_fail'], $network_2['nb_fail']);
                                } else {
                                    echo "<p>" . $network_1['route'][$i] . "</p>";
                                    $routers_failed[$network_1['route'][$i]][] = $network_1['nb_fail'];
                                    $routers_failed[$network_1['route'][$i]][] = $network_2['nb_fail'];
                                }
                            }
                            $i--;
                        }
                    }
                }
            }
        } else {
            foreach ($network_for_failed_checker as $network_1) {
                $routeur = $network_1['route'][count($network_1['route']) - 1][0];
                $routeurs_failed = array($routeur => array($network_1['nb_fail']));
            }
        }
        $routeurs_for_display = array();
        foreach ($routeurs_failed as $routeur => $fails) {
            $total_fails_for_routeur = 0;
            foreach ($fails as $nb_fails) {
                $total_fails_for_routeur = $total_fails_for_routeur + $nb_fails;
            }
            $routeurs_for_display[] = array('routeur' => $routeur, 'nb_fail' => $total_fails_for_routeur);
        }
        $nb_all = count($nb_all_fail);
        if (count($routeurs_for_display) > 0 && $conf['active_logstalgia'] == 1) {
            $final_results[] = array('factors' => $routeurs_for_display, 'type_of_checker' => 'failed');
            foreach ($routeurs_for_display as $routeur) {
                $item_log_explode = explode(":", $item_checked);
                $size = $routeur['nb_fail'] * 100 / $nb_all * $conf['size_super_ball'];
                $item_log = time() . "|" . $routeur['routeur'] . "|" . $item_log_explode[0] . "|" . "Fail" . "|" . $size . "|" . "0" . "|" . "FF0000" . "\n";
                file_put_contents($conf['directory_tmp'] . "logstalgia.txt", $item_log, FILE_APPEND);
            }
        }
        // Calculate routeurs ip for lost checker
        //we initate networks array
        $network_for_lost_checker = array();
        foreach ($lost as $checker) {
            //we try to take route for network from db
            $route = "";
            $network = netmask($checker['checker_ip'], $conf['minimum_netmask_for_groups']);
            $sql = "SELECT route FROM traceroute WHERE check_net=:check and  check_timestamp > :time";
            $statement = $pdo->prepare($sql);
            $statement->bindValue(':check', $network);
            $statement->bindValue(':time', date('Y-m-d H:i:s', time()));
            if (!$statement->execute()) {
                throw new PDOException();
            }
            $result = $statement->fetch(PDO::FETCH_ASSOC);
            //if we have the traceroute for ip checker
            if (!!$result) {
                //we first check if network has no no fail ip checkers
                $i = 0;
                while ($i < count($no_fails) and $network != netmask($no_fails[$i]['checker_ip'], $conf['minimum_netmask_for_groups'])) {
                    $i++;
                }
                if ($i == count($no_fails)) {
                    $i = 0;
                    while ($i < count($fails) and $network != netmask($fails[$i]['checker_ip'], $conf['minimum_netmask_for_groups'])) {
                        $i++;
                    }
                    if ($i == count($fails)) {
                        //if the networks exists in the array, we only add the ip checker to compare to the network key
                        if (!array_key_exists($network, $network_for_lost_checker)) {
                            $network_for_lost_checker[$network] = array();
                            $route = explode(';', $result['route']);
                            $ip_routeurs = array();
                            foreach ($route as $routeur) {
                                $ip_routeurs = explode(';', $routeur);
                            }
                            $network_for_lost_checker[$network]['route'] = $ip_routeurs;
                            $network_for_lost_checker[$network]['nb_fail'] = 1;
                        } else {
                            $network_for_lost_checker[$network]['nb_fail'] = $network_for_lost_checker[$network]['nb_fail'] + 1;
                        }
                    }
                }
            }
        }
        $routers_lost = array();
        if (count($network_for_lost_checker) > 1) {
            $network_keys = array_keys($network_for_lost_checker);
            foreach ($network_keys as $net) {
                $network_1 = $network_for_lost_checker[$net];
                $networks_left = $network_keys;
                $networks_left = array_slice($networks_left, 0, array_search($net, $networks_left));
                foreach ($networks_left as $net_compare) {
                    $network_2 = $network_for_lost_checker[$net_compare];
                    if ($network_1 != $network_2) {
                        $trouver = false;
                        $i = count($network_1['route']) - 1;
                        while ($i >= 0 and !$trouver) {
                            $j = count($network_2['route']) - 1;
                            while ($j >= 0 and $network_1['route'][$i] != $network_2['route'][$j]) {
                                $j--;
                            }
                            if ($j >= 0) {
                                $trouver = true;
                                if (!array_key_exists($network_1['route'][$i], $routeurs_failed)) {
                                    $routeurs_failed[$network_1['route'][$i]] = array($network_1['nb_fail'], $network_2['nb_fail']);
                                } else {
                                    echo "<p>" . $network_1['route'][$i] . "</p>";
                                    $routers_failed[$network_1['route'][$i]][] = $network_1['nb_fail'];
                                    $routers_failed[$network_1['route'][$i]][] = $network_2['nb_fail'];
                                }
                            }
                            $i--;
                        }
                    }
                }
            }
        } else {
            //problem, on doit choper le seul element pour prendre son routeur le plus à gauche
            foreach ($network_for_lost_checker as $network_1) {
                $routeur = $network_1['route'][count($network_1['route']) - 1][0];
                $routeurs_lost = array($routeur => $network_1['nb_fail']);
            }
        }
        $routeurs_for_lost_display = array();
        foreach ($routers_lost as $routeur) {
            $total_lost_for_routeur = 0;
            foreach ($routeur as $nb_lost) {
                $total_lost_for_routeur = $total_lost_for_routeur + $nb_lost;
            }
            $routeurs_for_lost_display[] = array('routeur' => $routeur, 'nb_lost' => $total_lost_for_routeur);
        }
        if (count($routeurs_for_lost_display) > 0 && $conf['active_logstalgia'] == 1) {
            $final_results[] = array('factors' => $routeurs_for_lost_display, 'type_of_checker' => 'lost');
            foreach ($routeurs_for_lost_display as $routeur) {
                $item_log_explode = explode(":", $item_checked);
                $size = $routeur['nb_lost'] * 100 / $nb_all * $conf['size_super_ball'];
                $item_log = time() . "|" . $routeur['routeur'] . "|" . $item_log_explode[0] . "|" . "Fail" . "|" . $size . "|" . "0" . "|" . "FF0000" . "\n";
                file_put_contents($conf['directory_tmp'] . "logstalgia.txt", $item_log, FILE_APPEND);
            }
        }
    }
    return $final_results;
}
コード例 #2
0
function validate_from_network($conf, $items, $checker)
{
    $netmask_max = $conf['maximum_netmask_for_groups'];
    $netmask_min = $conf['minimum_netmask_for_groups'];
    $items_final = $items;
    if ($conf['check_in_network_only'] == 1) {
        foreach ($items as $item) {
            $netmask = $netmask_max;
            $find = false;
            while ($netmask >= $netmask_min and !$find) {
                $ip_item = gethostbyname($item['hostname']);
                if (netmask($ip_item, $netmask) == netmask($checker, $netmask)) {
                    $find = true;
                } else {
                    $netmask--;
                }
            }
            if (!$find) {
                array_splice($items_final, array_search($item, $items_final), 1);
            }
        }
    }
    return $items_final;
}
コード例 #3
0
ファイル: runeaudio.php プロジェクト: adam-fonseca/RuneUI
function wrk_netconfig($redis, $action, $args = null, $configonly = null)
{
    $updateh = 0;
    switch ($action) {
        case 'setnics':
            // nics blacklist
            $excluded_nics = array('ifb0', 'ifb1', 'p2p0', 'bridge');
            // flush nics Redis hash table
            $transaction = $redis->multi();
            $transaction->del('nics');
            $interfaces = sysCmd("ip addr |grep \"BROADCAST,\" |cut -d':' -f1-2 |cut -d' ' -f2");
            $interfaces = array_diff($interfaces, $excluded_nics);
            foreach ($interfaces as $interface) {
                $ip = sysCmd("ip addr list " . $interface . " |grep \"inet \" |cut -d' ' -f6|cut -d/ -f1");
                $netmask = sysCmd("ip addr list " . $interface . " |grep \"inet \" |cut -d' ' -f6|cut -d/ -f2");
                if (isset($netmask[0])) {
                    $netmask = netmask($netmask[0]);
                } else {
                    unset($netmask);
                }
                $gw = sysCmd("route -n |grep \"0.0.0.0\" |grep \"UG\" |cut -d' ' -f10");
                $dns = sysCmd("cat /etc/resolv.conf |grep \"nameserver\" |cut -d' ' -f2");
                $type = sysCmd("iwconfig " . $interface . " 2>&1 | grep \"no wireless\"");
                runelog('interface type', $type[0]);
                // if (empty(sysCmd("iwlist ".$interface." scan 2>&1 | grep \"Interface doesn't support scanning\""))) {
                if (empty($type[0])) {
                    $speed = sysCmd("iwconfig " . $interface . " 2>&1 | grep 'Bit Rate' | cut -d '=' -f 2 | cut -d ' ' -f 1-2");
                    $currentSSID = sysCmd("iwconfig " . $interface . " | grep 'ESSID' | cut -d ':' -f 2 | cut -d '\"' -f 2");
                    $currentSSID = sysCmd("iwconfig " . $interface . " | grep 'ESSID' | cut -d ':' -f 2 | cut -d '\"' -f 2");
                    $transaction->hSet('nics', $interface, json_encode(array('ip' => $ip[0], 'netmask' => $netmask, 'gw' => $gw[0], 'dns1' => $dns[0], 'dns2' => $dns[1], 'speed' => $speed[0], 'wireless' => 1, 'currentssid' => $currentSSID[0])));
                    //// $scanwifi = 1;
                } else {
                    $speed = sysCmd("ethtool " . $interface . " 2>&1 | grep -i speed | cut -d':' -f2");
                    $transaction->hSet('nics', $interface, json_encode(array('ip' => $ip[0], 'netmask' => $netmask, 'gw' => $gw[0], 'dns1' => $dns[0], 'dns2' => $dns[1], 'speed' => $speed[0], 'wireless' => 0)));
                }
            }
            //// if ($scanwifi) sysCmdAsync('/var/www/command/refresh_nics');
            // check wpa_supplicant auto-start and purge interface list
            $wpa_supplicant_start = sysCmd("ls -lah /etc/systemd/system/multi-user.target.wants/wpa_supplicant@*.service | cut -d '@' -f 2 | cut -d '.' -f 1");
            $disable_wpa_supplicant = array_diff($wpa_supplicant_start, $interfaces);
            if (!empty($disable_wpa_supplicant)) {
                foreach ($disable_wpa_supplicant as $interface_name) {
                    unlink('/etc/systemd/system/multi-user.target.wants/wpa_supplicant@' . $interface_name . '.service');
                }
            }
            $transaction->exec();
            break;
        case 'getnics':
            foreach ($redis->hGetAll('nics') as $interface => $details) {
                $interfaces[$interface] = json_decode($details);
            }
            return $interfaces;
            break;
        case 'writecfg':
            // ArchLinux netctl config for wired ethernet
            $nic = "Description='" . $args->name . " connection'\n";
            $nic .= "Interface=" . $args->name . "\n";
            $nic .= "ForceConnect=yes\n";
            $nic .= "SkipNoCarrier=yes\n";
            if ($args->wireless === '1') {
                // Wireless configuration
                $nic .= "Connection=wireless\n";
                $nic .= "Security=wpa-config\n";
                $nic .= "WPAConfigFile='/etc/wpa_supplicant/wpa_supplicant.conf'\n";
            } else {
                // Wired configuration
                $nic .= "Connection=ethernet\n";
            }
            if ($args->dhcp === '1') {
                // DHCP configuration
                $nic .= "IP=dhcp\n";
                // Prepare data object for Redis record
                $args = array('name' => $args->name, 'dhcp' => $args->dhcp);
                $args = (object) $args;
            } else {
                // STATIC configuration
                $nic .= "AutoWired=yes\n";
                $nic .= "IP=static\n";
                $nic .= "Address=('" . $args->ip . "/" . $args->netmask . "')\n";
                $nic .= "Gateway='" . $args->gw . "'\n";
                if (!empty($args->dns2)) {
                    $nic .= "DNS=('" . $args->dns1 . "' '" . $args->dns2 . "')\n";
                } else {
                    $nic .= "DNS=('" . $args->dns1 . "')\n";
                }
            }
            // ntp sync after connect
            $nic .= "ExecUpPost='/usr/bin/ntpd -gq || true'\n";
            // set advanced DNS options
            $newArray = wrk_replaceTextLine('/etc/resolvconf.conf', '', 'resolv_conf_options=', "resolv_conf_options=('timeout:" . $redis->hGet('resolvconf', 'timeout') . " attempts:" . $redis->hGet('resolvconf', 'attempts') . "')", '#name_servers=127.0.0.1', 1);
            // Commit changes to /etc/resolvconf.conf
            $fp = fopen('/etc/resolvconf.conf', 'w');
            fwrite($fp, implode("", $newArray));
            fclose($fp);
            // tell the system to update /etc/resolv.conf
            sysCmd('resolvconf -u');
            // write current network config
            $redis->set($args->name, json_encode($args));
            $fp = fopen('/etc/netctl/' . $args->name, 'w');
            fwrite($fp, $nic);
            fclose($fp);
            if (!isset($configonly)) {
                $updateh = 1;
            }
            break;
        case 'manual':
            $file = '/etc/netctl/' . $args['name'];
            $fp = fopen($file, 'w');
            fwrite($fp, $args['config']);
            fclose($fp);
            $updateh = 1;
            break;
        case 'reset':
            wrk_netconfig($redis, 'setnics');
            $args = new stdClass();
            $args->dhcp = '1';
            $args->name = 'eth0';
            wrk_netconfig($redis, 'writecfg', $args);
            $updateh = 1;
            break;
    }
    if ($updateh === 1) {
        runelog('wireless NIC?', $args->wireless);
        // activate configuration (RuneOS)
        wrk_mpdPlaybackStatus($redis, 'record');
        sysCmd('netctl stop ' . $args->name);
        sysCmd('ip addr flush dev ' . $args->name);
        if ($args->dhcp === '1') {
            // dhcp configuration
            if ($args->wireless !== '1') {
                // $cmd = 'systemctl enable ifplugd@'.$args->name;
                sysCmd("ln -s '/usr/lib/systemd/system/netctl-ifplugd@.service' '/etc/systemd/system/multi-user.target.wants/netctl-ifplugd@" . $args->name . ".service'");
                sysCmd('systemctl daemon-reload');
                sysCmd('systemctl start netctl-ifplugd@' . $args->name);
            }
            // sysCmd('systemctl daemon-reload');
        } else {
            // static configuration
            if ($args->wireless !== '1') {
                // $cmd = 'systemctl disable ifplugd@'.$args->name;
                sysCmd("rm '/etc/systemd/system/multi-user.target.wants/netctl-ifplugd@" . $args->name . ".service'");
                sysCmd('systemctl daemon-reload');
                // kill ifplugd
                sysCmd('killall ifplugd');
            }
            // get pids of dhcpcd
            $pids = sysCmd('pgrep -xf "dhcpcd -4 -q -t 30 -L ' . $args->name . '"');
            foreach ($pids as $pid) {
                // debug
                runelog('kill pid:', $pid);
                // kill dhcpcd
                sysCmd('kill ' . $pid);
            }
        }
        // start netctl profile for wired nics
        if ($args->wireless !== '1') {
            sysCmd('netctl start ' . $args->name);
        }
        sysCmd('systemctl restart mpd');
        // set process priority
        sysCmdAsync('sleep 1 && rune_prio nice');
    }
    // update hash if necessary
    $updateh === 0 || $redis->set($args->name . '_hash', md5_file('/etc/netctl/' . $args->name));
    if (wrk_mpdPlaybackStatus($redis, 'laststate') === 'playing') {
        sysCmd('mpc play');
    }
}
コード例 #4
0
function check_routes()
{
    global $conf;
    $pdo = connexionbdd();
    //outskirts of recovering all the testers
    $checkers = array();
    $sql = "SELECT DISTINCT checker_id, checker_ip FROM results";
    $statement = $pdo->prepare($sql);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $checker) {
        $checkers[] = $checker;
    }
    $statement->closeCursor();
    //var_dump($checkers);
    //then retrieves all roads
    $networks = array();
    $sql = "SELECT check_net,timestamp FROM traceroute";
    $statement = $pdo->prepare($sql);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $network) {
        $networks[] = $network;
    }
    //var_dump($networks);
    foreach ($checkers as $checker) {
        //echo $checker['checker_ip']."\n";
        $checker_network = netmask($checker['checker_ip'], $conf['minimum_netmask_for_groups']);
        //echo $checker_network."\n";
        $network_find = false;
        $i = 0;
        while ($i < count($networks) and !$network_find) {
            if ($checker_network . "/" . $conf['minimum_netmask_for_groups'] == $networks[$i]['check_net']) {
                $network_find = true;
            } else {
                $i++;
            }
        }
        if (!$network_find) {
            $routes = shell_exec("traceroute -nI " . $checker['checker_ip']);
            $routes = explode("\n", $routes);
            array_splice($routes, 0, 1);
            $final_route = array();
            foreach ($routes as $route) {
                $route_explode = explode(' ', $route);
                if (count($route_explode) > 1) {
                    $final_route[] = $route_explode[3] . ':' . $route_explode[5];
                }
            }
            $net_with_mask = $checker_network . "/" . $conf['minimum_netmask_for_groups'];
            //echo $net_with_mask;
            $sql = "INSERT INTO traceroute (check_net,route,timestamp) VALUES (:check,:route,:time)";
            $statement = $pdo->prepare($sql);
            $statement->bindValue(':check', $net_with_mask);
            $statement->bindValue(':route', implode(';', $final_route));
            $statement->bindValue(':time', date('Y-m-d H:i:s', time()));
            if (!$statement->execute()) {
                throw new PDOException();
            }
            $networks[] = array('check_net' => $net_with_mask, 'route' => implode(';', $final_route), 'timestamp' => date('Y-m-d H:i:s', time()));
        } else {
            if ($networks[$i]['timestamp'] < time() - $conf['maximum_route_time_validaty'] * 3600000) {
                $routes = shell_exec("traceroute -nI " . $checker['checker_ip']);
                $routes = explode("\n", $routes);
                array_splice($routes, 0, 1);
                $final_route = array();
                foreach ($routes as $route) {
                    $route_explode = explode(' ', $route);
                    if (count($route_explode) > 1) {
                        $final_route[] = $route_explode[3];
                    }
                }
                $sql = "update traceroute set route = :route, timestamp = :time where check_net = :check";
                $statement = $pdo->prepare($sql);
                $statement->bindValue(':check', $checker_network . "/" . $conf['minimum_netmask_for_groups']);
                $statement->bindValue(':route', implode(';', $final_route));
                $statement->bindValue(':time', date('Y-m-d H:i:s', time()));
                if (!$statement->execute()) {
                    throw new PDOException();
                }
            }
        }
    }
}
コード例 #5
0
function common_routeur($conf, $item_md5)
{
    //first we connect to database
    $pdo = connexionbdd($conf);
    $sql = "SELECT item from global_result WHERE md5=:md5";
    $statement = $pdo->prepare($sql);
    $statement->bindValue(':md5', $item_md5);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetch(PDO::FETCH_ASSOC);
    $item = $result['item'];
    $statement->closeCursor();
    //second we take fails checker ip
    $fails = array();
    $sql = "SELECT DISTINCT checker_id, checker_ip FROM results WHERE result!=0 AND item=:item";
    $statement = $pdo->prepare($sql);
    $statement->bindValue(':item', $item);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $checker) {
        $fails[] = $checker;
    }
    $statement->closeCursor();
    $nb_all_fail = $fails;
    //third we take no fails checker ip
    $no_fails = array();
    $sql = "SELECT DISTINCT checker_id, checker_ip FROM results WHERE result=0 AND item=:item";
    $statement = $pdo->prepare($sql);
    $statement->bindValue(':item', $item);
    if (!$statement->execute()) {
        throw new PDOException();
    }
    $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    foreach ($result as $checker) {
        $no_fails[] = $checker;
    }
    $statement->closeCursor();
    $nb_all_no_fail = $no_fails;
    // Calculate routeur ip for failed checker
    //we initate networks array
    $network_for_failed_checker = array();
    foreach ($fails as $checker) {
        //we try to take route for network from db
        $route = "";
        $network = netmask($checker['checker_ip'], $conf['minimum_netmask_for_groups']);
        $network = $network . "/" . $conf['minimum_netmask_for_groups'];
        $sql = "select route from traceroute where check_net = :network";
        $statement = $pdo->prepare($sql);
        $statement->bindValue(':network', $network);
        if (!$statement->execute()) {
            throw new PDOException();
        }
        $result = $statement->fetch(PDO::FETCH_ASSOC);
        $route = $result['route'];
        //if we have the traceroute for ip checker
        if (!!$result) {
            //we first check if network has no no fail ip checkers
            $i = 0;
            while ($i < count($no_fails) and $network != netmask($no_fails[$i]['checker_ip'], $conf['minimum_netmask_for_groups'])) {
                $i++;
            }
            if ($i == count($no_fails)) {
                //if the networks exists in the array, we only add the ip checker to compare to the network key
                if (!array_key_exists($network, $network_for_failed_checker)) {
                    $network_for_failed_checker[$network] = array();
                    $route = explode(';', $result['route']);
                    $ip_routeurs = array();
                    foreach ($route as $routeur) {
                        $ip_routeurs[] = explode(';', $routeur);
                    }
                    $network_for_failed_checker[$network]['route'] = $ip_routeurs;
                    $network_for_failed_checker[$network]['nb_fail'] = 1;
                } else {
                    $network_for_failed_checker[$network]['nb_fail'] = $network_for_failed_checker[$network]['nb_fail'] + 1;
                }
            }
        } else {
            //one must add the road
            $routes = shell_exec("traceroute -nI " . $checker['checker_ip']);
            $routes = explode("\n", $routes);
            array_splice($routes, 0, 1);
            $final_route = array();
            foreach ($routes as $route) {
                $route_explode = explode(' ', $route);
                if (count($route_explode) > 1) {
                    $final_route[] = $route_explode[3];
                }
            }
            $net_with_mask = $checker_network . "/" . $conf['minimum_netmask_for_groups'];
            $sql = "INSERT INTO traceroute (check_net,route,timestamp) VALUES (:check,:route,:time)";
            $statement = $pdo->prepare($sql);
            $statement->bindValue(':check', $net_with_mask);
            $statement->bindValue(':route', implode(';', $final_route));
            $statement->bindValue(':time', date('Y-m-d H:i:s', time()));
            if (!$statement->execute()) {
                throw new PDOException();
            }
        }
    }
    $routeurs_failed = array();
    if (count($network_for_failed_checker) > 1) {
        $network_keys = array_keys($network_for_failed_checker);
        foreach ($network_keys as $net) {
            $network_1 = $network_for_failed_checker[$net];
            $networks_left = $network_keys;
            $networks_left = array_slice($networks_left, 0, array_search($net, $networks_left));
            foreach ($networks_left as $net_compare) {
                $network_2 = $network_for_failed_checker[$net_compare];
                if ($network_1 != $network_2) {
                    $trouver = false;
                    $i = count($network_1['route']) - 1;
                    while ($i >= 0 and !$trouver) {
                        $j = count($network_2['route']) - 1;
                        while ($j >= 0 and $network_1['route'][$i] != $network_2['route'][$j]) {
                            $j--;
                        }
                        if ($j >= 0) {
                            $trouver = true;
                            if (!array_key_exists($network_1['route'][$i], $routeurs_failed)) {
                                $routeurs_failed[$network_1['route'][$i]] = array($network_1['nb_fail'], $network_2['nb_fail']);
                            } else {
                                echo "<p>" . $network_1['route'][$i] . "</p>";
                                $routers_failed[$network_1['route'][$i]][] = $network_1['nb_fail'];
                                $routers_failed[$network_1['route'][$i]][] = $network_2['nb_fail'];
                            }
                        }
                        $i--;
                    }
                }
            }
        }
    } else {
        foreach ($network_for_failed_checker as $network_1) {
            $routeur = $network_1['route'][count($network_1['route']) - 1][0];
            $routeurs_failed = array($routeur => array($network_1['nb_fail']));
        }
    }
    $routeurs_for_display = array();
    foreach ($routeurs_failed as $routeur => $fails) {
        $total_fails_for_routeur = 0;
        foreach ($fails as $nb_fails) {
            $total_fails_for_routeur = $total_fails_for_routeur + $nb_fails;
        }
        $routeurs_for_display[] = array('routeur' => $routeur, 'nb_fail' => $total_fails_for_routeur);
    }
    $nb_all = count($nb_all_fail) + count($nb_all_no_fail);
    $return_final = $routeurs_for_display;
    return $return_final;
}