• Linux下的PHP实时流量统计程序 王道


    本程序分析/proc/net/ip_conntrack来进行分析实时在线及流量情况,精度不敢说,但是很实用。

    做的有点太水了。不过能用就好

    #!/usr/local/bin/php -q
    <?php
    $my_OverIp = array ();
    $my_OverBytes = array ();
    $k = 0;
    $kk = 0;
    $my_s=1;
    $fd = fopen ( "/proc/net/ip_conntrack", "r" );
    while ( ! feof ( $fd ) ) {
            $my_FileLineCache = fgets ( $fd );
            preg_match ( "/src=[0-9]+.[0-9]+.[0-9]+.[0-9]{1,3}/", $my_FileLineCache, $match_ip );
            preg_match ( "/bytes=[0-9]+/", $my_FileLineCache, $match_bytes );
            for($i = 0; $i < sizeof ( $my_OverIp ); $i ++) {
                    if ($my_OverIp [$i] == $match_ip [0]) {
                            $k = 1;
                            $kk = $i;
                            break;
                    }
            }
            $my_byte=preg_replace("/bytes=/","",$match_bytes [0]);
            if ($k == 0) {
                    $my_OverIp [] = $match_ip [0];
                    $my_OverBytes [] = 0;
            } else {
                    if ($my_s % 2 == 0)
                    {
                            $my_OverBytes [$kk] += $my_byte;
                    }
            }
            $k = 0;
            $my_s++;
    }
    fclose ( $fd );
    for($j = 0; $j < sizeof ( $my_OverIp ); $j ++) {
            if ($j<>sizeof ( $my_OverIp )-1)
            {
                    print $my_OverIp[$j];
                    print " bytes=$my_OverBytes[$j]\n";
            }
    }
    ?>

  • 相关阅读:
    白书数据结构基础总结
    UVA 10557 XYZZY 结题报告
    UVA 10047 The Monocycle 解题报告
    二叉查找树及其C语言实现
    堆排序及其c语言实现
    约瑟夫环问题小结
    KMP算法总结
    UVA 10129 Play on Words 解题报告
    深入浅出Node.js (8)
    洛谷 P1757 通天之分组背包
  • 原文地址:https://www.cnblogs.com/makewong/p/1817891.html
Copyright © 2020-2023  润新知