Example #1
0
 /**
  * Finds the xen traffic on all the interfaces.
  * 
  * It recollect the total, incoming and outgoing traffic from
  * all the xen interfaces.
  *
  * @author Anonymous <*****@*****.**>
  * @author Ángel Guzmán Maeso <*****@*****.**>
  *
  * @return void
  */
 public static function find_traffic()
 {
     global $gbl, $sgbl, $login, $ghtml;
     // Only apply if xeninterface.list file exist
     if (lxfile_exists('__path_program_etc/xeninterface.list')) {
         // Get the data trimmed from xeninterface.list
         $interfaces_list = lfile_trim('__path_program_etc/xeninterface.list');
         if (!lxfile_exists('__path_program_etc/newxeninterfacebw.data')) {
             $total = NULL;
             if (!empty($interfaces_list)) {
                 foreach ($interfaces_list as $interface) {
                     $total[$interface] = self::get_bytes_for_interface($interface);
                 }
             }
             // Print debugging info recollected
             dprintr($total);
             lfile_put_contents('__path_program_etc/newxeninterfacebw.data', serialize($total));
             return;
         }
         $data = unserialize(lfile_get_contents('__path_program_etc/newxeninterfacebw.data'));
         $total = NULL;
         if (!empty($interfaces_list)) {
             foreach ($interfaces_list as $interface) {
                 $total[$interface] = self::get_bytes_for_interface($interface);
                 if (isset($data[$interface])) {
                     if ($total[$interface]['total'] < $data[$interface]['total']) {
                         $total_traffic = $total[$interface]['total'];
                         $total_traffic_incoming = $total[$interface]['incoming'];
                         $total_traffic_outgoing = $total[$interface]['outgoing'];
                     } else {
                         $total_traffic = $total[$interface]['total'] - $data[$interface]['total'];
                         $total_traffic_incoming = $total[$interface]['incoming'] - $data[$interface]['incoming'];
                         $total_traffic_outgoing = $total[$interface]['outgoing'] - $data[$interface]['outgoing'];
                     }
                 } else {
                     $total_traffic = $total[$interface]['total'];
                     $total_traffic_incoming = $total[$interface]['incoming'];
                     $total_traffic_outgoing = $total[$interface]['outgoing'];
                 }
                 execRrdTraffic('xen-' . $interface, $total_traffic, '-' . $total_traffic_incoming, $total_traffic_outgoing);
                 $stringa[] = time() . ' ' . date('d-M-Y:H:i') . ' ' . $interface . ' ' . $total_traffic . ' ' . $total_traffic_incoming . ' ' . $total_traffic_outgoing;
             }
         }
         dprintr($total);
         $string = implode(PHP_EOL, $stringa);
         lfile_put_contents('/var/log/lxinterfacetraffic.log', $string . PHP_EOL, FILE_APPEND);
         lfile_put_contents('__path_program_etc/newxeninterfacebw.data', serialize($total));
     }
 }
Example #2
0
function iptraffic_main_v6()
{
    global $global_dontlogshell;
    $res = lxshell_output("ip6tables", "-nvx", "-L", "FORWARD");
    $res = explode("\n", $res);
    $outgoing = null;
    foreach ($res as $r) {
        // First column may have spaces because of the number of digits in the column
        $r = trim($r, ' ');
        // Trim internal spaces
        $r = trimSpaces($r);
        $list = explode(' ', $r);
        if (stripos($r, "source") !== false && stripos($r, "destination") !== false) {
            // header, get important columns number
            for ($i = 0; $i < count($list); $i++) {
                if ($list[$i] == "bytes") {
                    $byteIdx = $i;
                }
                // Removing 1, because the header has an extra field cause of 'target' column
                // FIXME: any better idea?
                if ($list[$i] == "source") {
                    $srcIdx = $i - 1;
                }
                if ($list[$i] == "destination") {
                    $dstIdx = $i - 1;
                }
            }
        }
        if (count($list) != 7) {
            continue;
        }
        $list[$dstIdx] = explode("/", $list[$dstIdx])[0];
        $list[$srcIdx] = explode("/", $list[$srcIdx])[0];
        if (csb($list[$dstIdx], "::")) {
            // Just make sure that we don't calculate this goddamn thing twice, which would happen if there are multiple copies of the same rule. So mark that we have already read it in the sourcelist.
            // OA: Since we dont care lines that have a rule set (fixed above), this wont happen
            if (!isset($sourcelist[$list[$srcIdx]])) {
                $outgoing[$list[$srcIdx]][] = $list[$byteIdx];
                $sourcelist[$list[$srcIdx]] = true;
            }
        } else {
            if (csb($list[$srcIdx], "::")) {
                if (!isset($dstlist[$list[$dstIdx]])) {
                    $incoming[$list[$dstIdx]][] = $list[$byteIdx];
                    $dstlist[$list[$dstIdx]] = true;
                }
            }
        }
    }
    if (!$outgoing) {
        return;
    }
    if (!isset($incoming)) {
        return;
    }
    $realtotalincoming = calculateRealTotal($incoming);
    $realtotaloutgoing = calculateRealTotal($outgoing);
    foreach ($realtotaloutgoing as $k => $v) {
        $vpsid = get_vpsid_from_ipaddress($k);
        if ($vpsid === 0) {
            continue;
        }
        if (!isset($vpsoutgoing[$vpsid])) {
            $vpsoutgoing[$vpsid] = 0;
        }
        if (!isset($vpsincoming[$vpsid])) {
            $vpsincoming[$vpsid] = 0;
        }
        $vpsoutgoing[$vpsid] += $realtotaloutgoing[$k];
        $vpsincoming[$vpsid] += $realtotalincoming[$k];
    }
    $ret = array();
    foreach ($vpsincoming as $k => $v) {
        $ret[$k]['in'] = $vpsincoming[$k];
        $ret[$k]['out'] = $vpsoutgoing[$k];
        $tot = $vpsincoming[$k] + $vpsoutgoing[$k];
        execRrdTraffic("openvzv6-{$k}", $tot, "-{$vpsincoming[$k]}", $vpsoutgoing[$k]);
        $stringa[] = time() . " " . date("d-M-Y:H:i") . " openvzv6-{$k} {$tot} {$vpsincoming[$k]} {$vpsoutgoing[$k]}";
    }
    if ($stringa) {
        $string = implode("\n", $stringa);
        lfile_put_contents("__path_iptraffic_file" . "v6", "{$string}\n", FILE_APPEND);
    }
    lxshell_return("ip6tables", "-Z", "FORWARD");
    return $ret;
}
Example #3
0
function iptraffic_main()
{
    global $global_dontlogshell;
    $res = lxshell_output("iptables", "-nv", "-L", "FORWARD");
    $res = explode("\n", $res);
    $outgoing = null;
    foreach ($res as $r) {
        $r = trimSpaces($r);
        $list = explode(' ', $r);
        if (!isset($list[7])) {
            continue;
        }
        if (csb($list[7], "0.0.0")) {
            // Just make sure that we don't calculate this goddamn thing twice, which would happen if there are multiple copies of the same rule. So mark that we have already read it in the sourcelist.
            if (!isset($sourcelist[$list[6]])) {
                $outgoing[$list[6]][] = $list[1];
                $sourcelist[$list[6]] = true;
            }
        } else {
            if (csb($list[6], "0.0.0")) {
                if (!isset($dstlist[$list[7]])) {
                    $incoming[$list[7]][] = $list[1];
                    $dstlist[$list[7]] = true;
                }
            }
        }
    }
    if (!$outgoing) {
        return;
    }
    if (!isset($incoming)) {
        return;
    }
    $realtotalincoming = calculateRealTotal($incoming);
    $realtotaloutgoing = calculateRealTotal($outgoing);
    foreach ($realtotaloutgoing as $k => $v) {
        $vpsid = get_vpsid_from_ipaddress($k);
        if ($vpsid === 0) {
            continue;
        }
        if (!isset($vpsoutgoing[$vpsid])) {
            $vpsoutgoing[$vpsid] = 0;
        }
        if (!isset($vpsincoming[$vpsid])) {
            $vpsincoming[$vpsid] = 0;
        }
        $vpsoutgoing[$vpsid] += $realtotaloutgoing[$k];
        $vpsincoming[$vpsid] += $realtotalincoming[$k];
    }
    foreach ($vpsincoming as $k => $v) {
        $tot = $vpsincoming[$k] + $vpsoutgoing[$k];
        execRrdTraffic("openvz-{$k}", $tot, "-{$vpsincoming[$k]}", $vpsoutgoing[$k]);
        $stringa[] = time() . " " . date("d-M-Y:H:i") . " openvz-{$k} {$tot} {$vpsincoming[$k]} {$vpsoutgoing[$k]}";
    }
    if ($stringa) {
        $string = implode("\n", $stringa);
        lfile_put_contents("__path_iptraffic_file", "{$string}\n", FILE_APPEND);
    }
    lxshell_return("iptables", "-Z");
}