From e76036f40b53246188060b0d14855e29df694f30 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 27 Jul 2014 14:43:55 +0800 Subject: [PATCH] refine config heartbeat, add new stats. 0.9.175 --- trunk/conf/full.conf | 20 +++++++-- trunk/conf/http.heartbeat.conf | 4 ++ trunk/src/app/srs_app_config.cpp | 62 ++++++++++++++++++---------- trunk/src/app/srs_app_config.hpp | 21 +++++++--- trunk/src/app/srs_app_heartbeat.cpp | 2 +- trunk/src/app/srs_app_utility.cpp | 9 +++- trunk/src/app/srs_app_utility.hpp | 2 + trunk/src/core/srs_core.hpp | 2 +- trunk/src/utest/srs_utest_config.cpp | 22 ++++++---- 9 files changed, 102 insertions(+), 42 deletions(-) diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 1982e9517..1db459e0e 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -42,7 +42,10 @@ max_connections 1000; # @remark: donot support reload. # default: on daemon on; + # heartbeat to api server +# @remark, the ip report to server, is retrieve from system stat, +# which need the config item stats.network_device_index. heartbeat { # whether heartbeat is enalbed. # default: off @@ -61,10 +64,6 @@ heartbeat { url http://127.0.0.1:8085/api/v1/servers; # the id of devide. device_id "my-srs-device"; - # the index of device ip. - # we may retrieve more than one network device. - # default: 0 - device_index 0; # whether report with summaries # if true, put /api/v1/summaries to the request data: # { @@ -115,6 +114,19 @@ http_stream { dir ./objs/nginx/html; } +# system statistics section. +# the main cycle will retrieve the system stat, +# for example, the cpu/mem/network/disk-io data, +# the http api, for instance, /api/v1/summaries will show these data. +# @remark the heartbeat depends on the network_device_index, +# for example, the eth0 maybe the device which index is 0. +stats { + # the index of device ip. + # we may retrieve more than one network device. + # default: 0 + network_device_index 0; +} + ############################################################################################# # RTMP/HTTP VHOST sections ############################################################################################# diff --git a/trunk/conf/http.heartbeat.conf b/trunk/conf/http.heartbeat.conf index 8455ad35f..c83eaa31a 100644 --- a/trunk/conf/http.heartbeat.conf +++ b/trunk/conf/http.heartbeat.conf @@ -8,6 +8,10 @@ heartbeat { interval 9.3; url http://127.0.0.1:8085/api/v1/servers; device_id "my-srs-device"; + summaries on; +} +stats { + network_device_index 0; } vhost __defaultVhost__ { } diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index a6777afb3..1d101dca9 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1202,7 +1202,7 @@ int SrsConfig::check_config() if (n != "listen" && n != "pid" && n != "chunk_size" && n != "ff_log_dir" && n != "srs_log_tank" && n != "srs_log_level" && n != "srs_log_file" && n != "max_connections" && n != "daemon" && n != "heartbeat" - && n != "http_api" && n != "http_stream" && n != "vhost" + && n != "http_api" && n != "http_stream" && n != "stats" && n != "vhost" && n != "pithy_print") { ret = ERROR_SYSTEM_CONFIG_INVALID; @@ -1237,7 +1237,7 @@ int SrsConfig::check_config() for (int i = 0; conf && i < (int)conf->directives.size(); i++) { string n = conf->at(i)->name; if (n != "enabled" && n != "interval" && n != "url" - && n != "device_id" && n != "device_index" && n != "summaries" + && n != "device_id" && n != "summaries" ) { ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("unsupported heartbeat directive %s, ret=%d", n.c_str(), ret); @@ -1245,6 +1245,17 @@ int SrsConfig::check_config() } } } + if (true) { + SrsConfDirective* conf = get_stats(); + for (int i = 0; conf && i < (int)conf->directives.size(); i++) { + string n = conf->at(i)->name; + if (n != "network_device_index") { + ret = ERROR_SYSTEM_CONFIG_INVALID; + srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret); + return ret; + } + } + } if (true) { SrsConfDirective* conf = get_pithy_print(); for (int i = 0; conf && i < (int)conf->directives.size(); i++) { @@ -1427,10 +1438,14 @@ int SrsConfig::check_config() get_heartbeat_interval(), ret); return ret; } - if (get_heartbeat_device_index() < 0) { + + //////////////////////////////////////////////////////////////////////// + // check stats + //////////////////////////////////////////////////////////////////////// + if (get_stats_network_device_index() < 0) { ret = ERROR_SYSTEM_CONFIG_INVALID; - srs_error("directive heartbeat device_index invalid, device_index=%d, ret=%d", - get_heartbeat_device_index(), ret); + srs_error("directive stats network_device_index invalid, network_device_index=%d, ret=%d", + get_stats_network_device_index(), ret); return ret; } @@ -3142,22 +3157,6 @@ string SrsConfig::get_heartbeat_device_id() return conf->arg0(); } -int SrsConfig::get_heartbeat_device_index() -{ - SrsConfDirective* conf = get_heartbeart(); - - if (!conf) { - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX; - } - - conf = conf->get("device_index"); - if (!conf || conf->arg0().empty()) { - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX; - } - - return ::atoi(conf->arg0().c_str()); -} - bool SrsConfig::get_heartbeat_summaries() { SrsConfDirective* conf = get_heartbeart(); @@ -3174,6 +3173,27 @@ bool SrsConfig::get_heartbeat_summaries() return true; } +SrsConfDirective* SrsConfig::get_stats() +{ + return root->get("stats"); +} + +int SrsConfig::get_stats_network_device_index() +{ + SrsConfDirective* conf = get_stats(); + + if (!conf) { + return SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX; + } + + conf = conf->get("network_device_index"); + if (!conf || conf->arg0().empty()) { + return SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX; + } + + return ::atoi(conf->arg0().c_str()); +} + namespace _srs_internal { SrsConfigBuffer::SrsConfigBuffer() diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index a54a85bab..07777a1a0 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -74,9 +74,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED false #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL 9.9 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL "http://"SRS_CONSTS_LOCALHOST":8085/api/v1/servers" -#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX 0 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES false +#define SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX 0 + #define SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS 10000 #define SRS_CONF_DEFAULT_STAGE_PUBLISH_USER_INTERVAL_MS 10000 #define SRS_CONF_DEFAULT_STAGE_FORWARDER_INTERVAL_MS 10000 @@ -926,14 +927,22 @@ public: */ virtual std::string get_heartbeat_device_id(); /** - * get the network device index, to report to server. - * for example, 0 means the eth0 maybe. - */ - virtual int get_heartbeat_device_index(); - /** * whether report with summaries of http api: /api/v1/summaries. */ virtual bool get_heartbeat_summaries(); +// stats section +private: + /** + * get the stats directive. + */ + virtual SrsConfDirective* get_stats(); +public: + /** + * get the network device index, used to retrieve the ip of device, + * for heartbeat to report to server, or to get the local ip. + * for example, 0 means the eth0 maybe. + */ + virtual int get_stats_network_device_index(); }; namespace _srs_internal diff --git a/trunk/src/app/srs_app_heartbeat.cpp b/trunk/src/app/srs_app_heartbeat.cpp index 259fc5b9e..c70ba7643 100644 --- a/trunk/src/app/srs_app_heartbeat.cpp +++ b/trunk/src/app/srs_app_heartbeat.cpp @@ -61,7 +61,7 @@ void SrsHttpHeartbeat::heartbeat() vector& ips = srs_get_local_ipv4_ips(); if (!ips.empty()) { - ip = ips[_srs_config->get_heartbeat_device_index() % (int)ips.size()]; + ip = ips[_srs_config->get_stats_network_device_index() % (int)ips.size()]; } std::stringstream ss; diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index 0e1a7d391..1493b0d6a 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -367,7 +367,7 @@ SrsDiskStat* srs_get_disk_stat() return &_srs_disk_stat; } -bool srs_get_disk_stat(SrsDiskStat& r) +bool srs_get_disk_vmstat_stat(SrsDiskStat& r) { FILE* f = fopen("/proc/vmstat", "r"); if (f == NULL) { @@ -401,10 +401,15 @@ bool srs_get_disk_stat(SrsDiskStat& r) return true; } +bool srs_get_disk_diskstats_stat(SrsDiskStat& r) +{ + return true; +} + void srs_update_disk_stat() { SrsDiskStat r; - if (!srs_get_disk_stat(r)) { + if (!srs_get_disk_vmstat_stat(r)) { return; } diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index 5270154b0..dafaae15e 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -348,6 +348,8 @@ public: // the out(write) page count, pgpgout*1024 is the write bytes. // Total number of kilobytes the system paged out to disk per second. unsigned long pgpgout; + + // @see: https://www.kernel.org/doc/Documentation/iostats.txt public: SrsDiskStat(); diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 61e0cd92b..9ffd1e556 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR "0" #define VERSION_MINOR "9" -#define VERSION_REVISION "174" +#define VERSION_REVISION "175" #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION // server info. #define RTMP_SIG_SRS_KEY "SRS" diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index 3a19d969a..567c0e2e7 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -96,10 +96,6 @@ std::string __full_conf = "" " url http://127.0.0.1:8085/api/v1/servers; \n" " # the id of devide. \n" " device_id \"my-srs-device\"; \n" - " # the index of device ip. \n" - " # we may retrieve more than one network device. \n" - " # default: 0 \n" - " device_index 0; \n" " # whether report with summaries \n" " # if true, put /api/v1/summaries to the request data: \n" " # { \n" @@ -149,6 +145,18 @@ std::string __full_conf = "" " # default: ./objs/nginx/html \n" " dir ./objs/nginx/html; \n" "} \n" + "# system statistics section. \n" + "# the main cycle will retrieve the system stat, \n" + "# for example, the cpu/mem/network/disk-io data, \n" + "# the http api, for instance, /api/v1/summaries will show these data. \n" + "# @remark the heartbeat depends on the network_device_index, \n" + "# for example, the eth0 maybe the device which index is 0. \n" + "stats { \n" + " # the index of device ip. \n" + " # we may retrieve more than one network device. \n" + " # default: 0 \n" + " network_device_index 0; \n" + "} \n" " \n" "############################################################################################# \n" "# RTMP/HTTP VHOST sections \n" @@ -1132,10 +1140,10 @@ VOID TEST(ConfigTest, CheckMacros) #ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL EXPECT_TRUE(false); #endif -#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX +#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES EXPECT_TRUE(false); #endif -#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES +#ifndef SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX EXPECT_TRUE(false); #endif #ifndef SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS @@ -1831,7 +1839,7 @@ VOID TEST(ConfigMainTest, ParseFullConf) EXPECT_EQ(9300, conf.get_heartbeat_interval()); EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str()); EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str()); - EXPECT_EQ(0, conf.get_heartbeat_device_index()); + EXPECT_EQ(0, conf.get_stats_network_device_index()); EXPECT_FALSE(conf.get_heartbeat_summaries()); EXPECT_TRUE(conf.get_http_api_enabled());