sharkd: output JSON for new_stat_ API.

Change-Id: I5b4b020029f3c1587f96ca1fcf5fbfa2ee78ac31
Reviewed-on: https://code.wireshark.org/review/21628
Petri-Dish: Jakub Zawadzki <darkjames-ws@darkjames.pl>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
Reviewed-by: Jakub Zawadzki <darkjames-ws@darkjames.pl>
This commit is contained in:
Jakub Zawadzki 2017-05-13 23:52:50 +02:00
parent c30bb2c75e
commit 60c8ada4c2
4 changed files with 166 additions and 0 deletions

View File

@ -875,6 +875,7 @@ libwireshark.so.0 libwireshark0 #MINVER#
new_frame_data_sequence@Base 1.12.0~rc1
new_page@Base 1.12.0~rc1
new_stat_tap_add_table@Base 1.99.8
new_stat_tap_by_name@@Base 2.3.0
new_stat_tap_get_field_data@Base 1.99.8
new_stat_tap_get_filter@Base 1.99.8
new_stat_tap_init_table@Base 1.99.8

View File

@ -155,6 +155,11 @@ void register_stat_tap_table_ui(stat_tap_table_ui *ui)
wmem_tree_insert_string(registered_stat_tables, ui->cli_string, ui, 0);
}
stat_tap_table_ui *new_stat_tap_by_name(const char *name)
{
return (stat_tap_table_ui *) wmem_tree_lookup_string(registered_stat_tables, name, 0);
}
void new_stat_tap_iterate_tables(wmem_foreach_func func, gpointer user_data)
{
wmem_tree_foreach(registered_stat_tables, func, user_data);

View File

@ -181,6 +181,8 @@ WS_DLL_PUBLIC stat_tap_table_item_type* new_stat_tap_get_field_data(const stat_t
WS_DLL_PUBLIC void new_stat_tap_set_field_data(stat_tap_table *stat_table, guint table_index, guint field_index, stat_tap_table_item_type* field_data);
WS_DLL_PUBLIC void reset_stat_table(stat_tap_table_ui* new_stat, new_stat_tap_gui_reset_cb gui_callback, void *callback_data);
WS_DLL_PUBLIC stat_tap_table_ui *new_stat_tap_by_name(const char *name);
/** Free all of the tables associated with a stat_tap_table_ui.
*
* Frees data created by stat_tap_ui.stat_tap_init_cb.

View File

@ -284,6 +284,21 @@ sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pi
return FALSE;
}
static gboolean
sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata)
{
stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value;
int *pi = (int *) userdata;
printf("%s{", (*pi) ? "," : "");
printf("\"name\":\"%s\"", new_stat_tap->title);
printf(",\"tap\":\"nstat:%s\"", (const char *) key);
printf("}");
*pi = *pi + 1;
return FALSE;
}
static gboolean
sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata)
{
@ -481,6 +496,11 @@ sharkd_session_process_info(void)
printf(",\"version\":");
json_puts_string(sharkd_version());
printf(",\"nstat\":[");
i = 0;
new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
printf("]");
printf(",\"convs\":[");
i = 0;
conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
@ -1430,6 +1450,121 @@ sharkd_session_free_tap_conv_cb(void *arg)
g_free(iu);
}
/**
* sharkd_session_process_tap_nstat_cb()
*
* Output nstat tap:
* (m) tap - tap name
* (m) type - tap output type
* (m) fields: array of objects with attributes:
* (m) c - name
*
* (m) tables: array of object with attributes:
* (m) t - table title
* (m) i - array of items
*/
static void
sharkd_session_process_tap_nstat_cb(void *arg)
{
new_stat_data_t *stat_data = (new_stat_data_t *) arg;
guint i, j, k;
printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
printf(",\"fields\":[");
for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
{
stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
if (i)
printf(",");
printf("{");
printf("\"c\":");
json_puts_string(field->column_name);
printf("}");
}
printf("]");
printf(",\"tables\":[");
for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
{
stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
const char *sepa = "";
if (i)
printf(",");
printf("{");
printf("\"t\":");
printf("\"%s\"", table->title);
printf(",\"i\":[");
for (j = 0; j < table->num_elements; j++)
{
stat_tap_table_item_type *field_data;
field_data = new_stat_tap_get_field_data(table, j, 0);
if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
continue;
printf("%s[", sepa);
for (k = 0; k < table->num_fields; k++)
{
field_data = new_stat_tap_get_field_data(table, j, k);
if (k)
printf(",");
switch (field_data->type)
{
case TABLE_ITEM_UINT:
printf("%u", field_data->value.uint_value);
break;
case TABLE_ITEM_INT:
printf("%d", field_data->value.uint_value);
break;
case TABLE_ITEM_STRING:
json_puts_string(field_data->value.string_value);
break;
case TABLE_ITEM_FLOAT:
printf("%f", field_data->value.float_value);
break;
case TABLE_ITEM_ENUM:
printf("%d", field_data->value.enum_value);
break;
case TABLE_ITEM_NONE:
printf("null");
break;
}
}
printf("]");
sepa = ",";
}
printf("]");
printf("}");
}
printf("]},");
}
static void
sharkd_session_free_tap_nstat_cb(void *arg)
{
new_stat_data_t *stat_data = (new_stat_data_t *) arg;
free_stat_tables(stat_data->stat_tap_data, NULL, NULL);
}
/**
* sharkd_session_process_tap_rtd_cb()
*
@ -1833,6 +1968,7 @@ sharkd_session_process_tap_rtp_cb(void *arg)
* (m) type - tap output type
* ...
* for type:stats see sharkd_session_process_tap_stats_cb()
* for type:nstat see sharkd_session_process_tap_nstat_cb()
* for type:conv see sharkd_session_process_tap_conv_cb()
* for type:host see sharkd_session_process_tap_conv_cb()
* for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
@ -1951,6 +2087,28 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
tap_data = &ct_data->hash;
tap_free = sharkd_session_free_tap_conv_cb;
}
else if (!strncmp(tok_tap, "nstat:", 6))
{
stat_tap_table_ui *stat = new_stat_tap_by_name(tok_tap + 6);
new_stat_data_t *stat_data;
if (!stat)
{
fprintf(stderr, "sharkd_session_process_tap() nstat=%s not found\n", tok_tap + 6);
continue;
}
stat->stat_tap_init_cb(stat, NULL, NULL);
stat_data = g_new0(new_stat_data_t, 1);
stat_data->stat_tap_data = stat;
stat_data->user_data = NULL;
tap_error = register_tap_listener(stat->tap_name, stat_data, tap_filter, 0, NULL, stat->packet_func, sharkd_session_process_tap_nstat_cb);
tap_data = stat_data;
tap_free = sharkd_session_free_tap_nstat_cb;
}
else if (!strncmp(tok_tap, "rtd:", 4))
{
register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);