winlin

support report summaries in heartbeat. 0.9.132

... ... @@ -241,6 +241,7 @@ Supported operating systems and hardware:
* 2013-10-17, Created.<br/>
## History
* v1.0, 2014-06-23, support report summaries in heartbeat. 0.9.132
* v1.0, 2014-06-22, performance refine, support [3k+](https://github.com/winlinvip/simple-rtmp-server/wiki/Performance#%E6%80%A7%E8%83%BD%E4%BE%8B%E8%A1%8C%E6%8A%A5%E5%91%8A4k) connections(270kbps). 0.9.130
* v1.0, 2014-06-21, support edge [token traverse](https://github.com/winlinvip/simple-rtmp-server/wiki/DRM#tokentraverse), fix [#104](https://github.com/winlinvip/simple-rtmp-server/issues/104). 0.9.129
* v1.0, 2014-06-19, add connections count to api summaries. 0.9.127
... ...
... ... @@ -61,6 +61,17 @@ 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:
# {
# "summaries": summaries object.
# }
# default: off
summaries off;
}
#############################################################################################
... ...
... ... @@ -2869,6 +2869,38 @@ 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();
if (!conf) {
return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES;
}
conf = conf->get("summaries");
if (!conf || conf->arg0() != "on") {
return SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES;
}
return true;
}
bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
{
// both NULL, equal.
... ...
... ... @@ -71,6 +71,8 @@ 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://127.0.0.1:8085/api/v1/servers"
#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_INDEX 0
#define SRS_CONF_DEFAULT_HTTP_HEAETBEAT_SUMMARIES false
#define SRS_STAGE_PLAY_USER_INTERVAL_MS 10000
#define SRS_STAGE_PUBLISH_USER_INTERVAL_MS 10000
... ... @@ -297,6 +299,8 @@ public:
virtual int64_t get_heartbeat_interval();
virtual std::string get_heartbeat_url();
virtual std::string get_heartbeat_device_id();
virtual int get_heartbeat_device_index();
virtual bool get_heartbeat_summaries();
};
/**
... ...
... ... @@ -61,26 +61,30 @@ void SrsHttpHeartbeat::heartbeat()
vector<string>& ips = srs_get_local_ipv4_ips();
if (!ips.empty()) {
ip = ips[0]; // TODO: FIXME: maybe need to config it.
ip = ips[_srs_config->get_heartbeat_device_index() % (int)ips.size()];
}
std::stringstream ss;
ss << JOBJECT_START
<< JFIELD_STR("device_id", device_id) << JFIELD_CONT
<< JFIELD_STR("ip", ip)
<< JOBJECT_END;
<< JFIELD_STR("ip", ip);
if (_srs_config->get_heartbeat_summaries()) {
ss << JFIELD_CONT << JFIELD_ORG("summaries", "");
srs_api_dump_summaries(ss);
}
ss << JOBJECT_END;
std::string data = ss.str();
std::string res;
SrsHttpClient http;
if ((ret = http.post(&uri, data, res)) != ERROR_SUCCESS) {
srs_error("http post hartbeart uri failed. "
srs_info("http post hartbeart uri failed. "
"url=%s, request=%s, response=%s, ret=%d",
url.c_str(), data.c_str(), res.c_str(), ret);
return;
}
srs_trace("http hook hartbeart success. "
srs_info("http hook hartbeart success. "
"url=%s, request=%s, response=%s, ret=%d",
url.c_str(), data.c_str(), res.c_str(), ret);
... ...
... ... @@ -382,96 +382,7 @@ bool SrsApiSummaries::can_handle(const char* path, int length, const char** /*pc
int SrsApiSummaries::do_process_request(SrsSocket* skt, SrsHttpMessage* req)
{
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();
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;
bool n_ok = false;
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;
}
n_ok = true;
nr_bytes += o.rbytes;
ns_bytes += o.sbytes;
n_sample_time = o.sample_time;
}
ss << JOBJECT_START
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
<< JFIELD_ORG("data", JOBJECT_START)
<< JFIELD_ORG("rusage_ok", (r->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("cpuinfo_ok", (c->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("meminfo_ok", (m->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("platform_ok", (p->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("network_ok", (n_ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("network_srs_ok", (nrs->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("now_ms", now) << JFIELD_CONT
<< JFIELD_ORG("self", JOBJECT_START)
<< JFIELD_ORG("pid", getpid()) << JFIELD_CONT
<< JFIELD_ORG("ppid", u->ppid) << JFIELD_CONT
<< JFIELD_STR("argv", _srs_config->argv()) << JFIELD_CONT
<< JFIELD_STR("cwd", _srs_config->cwd()) << JFIELD_CONT
<< JFIELD_ORG("mem_kbyte", r->r.ru_maxrss) << JFIELD_CONT
<< JFIELD_ORG("mem_percent", self_mem_percent) << JFIELD_CONT
<< JFIELD_ORG("cpu_percent", u->percent) << JFIELD_CONT
<< JFIELD_ORG("srs_uptime", srs_uptime)
<< JOBJECT_END << JFIELD_CONT
<< JFIELD_ORG("system", JOBJECT_START)
<< JFIELD_ORG("cpu_percent", s->percent) << JFIELD_CONT
<< JFIELD_ORG("mem_ram_kbyte", m->MemTotal) << JFIELD_CONT
<< JFIELD_ORG("mem_ram_percent", m->percent_ram) << JFIELD_CONT
<< JFIELD_ORG("mem_swap_kbyte", m->SwapTotal) << JFIELD_CONT
<< JFIELD_ORG("mem_swap_percent", m->percent_swap) << JFIELD_CONT
<< JFIELD_ORG("cpus", c->nb_processors) << JFIELD_CONT
<< JFIELD_ORG("cpus_online", c->nb_processors_online) << JFIELD_CONT
<< JFIELD_ORG("uptime", p->os_uptime) << JFIELD_CONT
<< JFIELD_ORG("ilde_time", p->os_ilde_time) << JFIELD_CONT
<< JFIELD_ORG("load_1m", p->load_one_minutes) << JFIELD_CONT
<< JFIELD_ORG("load_5m", p->load_five_minutes) << JFIELD_CONT
<< JFIELD_ORG("load_15m", p->load_fifteen_minutes) << JFIELD_CONT
<< JFIELD_ORG("net_sample_time", n_sample_time) << JFIELD_CONT
<< JFIELD_ORG("net_recv_bytes", nr_bytes) << JFIELD_CONT
<< JFIELD_ORG("net_send_bytes", ns_bytes) << JFIELD_CONT
<< JFIELD_ORG("srs_sample_time", nrs->sample_time) << JFIELD_CONT
<< JFIELD_ORG("srs_recv_bytes", nrs->rbytes) << JFIELD_CONT
<< JFIELD_ORG("srs_recv_kbps", nrs->rkbps) << JFIELD_CONT
<< JFIELD_ORG("srs_send_bytes", nrs->sbytes) << JFIELD_CONT
<< JFIELD_ORG("srs_send_kbps", nrs->skbps) << JFIELD_CONT
<< JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_ls", nrs->nb_conn_sys_ls) << JFIELD_CONT
<< JFIELD_ORG("conn_srs", nrs->nb_conn_srs)
<< JOBJECT_END
<< JOBJECT_END
<< JOBJECT_END;
srs_api_dump_summaries(ss);
return res_json(skt, req, ss.str());
}
... ...
... ... @@ -64,7 +64,7 @@ int SrsHttpClient::post(SrsHttpUri* uri, string req, string& res)
}
if ((ret = connect(uri)) != ERROR_SUCCESS) {
srs_error("http connect server failed. ret=%d", ret);
srs_warn("http connect server failed. ret=%d", ret);
return ret;
}
... ...
... ... @@ -35,6 +35,7 @@ using namespace std;
#include <srs_kernel_utility.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_kbps.hpp>
#include <srs_app_json.hpp>
#define SRS_LOCAL_LOOP_IP "127.0.0.1"
... ... @@ -731,3 +732,96 @@ string srs_get_peer_ip(int fd)
return ip;
}
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();
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;
bool n_ok = false;
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;
}
n_ok = true;
nr_bytes += o.rbytes;
ns_bytes += o.sbytes;
n_sample_time = o.sample_time;
}
ss << JOBJECT_START
<< JFIELD_ERROR(ERROR_SUCCESS) << JFIELD_CONT
<< JFIELD_ORG("data", JOBJECT_START)
<< JFIELD_ORG("rusage_ok", (r->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("self_cpu_stat_ok", (u->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("system_cpu_stat_ok", (s->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("cpuinfo_ok", (c->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("meminfo_ok", (m->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("platform_ok", (p->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("network_ok", (n_ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("network_srs_ok", (nrs->ok? "true":"false")) << JFIELD_CONT
<< JFIELD_ORG("now_ms", now) << JFIELD_CONT
<< JFIELD_ORG("self", JOBJECT_START)
<< JFIELD_STR("version", RTMP_SIG_SRS_VERSION) << JFIELD_CONT
<< JFIELD_ORG("pid", getpid()) << JFIELD_CONT
<< JFIELD_ORG("ppid", u->ppid) << JFIELD_CONT
<< JFIELD_STR("argv", _srs_config->argv()) << JFIELD_CONT
<< JFIELD_STR("cwd", _srs_config->cwd()) << JFIELD_CONT
<< JFIELD_ORG("mem_kbyte", r->r.ru_maxrss) << JFIELD_CONT
<< JFIELD_ORG("mem_percent", self_mem_percent) << JFIELD_CONT
<< JFIELD_ORG("cpu_percent", u->percent) << JFIELD_CONT
<< JFIELD_ORG("srs_uptime", srs_uptime)
<< JOBJECT_END << JFIELD_CONT
<< JFIELD_ORG("system", JOBJECT_START)
<< JFIELD_ORG("cpu_percent", s->percent) << JFIELD_CONT
<< JFIELD_ORG("mem_ram_kbyte", m->MemTotal) << JFIELD_CONT
<< JFIELD_ORG("mem_ram_percent", m->percent_ram) << JFIELD_CONT
<< JFIELD_ORG("mem_swap_kbyte", m->SwapTotal) << JFIELD_CONT
<< JFIELD_ORG("mem_swap_percent", m->percent_swap) << JFIELD_CONT
<< JFIELD_ORG("cpus", c->nb_processors) << JFIELD_CONT
<< JFIELD_ORG("cpus_online", c->nb_processors_online) << JFIELD_CONT
<< JFIELD_ORG("uptime", p->os_uptime) << JFIELD_CONT
<< JFIELD_ORG("ilde_time", p->os_ilde_time) << JFIELD_CONT
<< JFIELD_ORG("load_1m", p->load_one_minutes) << JFIELD_CONT
<< JFIELD_ORG("load_5m", p->load_five_minutes) << JFIELD_CONT
<< JFIELD_ORG("load_15m", p->load_fifteen_minutes) << JFIELD_CONT
<< JFIELD_ORG("net_sample_time", n_sample_time) << JFIELD_CONT
<< JFIELD_ORG("net_recv_bytes", nr_bytes) << JFIELD_CONT
<< JFIELD_ORG("net_send_bytes", ns_bytes) << JFIELD_CONT
<< JFIELD_ORG("srs_sample_time", nrs->sample_time) << JFIELD_CONT
<< JFIELD_ORG("srs_recv_bytes", nrs->rbytes) << JFIELD_CONT
<< JFIELD_ORG("srs_recv_kbps", nrs->rkbps) << JFIELD_CONT
<< JFIELD_ORG("srs_send_bytes", nrs->sbytes) << JFIELD_CONT
<< JFIELD_ORG("srs_send_kbps", nrs->skbps) << JFIELD_CONT
<< JFIELD_ORG("conn_sys", nrs->nb_conn_sys) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_et", nrs->nb_conn_sys_et) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_tw", nrs->nb_conn_sys_tw) << JFIELD_CONT
<< JFIELD_ORG("conn_sys_ls", nrs->nb_conn_sys_ls) << JFIELD_CONT
<< JFIELD_ORG("conn_srs", nrs->nb_conn_srs)
<< JOBJECT_END
<< JOBJECT_END
<< JOBJECT_END;
}
... ...
... ... @@ -32,6 +32,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <vector>
#include <string>
#include <sstream>
#include <sys/resource.h>
... ... @@ -439,4 +440,6 @@ std::string srs_get_local_ip(int fd);
// where peer ip is the client public ip which connected to server.
std::string srs_get_peer_ip(int fd);
void srs_api_dump_summaries(std::stringstream& ss);
#endif
... ...
... ... @@ -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 "131"
#define VERSION_REVISION "132"
#define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
// server info.
#define RTMP_SIG_SRS_KEY "SRS"
... ...