ped_snmp.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ped_snmp.c 346460 2009-11-14 05:06:47Z ssiano $
00003  *
00004  * This code is provided as is by Juniper Networks SDK Developer Support.
00005  * It is provided with no warranties or guarantees, and Juniper Networks
00006  * will not provide support or maintenance of this code in any fashion.
00007  * The code is provided only to help a developer better understand how
00008  * the SDK can be used.
00009  *
00010  * Copyright (c) 2007-2008, Juniper Networks, Inc.
00011  * All rights reserved.
00012  */
00013 
00022 #include <net-snmp/net-snmp-config.h>
00023 #include <net-snmp/net-snmp-includes.h>
00024 #include <net-snmp/agent/net-snmp-agent-includes.h>
00025 #include <net-snmp/agent/agent_trap.h>
00026 #include <net-snmp/library/snmp_jnx_ext.h>
00027 #include <jnx/netsnmp_trap.h>
00028 #include <agent/snmpd.h>
00029 #include <isc/eventlib.h>
00030 #include <snmp/snmp_pathnames.h>
00031 #include <sync/common.h>
00032 #include "ped_snmp.h"
00033 #include "ped_daemon_name.h"
00034 #include "ped_policy_table.h"
00035 #include "ped_services.h"
00036 #include "ped_kcom.h"
00037 
00038 #include PE_OUT_H
00039 
00040 /*** Constants ***/
00041 
00042 
00043 /*** Data Structures ***/
00044 
00045 extern evContext ped_ctx;               
00046 
00047 static oid if_total_oid[] = {PE_MIB_IF_TOTAL_OID};    
00048 static oid if_table_oid[] = {PE_MIB_IF_TABLE_OID};    
00049 static oid if_count_oid[] = {PE_MIB_IF_COUNT_OID};    
00050 static oid snmp_ver_oid[] = {PE_MIB_SNMP_VER_OID};    
00051 static oid psd_state_oid[] = {PE_MIB_PSD_STATE_OID};  
00052 static oid psd_time_oid[] = {PE_MIB_PSD_TIME_OID};    
00053 static oid notify_psd_state_oid[] = {PE_MIB_NOTIFY_PSD_STATE_OID};    
00054 
00055 
00056 /*** STATIC/INTERNAL Functions ***/
00057 
00062 static void
00063 init_netsnmp_ds(void)
00064 {
00065     netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
00066             NETSNMP_DS_LIB_APPTYPE, DNAME_PED);
00067 
00068     netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID, 
00069             NETSNMP_DS_AGENT_X_SOCKET, PATH_MASTER_AGENTX);
00070 
00071     netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
00072             NETSNMP_DS_AGENT_ROLE, SUB_AGENT);
00073 }
00074 
00075 
00086 static int
00087 get_route_count(ped_policy_route_t *route)
00088 {
00089     int count = 0;
00090 
00091     while(route) {
00092         count++;
00093         route = route->next;
00094     }
00095     return count;
00096 }
00097 
00098 
00118 static netsnmp_variable_list *
00119 table_get_next_data_point(
00120     void **loop_context,
00121     void **data_context,
00122     netsnmp_variable_list *put_index_data,
00123     netsnmp_iterator_info *data __unused)
00124 {
00125     policy_table_entry_t *entry;
00126 
00127     entry = policy_table_next();
00128     if(entry == NULL) {
00129         return NULL;
00130     }
00131 
00132     *loop_context = (void *)entry;
00133     *data_context = (void *)entry;
00134 
00135     snmp_set_var_typed_value(put_index_data, ASN_OCTET_STR,
00136             entry->ifname, strlen(entry->ifname));
00137 
00138     junos_trace(PED_TRACEFLAG_SNMP,
00139             "%s: index: %s", __func__, entry->ifname);  
00140 
00141     return put_index_data;
00142 }
00143 
00144 
00164 static netsnmp_variable_list *
00165 table_get_first_data_point(
00166     void **loop_context,
00167     void **data_context,
00168     netsnmp_variable_list *put_index_data,
00169     netsnmp_iterator_info *data)
00170 {
00171     policy_table_iterator_reset();
00172 
00173     return table_get_next_data_point(loop_context, data_context,
00174             put_index_data, data);
00175 }
00176 
00177 
00197 static int
00198 handler_if_table (
00199     netsnmp_mib_handler             *handler __unused,
00200     netsnmp_handler_registration    *reginfo __unused,
00201     netsnmp_agent_request_info      *reqinfo,
00202     netsnmp_request_info            *requests)
00203 {
00204     netsnmp_request_info       *request;
00205     netsnmp_table_request_info *table_info;
00206     policy_table_entry_t *entry;
00207     int tmp;
00208 
00209     switch (reqinfo->mode) {
00210         case MODE_GET:
00211             request = requests;
00212             while(request) {
00213                 entry = (policy_table_entry_t *)
00214                         netsnmp_extract_iterator_context(request);
00215                 table_info = netsnmp_extract_table_info(request);
00216 
00217                 if(entry == NULL) {
00218                     request = request->next;
00219                     continue;
00220                 }
00221 
00222                 switch(table_info->colnum) {
00223                     case 1:
00224                         snmp_set_var_typed_value(request->requestvb,
00225                                 ASN_OCTET_STR, entry->ifname,
00226                                 strlen(entry->ifname));
00227                         break;
00228                     case 2:
00229                         tmp = (int)entry->af;
00230                         snmp_set_var_typed_value(request->requestvb,
00231                                 ASN_INTEGER, (const u_char *)&tmp,
00232                                 sizeof(int));
00233                         break;
00234                     case 3:
00235                         if((entry->filter) &&
00236                                 (entry->filter->filter_data.input_filter[0])) {
00237                             snmp_set_var_typed_value(request->requestvb,
00238                                     ASN_OCTET_STR,
00239                                     entry->filter->filter_data.input_filter,
00240                                     strlen(entry->filter->filter_data.input_filter));
00241                         } else {
00242                             snmp_set_var_typed_value(request->requestvb,
00243                                     ASN_OCTET_STR,
00244                                     "N/A", strlen("N/A"));
00245                         }
00246                         break;
00247                     case 4:
00248                         if((entry->filter) &&
00249                                 (entry->filter->filter_data.output_filter[0])) {
00250                             snmp_set_var_typed_value(request->requestvb,
00251                                     ASN_OCTET_STR,
00252                                     entry->filter->filter_data.output_filter,
00253                                     strlen(entry->filter->filter_data.output_filter));
00254                         } else {
00255                             snmp_set_var_typed_value(request->requestvb,
00256                                     ASN_OCTET_STR,
00257                                     "N/A", strlen("N/A"));
00258                         }
00259                         break;
00260                     case 5:
00261                         tmp = get_route_count(entry->route);
00262                         snmp_set_var_typed_value(request->requestvb,
00263                                 ASN_INTEGER, (const u_char *)&tmp,
00264                                 sizeof(int));
00265                         break;
00266                 }
00267                 request = request->next;
00268             }
00269             break;
00270         default:
00271             /* we should never get here, so this is a really bad error */
00272             return SNMP_ERR_GENERR;
00273     }
00274     return SNMP_ERR_NOERROR;
00275 }
00276 
00277 
00297 static int
00298 handler_snmp_ver(
00299     netsnmp_mib_handler *handler __unused,
00300     netsnmp_handler_registration *reginfo __unused,
00301     netsnmp_agent_request_info   *reqinfo,
00302     netsnmp_request_info         *requests)
00303 {
00304     const u_char *netsnmp_version;
00305 
00306     netsnmp_version = netsnmp_get_version();
00307     switch(reqinfo->mode) {
00308         case MODE_GET:
00309             snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
00310                     netsnmp_version, strlen(netsnmp_version));
00311             break;
00312         default:
00313             /* we should never get here, so this is a really bad error */
00314             return SNMP_ERR_GENERR;
00315     }
00316 
00317     return SNMP_ERR_NOERROR;
00318 }
00319 
00320 
00340 static int
00341 handler_if_count(
00342     netsnmp_mib_handler *handler __unused,
00343     netsnmp_handler_registration *reginfo __unused,
00344     netsnmp_agent_request_info   *reqinfo,
00345     netsnmp_request_info         *requests)
00346 {
00347     int count;
00348 
00349     count = policy_table_entry_count();
00350     switch(reqinfo->mode) {
00351         case MODE_GET:
00352             snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
00353                     (const u_char *)&count, sizeof(int));
00354             break;
00355         default:
00356             /* we should never get here, so this is a really bad error */
00357             return SNMP_ERR_GENERR;
00358     }
00359 
00360     return SNMP_ERR_NOERROR;
00361 }
00362 
00363 
00383 static int
00384 handler_if_total(
00385     netsnmp_mib_handler *handler __unused,
00386     netsnmp_handler_registration *reginfo __unused,
00387     netsnmp_agent_request_info   *reqinfo,
00388     netsnmp_request_info         *requests)
00389 {
00390     int count;
00391 
00392     count = get_interface_total();
00393     switch(reqinfo->mode) {
00394         case MODE_GET:
00395             snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
00396                     (const u_char *)&count, sizeof(int));
00397             break;
00398         default:
00399             /* we should never get here, so this is a really bad error */
00400             return SNMP_ERR_GENERR;
00401     }
00402 
00403     return SNMP_ERR_NOERROR;
00404 }
00405 
00406 
00426 static int
00427 handler_psd_state(
00428     netsnmp_mib_handler *handler __unused,
00429     netsnmp_handler_registration *reginfo __unused,
00430     netsnmp_agent_request_info   *reqinfo,
00431     netsnmp_request_info         *requests)
00432 {
00433     int state;
00434 
00435     state = (int)psd_conn_state();
00436     switch(reqinfo->mode) {
00437         case MODE_GET:
00438             snmp_set_var_typed_value(requests->requestvb, ASN_INTEGER,
00439                     (const u_char *)&state, sizeof(int));
00440             break;
00441         default:
00442             /* we should never get here, so this is a really bad error */
00443             return SNMP_ERR_GENERR;
00444     }
00445 
00446     return SNMP_ERR_NOERROR;
00447 }
00448 
00449 
00469 static int
00470 handler_psd_time(
00471     netsnmp_mib_handler *handler __unused,
00472     netsnmp_handler_registration *reginfo __unused,
00473     netsnmp_agent_request_info   *reqinfo,
00474     netsnmp_request_info         *requests)
00475 {
00476     int psd_time;
00477     char buf[64];
00478 
00479     psd_time = get_psd_conn_time();
00480     sprintf(buf, "%02d:%02d:%02d", (psd_time / 3600) % 60,
00481             (psd_time / 60) % 60, psd_time % 60);
00482     switch(reqinfo->mode) {
00483         case MODE_GET:
00484             snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
00485                     buf, strlen(buf));
00486             break;
00487         default:
00488             /* we should never get here, so this is a really bad error */
00489             return SNMP_ERR_GENERR;
00490     }
00491 
00492     return SNMP_ERR_NOERROR;
00493 }
00494 
00495 
00500 static void
00501 init_mib_policy_table(void)
00502 {
00503     size_t if_table_oid_len = OID_LENGTH(if_table_oid);
00504     netsnmp_handler_registration    *reg;
00505     netsnmp_iterator_info           *iinfo;
00506     netsnmp_table_registration_info *table_info;
00507 
00508     reg = netsnmp_create_handler_registration(
00509             "peInterfaceTable", handler_if_table,
00510             if_table_oid, if_table_oid_len, HANDLER_CAN_RONLY);
00511 
00512     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
00513     netsnmp_table_helper_add_indexes(table_info,
00514                            ASN_OCTET_STR,  /* index: interface name */
00515                            0);
00516     table_info->min_column = 1;
00517     table_info->max_column = 5;
00518 
00519     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
00520     iinfo->get_first_data_point = table_get_first_data_point;
00521     iinfo->get_next_data_point  = table_get_next_data_point;
00522     iinfo->table_reginfo        = table_info;
00523 
00524     netsnmp_register_table_iterator(reg, iinfo);
00525 }
00526 
00527 
00532 static void
00533 ped_init_mib(void)
00534 {
00535     init_mib_policy_table();
00536 
00537     netsnmp_register_scalar(
00538             netsnmp_create_handler_registration("PE_PSD_State",
00539                     handler_if_count, if_count_oid,
00540                     OID_LENGTH(if_count_oid), HANDLER_CAN_RONLY));
00541 
00542     netsnmp_register_scalar(
00543             netsnmp_create_handler_registration("PE_SNMP_Version",
00544                     handler_snmp_ver, snmp_ver_oid,
00545                     OID_LENGTH(snmp_ver_oid), HANDLER_CAN_RONLY));
00546 
00547     netsnmp_register_scalar(
00548             netsnmp_create_handler_registration("PE_Interface_Count",
00549                     handler_psd_state, psd_state_oid,
00550                     OID_LENGTH(psd_state_oid), HANDLER_CAN_RONLY));
00551 
00552     netsnmp_register_scalar(
00553             netsnmp_create_handler_registration("PE_Interface_Total",
00554                     handler_if_total, if_total_oid,
00555                     OID_LENGTH(if_total_oid), HANDLER_CAN_RONLY));
00556 
00557     netsnmp_register_scalar(
00558             netsnmp_create_handler_registration("PE_PSD_Time",
00559                     handler_psd_time, psd_time_oid,
00560                     OID_LENGTH(psd_time_oid), HANDLER_CAN_RONLY));
00561 }
00562 
00563 
00564 /*** GLOBAL/EXTERNAL Functions ***/
00565 
00566 
00567 
00572 void
00573 netsnmp_subagent_init(void)
00574 {
00575     /* setup context */
00576     netsnmp_set_evcontext(ped_ctx);
00577 
00578     /* set netsnmp parameters */
00579     init_netsnmp_ds();
00580 
00581     /* initialize agent */
00582     init_agent(DNAME_PED);
00583 
00584     /* register mib modules */
00585     ped_init_mib();
00586 
00587     /* start library */
00588     init_snmp(DNAME_PED);
00589 }
00590 
00598 void
00599 ped_notify_psd_state(boolean state)
00600 {
00601     netsnmp_variable_list *var_list;
00602 
00603     junos_trace(PED_TRACEFLAG_SNMP,
00604             "%s: state: %d", __func__, state);  
00605 
00606     var_list = netsnmp_trap_sdk_request_header(notify_psd_state_oid,
00607             OID_LENGTH(notify_psd_state_oid));
00608 
00609     snmp_varlist_add_variable(&var_list, psd_state_oid,
00610             OID_LENGTH(psd_state_oid), ASN_INTEGER,
00611             (const char *)&state, sizeof(char));
00612 
00613     netsnmp_trap_request_send(var_list);
00614 
00615     snmp_free_varbind(var_list);
00616 }

2007-2009 Juniper Networks, Inc. All rights reserved. The information contained herein is confidential information of Juniper Networks, Inc., and may not be used, disclosed, distributed, modified, or copied without the prior written consent of Juniper Networks, Inc. in an express license. This information is subject to change by Juniper Networks, Inc. Juniper Networks, the Juniper Networks logo, and JUNOS are registered trademarks of Juniper Networks, Inc. in the United States and other countries. All other trademarks, service marks, registered trademarks, or registered service marks are the property of their respective owners.
Generated on Sun May 30 20:27:06 2010 for SDK Your Net Corporation Policy Manager Example: Policy Enforcement Daemon 1.0 by Doxygen 1.5.1