Blame view

trunk/src/app/srs_app_utility.cpp 36.8 KB
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <srs_app_utility.hpp>
25
26
#include <sys/types.h>
27
#include <unistd.h>
28 29 30
#include <ifaddrs.h>
#include <arpa/inet.h>
31 32 33
#ifdef SRS_OSX
#include <sys/sysctl.h>
#endif
34
#include <stdlib.h>
35
#include <sys/time.h>
36
using namespace std;
37
38 39
#include <srs_kernel_log.hpp>
#include <srs_app_config.hpp>
40
#include <srs_kernel_utility.hpp>
41
#include <srs_kernel_error.hpp>
42
#include <srs_app_kbps.hpp>
43
#include <srs_app_json.hpp>
44
#include <srs_kernel_stream.hpp>
45
46
int srs_socket_connect(string server, int port, int64_t timeout, st_netfd_t* pstfd)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
{
    int ret = ERROR_SUCCESS;
    
    *pstfd = NULL;
    st_netfd_t stfd = NULL;
    sockaddr_in addr;
    
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1){
        ret = ERROR_SOCKET_CREATE;
        srs_error("create socket error. ret=%d", ret);
        return ret;
    }
    
    srs_assert(!stfd);
    stfd = st_netfd_open_socket(sock);
    if(stfd == NULL){
        ret = ERROR_ST_OPEN_SOCKET;
        srs_error("st_netfd_open_socket failed. ret=%d", ret);
        return ret;
    }
    
    // connect to server.
    std::string ip = srs_dns_resolve(server);
    if (ip.empty()) {
        ret = ERROR_SYSTEM_IP_INVALID;
        srs_error("dns resolve server error, ip empty. ret=%d", ret);
        goto failed;
    }
    
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    
    if (st_connect(stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), timeout) == -1){
        ret = ERROR_ST_CONNECT;
        srs_error("connect to server error. ip=%s, port=%d, ret=%d", ip.c_str(), port, ret);
        goto failed;
    }
    srs_info("connect ok. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
    
    *pstfd = stfd;
    return ret;
    
failed:
    if (stfd) {
        srs_close_stfd(stfd);
    }
    return ret;
}
98
int srs_get_log_level(string level)
99
{
100
    if ("verbose" == level) {
101
        return SrsLogLevel::Verbose;
102
    } else if ("info" == level) {
103
        return SrsLogLevel::Info;
104
    } else if ("trace" == level) {
105
        return SrsLogLevel::Trace;
106
    } else if ("warn" == level) {
107
        return SrsLogLevel::Warn;
108
    } else if ("error" == level) {
109 110
        return SrsLogLevel::Error;
    } else {
111
        return SrsLogLevel::Disabled;
112 113
    }
}
114
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
string srs_path_build_stream(string template_path, string vhost, string app, string stream)
{
    std::string path = template_path;
    
    // variable [vhost]
    path = srs_string_replace(path, "[vhost]", vhost);
    // variable [app]
    path = srs_string_replace(path, "[app]", app);
    // variable [stream]
    path = srs_string_replace(path, "[stream]", stream);
    
    return path;
}

string srs_path_build_timestamp(string template_path)
{
    std::string path = template_path;
    
    
    // date and time substitude
    // clock time
    timeval tv;
    if (gettimeofday(&tv, NULL) == -1) {
        return path;
    }
    
    // to calendar time
    struct tm* tm;
winlin authored
143 144 145 146 147 148 149 150
    if (_srs_config->get_utc_time()) {
        if ((tm = gmtime(&tv.tv_sec)) == NULL) {
            return path;
        }
    } else {
        if ((tm = localtime(&tv.tv_sec)) == NULL) {
            return path;
        }
151 152 153 154 155 156 157 158 159 160 161 162
    }
    
    // the buffer to format the date and time.
    char buf[64];
    
    // [2006], replace with current year.
    if (true) {
        snprintf(buf, sizeof(buf), "%d", 1900 + tm->tm_year);
        path = srs_string_replace(path, "[2006]", buf);
    }
    // [2006], replace with current year.
    if (true) {
winlin authored
163
        snprintf(buf, sizeof(buf), "%04d", 1900 + tm->tm_year);
164 165 166 167
        path = srs_string_replace(path, "[2006]", buf);
    }
    // [01], replace this const to current month.
    if (true) {
winlin authored
168
        snprintf(buf, sizeof(buf), "%02d", 1 + tm->tm_mon);
169 170 171 172
        path = srs_string_replace(path, "[01]", buf);
    }
    // [02], replace this const to current date.
    if (true) {
winlin authored
173
        snprintf(buf, sizeof(buf), "%02d", tm->tm_mday);
174 175 176 177
        path = srs_string_replace(path, "[02]", buf);
    }
    // [15], replace this const to current hour.
    if (true) {
winlin authored
178
        snprintf(buf, sizeof(buf), "%02d", tm->tm_hour);
179 180 181 182
        path = srs_string_replace(path, "[15]", buf);
    }
    // [04], repleace this const to current minute.
    if (true) {
winlin authored
183
        snprintf(buf, sizeof(buf), "%02d", tm->tm_min);
184 185 186 187
        path = srs_string_replace(path, "[04]", buf);
    }
    // [05], repleace this const to current second.
    if (true) {
winlin authored
188
        snprintf(buf, sizeof(buf), "%02d", tm->tm_sec);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        path = srs_string_replace(path, "[05]", buf);
    }
    // [999], repleace this const to current millisecond.
    if (true) {
        snprintf(buf, sizeof(buf), "%03d", (int)(tv.tv_usec / 1000));
        path = srs_string_replace(path, "[999]", buf);
    }
    // [timestamp],replace this const to current UNIX timestamp in ms.
    if (true) {
        int64_t now_us = ((int64_t)tv.tv_sec) * 1000 * 1000 + (int64_t)tv.tv_usec;
        snprintf(buf, sizeof(buf), "%"PRId64, now_us / 1000);
        path = srs_string_replace(path, "[timestamp]", buf);
    }
    
    return path;
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
void srs_parse_endpoint(string ip_port, string& ip, string& port)
{
    ip = "0.0.0.0";
    port = ip_port;
    
    size_t pos = string::npos;
    if ((pos = port.find(":")) != string::npos) {
        ip = port.substr(0, pos);
        port = port.substr(pos + 1);
    }
}

void srs_parse_endpoint(string ip_port, string& ip, int& port)
{
    std::string the_port;
    srs_parse_endpoint(ip_port, ip, the_port);
    port = ::atoi(the_port.c_str());
}
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static SrsRusage _srs_system_rusage;

SrsRusage::SrsRusage()
{
    ok = false;
    sample_time = 0;
    memset(&r, 0, sizeof(rusage));
}

SrsRusage* srs_get_system_rusage()
{
    return &_srs_system_rusage;
}

void srs_update_system_rusage()
{
    if (getrusage(RUSAGE_SELF, &_srs_system_rusage.r) < 0) {
        srs_warn("getrusage failed, ignore");
        return;
    }
245
    
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    _srs_system_rusage.sample_time = srs_get_system_time_ms();
    
    _srs_system_rusage.ok = true;
}

static SrsProcSelfStat _srs_system_cpu_self_stat;
static SrsProcSystemStat _srs_system_cpu_system_stat;

SrsProcSelfStat::SrsProcSelfStat()
{
    ok = false;
    sample_time = 0;
    percent = 0;
    
    pid = 0;
    memset(comm, 0, sizeof(comm));
262
    state = '0';
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    ppid = 0;
    pgrp = 0;
    session = 0;
    tty_nr = 0;
    tpgid = 0;
    flags = 0;
    minflt = 0;
    cminflt = 0;
    majflt = 0;
    cmajflt = 0;
    utime = 0;
    stime = 0;
    cutime = 0;
    cstime = 0;
    priority = 0;
    nice = 0;
    num_threads = 0;
    itrealvalue = 0;
    starttime = 0;
    vsize = 0;
    rss = 0;
    rsslim = 0;
    startcode = 0;
    endcode = 0;
    startstack = 0;
    kstkesp = 0;
    kstkeip = 0;
    signal = 0;
    blocked = 0;
    sigignore = 0;
    sigcatch = 0;
    wchan = 0;
    nswap = 0;
    cnswap = 0;
    exit_signal = 0;
    processor = 0;
    rt_priority = 0;
    policy = 0;
    delayacct_blkio_ticks = 0;
    guest_time = 0;
    cguest_time = 0;
}

SrsProcSystemStat::SrsProcSystemStat()
{
    ok = false;
    sample_time = 0;
    percent = 0;
311
    total_delta = 0;
312 313 314 315 316 317 318 319 320 321 322
    user = 0;
    nice = 0;
    sys = 0;
    idle = 0;
    iowait = 0;
    irq = 0;
    softirq = 0;
    steal = 0;
    guest = 0;
}
323 324 325 326 327
int64_t SrsProcSystemStat::total()
{
    return user + nice + sys + idle + iowait + irq + softirq + steal + guest;
}
328 329 330 331 332 333 334 335 336 337 338 339
SrsProcSelfStat* srs_get_self_proc_stat()
{
    return &_srs_system_cpu_self_stat;
}

SrsProcSystemStat* srs_get_system_proc_stat()
{
    return &_srs_system_cpu_system_stat;
}

bool get_proc_system_stat(SrsProcSystemStat& r)
{
340
#ifndef SRS_OSX
341 342 343 344 345 346
    FILE* f = fopen("/proc/stat", "r");
    if (f == NULL) {
        srs_warn("open system cpu stat failed, ignore");
        return false;
    }
    
347 348 349 350
    static char buf[1024];
    while (fgets(buf, sizeof(buf), f)) {
        if (strncmp(buf, "cpu ", 4) != 0) {
            continue;
351 352
        }
        
353 354 355 356 357 358 359 360 361 362 363 364
        // @see: read_stat_cpu() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L88
        // @remark, ignore the filed 10 cpu_guest_nice
        sscanf(buf + 5, "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n", 
            &r.user, 
            &r.nice, 
            &r.sys, 
            &r.idle, 
            &r.iowait, 
            &r.irq, 
            &r.softirq, 
            &r.steal, 
            &r.guest);
365 366

        break;
367 368 369
    }
    
    fclose(f);
370 371 372 373
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
374 375

    r.ok = true;
376
    
377
    return true;
378 379 380 381
}

bool get_proc_self_stat(SrsProcSelfStat& r)
{
382
#ifndef SRS_OSX
383 384 385 386 387 388
    FILE* f = fopen("/proc/self/stat", "r");
    if (f == NULL) {
        srs_warn("open self cpu stat failed, ignore");
        return false;
    }
    
389
    fscanf(f, "%d %32s %c %d %d %d %d "
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        "%d %u %lu %lu %lu %lu "
        "%lu %lu %ld %ld %ld %ld "
        "%ld %ld %llu %lu %ld "
        "%lu %lu %lu %lu %lu "
        "%lu %lu %lu %lu %lu "
        "%lu %lu %lu %d %d "
        "%u %u %llu "
        "%lu %ld", 
        &r.pid, r.comm, &r.state, &r.ppid, &r.pgrp, &r.session, &r.tty_nr,
        &r.tpgid, &r.flags, &r.minflt, &r.cminflt, &r.majflt, &r.cmajflt,
        &r.utime, &r.stime, &r.cutime, &r.cstime, &r.priority, &r.nice,
        &r.num_threads, &r.itrealvalue, &r.starttime, &r.vsize, &r.rss,
        &r.rsslim, &r.startcode, &r.endcode, &r.startstack, &r.kstkesp,
        &r.kstkeip, &r.signal, &r.blocked, &r.sigignore, &r.sigcatch,
        &r.wchan, &r.nswap, &r.cnswap, &r.exit_signal, &r.processor,
        &r.rt_priority, &r.policy, &r.delayacct_blkio_ticks, 
        &r.guest_time, &r.cguest_time);
    
    fclose(f);
409 410 411 412
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
413
    
414 415 416
    r.ok = true;
    
    return true;
417 418 419 420
}

void srs_update_proc_stat()
{
421 422 423 424 425 426 427 428 429 430 431
    // always assert the USER_HZ is 1/100ths
    // @see: http://stackoverflow.com/questions/7298646/calculating-user-nice-sys-idle-iowait-irq-and-sirq-from-proc-stat/7298711
    static bool user_hz_assert = false;
    if (!user_hz_assert) {
        user_hz_assert = true;
        
        int USER_HZ = sysconf(_SC_CLK_TCK);
        srs_trace("USER_HZ=%d", USER_HZ);
        srs_assert(USER_HZ == 100);
    }
    
432 433 434 435 436 437 438 439 440 441 442 443 444
    // system cpu stat
    if (true) {
        SrsProcSystemStat r;
        if (!get_proc_system_stat(r)) {
            return;
        }
        
        r.sample_time = srs_get_system_time_ms();
        
        // calc usage in percent
        SrsProcSystemStat& o = _srs_system_cpu_system_stat;
        
        // @see: http://blog.csdn.net/nineday/article/details/1928847
445 446 447 448 449 450 451
        // @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
        if (o.total() > 0) {
            r.total_delta = r.total() - o.total();
        }
        if (r.total_delta > 0) {
            int64_t idle = r.idle - o.idle;
            r.percent = (float)(1 - idle / (double)r.total_delta);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
        }
        
        // upate cache.
        _srs_system_cpu_system_stat = r;
    }
    
    // self cpu stat
    if (true) {
        SrsProcSelfStat r;
        if (!get_proc_self_stat(r)) {
            return;
        }
        
        r.sample_time = srs_get_system_time_ms();
        
        // calc usage in percent
        SrsProcSelfStat& o = _srs_system_cpu_self_stat;
        
        // @see: http://stackoverflow.com/questions/16011677/calculating-cpu-usage-using-proc-files
        int64_t total = r.sample_time - o.sample_time;
        int64_t usage = (r.utime + r.stime) - (o.utime + o.stime);
        if (total > 0) {
            r.percent = (float)(usage * 1000 / (double)total / 100);
        }
        
        // upate cache.
        _srs_system_cpu_self_stat = r;
    }
}
482 483 484 485 486
SrsDiskStat::SrsDiskStat()
{
    ok = false;
    sample_time = 0;
    in_KBps = out_KBps = 0;
487
    busy = 0;
488 489 490
    
    pgpgin = 0;
    pgpgout = 0;
491 492 493 494 495 496 497 498
    
    rd_ios = rd_merges = 0;
    rd_sectors = 0;
    rd_ticks = 0;
    
    wr_ios = wr_merges = 0;
    wr_sectors = 0;
    wr_ticks = nb_current = ticks = aveq = 0;
499 500 501 502 503 504 505 506 507
}

static SrsDiskStat _srs_disk_stat;

SrsDiskStat* srs_get_disk_stat()
{
    return &_srs_disk_stat;
}
508
bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
509
{
510
#ifndef SRS_OSX
511 512 513 514 515 516 517 518
    FILE* f = fopen("/proc/vmstat", "r");
    if (f == NULL) {
        srs_warn("open vmstat failed, ignore");
        return false;
    }
    
    r.sample_time = srs_get_system_time_ms();
    
519 520
    static char buf[1024];
    while (fgets(buf, sizeof(buf), f)) {
521
        // @see: read_vmstat_paging() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L495
522
        if (strncmp(buf, "pgpgin ", 7) == 0) {
523
            sscanf(buf + 7, "%lu\n", &r.pgpgin);
524
        } else if (strncmp(buf, "pgpgout ", 8) == 0) {
525
            sscanf(buf + 8, "%lu\n", &r.pgpgout);
526 527 528 529
        }
    }
    
    fclose(f);
530 531 532 533
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
534 535 536 537 538 539
    
    r.ok = true;
    
    return true;
}
540 541
bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
{
542
    r.ok = true;
543 544 545 546 547 548 549 550
    r.sample_time = srs_get_system_time_ms();
    
    // if disabled, ignore all devices.
    SrsConfDirective* conf = _srs_config->get_stats_disk_device();
    if (conf == NULL) {
        return true;
    }
    
551
#ifndef SRS_OSX
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
    FILE* f = fopen("/proc/diskstats", "r");
    if (f == NULL) {
        srs_warn("open vmstat failed, ignore");
        return false;
    }
    
    static char buf[1024];
    while (fgets(buf, sizeof(buf), f)) {
        unsigned int major = 0;
        unsigned int minor = 0;
        static char name[32];
        unsigned int rd_ios = 0;
        unsigned int rd_merges = 0;
        unsigned long long rd_sectors = 0;
        unsigned int rd_ticks = 0;
        unsigned int wr_ios = 0;
        unsigned int wr_merges = 0;
        unsigned long long wr_sectors = 0;
        unsigned int wr_ticks = 0;
        unsigned int nb_current = 0;
        unsigned int ticks = 0;
        unsigned int aveq = 0;
574
        memset(name, 0, sizeof(name));
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        
        sscanf(buf, "%4d %4d %31s %u %u %llu %u %u %u %llu %u %u %u %u", 
            &major, 
            &minor, 
            name, 
            &rd_ios, 
            &rd_merges,
            &rd_sectors, 
            &rd_ticks, 
            &wr_ios, 
            &wr_merges,
            &wr_sectors, 
            &wr_ticks, 
            &nb_current, 
            &ticks, 
            &aveq);
591 592 593 594 595 596 597 598

        for (int i = 0; i < (int)conf->args.size(); i++) {
            string name_ok = conf->args.at(i);
            
            if (strcmp(name_ok.c_str(), name) != 0) {
                continue;
            }
            
599 600 601 602 603 604 605 606 607 608 609
            r.rd_ios += rd_ios;
            r.rd_merges += rd_merges;
            r.rd_sectors += rd_sectors;
            r.rd_ticks += rd_ticks;
            r.wr_ios += wr_ios;
            r.wr_merges += wr_merges;
            r.wr_sectors += wr_sectors;
            r.wr_ticks += wr_ticks;
            r.nb_current += nb_current;
            r.ticks += ticks;
            r.aveq += aveq;
610 611
            
            break;
612 613 614 615
        }
    }
    
    fclose(f);
616 617 618 619
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
620 621 622
    
    r.ok = true;
    
623 624 625
    return true;
}
626 627 628
void srs_update_disk_stat()
{
    SrsDiskStat r;
629
    if (!srs_get_disk_vmstat_stat(r)) {
630 631
        return;
    }
632 633 634
    if (!srs_get_disk_diskstats_stat(r)) {
        return;
    }
635 636 637
    if (!get_proc_system_stat(r.cpu)) {
        return;
    }
638 639
    
    SrsDiskStat& o = _srs_disk_stat;
640 641 642 643 644 645 646
    if (!o.ok) {
        _srs_disk_stat = r;
        return;
    }
    
    // vmstat
    if (true) {
647 648 649 650 651 652 653 654 655 656 657 658
        int64_t duration_ms = r.sample_time - o.sample_time;
        
        if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) {
            // KBps = KB * 1000 / ms = KB/s
            r.in_KBps = (r.pgpgin - o.pgpgin) * 1000 / duration_ms;
        }
        
        if (o.pgpgout > 0 && r.pgpgout > o.pgpgout && duration_ms > 0) {
            // KBps = KB * 1000 / ms = KB/s
            r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms;
        }
    }
659 660
    
    // diskstats
661
    if (r.cpu.ok && o.cpu.ok) {
662
        SrsCpuInfo* cpuinfo = srs_get_cpuinfo();
663
        r.cpu.total_delta = r.cpu.total() - o.cpu.total();
664
        
665
        if (r.cpu.ok && r.cpu.total_delta > 0
666 667 668
            && cpuinfo->ok && cpuinfo->nb_processors > 0
            && o.ticks < r.ticks
        ) {
669
            // @see: write_ext_stat() from https://github.com/sysstat/sysstat/blob/master/iostat.c#L979
670
            // TODO: FIXME: the USER_HZ assert to 100, so the total_delta ticks *10 is ms.
671
            double delta_ms = r.cpu.total_delta * 10 / cpuinfo->nb_processors;
672 673 674
            unsigned int ticks = r.ticks - o.ticks;
            
            // busy in [0, 1], where 0.1532 means 15.32%
675
            r.busy = (float)(ticks / delta_ms);
676 677 678 679
        }
    }
    
    _srs_disk_stat = r;
680 681
}
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
SrsMemInfo::SrsMemInfo()
{
    ok = false;
    sample_time = 0;
    
    percent_ram = 0;
    percent_swap = 0;
    
    MemActive = 0;
    RealInUse = 0;
    NotInUse = 0;
    MemTotal = 0;
    MemFree = 0;
    Buffers = 0;
    Cached = 0;
    SwapTotal = 0;
    SwapFree = 0;
}

static SrsMemInfo _srs_system_meminfo;

SrsMemInfo* srs_get_meminfo()
{
    return &_srs_system_meminfo;
}

void srs_update_meminfo()
{
710 711 712
    SrsMemInfo& r = _srs_system_meminfo;
    
#ifndef SRS_OSX
713 714 715 716 717 718
    FILE* f = fopen("/proc/meminfo", "r");
    if (f == NULL) {
        srs_warn("open meminfo failed, ignore");
        return;
    }
    
719 720
    static char buf[1024];
    while (fgets(buf, sizeof(buf), f)) {
721
        // @see: read_meminfo() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L227
722
        if (strncmp(buf, "MemTotal:", 9) == 0) {
723
            sscanf(buf + 9, "%lu", &r.MemTotal);
724
        } else if (strncmp(buf, "MemFree:", 8) == 0) {
725
            sscanf(buf + 8, "%lu", &r.MemFree);
726
        } else if (strncmp(buf, "Buffers:", 8) == 0) {
727
            sscanf(buf + 8, "%lu", &r.Buffers);
728
        } else if (strncmp(buf, "Cached:", 7) == 0) {
729
            sscanf(buf + 7, "%lu", &r.Cached);
730
        } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
731
            sscanf(buf + 10, "%lu", &r.SwapTotal);
732
        } else if (strncmp(buf, "SwapFree:", 9) == 0) {
733
            sscanf(buf + 9, "%lu", &r.SwapFree);
734 735 736 737
        }
    }
    
    fclose(f);
738 739 740 741
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
742 743 744 745 746 747 748 749 750 751 752 753
    
    r.sample_time = srs_get_system_time_ms();
    r.MemActive = r.MemTotal - r.MemFree;
    r.RealInUse = r.MemActive - r.Buffers - r.Cached;
    r.NotInUse = r.MemTotal - r.RealInUse;
    
    if (r.MemTotal > 0) {
        r.percent_ram = (float)(r.RealInUse / (double)r.MemTotal);
    }
    if (r.SwapTotal > 0) {
        r.percent_swap = (float)((r.SwapTotal - r.SwapFree) / (double)r.SwapTotal);
    }
754 755
    
    r.ok = true;
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
}

SrsCpuInfo::SrsCpuInfo()
{
    ok = false;
    
    nb_processors = 0;
    nb_processors_online = 0;
}

SrsCpuInfo* srs_get_cpuinfo()
{
    static SrsCpuInfo* cpu = NULL;
    if (cpu != NULL) {
        return cpu;
    }
    
    // initialize cpu info.
    cpu = new SrsCpuInfo();
    cpu->ok = true;
    cpu->nb_processors = sysconf(_SC_NPROCESSORS_CONF);
    cpu->nb_processors_online = sysconf(_SC_NPROCESSORS_ONLN);
    
    return cpu;
}

SrsPlatformInfo::SrsPlatformInfo()
{
    ok = false;
    
786
    srs_startup_time = 0;
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    
    os_uptime = 0;
    os_ilde_time = 0;
    
    load_one_minutes = 0;
    load_five_minutes = 0;
    load_fifteen_minutes = 0;
}

static SrsPlatformInfo _srs_system_platform_info;

SrsPlatformInfo* srs_get_platform_info()
{
    return &_srs_system_platform_info;
}

void srs_update_platform_info()
{
    SrsPlatformInfo& r = _srs_system_platform_info;
    
807 808
    r.srs_startup_time = srs_get_system_startup_time_ms();
    
809
#ifndef SRS_OSX
810 811 812 813 814 815 816
    if (true) {
        FILE* f = fopen("/proc/uptime", "r");
        if (f == NULL) {
            srs_warn("open uptime failed, ignore");
            return;
        }
        
817
        fscanf(f, "%lf %lf\n", &r.os_uptime, &r.os_ilde_time);
818 819 820 821 822 823 824 825 826 827 828
    
        fclose(f);
    }
    
    if (true) {
        FILE* f = fopen("/proc/loadavg", "r");
        if (f == NULL) {
            srs_warn("open loadavg failed, ignore");
            return;
        }
        
829 830 831 832 833 834
        // @see: read_loadavg() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L402
        // @remark, we use our algorithm, not sysstat.
        fscanf(f, "%lf %lf %lf\n", 
            &r.load_one_minutes, 
            &r.load_five_minutes, 
            &r.load_fifteen_minutes);
835 836 837
    
        fclose(f);
    }
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
#else
    // man 3 sysctl
    if (true) {
        struct timeval tv;
        size_t len = sizeof(timeval);
        
        int mib[2];
        mib[0] = CTL_KERN;
        mib[1] = KERN_BOOTTIME;
        if (sysctl(mib, 2, &tv, &len, NULL, 0) < 0) {
            srs_warn("sysctl boottime failed, ignore");
            return;
        }
        
        time_t bsec = tv.tv_sec;
        time_t csec = ::time(NULL);
        r.os_uptime = difftime(csec, bsec);
    }
    
    // man 3 sysctl
    if (true) {
        struct loadavg la;
        size_t len = sizeof(loadavg);
        
        int mib[2];
        mib[0] = CTL_VM;
        mib[1] = VM_LOADAVG;
        if (sysctl(mib, 2, &la, &len, NULL, 0) < 0) {
            srs_warn("sysctl loadavg failed, ignore");
            return;
        }
        
        r.load_one_minutes = (double)la.ldavg[0] / la.fscale;
        r.load_five_minutes = (double)la.ldavg[1] / la.fscale;
        r.load_fifteen_minutes = (double)la.ldavg[2] / la.fscale;
    }
#endif
875 876
    
    r.ok = true;
877
}
878
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
SrsNetworkDevices::SrsNetworkDevices()
{
    ok = false;
    
    memset(name, 0, sizeof(name));
    sample_time = 0;
    
    rbytes = 0;
    rpackets = 0;
    rerrs = 0;
    rdrop = 0;
    rfifo = 0;
    rframe = 0;
    rcompressed = 0;
    rmulticast = 0;
    
    sbytes = 0;
    spackets = 0;
    serrs = 0;
    sdrop = 0;
    sfifo = 0;
    scolls = 0;
    scarrier = 0;
    scompressed = 0;
}

#define MAX_NETWORK_DEVICES_COUNT 16
static SrsNetworkDevices _srs_system_network_devices[MAX_NETWORK_DEVICES_COUNT];
static int _nb_srs_system_network_devices = -1;

SrsNetworkDevices* srs_get_network_devices()
{
    return _srs_system_network_devices;
}

int srs_get_network_devices_count()
{
    return _nb_srs_system_network_devices;
}

void srs_update_network_devices()
{
921
#ifndef SRS_OSX
922 923 924 925 926 927 928 929 930 931 932 933 934
    if (true) {
        FILE* f = fopen("/proc/net/dev", "r");
        if (f == NULL) {
            srs_warn("open proc network devices failed, ignore");
            return;
        }
        
        // ignore title.
        static char buf[1024];
        fgets(buf, sizeof(buf), f);
        fgets(buf, sizeof(buf), f);
    
        for (int i = 0; i < MAX_NETWORK_DEVICES_COUNT; i++) {
935 936 937 938
            if (!fgets(buf, sizeof(buf), f)) {
                break;
            }
            
939 940
            SrsNetworkDevices& r = _srs_system_network_devices[i];
    
941 942
            // @see: read_net_dev() from https://github.com/sysstat/sysstat/blob/master/rd_stats.c#L786
            // @remark, we use our algorithm, not sysstat.
943
            sscanf(buf, "%6[^:]:%llu %lu %lu %lu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu %lu %lu\n",
944 945 946
                r.name, &r.rbytes, &r.rpackets, &r.rerrs, &r.rdrop, &r.rfifo, &r.rframe, &r.rcompressed, &r.rmulticast,
                &r.sbytes, &r.spackets, &r.serrs, &r.sdrop, &r.sfifo, &r.scolls, &r.scarrier, &r.scompressed);
                
947 948
            r.name[sizeof(r.name) - 1] = 0;
            _nb_srs_system_network_devices = i + 1;
949
            
950 951
            r.sample_time = srs_get_system_time_ms();
            r.ok = true;
952 953 954 955
        }
    
        fclose(f);
    }
956 957 958 959
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
960 961
}
962 963 964 965
SrsNetworkRtmpServer::SrsNetworkRtmpServer()
{
    ok = false;
    sample_time = rbytes = sbytes = 0;
966
    nb_conn_sys = nb_conn_srs = 0;
967
    nb_conn_sys_et = nb_conn_sys_tw = 0;
968
    nb_conn_sys_udp = 0;
969 970 971 972 973 974 975 976 977
}

static SrsNetworkRtmpServer _srs_network_rtmp_server;

SrsNetworkRtmpServer* srs_get_network_rtmp_server()
{
    return &_srs_network_rtmp_server;
}
978 979
// @see: http://stackoverflow.com/questions/5992211/list-of-possible-internal-socket-statuses-from-proc
enum {
980 981 982 983 984 985 986 987 988 989 990 991 992
    SYS_TCP_ESTABLISHED =      0x01,
    SYS_TCP_SYN_SENT,       // 0x02
    SYS_TCP_SYN_RECV,       // 0x03
    SYS_TCP_FIN_WAIT1,      // 0x04
    SYS_TCP_FIN_WAIT2,      // 0x05
    SYS_TCP_TIME_WAIT,      // 0x06
    SYS_TCP_CLOSE,          // 0x07
    SYS_TCP_CLOSE_WAIT,     // 0x08
    SYS_TCP_LAST_ACK,       // 0x09
    SYS_TCP_LISTEN,         // 0x0A
    SYS_TCP_CLOSING,        // 0x0B /* Now a valid state */

    SYS_TCP_MAX_STATES      // 0x0C /* Leave at the end! */
993 994 995
};

void srs_update_rtmp_server(int nb_conn, SrsKbps* kbps)
996 997
{
    SrsNetworkRtmpServer& r = _srs_network_rtmp_server;
998 999 1000 1001 1002 1003 1004 1005
        
    int nb_socks = 0;
    int nb_tcp4_hashed = 0;
    int nb_tcp_orphans = 0;
    int nb_tcp_tws = 0;
    int nb_tcp_total = 0;
    int nb_tcp_mem = 0;
    int nb_udp4 = 0;
1006
    
1007
#ifndef SRS_OSX
1008
    if (true) {
1009
        FILE* f = fopen("/proc/net/sockstat", "r");
1010
        if (f == NULL) {
1011
            srs_warn("open proc network sockstat failed, ignore");
1012 1013 1014 1015 1016 1017
            return;
        }
        
        // ignore title.
        static char buf[1024];
        fgets(buf, sizeof(buf), f);
1018
        
1019
        while (fgets(buf, sizeof(buf), f)) {
1020
            // @see: et_sockstat_line() from https://github.com/shemminger/iproute2/blob/master/misc/ss.c
1021
            if (strncmp(buf, "sockets: used ", 14) == 0) {
1022
                sscanf(buf + 14, "%d\n", &nb_socks);
1023
            } else if (strncmp(buf, "TCP: ", 5) == 0) {
1024 1025 1026 1027 1028 1029
                sscanf(buf + 5, "%*s %d %*s %d %*s %d %*s %d %*s %d\n", 
                    &nb_tcp4_hashed, 
                    &nb_tcp_orphans, 
                    &nb_tcp_tws, 
                    &nb_tcp_total, 
                    &nb_tcp_mem);
1030
            } else if (strncmp(buf, "UDP: ", 5) == 0) {
1031
                sscanf(buf + 5, "%*s %d\n", &nb_udp4);
1032 1033
            }
        }
1034
    
1035 1036
        fclose(f);
    }
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
    nb_socks = 0;
    nb_tcp4_hashed = 0;
    nb_tcp_orphans = 0;
    nb_tcp_tws = 0;
    nb_tcp_total = 0;
    nb_tcp_mem = 0;
    nb_udp4 = 0;
#endif
1048
    
1049 1050
    int nb_tcp_estab = 0;
    
1051
#ifndef SRS_OSX
1052
    if (true) {
1053
        FILE* f = fopen("/proc/net/snmp", "r");
1054
        if (f == NULL) {
1055
            srs_warn("open proc network snmp failed, ignore");
1056 1057 1058 1059 1060 1061 1062
            return;
        }
        
        // ignore title.
        static char buf[1024];
        fgets(buf, sizeof(buf), f);
        
1063
        // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
1064
        while (fgets(buf, sizeof(buf), f)) {
1065 1066 1067 1068 1069 1070 1071 1072 1073
            // @see: get_snmp_int("Tcp:", "CurrEstab", &sn.tcp_estab)
            // tcp stat title
            if (strncmp(buf, "Tcp: ", 5) == 0) {
                // read tcp stat data
                if (!fgets(buf, sizeof(buf), f)) {
                    break;
                }
                // parse tcp stat data
                if (strncmp(buf, "Tcp: ", 5) == 0) {
1074
                    sscanf(buf + 5, "%*d %*d %*d %*d %*d %*d %*d %*d %d\n", &nb_tcp_estab);
1075
                }
1076 1077 1078 1079 1080
            }
        }
    
        fclose(f);
    }
1081 1082 1083 1084
#else
    // TODO: FIXME: impelments it.
    // Fuck all of you who use osx for a long time and never patch the osx features for srs.
#endif
1085
    
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
    // @see: https://github.com/shemminger/iproute2/blob/master/misc/ss.c
    // TODO: FIXME: ignore the slabstat, @see: get_slabstat()
    if (true) {
        // @see: print_summary()
        r.nb_conn_sys = nb_tcp_total + nb_tcp_tws;
        r.nb_conn_sys_et = nb_tcp_estab;
        r.nb_conn_sys_tw = nb_tcp_tws;
        r.nb_conn_sys_udp = nb_udp4;
    }
    
1096
    if (true) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        r.ok = true;
        
        r.nb_conn_srs = nb_conn;
        r.sample_time = srs_get_system_time_ms();
        
        r.rbytes = kbps->get_recv_bytes();
        r.rkbps = kbps->get_recv_kbps();
        r.rkbps_30s = kbps->get_recv_kbps_30s();
        r.rkbps_5m = kbps->get_recv_kbps_5m();
        
        r.sbytes = kbps->get_send_bytes();
        r.skbps = kbps->get_send_kbps();
        r.skbps_30s = kbps->get_send_kbps_30s();
        r.skbps_5m = kbps->get_send_kbps_5m();
    }
1112 1113
}
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
vector<string> _srs_system_ipv4_ips;

void retrieve_local_ipv4_ips()
{
    vector<string>& ips = _srs_system_ipv4_ips;
    
    ips.clear();
    
    ifaddrs* ifap;
    if (getifaddrs(&ifap) == -1) {
        srs_warn("retrieve local ips, ini ifaddrs failed.");
        return;
    }
    
    ifaddrs* p = ifap;
    while (p != NULL) {
        sockaddr* addr = p->ifa_addr;
        
1132
        // retrieve ipv4 addr
1133 1134
        // ignore the tun0 network device, 
        // which addr is NULL.
1135
        // @see: https://github.com/simple-rtmp-server/srs/issues/141
1136
        if (addr && addr->sa_family == AF_INET) {
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
            in_addr* inaddr = &((sockaddr_in*)addr)->sin_addr;
            
            char buf[16];
            memset(buf, 0, sizeof(buf));
            
            if ((inet_ntop(addr->sa_family, inaddr, buf, sizeof(buf))) == NULL) {
                srs_warn("convert local ip failed");
                break;
            }
            
            std::string ip = buf;
winlin authored
1148
            if (ip != SRS_CONSTS_LOCALHOST) {
1149
                srs_trace("retrieve local ipv4 ip=%s, index=%d", ip.c_str(), (int)ips.size());
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
                ips.push_back(ip);
            }
        }
        
        p = p->ifa_next;
    }

    freeifaddrs(ifap);
}

vector<string>& srs_get_local_ipv4_ips()
{
    if (_srs_system_ipv4_ips.empty()) {
        retrieve_local_ipv4_ips();
    }

    return _srs_system_ipv4_ips;
}
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195

string srs_get_local_ip(int fd)
{
    std::string ip;

    // discovery client information
    sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {
        return ip;
    }
    srs_verbose("get local ip success.");

    // ip v4 or v6
    char buf[INET6_ADDRSTRLEN];
    memset(buf, 0, sizeof(buf));

    if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
        return ip;
    }

    ip = buf;

    srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd);

    return ip;
}
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
int srs_get_local_port(int fd)
{
    // discovery client information
    sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {
        return 0;
    }
    srs_verbose("get local ip success.");
    
    int port = ntohs(addr.sin_port);

    srs_verbose("get local ip of client port=%s, fd=%d", port, fd);

    return port;
}
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
string srs_get_peer_ip(int fd)
{
    std::string ip;
    
    // discovery client information
    sockaddr_in addr;
    socklen_t addrlen = sizeof(addr);
    if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
        return ip;
    }
    srs_verbose("get peer name success.");

    // ip v4 or v6
    char buf[INET6_ADDRSTRLEN];
    memset(buf, 0, sizeof(buf));
    
    if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
        return ip;
    }
    srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
    
    ip = buf;
    
1236
    srs_verbose("get peer ip success. ip=%s, fd=%d", ip.c_str(), fd);
1237 1238 1239
    
    return ip;
}
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

void srs_api_dump_summaries(std::stringstream& ss)
{
    SrsRusage* r = srs_get_system_rusage();
    SrsProcSelfStat* u = srs_get_self_proc_stat();
    SrsProcSystemStat* s = srs_get_system_proc_stat();
    SrsCpuInfo* c = srs_get_cpuinfo();
    SrsMemInfo* m = srs_get_meminfo();
    SrsPlatformInfo* p = srs_get_platform_info();
    SrsNetworkDevices* n = srs_get_network_devices();
    SrsNetworkRtmpServer* nrs = srs_get_network_rtmp_server();
1251
    SrsDiskStat* d = srs_get_disk_stat();
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    
    float self_mem_percent = 0;
    if (m->MemTotal > 0) {
        self_mem_percent = (float)(r->r.ru_maxrss / (double)m->MemTotal);
    }
    
    int64_t now = srs_get_system_time_ms();
    double srs_uptime = (now - p->srs_startup_time) / 100 / 10.0;
    
    int64_t n_sample_time = 0;
    int64_t nr_bytes = 0;
    int64_t ns_bytes = 0;
    int nb_n = srs_get_network_devices_count();
    for (int i = 0; i < nb_n; i++) {
        SrsNetworkDevices& o = n[i];
        
        // ignore the lo interface.
        std::string inter = o.name;
        if (!o.ok || inter == "lo") {
            continue;
        }
        
        nr_bytes += o.rbytes;
        ns_bytes += o.sbytes;
        n_sample_time = o.sample_time;
    }
    
1279 1280
    // all data is ok?
    bool ok = (r->ok && u->ok && s->ok && c->ok 
1281
        && d->ok && m->ok && p->ok && nrs->ok);
1282
    
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
    ss << SRS_JOBJECT_START
        << SRS_JFIELD_ERROR(ERROR_SUCCESS) << SRS_JFIELD_CONT
        << SRS_JFIELD_ORG("data", SRS_JOBJECT_START)
            << SRS_JFIELD_ORG("ok", (ok? "true":"false")) << SRS_JFIELD_CONT
            << SRS_JFIELD_ORG("now_ms", now) << SRS_JFIELD_CONT
            << SRS_JFIELD_ORG("self", SRS_JOBJECT_START)
                << SRS_JFIELD_STR("version", RTMP_SIG_SRS_VERSION) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("pid", getpid()) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("ppid", u->ppid) << SRS_JFIELD_CONT
                << SRS_JFIELD_STR("argv", _srs_config->argv()) << SRS_JFIELD_CONT
                << SRS_JFIELD_STR("cwd", _srs_config->cwd()) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_kbyte", r->r.ru_maxrss) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_percent", self_mem_percent) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("cpu_percent", u->percent) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("srs_uptime", srs_uptime)
            << SRS_JOBJECT_END << SRS_JFIELD_CONT
            << SRS_JFIELD_ORG("system", SRS_JOBJECT_START)
                << SRS_JFIELD_ORG("cpu_percent", s->percent) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("disk_read_KBps", d->in_KBps) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("disk_write_KBps", d->out_KBps) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("disk_busy_percent", d->busy) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_ram_kbyte", m->MemTotal) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_ram_percent", m->percent_ram) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_swap_kbyte", m->SwapTotal) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("mem_swap_percent", m->percent_swap) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("cpus", c->nb_processors) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("cpus_online", c->nb_processors_online) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("uptime", p->os_uptime) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("ilde_time", p->os_ilde_time) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("load_1m", p->load_one_minutes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("load_5m", p->load_five_minutes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("load_15m", p->load_fifteen_minutes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("net_sample_time", n_sample_time) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("net_recv_bytes", nr_bytes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("net_send_bytes", ns_bytes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("srs_sample_time", nrs->sample_time) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("srs_recv_bytes", nrs->rbytes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("srs_send_bytes", nrs->sbytes) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("conn_sys_udp", nrs->nb_conn_sys_udp) << SRS_JFIELD_CONT
                << SRS_JFIELD_ORG("conn_srs", nrs->nb_conn_srs)
            << SRS_JOBJECT_END
        << SRS_JOBJECT_END
        << SRS_JOBJECT_END;
1329
}
1330