static void dump_kern_db(FILE *fp) { json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; struct ifstat_ent *n; if (jw) { jsonw_start_object(jw); jsonw_pretty(jw, pretty); jsonw_name(jw, info_source); jsonw_start_object(jw); } else print_head(fp); for (n = kern_db; n; n = n->next) { if (!match(n->name)) continue; if (jw) print_one_json(jw, n, n->val); else print_one_if(fp, n, n->val); } if (json_output) fprintf(fp, "\n} }\n"); }
static int vlan_show(int argc, char **argv) { char *filter_dev = NULL; while (argc > 0) { if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); if (filter_dev) duparg("dev", *argv); filter_dev = *argv; } else if (strcmp(*argv, "vid") == 0) { NEXT_ARG(); if (filter_vlan) duparg("vid", *argv); filter_vlan = atoi(*argv); } argc--; argv++; } if (filter_dev) { if ((filter_index = if_nametoindex(filter_dev)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", filter_dev); return -1; } } if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK, (compress_vlans ? RTEXT_FILTER_BRVLAN_COMPRESSED : RTEXT_FILTER_BRVLAN)) < 0) { perror("Cannont send dump request"); exit(1); } if (json_output) { jw_global = jsonw_new(stdout); if (!jw_global) { fprintf(stderr, "Error allocation json object\n"); exit(1); } jsonw_start_object(jw_global); } else { printf("port\tvlan ids\n"); } if (rtnl_dump_filter(&rth, print_vlan, stdout) < 0) { fprintf(stderr, "Dump ternminated\n"); exit(1); } if (jw_global) { jsonw_end_object(jw_global); jsonw_destroy(&jw_global); } return 0; }
static void dump_raw_db(FILE *fp, int to_hist) { json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; struct ifstat_ent *n, *h; h = hist_db; if (jw) { jsonw_start_object(jw); jsonw_pretty(jw, pretty); jsonw_name(jw, info_source); jsonw_start_object(jw); } else fprintf(fp, "#%s\n", info_source); for (n = kern_db; n; n = n->next) { int i; unsigned long long *vals = n->val; double *rates = n->rate; if (!match(n->name)) { struct ifstat_ent *h1; if (!to_hist) continue; for (h1 = h; h1; h1 = h1->next) { if (h1->ifindex == n->ifindex) { vals = h1->val; rates = h1->rate; h = h1->next; break; } } } if (jw) { jsonw_name(jw, n->name); jsonw_start_object(jw); for (i = 0; i < MAXS && stats[i]; i++) jsonw_uint_field(jw, stats[i], vals[i]); jsonw_end_object(jw); } else { fprintf(fp, "%d %s ", n->ifindex, n->name); for (i = 0; i < MAXS; i++) fprintf(fp, "%llu %u ", vals[i], (unsigned int)rates[i]); fprintf(fp, "\n"); } } if (jw) { jsonw_end_object(jw); jsonw_end_object(jw); jsonw_destroy(&jw); } }
static json_writer_t *get_btf_writer(void) { json_writer_t *jw = jsonw_new(stdout); if (!jw) return NULL; jsonw_pretty(jw, true); return jw; }
void new_json_obj(int json) { if (json) { _jw = jsonw_new(stdout); if (!_jw) { perror("json object"); exit(1); } jsonw_pretty(_jw, true); jsonw_start_array(_jw); } }
int main(int argc, char **argv) { json_writer_t *wr = jsonw_new(stdout); jsonw_start_object(wr); jsonw_pretty(wr, true); jsonw_name(wr, "Vyatta"); jsonw_start_object(wr); jsonw_string_field(wr, "url", "http://vyatta.com"); jsonw_uint_field(wr, "downloads", 2000000ul); jsonw_float_field(wr, "stock", 8.16); jsonw_name(wr, "ARGV"); jsonw_start_array(wr); while (--argc) jsonw_string(wr, *++argv); jsonw_end_array(wr); jsonw_name(wr, "empty"); jsonw_start_array(wr); jsonw_end_array(wr); jsonw_name(wr, "NIL"); jsonw_start_object(wr); jsonw_end_object(wr); jsonw_null_field(wr, "my_null"); jsonw_name(wr, "special chars"); jsonw_start_array(wr); jsonw_string_field(wr, "slash", "/"); jsonw_string_field(wr, "newline", "\n"); jsonw_string_field(wr, "tab", "\t"); jsonw_string_field(wr, "ff", "\f"); jsonw_string_field(wr, "quote", "\""); jsonw_string_field(wr, "tick", "\'"); jsonw_string_field(wr, "backslash", "\\"); jsonw_end_array(wr); jsonw_end_object(wr); jsonw_end_object(wr); jsonw_destroy(&wr); return 0; }
static void dump_incr_db(FILE *fp) { struct ifstat_ent *n, *h; json_writer_t *jw = json_output ? jsonw_new(fp) : NULL; h = hist_db; if (jw) { jsonw_start_object(jw); jsonw_pretty(jw, pretty); jsonw_name(jw, info_source); jsonw_start_object(jw); } else print_head(fp); for (n = kern_db; n; n = n->next) { int i; unsigned long long vals[MAXS]; struct ifstat_ent *h1; memcpy(vals, n->val, sizeof(vals)); for (h1 = h; h1; h1 = h1->next) { if (h1->ifindex == n->ifindex) { for (i = 0; i < MAXS; i++) vals[i] -= h1->val[i]; h = h1->next; break; } } if (!match(n->name)) continue; if (jw) print_one_json(jw, n, n->val); else print_one_if(fp, n, vals); } if (jw) { jsonw_end_object(jw); jsonw_end_object(jw); jsonw_destroy(&jw); } }
int main(int argc, char **argv) { static const struct option options[] = { { "json", no_argument, NULL, 'j' }, { "help", no_argument, NULL, 'h' }, { "pretty", no_argument, NULL, 'p' }, { "version", no_argument, NULL, 'V' }, { "bpffs", no_argument, NULL, 'f' }, { "mapcompat", no_argument, NULL, 'm' }, { "nomount", no_argument, NULL, 'n' }, { 0 } }; int opt, ret; last_do_help = do_help; pretty_output = false; json_output = false; show_pinned = false; block_mount = false; bin_name = argv[0]; hash_init(prog_table.table); hash_init(map_table.table); opterr = 0; while ((opt = getopt_long(argc, argv, "Vhpjfmn", options, NULL)) >= 0) { switch (opt) { case 'V': return do_version(argc, argv); case 'h': return do_help(argc, argv); case 'p': pretty_output = true; /* fall through */ case 'j': if (!json_output) { json_wtr = jsonw_new(stdout); if (!json_wtr) { p_err("failed to create JSON writer"); return -1; } json_output = true; } jsonw_pretty(json_wtr, pretty_output); break; case 'f': show_pinned = true; break; case 'm': bpf_flags = MAPS_RELAX_COMPAT; break; case 'n': block_mount = true; break; default: p_err("unrecognized option '%s'", argv[optind - 1]); if (json_output) clean_and_exit(-1); else usage(); } } argc -= optind; argv += optind; if (argc < 0) usage(); ret = cmd_select(cmds, argc, argv, do_help); if (json_output) jsonw_destroy(&json_wtr); if (show_pinned) { delete_pinned_obj_table(&prog_table); delete_pinned_obj_table(&map_table); } return ret; }