function guifi_live_traceroute($device_id)
{
    if (empty($device_id)) {
        $output = t('Target device not specified.');
    } else {
        $gs = guifi_service_load(guifi_graphs_get_server($device_id, 'device'));
        $ipd = guifi_main_ip($device_id);
        $ipf = guifi_main_ip($gs->device_id);
    }
    $title = t('Live traceroute from %from (%ipf) to %dest (%ipd)', array('%ipd' => $ipd['ipv4'], '%dest' => guifi_get_hostname($device_id), '%ipf' => $ipf['ipv4'], '%from' => guifi_get_hostname($gs->device_id)));
    drupal_set_title($title);
    print theme('page', guifi_cnml_live('livetraceroute', $device_id, $ipd['ipv4'], $gs), FALSE);
    exit;
}
/**
 * Returns image url to CNML
 */
function guifi_cnml_availability($args, $gs = NULL)
{
    if (is_null($gs)) {
        $gs = guifi_service_load(guifi_graphs_get_server($args['device'], 'device'));
    }
    $img_url = '<img src="' . guifi_cnml_call_service($gs, 'availability', $args) . '">';
    if ($gs->var['version'] >= 2.0) {
        return l($img_url, 'guifi/menu/ip/liveping/' . $args['device'], array('html' => TRUE, 'attributes' => array('title' => t('live ping/traceroute to %device', array('%device' => guifi_get_hostname($args['device']))), 'target' => '_blank')));
    } else {
        // old v1.0 format for backward compatibility
        return $img_url;
    }
}
/**
 * Load statistics from remote CNML graph servers into the database
 *
 * @param $graph_server
 *   Graph server ID.
 *
 * @param $verbose
 *   If TRUE, it will return theme()
 *
 * @return
 *   theme() if $verbose==TRUE
 */
function guifi_cron_loadCNMLstats($graph_server, $verbose = FALSE)
{
    if (is_null($gs)) {
        $gs = guifi_service_load($graph_server);
    }
    if ($gs->var['version'] >= 2.0) {
        $handle = fopen(guifi_cnml_call_service($gs, 'stats', array()), "r");
    } else {
        $output .= t("This graph server doesn't support v2.0 CNML calls syntax.\n");
    }
    if ($handle) {
        $c = 0;
        $u = 0;
        while (!feof($handle)) {
            $c++;
            $updatestr = array();
            $stat = stream_get_line($handle, 4096, "\n");
            $vstats = explode('|', $stat);
            $device_id = array_shift($vstats);
            $availability_stats = array_shift($vstats);
            list($latmax, $latavg, $availability, $lastonline, $laststatdate, $laststattime, $lastavailability) = explode(',', $availability_stats);
            $tlaststat = to_date($laststatdate . $laststattime);
            if (!$tlaststat) {
                continue;
            }
            $dev = db_fetch_object(db_query('SELECT last_stats,last_online,last_flag,ly_availability ' . 'FROM {guifi_devices} ' . 'WHERE id=%d', $device_id));
            if ($tlaststat <= $dev->last_stats) {
                continue;
            }
            $u++;
            $tlastonline = to_date($lastonline);
            if ($tlastonline > $dev->last_online) {
                $updatestr[] = 'last_online=' . $tlastonline;
            }
            if ($lastavailability <= 100 and $lastavailability >= 0) {
                if ($lastavailability == 0) {
                    $updatestr[] = 'last_flag="Down"';
                } else {
                    $updatestr[] = 'last_flag="Up"';
                }
            }
            $updatestr[] = 'last_stats=' . $tlaststat;
            if ($availability <= 100 and $availability >= 0) {
                $updatestr[] = 'ly_availability=' . $availability;
            }
            if ($latavg) {
                $updatestr[] = 'latency_avg=' . $latavg;
            }
            if ($latmax) {
                $updatestr[] = 'latency_max=' . $latmax;
            }
            // Update availability statistics
            db_query('UPDATE {guifi_devices} SET ' . implode(', ', $updatestr) . ' WHERE id=%d', $device_id);
            // Now going to update traffic statistics
            $nradios = db_fetch_object(db_query('SELECT count(*) count ' . 'FROM {guifi_radios} ' . 'WHERE id=%d', $device_id));
            if ($nradios->count == 1) {
                // if just one radio, sum all values
                $in = 0;
                $out = 0;
                foreach ($vstats as $value) {
                    $traffic = explode(',', $value);
                    $in += $traffic[1];
                    $out += $traffic[2];
                }
                if ($in or $out) {
                    db_query('UPDATE {guifi_radios} ' . 'SET ly_mb_in=%d, ly_mb_out=%d ' . 'WHERE id=%d', $in, $out, $device_id);
                }
            } else {
                // more than one radio, give the value to each radio
                foreach ($vstats as $value) {
                    db_query('UPDATE {guifi_radios} ' . 'SET ly_mb_in=%d, ly_mb_out=%d ' . 'WHERE id=%d AND radiodev_counter=%d', $traffic[1], $traffic[2], $device_id, $traffic[0]);
                }
            }
            $stats .= $c . ' ' . $stat . "\n";
        }
        watchdog('guifi', 'Loaded statistics from %name, %ndevices updated', array('%name' => guifi_service_str($graph_server), '%ndevices' => $u), WATCHDOG_NOTICE);
        //    $stats =  stream_get_contents($handle);
        fclose($handle);
        $output .= '<pre>' . $stats . '</pre>';
    } else {
        $output .= t('Get stats failed.');
    }
    if ($verbose) {
        return theme('box', t("Load statistics from '%server'", array('%server' => guifi_service_str($graph_server))), $output);
    }
}
/**
 * guifi_node_graph_overview
 * outputs an overiew graph of the node
**/
function theme_guifi_node_graphs_overview($node, $links = FALSE)
{
    $gs = guifi_service_load(guifi_graphs_get_server($node->id, 'node'));
    $radios = array();
    $query = db_query("SELECT * FROM {guifi_radios} WHERE nid=%d", $node->id);
    while ($radio = db_fetch_array($query)) {
        $radios[] = $radio;
    }
    // print "Count radios: ".count($radios)."\n<br />";
    if (count($radios) > 1) {
        if (substr($gs->var['url'], 0, 3) == "fot") {
            //  graph all devices.about a node. Ferran Ot
            while ($radio = db_fetch_object($query)) {
                $ssid = get_SSID_radio($radio->id);
                $ssid = strtolower($ssid);
                $mrtg_url = substr($gs->var['url'], 3);
                $rows[] = array('<a href="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_6&cfg=mrtg.cfg" target="_blank"> <img src="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_6&cfg=mrtg.cfg&png=weekly"></a>');
                $rows[] = array('<a href="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_ping&cfg=mrtg.cfg" target="_blank"> <img src="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_ping&cfg=mrtg.cfg&png=weekly"></a>');
            }
            $ret = array_merge($rows);
        } else {
            $args = array('type' => 'supernode', 'node' => $node->id);
            //      $args = sprintf('type=supernode&node=%d&direction=',$node->id);
            $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args, 'direction=in') . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args, 'direction=in') . '"></a>', 'align' => 'center'));
            $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args, 'direction=out') . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args, 'direction=out') . '"></a>', 'align' => 'center'));
            //      $rows[] = array(
            //      guifi_cnml_call_service($gs->var['url'],'graph',$args,'direction=in'sprintf('<a href="'.base_path().'guifi/graph_detail?'.$args.'in"><img src="'.$gs->var['url'].'?'.$args.'in"></a>',$node->id));
            //      $rows[] = array(sprintf('<a href="'.base_path().'guifi/graph_detail?'.$args.'out"><img src="'.$gs->var['url'].'?'.$args.'out"></a>',$node->id));
            $ret = array_merge($rows);
        }
    } else {
        if (count($radios) == 1) {
            $ret = guifi_device_graph_overview($radios[0]);
        }
    }
    $output = theme('table', NULL, $ret);
    if ($links) {
        $node = node_load(array('nid' => $node->id));
        drupal_set_title(t('graph overview @ %node', array('%node' => $node->title)));
        drupal_set_breadcrumb(guifi_node_ariadna($node));
        $output .= theme_links(module_invoke_all('link', 'node', $node, FALSE));
        print theme('page', $output, FALSE);
        return;
    }
    return $output;
}
Beispiel #5
0
function guifi_api_service_remove($gapi, $parameters)
{
    global $user;
    if (!guifi_api_check_fields($gapi, array('service_id'), $parameters)) {
        return FALSE;
    }
    $service = guifi_service_load($parameters['service_id']);
    if (!$service) {
        $gapi->addError(403, "This service does not exist.");
        return FALSE;
    }
    $to = explode(',', $service->notification);
    $to[] = variable_get('guifi_contact', '*****@*****.**');
    $log = '';
    $service->deleted = TRUE;
    /* Delete from guifi tables */
    $nservice = _guifi_db_sql('guifi_services', array('id' => $service->id), (array) $service, $log, $to);
    /* delete node */
    node_delete($service->id);
    $gapi->addResponseField('service', $service);
    return TRUE;
}
/**
 * guifi_device_graph_overview
 * outputs an overiew graph of the device
**/
function guifi_device_graph_overview($radio)
{
    guifi_log(GUIFILOG_TRACE, 'guifi_device_graph_overview()', $radio);
    if (isset($radio['mode'])) {
        $radio['type'] = 'radio';
    }
    if ($radio['graph_server'] == -1) {
        $rows[] = array(t('This device has the graphs disabled.'));
        return array_merge($rows);
    }
    if (empty($radio['graph_server'])) {
        $gs = guifi_service_load(guifi_graphs_get_server($radio['id'], 'device'));
    } else {
        $gs = guifi_service_load($radio['graph_server']);
    }
    if (substr($server_mrtg, 0, 3) == "fot") {
        $ssid = get_SSID_radio($radio['id']);
        $ssid = strtolower($ssid);
        $mrtg_url = substr($server_mrtg, 3);
        $rows[] = array('<a href="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_6&cfg=mrtg.cfg" target="_blank" > <img src="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_6&cfg=mrtg.cfg&png=weekly"></a>');
        $rows[] = array('<a href="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_ping&cfg=mrtg.cfg" target="_blank" > <img src="' . $mrtg_url . '/14all.cgi?log=' . $ssid . '_ping&cfg=mrtg.cfg&png=weekly"></a>');
        return array_merge($rows);
    } else {
        $clients = db_fetch_object(db_query("SELECT count(c.id) count " . "FROM {guifi_links} c " . "WHERE c.device_id=%d " . "  AND c.link_type IN ('wds','ap/client','bridge')", $radio['id']));
        $args = array('type' => 'clients', 'node' => $radio['nid'], 'device' => $radio['id']);
        if ($clients->count > 1) {
            $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args, 'direction=in') . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args, 'direction=in') . '"></a>', 'align' => 'center'));
            $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args, 'direction=out') . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args, 'direction=out') . '"></a>', 'align' => 'center'));
        } else {
            if ($radio['type'] == 'radio' or $radio['variable']['mrtg_index'] != '') {
                $args['type'] = 'device';
                $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args) . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args) . '"></a>', 'align' => 'center'));
            }
        }
        $args['type'] = 'pings';
        $rows[] = array(array('data' => '<a href=' . base_path() . 'guifi/graph_detail?' . guifi_cnml_args($args) . '><img src="' . guifi_cnml_call_service($gs->var['url'], 'graph', $args) . '"></a>', 'align' => 'center'));
        return array_merge($rows);
    }
}
function guifi_service_str($id, $emptystr = 'Take from parents')
{
    if (empty($id)) {
        return t($emptystr);
    }
    if ($id == -1) {
        return t('No service');
    }
    // there is a value, create the string
    $proxy = guifi_service_load($id);
    $proxystr = $id . '-' . guifi_get_zone_name($proxy->zone_id) . ', ' . $proxy->nick;
    return $proxystr;
}