V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Livid
V2EX  ›  Python

关于用 Python 获得一台 Linux 服务器的 Connections 数

  •  
  •   Livid · 2014-06-14 14:49:09 +08:00 · 4188 次点击
    这是一个创建于 3576 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前试过用 psutil,但是貌似不太有效率,因为需要查询每个 Process 的 Connections 数然后累加。

    有什么更高效的方式么?/proc 下有系统的总 Connections 数么?
    9 条回复    2014-06-15 02:04:29 +08:00
    hepochen
        1
    hepochen  
       2014-06-14 14:58:40 +08:00 via iPhone   ❤️ 2
    ss 这个系统命令是最高效的
    dreampuf
        2
    dreampuf  
       2014-06-14 15:05:12 +08:00   ❤️ 1
    https://github.com/giampaolo/psutil/blob/master/psutil/_pslinux.py#L362

    自己改写逻辑?按时间倒排增量?
    psutil 只是解析 /proc/net/* 中的内容,以枚举器的形式返回。/proc 在内存中,IO问题相对来说没有disk那么严重
    Livid
        3
    Livid  
    MOD
    OP
       2014-06-14 15:13:13 +08:00
    @dreampuf 在非常忙的服务器上,这个过程的耗时太长。
    dreampuf
        4
    dreampuf  
       2014-06-14 15:27:50 +08:00
    @Livid ss也是读取的/proc/net/中的统计信息。如果你不需要pid相关信息的话,我觉得psutil更易用更容易定制
    liwei
        5
    liwei  
       2014-06-14 16:29:19 +08:00 via Android   ❤️ 2
    @dreampuf 非也,netstat就是因为读取/proc目录的文件导致在大量链接的情况下速度不理想,ss正是为了改善这一状况而产生的,使用的是内核的 ip_diag和tcp_diag功能,走的是netlink接口。
    liwei
        6
    liwei  
       2014-06-14 16:31:17 +08:00 via Android
    est
        7
    est  
       2014-06-14 16:32:56 +08:00   ❤️ 2
    @dreampuf 不是。ss是直接netlink度内核。netstat读/proc
    dreampuf
        8
    dreampuf  
       2014-06-15 01:49:57 +08:00
    @liwei
    @est

    多谢告知。感谢已送。
    ss.c 里的统计信息似乎还是读取/proc/* 。是我下了错误的版本吗?
    $ uname -a
    Linux dt 3.11.0-12-generic #19-Ubuntu SMP Wed Oct 9 16:20:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

    $ sudo apt-get source iproute

    ~/iproute2-3.12.0/misc$ grep -A13 'static FILE \*generic_proc_open' ss.c
    static FILE *generic_proc_open(const char *env, const char *name)
    {
    const char *p = getenv(env);
    char store[128];

    if (!p) {
    p = getenv("PROC_ROOT") ? : "/proc";
    snprintf(store, sizeof(store)-1, "%s/%s", p, name);
    p = store;
    }

    return fopen(p, "r");
    }
    dreampuf
        9
    dreampuf  
       2014-06-15 02:04:29 +08:00
    再看了眼,proc只有在netlink失败后才会作为备选方案解析。

    static int tcp_show(struct filter *f, int socktype)
    {
    FILE *fp = NULL;
    char *buf = NULL;
    int bufsize = 64*1024;

    dg_proto = TCP_PROTO;

    if (getenv("TCPDIAG_FILE"))
    return tcp_show_netlink_file(f);

    if (!getenv("PROC_NET_TCP") && !getenv("PROC_ROOT")
    && inet_show_netlink(f, NULL, socktype) == 0)
    return 0;

    /* Sigh... We have to parse /proc/net/tcp... */
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2802 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.