winlin

refine config heartbeat, add new stats. 0.9.175

@@ -42,7 +42,10 @@ max_connections 1000; @@ -42,7 +42,10 @@ max_connections 1000;
42 # @remark: donot support reload. 42 # @remark: donot support reload.
43 # default: on 43 # default: on
44 daemon on; 44 daemon on;
  45 +
45 # heartbeat to api server 46 # heartbeat to api server
  47 +# @remark, the ip report to server, is retrieve from system stat,
  48 +# which need the config item stats.network_device_index.
46 heartbeat { 49 heartbeat {
47 # whether heartbeat is enalbed. 50 # whether heartbeat is enalbed.
48 # default: off 51 # default: off
@@ -61,10 +64,6 @@ heartbeat { @@ -61,10 +64,6 @@ heartbeat {
61 url http://127.0.0.1:8085/api/v1/servers; 64 url http://127.0.0.1:8085/api/v1/servers;
62 # the id of devide. 65 # the id of devide.
63 device_id "my-srs-device"; 66 device_id "my-srs-device";
64 - # the index of device ip.  
65 - # we may retrieve more than one network device.  
66 - # default: 0  
67 - device_index 0;  
68 # whether report with summaries 67 # whether report with summaries
69 # if true, put /api/v1/summaries to the request data: 68 # if true, put /api/v1/summaries to the request data:
70 # { 69 # {
@@ -115,6 +114,19 @@ http_stream { @@ -115,6 +114,19 @@ http_stream {
115 dir ./objs/nginx/html; 114 dir ./objs/nginx/html;
116 } 115 }
117 116
  117 +# system statistics section.
  118 +# the main cycle will retrieve the system stat,
  119 +# for example, the cpu/mem/network/disk-io data,
  120 +# the http api, for instance, /api/v1/summaries will show these data.
  121 +# @remark the heartbeat depends on the network_device_index,
  122 +# for example, the eth0 maybe the device which index is 0.
  123 +stats {
  124 + # the index of device ip.
  125 + # we may retrieve more than one network device.
  126 + # default: 0
  127 + network_device_index 0;
  128 +}
  129 +
118 ############################################################################################# 130 #############################################################################################
119 # RTMP/HTTP VHOST sections 131 # RTMP/HTTP VHOST sections
120 ############################################################################################# 132 #############################################################################################
@@ -8,6 +8,10 @@ heartbeat { @@ -8,6 +8,10 @@ heartbeat {
8 interval 9.3; 8 interval 9.3;
9 url http://127.0.0.1:8085/api/v1/servers; 9 url http://127.0.0.1:8085/api/v1/servers;
10 device_id "my-srs-device"; 10 device_id "my-srs-device";
  11 + summaries on;
  12 +}
  13 +stats {
  14 + network_device_index 0;
11 } 15 }
12 vhost __defaultVhost__ { 16 vhost __defaultVhost__ {
13 } 17 }
@@ -1202,7 +1202,7 @@ int SrsConfig::check_config() @@ -1202,7 +1202,7 @@ int SrsConfig::check_config()
1202 if (n != "listen" && n != "pid" && n != "chunk_size" && n != "ff_log_dir" 1202 if (n != "listen" && n != "pid" && n != "chunk_size" && n != "ff_log_dir"
1203 && n != "srs_log_tank" && n != "srs_log_level" && n != "srs_log_file" 1203 && n != "srs_log_tank" && n != "srs_log_level" && n != "srs_log_file"
1204 && n != "max_connections" && n != "daemon" && n != "heartbeat" 1204 && n != "max_connections" && n != "daemon" && n != "heartbeat"
1205 - && n != "http_api" && n != "http_stream" && n != "vhost" 1205 + && n != "http_api" && n != "http_stream" && n != "stats" && n != "vhost"
1206 && n != "pithy_print") 1206 && n != "pithy_print")
1207 { 1207 {
1208 ret = ERROR_SYSTEM_CONFIG_INVALID; 1208 ret = ERROR_SYSTEM_CONFIG_INVALID;
@@ -1237,7 +1237,7 @@ int SrsConfig::check_config() @@ -1237,7 +1237,7 @@ int SrsConfig::check_config()
1237 for (int i = 0; conf && i < (int)conf->directives.size(); i++) { 1237 for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
1238 string n = conf->at(i)->name; 1238 string n = conf->at(i)->name;
1239 if (n != "enabled" && n != "interval" && n != "url" 1239 if (n != "enabled" && n != "interval" && n != "url"
1240 - && n != "device_id" && n != "device_index" && n != "summaries" 1240 + && n != "device_id" && n != "summaries"
1241 ) { 1241 ) {
1242 ret = ERROR_SYSTEM_CONFIG_INVALID; 1242 ret = ERROR_SYSTEM_CONFIG_INVALID;
1243 srs_error("unsupported heartbeat directive %s, ret=%d", n.c_str(), ret); 1243 srs_error("unsupported heartbeat directive %s, ret=%d", n.c_str(), ret);
@@ -1246,6 +1246,17 @@ int SrsConfig::check_config() @@ -1246,6 +1246,17 @@ int SrsConfig::check_config()
1246 } 1246 }
1247 } 1247 }
1248 if (true) { 1248 if (true) {
  1249 + SrsConfDirective* conf = get_stats();
  1250 + for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
  1251 + string n = conf->at(i)->name;
  1252 + if (n != "network_device_index") {
  1253 + ret = ERROR_SYSTEM_CONFIG_INVALID;
  1254 + srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret);
  1255 + return ret;
  1256 + }
  1257 + }
  1258 + }
  1259 + if (true) {
1249 SrsConfDirective* conf = get_pithy_print(); 1260 SrsConfDirective* conf = get_pithy_print();
1250 for (int i = 0; conf && i < (int)conf->directives.size(); i++) { 1261 for (int i = 0; conf && i < (int)conf->directives.size(); i++) {
1251 string n = conf->at(i)->name; 1262 string n = conf->at(i)->name;
@@ -1427,10 +1438,14 @@ int SrsConfig::check_config() @@ -1427,10 +1438,14 @@ int SrsConfig::check_config()
1427 get_heartbeat_interval(), ret); 1438 get_heartbeat_interval(), ret);
1428 return ret; 1439 return ret;
1429 } 1440 }
1430 - if (get_heartbeat_device_index() < 0) { 1441 +
  1442 + ////////////////////////////////////////////////////////////////////////
  1443 + // check stats
  1444 + ////////////////////////////////////////////////////////////////////////
  1445 + if (get_stats_network_device_index() < 0) {
1431 ret = ERROR_SYSTEM_CONFIG_INVALID; 1446 ret = ERROR_SYSTEM_CONFIG_INVALID;
1432 - srs_error("directive heartbeat device_index invalid, device_index=%d, ret=%d",  
1433 - get_heartbeat_device_index(), ret); 1447 + srs_error("directive stats network_device_index invalid, network_device_index=%d, ret=%d",
  1448 + get_stats_network_device_index(), ret);
1434 return ret; 1449 return ret;
1435 } 1450 }
1436 1451
@@ -3142,36 +3157,41 @@ string SrsConfig::get_heartbeat_device_id() @@ -3142,36 +3157,41 @@ string SrsConfig::get_heartbeat_device_id()
3142 return conf->arg0(); 3157 return conf->arg0();
3143 } 3158 }
3144 3159
3145 -int SrsConfig::get_heartbeat_device_index() 3160 +bool SrsConfig::get_heartbeat_summaries()
3146 { 3161 {
3147 SrsConfDirective* conf = get_heartbeart(); 3162 SrsConfDirective* conf = get_heartbeart();
3148 3163
3149 if (!conf) { 3164 if (!conf) {
3150 - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX; 3165 + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES;
3151 } 3166 }
3152 3167
3153 - conf = conf->get("device_index");  
3154 - if (!conf || conf->arg0().empty()) {  
3155 - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX; 3168 + conf = conf->get("summaries");
  3169 + if (!conf || conf->arg0() != "on") {
  3170 + return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES;
3156 } 3171 }
3157 3172
3158 - return ::atoi(conf->arg0().c_str()); 3173 + return true;
3159 } 3174 }
3160 3175
3161 -bool SrsConfig::get_heartbeat_summaries() 3176 +SrsConfDirective* SrsConfig::get_stats()
3162 { 3177 {
3163 - SrsConfDirective* conf = get_heartbeart(); 3178 + return root->get("stats");
  3179 +}
  3180 +
  3181 +int SrsConfig::get_stats_network_device_index()
  3182 +{
  3183 + SrsConfDirective* conf = get_stats();
3164 3184
3165 if (!conf) { 3185 if (!conf) {
3166 - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES; 3186 + return SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX;
3167 } 3187 }
3168 3188
3169 - conf = conf->get("summaries");  
3170 - if (!conf || conf->arg0() != "on") {  
3171 - return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES; 3189 + conf = conf->get("network_device_index");
  3190 + if (!conf || conf->arg0().empty()) {
  3191 + return SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX;
3172 } 3192 }
3173 3193
3174 - return true; 3194 + return ::atoi(conf->arg0().c_str());
3175 } 3195 }
3176 3196
3177 namespace _srs_internal 3197 namespace _srs_internal
@@ -74,9 +74,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -74,9 +74,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED false 74 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_ENABLED false
75 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL 9.9 75 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INTERVAL 9.9
76 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL "http://"SRS_CONSTS_LOCALHOST":8085/api/v1/servers" 76 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL "http://"SRS_CONSTS_LOCALHOST":8085/api/v1/servers"
77 -#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX 0  
78 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES false 77 #define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES false
79 78
  79 +#define SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX 0
  80 +
80 #define SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS 10000 81 #define SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS 10000
81 #define SRS_CONF_DEFAULT_STAGE_PUBLISH_USER_INTERVAL_MS 10000 82 #define SRS_CONF_DEFAULT_STAGE_PUBLISH_USER_INTERVAL_MS 10000
82 #define SRS_CONF_DEFAULT_STAGE_FORWARDER_INTERVAL_MS 10000 83 #define SRS_CONF_DEFAULT_STAGE_FORWARDER_INTERVAL_MS 10000
@@ -926,14 +927,22 @@ public: @@ -926,14 +927,22 @@ public:
926 */ 927 */
927 virtual std::string get_heartbeat_device_id(); 928 virtual std::string get_heartbeat_device_id();
928 /** 929 /**
929 - * get the network device index, to report to server.  
930 - * for example, 0 means the eth0 maybe.  
931 - */  
932 - virtual int get_heartbeat_device_index();  
933 - /**  
934 * whether report with summaries of http api: /api/v1/summaries. 930 * whether report with summaries of http api: /api/v1/summaries.
935 */ 931 */
936 virtual bool get_heartbeat_summaries(); 932 virtual bool get_heartbeat_summaries();
  933 +// stats section
  934 +private:
  935 + /**
  936 + * get the stats directive.
  937 + */
  938 + virtual SrsConfDirective* get_stats();
  939 +public:
  940 + /**
  941 + * get the network device index, used to retrieve the ip of device,
  942 + * for heartbeat to report to server, or to get the local ip.
  943 + * for example, 0 means the eth0 maybe.
  944 + */
  945 + virtual int get_stats_network_device_index();
937 }; 946 };
938 947
939 namespace _srs_internal 948 namespace _srs_internal
@@ -61,7 +61,7 @@ void SrsHttpHeartbeat::heartbeat() @@ -61,7 +61,7 @@ void SrsHttpHeartbeat::heartbeat()
61 61
62 vector<string>& ips = srs_get_local_ipv4_ips(); 62 vector<string>& ips = srs_get_local_ipv4_ips();
63 if (!ips.empty()) { 63 if (!ips.empty()) {
64 - ip = ips[_srs_config->get_heartbeat_device_index() % (int)ips.size()]; 64 + ip = ips[_srs_config->get_stats_network_device_index() % (int)ips.size()];
65 } 65 }
66 66
67 std::stringstream ss; 67 std::stringstream ss;
@@ -367,7 +367,7 @@ SrsDiskStat* srs_get_disk_stat() @@ -367,7 +367,7 @@ SrsDiskStat* srs_get_disk_stat()
367 return &_srs_disk_stat; 367 return &_srs_disk_stat;
368 } 368 }
369 369
370 -bool srs_get_disk_stat(SrsDiskStat& r) 370 +bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
371 { 371 {
372 FILE* f = fopen("/proc/vmstat", "r"); 372 FILE* f = fopen("/proc/vmstat", "r");
373 if (f == NULL) { 373 if (f == NULL) {
@@ -401,10 +401,15 @@ bool srs_get_disk_stat(SrsDiskStat& r) @@ -401,10 +401,15 @@ bool srs_get_disk_stat(SrsDiskStat& r)
401 return true; 401 return true;
402 } 402 }
403 403
  404 +bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
  405 +{
  406 + return true;
  407 +}
  408 +
404 void srs_update_disk_stat() 409 void srs_update_disk_stat()
405 { 410 {
406 SrsDiskStat r; 411 SrsDiskStat r;
407 - if (!srs_get_disk_stat(r)) { 412 + if (!srs_get_disk_vmstat_stat(r)) {
408 return; 413 return;
409 } 414 }
410 415
@@ -348,6 +348,8 @@ public: @@ -348,6 +348,8 @@ public:
348 // the out(write) page count, pgpgout*1024 is the write bytes. 348 // the out(write) page count, pgpgout*1024 is the write bytes.
349 // Total number of kilobytes the system paged out to disk per second. 349 // Total number of kilobytes the system paged out to disk per second.
350 unsigned long pgpgout; 350 unsigned long pgpgout;
  351 +
  352 + // @see: https://www.kernel.org/doc/Documentation/iostats.txt
351 353
352 public: 354 public:
353 SrsDiskStat(); 355 SrsDiskStat();
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "174" 34 +#define VERSION_REVISION "175"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"
@@ -96,10 +96,6 @@ std::string __full_conf = "" @@ -96,10 +96,6 @@ std::string __full_conf = ""
96 " url http://127.0.0.1:8085/api/v1/servers; \n" 96 " url http://127.0.0.1:8085/api/v1/servers; \n"
97 " # the id of devide. \n" 97 " # the id of devide. \n"
98 " device_id \"my-srs-device\"; \n" 98 " device_id \"my-srs-device\"; \n"
99 - " # the index of device ip. \n"  
100 - " # we may retrieve more than one network device. \n"  
101 - " # default: 0 \n"  
102 - " device_index 0; \n"  
103 " # whether report with summaries \n" 99 " # whether report with summaries \n"
104 " # if true, put /api/v1/summaries to the request data: \n" 100 " # if true, put /api/v1/summaries to the request data: \n"
105 " # { \n" 101 " # { \n"
@@ -149,6 +145,18 @@ std::string __full_conf = "" @@ -149,6 +145,18 @@ std::string __full_conf = ""
149 " # default: ./objs/nginx/html \n" 145 " # default: ./objs/nginx/html \n"
150 " dir ./objs/nginx/html; \n" 146 " dir ./objs/nginx/html; \n"
151 "} \n" 147 "} \n"
  148 + "# system statistics section. \n"
  149 + "# the main cycle will retrieve the system stat, \n"
  150 + "# for example, the cpu/mem/network/disk-io data, \n"
  151 + "# the http api, for instance, /api/v1/summaries will show these data. \n"
  152 + "# @remark the heartbeat depends on the network_device_index, \n"
  153 + "# for example, the eth0 maybe the device which index is 0. \n"
  154 + "stats { \n"
  155 + " # the index of device ip. \n"
  156 + " # we may retrieve more than one network device. \n"
  157 + " # default: 0 \n"
  158 + " network_device_index 0; \n"
  159 + "} \n"
152 " \n" 160 " \n"
153 "############################################################################################# \n" 161 "############################################################################################# \n"
154 "# RTMP/HTTP VHOST sections \n" 162 "# RTMP/HTTP VHOST sections \n"
@@ -1132,10 +1140,10 @@ VOID TEST(ConfigTest, CheckMacros) @@ -1132,10 +1140,10 @@ VOID TEST(ConfigTest, CheckMacros)
1132 #ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL 1140 #ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_URL
1133 EXPECT_TRUE(false); 1141 EXPECT_TRUE(false);
1134 #endif 1142 #endif
1135 -#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX 1143 +#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES
1136 EXPECT_TRUE(false); 1144 EXPECT_TRUE(false);
1137 #endif 1145 #endif
1138 -#ifndef SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES 1146 +#ifndef SRS_CONF_DEFAULT_STATS_NETWORK_DEVICE_INDEX
1139 EXPECT_TRUE(false); 1147 EXPECT_TRUE(false);
1140 #endif 1148 #endif
1141 #ifndef SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS 1149 #ifndef SRS_CONF_DEFAULT_STAGE_PLAY_USER_INTERVAL_MS
@@ -1831,7 +1839,7 @@ VOID TEST(ConfigMainTest, ParseFullConf) @@ -1831,7 +1839,7 @@ VOID TEST(ConfigMainTest, ParseFullConf)
1831 EXPECT_EQ(9300, conf.get_heartbeat_interval()); 1839 EXPECT_EQ(9300, conf.get_heartbeat_interval());
1832 EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str()); 1840 EXPECT_STREQ("http://127.0.0.1:8085/api/v1/servers", conf.get_heartbeat_url().c_str());
1833 EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str()); 1841 EXPECT_STREQ("my-srs-device", conf.get_heartbeat_device_id().c_str());
1834 - EXPECT_EQ(0, conf.get_heartbeat_device_index()); 1842 + EXPECT_EQ(0, conf.get_stats_network_device_index());
1835 EXPECT_FALSE(conf.get_heartbeat_summaries()); 1843 EXPECT_FALSE(conf.get_heartbeat_summaries());
1836 1844
1837 EXPECT_TRUE(conf.get_http_api_enabled()); 1845 EXPECT_TRUE(conf.get_http_api_enabled());