-
Notifications
You must be signed in to change notification settings - Fork 876
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to write capture stats to a file.
With -s, periodically fetch capture stats from the inspector and write them to the provided file. Separate class StatsFileWriter handles the details. It does rely on a timer + SIGALRM handler so you can only practically create a single object, but it does keep the code/state separate. The output format has a sample number, the set of current stats, a delta with the difference from the prior sample, and the percentage of events dropped during that sample.
- Loading branch information
Showing
4 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <sys/time.h> | ||
#include <signal.h> | ||
|
||
#include "statsfilewriter.h" | ||
|
||
using namespace std; | ||
|
||
static bool g_save_stats = false; | ||
static void timer_handler (int signum) | ||
{ | ||
g_save_stats = true; | ||
} | ||
|
||
StatsFileWriter::StatsFileWriter() | ||
: m_num_stats(0), m_inspector(NULL) | ||
{ | ||
} | ||
|
||
StatsFileWriter::~StatsFileWriter() | ||
{ | ||
m_output.close(); | ||
} | ||
|
||
bool StatsFileWriter::init(sinsp *inspector, string &filename, uint32_t interval_sec, string &errstr) | ||
{ | ||
struct itimerval timer; | ||
struct sigaction handler; | ||
|
||
m_inspector = inspector; | ||
|
||
m_output.exceptions ( ofstream::failbit | ofstream::badbit ); | ||
m_output.open(filename, ios_base::app); | ||
|
||
memset (&handler, 0, sizeof (handler)); | ||
handler.sa_handler = &timer_handler; | ||
if (sigaction(SIGALRM, &handler, NULL) == -1) | ||
{ | ||
errstr = string("Could not set up signal handler for periodic timer: ") + strerror(errno); | ||
return false; | ||
} | ||
|
||
timer.it_value.tv_sec = interval_sec; | ||
timer.it_value.tv_usec = 0; | ||
timer.it_interval = timer.it_value; | ||
if (setitimer(ITIMER_REAL, &timer, NULL) == -1) | ||
{ | ||
errstr = string("Could not set up periodic timer: ") + strerror(errno); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void StatsFileWriter::handle() | ||
{ | ||
if (g_save_stats) | ||
{ | ||
scap_stats cstats; | ||
scap_stats delta; | ||
|
||
g_save_stats = false; | ||
m_num_stats++; | ||
m_inspector->get_capture_stats(&cstats); | ||
|
||
if(m_num_stats == 1) | ||
{ | ||
delta = cstats; | ||
} | ||
else | ||
{ | ||
delta.n_evts = cstats.n_evts - m_last_stats.n_evts; | ||
delta.n_drops = cstats.n_drops - m_last_stats.n_drops; | ||
delta.n_preemptions = cstats.n_preemptions - m_last_stats.n_preemptions; | ||
} | ||
|
||
m_output << "{\"sample\": " << m_num_stats << | ||
", \"cur\": {" << | ||
"\"events\": " << cstats.n_evts << | ||
", \"drops\": " << cstats.n_drops << | ||
", \"preemptions\": " << cstats.n_preemptions << | ||
"}, \"delta\": {" << | ||
"\"events\": " << delta.n_evts << | ||
", \"drops\": " << delta.n_drops << | ||
", \"preemptions\": " << delta.n_preemptions << | ||
"}, \"drop_pct\": " << (delta.n_evts == 0 ? 0 : (100.0*delta.n_drops/delta.n_evts)) << | ||
"}," << endl; | ||
|
||
m_last_stats = cstats; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <fstream> | ||
|
||
#include <sinsp.h> | ||
|
||
// Periodically collects scap stats files and writes them to a file as | ||
// json. | ||
|
||
class StatsFileWriter { | ||
public: | ||
StatsFileWriter(); | ||
virtual ~StatsFileWriter(); | ||
|
||
// Returns success as bool. On false fills in errstr. | ||
bool init(sinsp *inspector, std::string &filename, | ||
uint32_t interval_sec, | ||
string &errstr); | ||
|
||
// Should be called often (like for each event in a sinsp | ||
// loop). | ||
void handle(); | ||
|
||
protected: | ||
uint32_t m_num_stats; | ||
sinsp *m_inspector; | ||
std::ofstream m_output; | ||
|
||
scap_stats m_last_stats; | ||
}; |