agentx.c
来自「wm PNE 3.3 source code, running at more 」· C语言 代码 · 共 1,633 行 · 第 1/3 页
C
1,633 行
do_register(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "register - register an object with a master agent\n"); break; case help_long: sty_puts(sty, "\The 'register <objectid> <range> <bound>' command registers an object\n\or range of objects with the current master agent. <objectid> is the\n\base object <range> is the index of the variable subid and <bound> is\n\the upper end of the <range> subid. <range> and <bound> are optional\n"); break; case help_none: /* build register request */ return(process_register(sty, argc, argv, ENVOY_AX_REGISTER)); }return(1);}boolean_t do_unregister(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "unregister - unregister an object with a master agent\n"); break; case help_long: sty_puts(sty, "\The 'unregister <objectid> <range> <bound>' command unregisters an object\n\or range of objects with the current master agent. <objectid> is the\n\base object <range> is the index of the variable subid and <bound> is\n\the upper end of the <range> subid. <range> and <bound> are optional\n"); break; case help_none: /* build unregister request */ return(process_register(sty, argc, argv, ENVOY_AX_UNREGISTER)); }return(1);}boolean_t do_ping(struct sty *sty, enum help_level help, int argc, char *argv[]){EBUFFER_T ebuff;ax_con_list_t *con_list;ax_foo_t *myfoo;switch (help) { case help_short: sty_puts(sty, "ping - send a ping request to a master agent\n"); break; case help_long: sty_puts(sty, "send a ping request to the master\n"); break; case help_none: con_list = find_con_list(&ax_dest_addr, ax_session_id, 1); if (con_list == 0) { sty_puts(sty, "Need to specify an open session\n"); break; } EBufferInitialize(&ebuff); if (envoy_ax_pkt_create_ping(&ebuff, 1, ax_byte_order | ax_ndc, ax_packet_id++, ax_session_id, sizeof(ax_context), (bits8_t *)ax_context)) { myfoo = (ax_foo_t *)tcp_get_cookie(con_list->con); myfoo->op = ENVOY_AX_PING; if (send_ax_queries(sty, con_list->con, &ebuff)) { myfoo->op = 0; EBufferClean(&ebuff); return(1); } EBufferClean(&ebuff); return(0); } break; }return(1);}/* register an object */boolean_t process_agent_caps(struct sty *sty, int argc, char *argv[], bits8_t op_req){EBUFFER_T ebuff;ax_con_list_t *con_list;OBJ_ID_T obj;OIDC_T oidc1[40];ax_foo_t *myfoo;char *descr;con_list = find_con_list(&ax_dest_addr, ax_session_id, 1);if (con_list == 0) { sty_puts(sty, "Need to specify an open session\n"); return(1); }if (op_req == ENVOY_AX_ADD_AC) { if ((argc != 2) && (argc != 3)) { sty_puts(sty, "Wrong arguments 'add_ac <objectid> <descr>'\n"); return(1); } }else { if (argc != 2) { sty_puts(sty, "Wrong arguments 'remove_ac <objectid>'\n"); return(1); } }obj.num_components = string2oid(argv[1], oidc1, sizeof(oidc1)/sizeof(OIDC_T));obj.component_list = oidc1;if (obj.num_components == 0) { sty_printf(sty, "No mib entry `%s'\n", argv[1]); return(1); }if (argc == 3) descr = argv[2];else descr = 0;EBufferInitialize(&ebuff);if (envoy_ax_pkt_create_agent_caps(&ebuff, 1, op_req, ax_byte_order | ax_ndc, ax_packet_id++, ax_session_id, STRLEN(descr), (bits8_t *)descr, sizeof(ax_context), (bits8_t *)ax_context, &obj) == 0) { myfoo = (ax_foo_t *)tcp_get_cookie(con_list->con); myfoo->op = op_req; if (send_ax_queries(sty, con_list->con, &ebuff)) { myfoo->op = 0; EBufferClean(&ebuff); return(1); } EBufferClean(&ebuff); return(0); }return(1);}boolean_t do_ac_add(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "add_ac - add an agent caps with a master agent\n"); break; case help_long: sty_puts(sty, "\The 'add_ac <objectid> <descr>' command adds an agent capabilities\n\with the current master agent. <objectid> is the id that should be\n\added. <descr> is an optional description string\n"); break; case help_none: /* build register request */ return(process_agent_caps(sty, argc, argv, ENVOY_AX_ADD_AC)); }return(1);}boolean_t do_ac_remove(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "remove_ac - remove an agent caps from a master agent\n"); break; case help_long: sty_puts(sty, "\The 'remove_ac <objectid>' command removes an agent capabilities\n\from the current master agent. <objectid> is the id that should be\n\removed.\n"); break; case help_none: /* build unregister request */ return(process_agent_caps(sty, argc, argv, ENVOY_AX_REMOVE_AC)); }return(1);}boolean_t process_varbinds(struct sty *sty, int argc, char *argv[], bits8_t op_req, bits8_t indx_flag, int cont_flag){ENVOY_AX_PKT_T *ax_pkt;ax_con_list_t *con_list;ax_foo_t *myfoo;EBUFFER_T ebuff;int error_ret, oid_len, val_len, count = 0;OIDC_T oidc[40], val_oidc[40];bits32_t high, low;boolean_t ret_flag = 1;con_list = find_con_list(&ax_dest_addr, ax_session_id, 1);if (con_list == 0) { sty_puts(sty, "Need to specify an open session\n"); return(1); }if ((argc == 0) || ((argc % 3) != 0)) { sty_puts(sty, "Need matching number of variables, type, and values.\n"); return(1); }ax_pkt = envoy_ax_pkt_create_variables(&error_ret, 1, op_req, ax_byte_order | indx_flag | ax_ndc, ax_session_id, ax_packet_id++, sizeof(ax_context), (bits8_t *)ax_context, BFL_IS_STATIC, argc/3);if (ax_pkt == 0) { sty_puts(sty, "Error create an agentX packet\n"); return(1); }while(argc) { oid_len = string2oid(argv[0], oidc, sizeof(oidc)/sizeof(OIDC_T)); if (oid_len == 0) { sty_printf(sty, "No mib entry `%s'\n", argv[0]); goto bug_out; } else if (oid_len > (sizeof(oidc)/sizeof(OIDC_T))) { sty_printf(sty, "Internal buffer exceeded, OID too long.\n"); goto bug_out; } if (STRNICMP("number", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_integer(ax_pkt, count, oid_len, oidc, atol(argv[2])); else if (STRNICMP("counter64", argv[1], STRLEN(argv[1])) == 0) { if (sscanf(argv[2], "%ld:%ld", &high, &low) != 2) { sty_puts(sty, "Counter 64 needs 2 arguments high:low\n"); goto bug_out; } envoy_ax_bind_uint_64(ax_pkt, count, oid_len, oidc, VT_COUNTER64, high, low); } else if (STRNICMP("string", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_string(ax_pkt, count, oid_len, oidc, VT_STRING, STRLEN(argv[2]), (bits8_t *)argv[2], 0);/* else if (STRNICMP("ipaddress", argv[1], STRLEN(argv[1])) == 0) { envoy_ax_bind_ip_address(ax_pkt, count, oid_len, oidc, ipaddr); }*/ else if (STRNICMP("counter", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_uint(ax_pkt, count, oid_len, oidc, VT_COUNTER, (bits32_t)atol(argv[2])); else if (STRNICMP("gauge", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_uint(ax_pkt, count, oid_len, oidc, VT_GAUGE, (bits32_t)atol(argv[2])); else if (STRNICMP("timeticks", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_uint(ax_pkt, count, oid_len, oidc, VT_TIMETICKS, (bits32_t)atol(argv[2])); else if (STRNICMP("empty", argv[1], STRLEN(argv[1])) == 0) envoy_ax_bind_null(ax_pkt, count, oid_len, oidc); else if (STRNICMP("objectid", argv[1], STRLEN(argv[1])) == 0) { val_len = string2oid(argv[2], val_oidc, sizeof(val_oidc)/sizeof(OIDC_T)); if (val_len == 0) { sty_printf(sty, "No mib entry `%s'\n", argv[2]); goto bug_out; } else if (val_len > (sizeof(val_oidc)/sizeof(OIDC_T))) { sty_printf(sty, "Internal buffer exceeded, OID too long.\n"); goto bug_out; } envoy_ax_bind_object_id(ax_pkt, count, oid_len, oidc, val_len, val_oidc); } else { sty_printf(sty, "Unknown type %s, try:\n", argv[1]); sty_puts(sty, "\number, string, ipaddress, counter, gauge,\n\timeticks, counter64, empty, or objectid\n"); goto bug_out; } argc -= 3; argv += 3; count += 1; }EBufferInitialize(&ebuff);if (envoy_ax_pkt_encode(ax_pkt, 0, &ebuff, 0) == 0) { myfoo = (ax_foo_t *)tcp_get_cookie(con_list->con); myfoo->op = op_req; if (send_ax_queries(sty, con_list->con, &ebuff) || cont_flag) myfoo->op = 0; else ret_flag = 0; }EBufferClean(&ebuff);bug_out:envoy_ax_pkt_free(ax_pkt);return(ret_flag);}boolean_t do_notify(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "notify - send a notify request to a master agent\n"); break; case help_long: sty_puts(sty, "\The 'notify {<object id> <type> <value>}...' command sends a request\n\to generate the given notification to a master agent\n"); break; case help_none: /* build request */ return(process_varbinds(sty, argc - 1, argv + 1, ENVOY_AX_NOTIFY, 0, 1)); }return(1);}boolean_t do_index_alloc(struct sty *sty, enum help_level help, int argc, char *argv[]){int iflag;bits8_t indx_flag;switch (help) { case help_short: sty_puts(sty, "alloc_index - add indexes to a master agent\n"); break; case help_long: sty_puts(sty, "\The 'alloc_index {<object id> <type> <value>}...' command adds the\n\indexes with the indicated values to the master agent\n"); break; case help_none: /* build request */ if ((argc < 2) || ((iflag = atoi(argv[1])) < 0) || (iflag > 2)) { sty_printf(sty, "\Incorrect index flag, must be 0 (specified), 1 (new) or 2 (any)\n"); return(1); } indx_flag = 0; switch(iflag) { case 1: indx_flag = ENVOY_AX_BIT_NEW_INDEX; break; case 2: indx_flag = ENVOY_AX_BIT_ANY_INDEX; break; } return(process_varbinds(sty, argc - 2, argv + 2, ENVOY_AX_INDEX_ALLOCATE, indx_flag, 0)); }return(1);}boolean_t do_index_dealloc(struct sty *sty, enum help_level help, int argc, char *argv[]){switch (help) { case help_short: sty_puts(sty, "dealloc_index - remove some indexes from a master agent\n"); break; case help_long: sty_puts(sty, "\The 'dealloc_index {<object id> <type> <value>}...' command removes the\n\indexes with the indicated values from the master agent\n"); break; case help_none: /* build request */ return(process_varbinds(sty, argc - 1, argv + 1, ENVOY_AX_INDEX_DEALLOCATE, 0, 0)); }return(1);}cmd_t commands[] = { { "quit", do_quit }, { "?", do_short_help }, { "help", do_help }, { "status", do_status }, { "session", do_session }, { "timeout", do_timeout }, { "priority", do_priority }, { "byte_order", do_byte_order }, { "context", do_context }, { "dest", do_dest }, { "open", do_open }, { "close", do_close }, { "register", do_register }, { "unregister", do_unregister }, { "ping", do_ping }, { "add_ac", do_ac_add }, { "remove_ac", do_ac_remove }, { "notify", do_notify }, { "alloc_index", do_index_alloc }, { "dealloc_index", do_index_dealloc }, { 0, 0 } /* the end */};cmd_t *command_lookup(char *name){cmd_t *cmd; for (cmd = commands; cmd->name; cmd++) if (STRNICMP(name, cmd->name, STRLEN(name)) == 0) return cmd;return 0;}static int command_run(struct sty *sty, int argc, char *argv[]){cmd_t *cmd = command_lookup(argv[0]); return (!cmd || !cmd->rtn || cmd->rtn(sty, help_none, argc, argv));}static void command_exec(struct sty *sty, char *line, void *cookie){char *argv[30];int argc; if (!line) /* EOF? */ return; /* Yeah, ok, no cleanup, just done */ argc = parse_line(line, argv, sizeof(argv)/sizeof(*argv));if (argc < 0 || argc > sizeof(argv)/sizeof(*argv)) { sty_puts(sty, "couldn't parse command\n"); }else if (argc) { /* Don't complain about blank line */ if (command_run(sty, argc, argv) == 0) return; } command_next(sty);}void command_next(struct sty *sty){if (quit_flag) snark_exit(0);else sty_read(sty, command_exec, prompt, 0);}void snark_main(struct task *task, void *cookie){/* read in mib for manager */if (read_mib(mib_filename) == 0) { snark_exit(1); return; }agentx_init();/* set up the prompt */if (!prompt) prompt = etc_strdup("agentx> ");command_next(cty);}/*sarneed to deal with closing tcp sessions when we are done with themdo we need an option to close a session without closing the tcp session*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?