📄 show_peer_stats.cc
字号:
false, true) ); } } } } queue().dispatch_complete(xe, this); }protected: string _ifn; string _vifn; IPv4 _a;};/** * Invoke Xrl to get peer stats on RIP address and pretty print result. */class GetPeerStats6 : public XrlJobBase {public: GetPeerStats6(XrlJobQueue& jq, const string& ifname, const string& vifname, IPv6 addr, IPv6 peer, bool single_line = false, bool hide_errors = false) : XrlJobBase(jq), _ifn(ifname), _vifn(vifname), _a(addr), _p(peer), _single_line(single_line), _hide_errs(hide_errors) {} bool dispatch() { XrlRipngV0p1Client cl(queue().sender()); return cl.send_get_peer_counters(queue().target().c_str(), _ifn, _vifn, _a, _p, callback(this, &GetPeerStats6::cmd_callback) ); }protected: void cmd_callback(const XrlError& xe, const XrlAtomList* descriptions, const XrlAtomList* values, const uint32_t* peer_last_active) { if (xe == XrlError::OKAY()) { if (_single_line) { pretty_print_counters_single_line(*descriptions, *values, *peer_last_active, _p, _ifn, _vifn); } else { print_peer_header(_p, _ifn, _vifn, _a); pretty_print_counters(*descriptions, *values, *peer_last_active); } queue().dispatch_complete(xe, this); } else if (_hide_errs) { // When invoked by GetAllPeerStats6 or GetPortPeerStats6 // we do not report an error. They queried RIP to get the // complete peers list and if we get here the specified // port has been garbage collected at the time it's stats // are queried. queue().dispatch_complete(XrlError::OKAY(), this); } else { queue().dispatch_complete(xe, this); } }protected: string _ifn; string _vifn; IPv6 _a; IPv6 _p; bool _single_line; bool _hide_errs;};/** * Invoke Xrl to get all peers, which we then use to get the counters * for to pretty print result. */class GetAllPeerStats6 : public XrlJobBase {public: GetAllPeerStats6(XrlJobQueue& jq, bool single_line = false) : XrlJobBase(jq), _single_line(single_line) {} bool dispatch() { XrlRipngV0p1Client cl(queue().sender()); return cl.send_get_all_peers(queue().target().c_str(), callback(this, &GetAllPeerStats6::cmd_callback) ); }protected: void cmd_callback(const XrlError& xe, const XrlAtomList* peers, const XrlAtomList* ifnames, const XrlAtomList* vifnames, const XrlAtomList* addrs) { if (xe == XrlError::OKAY()) { if (peers->size() == 0) { cout << NO_PEERS << endl; } else { for (size_t i = 0; i < peers->size(); i++) { const IPv6& peer_addr = peers->get(i).ipv6(); const string& ifn = ifnames->get(i).text(); const string& vifn = vifnames->get(i).text(); const IPv6& addr = addrs->get(i).ipv6(); queue().enqueue( new GetPeerStats6(queue(), ifn, vifn, addr, peer_addr, _single_line, true) ); } } } queue().dispatch_complete(xe, this); } bool _single_line;};/** * Invoke Xrl to get peers on if/vif/addr, which we then use to get * the counters for to pretty print result. */class GetPortPeerStats6 : public XrlJobBase {public: GetPortPeerStats6(XrlJobQueue& jq, const string& ifname, const string& vifname, const IPv6& addr) : XrlJobBase(jq), _ifn(ifname), _vifn(vifname), _a(addr) {} bool dispatch() { XrlRipngV0p1Client cl(queue().sender()); return cl.send_get_all_peers(queue().target().c_str(), callback(this, &GetPortPeerStats6::cmd_callback)); }protected: void cmd_callback(const XrlError& xe, const XrlAtomList* peers, const XrlAtomList* ifnames, const XrlAtomList* vifnames, const XrlAtomList* addrs) { if (xe == XrlError::OKAY()) { if (peers->size() == 0) { cout << NO_PEERS_ON_ADDR << _ifn << " " << _vifn << " " << _a.str() << endl; } else { for (size_t i = 0; i < peers->size(); i++) { const IPv6& peer_addr = peers->get(i).ipv6(); const string& ifn = ifnames->get(i).text(); const string& vifn = vifnames->get(i).text(); const IPv6& addr = addrs->get(i).ipv6(); if (ifn == _ifn && vifn == _vifn && _a == addr) { queue().enqueue( new GetPeerStats6(queue(), ifn, vifn, addr, peer_addr, false, true) ); } } } } queue().dispatch_complete(xe, this); }protected: string _ifn; string _vifn; IPv6 _a;};// ----------------------------------------------------------------------------// Mainintmain(int argc, char* const argv[]){ // // Initialize and start xlog // xlog_init(argv[0], NULL); xlog_set_verbose(XLOG_VERBOSE_LOW); // Least verbose messages xlog_level_set_verbose(XLOG_LEVEL_ERROR, XLOG_VERBOSE_HIGH); xlog_add_default_output(); xlog_start(); try { bool do_single_line = false; bool do_run = true; string finder_host = FinderConstants::FINDER_DEFAULT_HOST().str(); uint16_t finder_port = FinderConstants::FINDER_DEFAULT_PORT(); string xrl_target; int ch; while ((ch = getopt(argc, argv, "1F:T:")) != -1) { switch (ch) { case '1': do_single_line = true; break; case 'F': do_run = parse_finder_args(optarg, finder_host, finder_port); break; case 'T': xrl_target = optarg; break; default: usage(); do_run = false; } } argc -= optind; argv += optind; if (argc == 0) { usage(); } uint32_t ip_version = rip_name_to_ip_version(argv[0]); if (ip_version == 0) { usage(); } argc -= 1; argv += 1; if (xrl_target.empty()) { const char* xt = default_xrl_target(ip_version); if (xt == 0) { usage(); } xrl_target = xt; } if (do_run) { EventLoop e; XrlJobQueue job_queue(e, finder_host, finder_port, xrl_target); if (do_single_line) {// Address Interface State Hello Rx Hello Tx Last Hello" << endl; ios::fmtflags fl = cout.flags(); cout.flags(ios::left); cout << setw(17) << " Address" << setw(17) << "Interface" << setw(8) << "State" << setw(12) << "Hello Rx" << setw(12) << "Hello Tx" << setw(10) << "Last Hello" << endl; cout.flags(fl); } if (argc == 4) { if (ip_version == 4) { job_queue.enqueue( new GetPeerStats4(job_queue, argv[0], argv[1], argv[2], argv[3], do_single_line) ); } else if (ip_version == 6) { job_queue.enqueue( new GetPeerStats6(job_queue, argv[0], argv[1], argv[2], argv[3], do_single_line) ); } } else if (argc == 3) { if (ip_version == 4) { job_queue.enqueue( new GetPortPeerStats4(job_queue, argv[0], argv[1], argv[2]) ); } else if (ip_version == 6) { job_queue.enqueue( new GetPortPeerStats6(job_queue, argv[0], argv[1], argv[2]) ); } } else if (argc == 0) { if (ip_version == 4) { job_queue.enqueue(new GetAllPeerStats4(job_queue, do_single_line)); } else if (ip_version == 6) { job_queue.enqueue(new GetAllPeerStats6(job_queue, do_single_line)); } } else { usage(); } job_queue.startup(); while (job_queue.status() != SERVICE_SHUTDOWN) { if (job_queue.status() == SERVICE_FAILED) { cerr << "Failed: " << job_queue.status_note() << endl; break; } e.run(); } } } catch (...) { xorp_print_standard_exceptions(); } // // Gracefully stop and exit xlog // xlog_stop(); xlog_exit(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -