1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

add config item for the stat disk device name

This commit is contained in:
winlin 2014-07-27 16:52:00 +08:00
parent 7c1dd97513
commit b334021836
5 changed files with 73 additions and 11 deletions

View file

@ -88,6 +88,9 @@ stats {
# we may retrieve more than one network device.
# default: 0
network_device_index 0;
# the device name to stat the disk iops.
# ignore the device of /proc/diskstats if not configed.
disk_device_name sda sdb xvda xvdb;
}
#############################################################################################

View file

@ -1249,7 +1249,7 @@ int SrsConfig::check_config()
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") {
if (n != "network_device_index" && n != "disk_device_name") {
ret = ERROR_SYSTEM_CONFIG_INVALID;
srs_error("unsupported stats directive %s, ret=%d", n.c_str(), ret);
return ret;
@ -3194,6 +3194,22 @@ int SrsConfig::get_stats_network_device_index()
return ::atoi(conf->arg0().c_str());
}
SrsConfDirective* SrsConfig::get_stats_disk_device()
{
SrsConfDirective* conf = get_stats();
if (!conf) {
return NULL;
}
conf = conf->get("disk_device_name");
if (!conf || conf->args.size() == 0) {
return NULL;
}
return conf;
}
namespace _srs_internal
{
SrsConfigBuffer::SrsConfigBuffer()

View file

@ -943,6 +943,12 @@ public:
* for example, 0 means the eth0 maybe.
*/
virtual int get_stats_network_device_index();
/**
* get the disk stat device name list.
* the device name configed in args of directive.
* @return the disk device name to stat. NULL if not configed.
*/
virtual SrsConfDirective* get_stats_disk_device();
};
namespace _srs_internal

View file

@ -415,15 +415,22 @@ bool srs_get_disk_vmstat_stat(SrsDiskStat& r)
bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
{
r.ok = false;
r.sample_time = srs_get_system_time_ms();
// if disabled, ignore all devices.
SrsConfDirective* conf = _srs_config->get_stats_disk_device();
if (conf == NULL) {
r.ok = true;
return true;
}
FILE* f = fopen("/proc/diskstats", "r");
if (f == NULL) {
srs_warn("open vmstat failed, ignore");
return false;
}
r.ok = false;
r.sample_time = srs_get_system_time_ms();
static char buf[1024];
while (fgets(buf, sizeof(buf), f)) {
unsigned int major = 0;
@ -448,8 +455,13 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
&wr_sectors, &wr_ticks, &nb_current, &ticks, &aveq);
srs_assert(ret == 14);
// TODO: FIMXE: config it.
if (strcmp("sda", name) == 0) {
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;
}
r.rd_ios += rd_ios;
r.rd_merges += rd_merges;
r.rd_sectors += rd_sectors;
@ -461,6 +473,8 @@ bool srs_get_disk_diskstats_stat(SrsDiskStat& r)
r.nb_current += nb_current;
r.ticks += ticks;
r.aveq += aveq;
break;
}
}
@ -477,9 +491,18 @@ void srs_update_disk_stat()
if (!srs_get_disk_vmstat_stat(r)) {
return;
}
if (!srs_get_disk_diskstats_stat(r)) {
return;
}
SrsDiskStat& o = _srs_disk_stat;
if (o.ok) {
if (!o.ok) {
_srs_disk_stat = r;
return;
}
// vmstat
if (true) {
int64_t duration_ms = r.sample_time - o.sample_time;
if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) {
@ -492,8 +515,6 @@ void srs_update_disk_stat()
r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms;
}
}
_srs_disk_stat = r;
}
SrsMemInfo::SrsMemInfo()

View file

@ -156,6 +156,9 @@ std::string __full_conf = ""
" # we may retrieve more than one network device. \n"
" # default: 0 \n"
" network_device_index 0; \n"
" # the device name to stat the disk iops. \n"
" # ignore the device of /proc/diskstats if not configed. \n"
" disk_device_name sda sdb xvda xvdb; \n"
"} \n"
" \n"
"############################################################################################# \n"
@ -1839,9 +1842,12 @@ 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_stats_network_device_index());
EXPECT_FALSE(conf.get_heartbeat_summaries());
EXPECT_EQ(0, conf.get_stats_network_device_index());
ASSERT_TRUE(conf.get_stats_disk_device() != NULL);
EXPECT_EQ(4, (int)conf.get_stats_disk_device()->args.size());
EXPECT_TRUE(conf.get_http_api_enabled());
EXPECT_EQ(1985, conf.get_http_api_listen());
@ -4633,6 +4639,16 @@ VOID TEST(ConfigMainTest, CheckConf_stats)
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{network_device_index -1;}"));
}
if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS == conf.parse(_MIN_OK_CONF"stats{disk_device_name sda;}"));
}
if (true) {
MockSrsConfig conf;
EXPECT_TRUE(ERROR_SUCCESS != conf.parse(_MIN_OK_CONF"stats{disk_device_names sda;}"));
}
}
VOID TEST(ConfigMainTest, CheckConf_http_stream)