/* ARGSUSED */ static ilb_status_t ilbadm_list_sg_srv(ilb_handle_t h, ilb_server_data_t *sd, const char *sgname, void *arg) { char ip_str[2*INET6_ADDRSTRLEN + 3] = ""; char port_str[INET6_ADDRSTRLEN]; list_arg_t *larg = (list_arg_t *)arg; ofmt_status_t oerr; int oflags = 0; int ocols = MAXCOLS; int h_minport, h_maxport; static ofmt_handle_t oh = (ofmt_handle_t)NULL; ofmt_field_t *ofp; if (larg->o_str != NULL) { if (oh == NULL) { if (sd->sd_addr.ia_af == AF_INET) ofp = sgfields_v6; else ofp = sgfields_v4; if (larg->flags & ILBADM_LIST_PARSE) oflags |= OFMT_PARSABLE; oerr = ofmt_open(larg->o_str, ofp, oflags, ocols, &oh); if (oerr != OFMT_SUCCESS) { char e[80]; ilbadm_err(gettext("ofmt_open failed: %s"), ofmt_strerror(oh, oerr, e, sizeof (e))); return (ILB_STATUS_GENERIC); } larg->oh = oh; } (void) i_list_sg_srv_ofmt((char *)sgname, sd, arg); return (ILB_STATUS_OK); } ip2str(&sd->sd_addr, ip_str, sizeof (ip_str), 0); h_minport = ntohs(sd->sd_minport); h_maxport = ntohs(sd->sd_maxport); if (h_minport == 0) *port_str = '\0'; else if (h_maxport > h_minport) (void) sprintf(port_str, ":%d-%d", h_minport, h_maxport); else (void) sprintf(port_str, ":%d", h_minport); (void) printf("%s: id:%s %s%s\n", sgname, sd->sd_srvID?sd->sd_srvID:"(null)", ip_str, port_str); return (ILB_STATUS_OK); }
/* * Output hc result of a specified rule or all rules. */ ilbadm_status_t ilbadm_show_hc_result(int argc, char *argv[]) { ilb_handle_t h = ILB_INVALID_HANDLE; ilb_status_t rclib = ILB_STATUS_OK; int i; ofmt_handle_t ofmt_h; ofmt_status_t ofmt_ret; /* ilbadm show-hc-result [rule-name] */ if (argc < 1) { ilbadm_err(gettext("usage: ilbadm show-hc-result" " [rule-name]")); return (ILBADM_LIBERR); } if ((ofmt_ret = ofmt_open("all", hc_results, 0, SHOW_HC_COLS, &ofmt_h)) != OFMT_SUCCESS) { char err_buf[SHOW_HC_COLS]; ilbadm_err(gettext("ofmt_open failed: %s"), ofmt_strerror(ofmt_h, ofmt_ret, err_buf, SHOW_HC_COLS)); return (ILBADM_LIBERR); } rclib = ilb_open(&h); if (rclib != ILB_STATUS_OK) goto out; /* If no rule name is given, show results for all rules. */ if (argc == 1) { rclib = ilb_walk_hc_srvs(h, ilbadm_print_hc_result, NULL, ofmt_h); } else { for (i = 1; i < argc; i++) { rclib = ilb_walk_hc_srvs(h, ilbadm_print_hc_result, argv[i], ofmt_h); if (rclib != ILB_STATUS_OK) break; } } out: ofmt_close(ofmt_h); if (h != ILB_INVALID_HANDLE) (void) ilb_close(h); if (rclib != ILB_STATUS_OK) { ilbadm_err(ilb_errstr(rclib)); return (ILBADM_LIBERR); } return (ILBADM_OK); }
/* ARGSUSED */ ilbadm_status_t ilbadm_show_hc(int argc, char *argv[]) { ilb_handle_t h = ILB_INVALID_HANDLE; ilb_status_t rclib; ofmt_handle_t ofmt_h; ofmt_status_t ofmt_ret; if ((ofmt_ret = ofmt_open("all", hc_fields, 0, SHOW_HC_COLS, &ofmt_h)) != OFMT_SUCCESS) { char err_buf[SHOW_HC_COLS]; ilbadm_err(gettext("ofmt_open failed: %s"), ofmt_strerror(ofmt_h, ofmt_ret, err_buf, SHOW_HC_COLS)); return (ILBADM_LIBERR); } rclib = ilb_open(&h); if (rclib != ILB_STATUS_OK) goto out; if (argc == 1) { rclib = ilb_walk_hc(h, ilbadm_print_hc, ofmt_h); } else { ilb_hc_info_t hc_info; int i; for (i = 1; i < argc; i++) { rclib = ilb_get_hc_info(h, argv[i], &hc_info); if (rclib == ILB_STATUS_OK) ofmt_print(ofmt_h, &hc_info); else break; } } out: ofmt_close(ofmt_h); if (h != ILB_INVALID_HANDLE) (void) ilb_close(h); if (rclib != ILB_STATUS_OK) { ilbadm_err(ilb_errstr(rclib)); return (ILBADM_LIBERR); } return (ILBADM_OK); }
static void flowadm_ofmt_check(ofmt_status_t oferr, boolean_t parsable, ofmt_handle_t ofmt) { char buf[OFMT_BUFSIZE]; if (oferr == OFMT_SUCCESS) return; (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf)); /* * All errors are considered fatal in parsable mode. * NOMEM errors are always fatal, regardless of mode. * For other errors, we print diagnostics in human-readable * mode and processs what we can. */ if (parsable || oferr == OFMT_ENOFIELDS) { ofmt_close(ofmt); die(buf); } else { warn(buf); } }
int main(int argc, char **argv) { int c; int err; const char *ofields = NULL; ofmt_status_t ofmterr; ofmt_field_t *fields = NULL; uint_t ofmtflags = 0; ipmp_handle_t ih; ipmp_qcontext_t qcontext = IPMP_QCONTEXT_SNAP; ipmpstat_cbfunc_t *cbfunc; ipmpstat_walker_t *walker; char errbuf[OFMT_BUFSIZE]; if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; else progname++; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); while ((c = getopt(argc, argv, "nLPo:agipt")) != EOF) { if (fields != NULL && strchr("agipt", c) != NULL) die("only one output format may be specified\n"); switch (c) { case 'n': opt |= IPMPSTAT_OPT_NUMERIC; break; case 'L': /* Undocumented option: for testing use ONLY */ qcontext = IPMP_QCONTEXT_LIVE; break; case 'P': opt |= IPMPSTAT_OPT_PARSABLE; ofmtflags |= OFMT_PARSABLE; break; case 'o': ofields = optarg; break; case 'a': walker = walk_addr; cbfunc = info_output_cbfunc; fields = addr_fields; break; case 'g': walker = walk_group; cbfunc = info_output_cbfunc; fields = group_fields; break; case 'i': walker = walk_if; cbfunc = info_output_cbfunc; fields = if_fields; break; case 'p': fields = probe_fields; break; case 't': walker = walk_if; cbfunc = targinfo_output_cbfunc; fields = targ_fields; break; default: usage(); break; } } if (argc > optind || fields == NULL) usage(); /* * Open a handle to the formatted output engine. */ ofmterr = ofmt_open(ofields, fields, ofmtflags, IPMPSTAT_NCOL, &ofmt); if (ofmterr != OFMT_SUCCESS) { /* * If some fields were badly formed in human-friendly mode, we * emit a warning and continue. Otherwise exit immediately. */ (void) ofmt_strerror(ofmt, ofmterr, errbuf, sizeof (errbuf)); if (ofmterr != OFMT_EBADFIELDS || (opt & IPMPSTAT_OPT_PARSABLE)) die("%s\n", errbuf); else warn("%s\n", errbuf); } /* * Obtain the window size and monitor changes to the size. This data * is used to redisplay the output headers when necessary. */ (void) sigset(SIGWINCH, sighandler); if ((err = ipmp_open(&ih)) != IPMP_SUCCESS) die_ipmperr(err, "cannot create IPMP handle"); if (ipmp_ping_daemon(ih) != IPMP_SUCCESS) die("cannot contact in.mpathd(1M) -- is IPMP in use?\n"); /* * If we've been asked to display probes, then call the probe output * function. Otherwise, snapshot IPMP state (or use live state) and * invoke the specified walker with the specified callback function. */ if (fields == probe_fields) { probe_output(ih, ofmt); } else { if ((err = ipmp_setqcontext(ih, qcontext)) != IPMP_SUCCESS) { if (qcontext == IPMP_QCONTEXT_SNAP) die_ipmperr(err, "cannot snapshot IPMP state"); else die_ipmperr(err, "cannot use live IPMP state"); } (*walker)(ih, cbfunc, ofmt); } ofmt_close(ofmt); ipmp_close(ih); return (EXIT_SUCCESS); }
static void do_show(int argc, char *argv[], const char *use) { int c; char *fields_str = NULL; char *names = NULL, *router; uint32_t i, in_cnt = 0, out_cnt; ofmt_status_t oferr; ofmt_handle_t ofmt; uint_t ofmt_flags = 0; vrrp_err_t err = VRRP_SUCCESS; boolean_t P_opt, x_opt; static char *dft_fields_str = "NAME,VRID,LINK,AF,PRIO,ADV_INTV,MODE,STATE,VNIC"; static char *ext_fields_str = "NAME,STATE,PRV_STAT,STAT_LAST,VNIC,PRIMARY_IP,VIRTUAL_IPS"; static char *peer_fields_str = "NAME,PEER,P_PRIO,P_INTV,P_ADV_LAST,M_DOWN_INTV"; /* * If parsable output is requested, add VIP_CNT into the output * for extended output. It is not needed for human-readable * output as it is obvious from the VIRTUAL_IPS list. */ static char *ext_parsable_fields_str = "NAME,STATE,PRV_STAT,STAT_LAST,VNIC,PRIMARY_IP,VIP_CNT," "VIRTUAL_IPS"; P_opt = x_opt = B_FALSE; fields_str = dft_fields_str; while ((c = getopt_long(argc, argv, ":Pxpo:", l_show_opts, NULL)) != EOF) { switch (c) { case 'o': fields_str = optarg; break; case 'p': ofmt_flags |= OFMT_PARSABLE; break; case 'P': P_opt = B_TRUE; fields_str = peer_fields_str; break; case 'x': x_opt = B_TRUE; fields_str = ext_fields_str; break; default: opterr_exit(optopt, c, use); } } if (x_opt && P_opt) err_exit("incompatible -P and -x options"); /* * If parsable output is requested, add VIP_CNT into the output * for extended output. */ if ((ofmt_flags & OFMT_PARSABLE) && (fields_str == ext_fields_str)) fields_str = ext_parsable_fields_str; if ((oferr = ofmt_open(fields_str, show_print_fields, ofmt_flags, 0, &ofmt)) != OFMT_SUCCESS) { char buf[OFMT_BUFSIZE]; /* * If some fields were badly formed in human-friendly mode, we * emit a warning and continue. Otherwise exit immediately. */ (void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf)); if (oferr != OFMT_EBADFIELDS || (ofmt_flags & OFMT_PARSABLE)) { ofmt_close(ofmt); err_exit(buf); } else { warn(buf); } } /* Show one router */ if (optind == argc - 1) { err = do_show_router(argv[optind], ofmt); goto done; } /* * Show all routers. First set in_cnt to 0 to find out the number * of vrrp routers. */ again: if ((in_cnt != 0) && (names = malloc(in_cnt * VRRP_NAME_MAX)) == NULL) { err = VRRP_ENOMEM; goto done; } out_cnt = in_cnt; if ((err = vrrp_list(vrrp_vh, VRRP_VRID_NONE, NULL, AF_UNSPEC, &out_cnt, names)) != VRRP_SUCCESS) { free(names); goto done; } /* * The VRRP routers has been changed between two vrrp_list() * calls, try again. */ if (out_cnt > in_cnt) { in_cnt = out_cnt; free(names); goto again; } /* * Each VRRP router name is separated by '\0` */ router = names; for (i = 0; i < in_cnt; i++) { (void) do_show_router(router, ofmt); router += strlen(router) + 1; } free(names); done: ofmt_close(ofmt); if (err != VRRP_SUCCESS) err_exit(vrrp_err2str(err)); }