Пример #1
0
function parse_states_info($states_data)
{
    foreach (preg_split("/^([^\\s]+)/m", $states_data, '-1', PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $state) {
        $states_pre[] = $state;
    }
    for ($i = 0; $i < count($states_pre); $i++) {
        $states[] = $states_pre[$i++] . $states_pre[$i];
    }
    $state_a = array();
    foreach ($states as $state) {
        $atom = array();
        foreach (preg_split("/[\\s\t\n]+/", $state, '-1', PREG_SPLIT_NO_EMPTY) as $state_atom) {
            $atom[] = $state_atom;
        }
        $state_a['origin'] = $atom[0];
        $state_a['proto'] = $atom[1];
        if ($atom[3] == "->") {
            $state_a['dir'] = "out";
            $state_a['src'] = $atom[2];
            if ($atom[5] == "->") {
                $state_a['dst'] = $atom[6];
                $state_a['gw'] = $atom[4];
                $current = 7;
            } else {
                $state_a['dst'] = $atom[4];
                $state_a['gw'] = "";
                $current = 5;
            }
        } else {
            $state_a['dir'] = "in";
            $state_a['dst'] = $atom[2];
            if ($atom[5] == "<-") {
                $state_a['src'] = $atom[6];
                $state_a['gw'] = $atom[4];
                $current = 7;
            } else {
                $state_a['src'] = $atom[4];
                $state_a['gw'] = "";
                $current = 5;
            }
        }
        $state_a['state_state'] = $atom[$current++];
        for ($i = $current; $i < count($atom); $i++) {
            switch ($atom[$i]) {
                case "age":
                case "rule":
                    $state_a[$atom[$i]] = chop($atom[++$i], ",");
                    break;
                case "expires":
                    $i += 2;
                    $state_a['expires'] = chop($atom[$i++], ",");
                    $packets_a = preg_split("/:/", $atom[$i]);
                    $state_a['packets'] = bytes_to_s($packets_a[0] + $packets_a[1]);
                    $i += 2;
                    $bytes_a = preg_split("/:/", $atom[$i]);
                    $state_a['bytes'] = bytes_to_s($bytes_a[0] + $bytes_a[1]) . "b";
                    break;
            }
        }
        $states_a[] = $state_a;
    }
    return $states_a;
}
Пример #2
0
function parse_queue_data($qdata)
{
    $qdata = preg_replace("/\n/", "", $qdata);
    foreach (preg_split("/queue/", $qdata, '-1', PREG_SPLIT_NO_EMPTY) as $qline) {
        /*
         * Sanitize the rule string so that we can deal with '{foo' as '{ foo' in 
         * the code further down without any special treatment
         */
        $qline = preg_replace("/{/", " { ", $qline);
        $qline = preg_replace("/}/", " } ", $qline);
        $qline = preg_replace("/\\(/", " ( ", $qline);
        $qline = preg_replace("/\\)/", " ) ", $qline);
        $qline = preg_replace("/,/", "", $qline);
        $qline = preg_replace("/\"/", " \" ", $qline);
        $qline = trim($qline);
        $qs = array();
        foreach (preg_split("/[\\s\t]+/", $qline) as $qstatement) {
            $qs[] = $qstatement;
        }
        $qad = array();
        $qad['name'] = $qs[0];
        for ($i = '1'; $i < count($qs); $i++) {
            switch ($qs[$i]) {
                case "bandwidth":
                case "priority":
                    $qad[$qs[$i]] = $qs[++$i];
                    break;
                case "cbq":
                case "priq":
                case "hsfs":
                    $qad["type"] = $qs[$i];
                    break;
                case "pkts:":
                    $qad["packets"] = bytes_to_s($qs[++$i]);
                    break;
                case "bytes:":
                    $qad["bytes"] = bytes_to_s($qs[++$i]) . "b";
                    break;
                case "borrows:":
                    $qad["borrows"] = bytes_to_s($qs[++$i]);
                    break;
                case "suspends:":
                    $qad["suspends"] = bytes_to_s($qs[++$i]);
                    break;
                case "dropped":
                    $i += 2;
                    $qad["dropped_packets"] = bytes_to_s($qs[$i]);
                    $i += 2;
                    $qad["dropped_bytes"] = bytes_to_s($qs[$i]) . "b";
                    break;
                case "qlength:":
                    $qad["queue_length"] = $qs[++$i] . $qs[++$i];
                    break;
                case "{":
                    while (preg_replace("/[\\s,]+/", "", $qs[++$i]) != "}") {
                        $qad['childqueues'][] = $qs[$i];
                    }
                    break;
                case "(":
                    while (preg_replace("/[\\s,]+/", "", $qs[++$i]) != ")") {
                        $qad['options'][] = $qs[$i];
                    }
                    break;
            }
        }
        $qa[] = $qad;
    }
    return $qa;
}