openserSIPCommonObjects.c

Go to the documentation of this file.
00001 /*
00002  * $Id: openserSIPCommonObjects.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * SNMPStats Module 
00005  * Copyright (C) 2006 SOMA Networks, INC.
00006  * Written by: Jeffrey Magder (jmagder@somanetworks.com)
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify it
00011  * under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023  * USA
00024  *
00025  * History:
00026  * --------
00027  * 2006-11-23 initial version (jmagder)
00028  * 2007-02-16 Moved all OID registrations from the experimental branch to 
00029  *            OpenSER's IANA assigned enterprise branch. (jmagder)
00030  * 
00031  * Note: this file originally auto-generated by mib2c using
00032  *        : mib2c.scalar.conf,v 1.9 2005/01/07 09:37:18 dts12 Exp $
00033  *
00034  * This file defines all registration and handling of all scalars defined in the
00035  * OPENSER-SIP-COMMON-MIB.  Please see OPENSER-SIP-COMMON-MIB for the complete
00036  * descriptions of the individual scalars.
00037  */
00038 
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <stdlib.h>
00042 
00043 #include "../../statistics.h"
00044 #include "../../config.h"
00045 
00046 #include <net-snmp/net-snmp-config.h>
00047 #include <net-snmp/net-snmp-includes.h>
00048 #include <net-snmp/agent/net-snmp-agent-includes.h>
00049 
00050 #include "openserSIPCommonObjects.h"
00051 #include "snmpstats_globals.h"
00052 #include "utilities.h"
00053 
00054 static char *openserVersion      = "SIP/2.0";
00055 static unsigned int openserEntityType = TC_SIP_ENTITY_ROLE_OTHER;
00056 
00057 /* 
00058  * Initializes the openserSIPCommonObjects MIB elements.  This involves:
00059  *
00060  *  - Registering all OID's
00061  *  - Setting up handlers for all OID's
00062  *
00063  * This function is mostly auto-generated.
00064  */
00065 void init_openserSIPCommonObjects(void)
00066 {
00067    static oid openserSIPProtocolVersion_oid[] =
00068       { OPENSER_OID,3,1,1,1,1,1 };
00069 
00070    static oid openserSIPServiceStartTime_oid[] =
00071       { OPENSER_OID,3,1,1,1,1,2 };
00072 
00073    static oid openserSIPEntityType_oid[] =
00074       { OPENSER_OID,3,1,1,1,1,4 };
00075 
00076    static oid openserSIPSummaryInRequests_oid[] =
00077       { OPENSER_OID,3,1,1,1,3,1 };
00078 
00079    static oid openserSIPSummaryOutRequests_oid[] =
00080       { OPENSER_OID,3,1,1,1,3,2 };
00081 
00082    static oid openserSIPSummaryInResponses_oid[] =
00083       { OPENSER_OID,3,1,1,1,3,3 };
00084 
00085    static oid openserSIPSummaryOutResponses_oid[] =
00086       { OPENSER_OID,3,1,1,1,3,4 };
00087 
00088    static oid openserSIPSummaryTotalTransactions_oid[] =
00089       { OPENSER_OID,3,1,1,1,3,5 };
00090 
00091    static oid openserSIPCurrentTransactions_oid[] =
00092       { OPENSER_OID,3,1,1,1,6,1 };
00093 
00094    static oid openserSIPNumUnsupportedUris_oid[] =
00095       { OPENSER_OID,3,1,1,1,8,1 };
00096 
00097    static oid openserSIPNumUnsupportedMethods_oid[] =
00098       { OPENSER_OID,3,1,1,1,8,2 };
00099 
00100    static oid openserSIPOtherwiseDiscardedMsgs_oid[] = 
00101       { OPENSER_OID,3,1,1,1,8,3 };
00102 
00103    DEBUGMSGTL(("openserSIPCommonObjects", "Initializing\n"));
00104 
00105    netsnmp_register_scalar(
00106       netsnmp_create_handler_registration(
00107          "openserSIPProtocolVersion",
00108          handle_openserSIPProtocolVersion,
00109          openserSIPProtocolVersion_oid,
00110          OID_LENGTH(openserSIPProtocolVersion_oid),
00111          HANDLER_CAN_RONLY)); 
00112 
00113    netsnmp_register_scalar(
00114       netsnmp_create_handler_registration(
00115          "openserSIPServiceStartTime",
00116          handle_openserSIPServiceStartTime,
00117          openserSIPServiceStartTime_oid,
00118          OID_LENGTH(openserSIPServiceStartTime_oid),
00119          HANDLER_CAN_RONLY));
00120 
00121    netsnmp_register_scalar(
00122       netsnmp_create_handler_registration(
00123          "openserSIPEntityType",
00124          handle_openserSIPEntityType,
00125          openserSIPEntityType_oid,
00126          OID_LENGTH(openserSIPEntityType_oid),
00127          HANDLER_CAN_RONLY));
00128 
00129    netsnmp_register_scalar(
00130       netsnmp_create_handler_registration(
00131          "openserSIPSummaryInRequests",
00132          handle_openserSIPSummaryInRequests,
00133          openserSIPSummaryInRequests_oid,
00134          OID_LENGTH(openserSIPSummaryInRequests_oid),
00135          HANDLER_CAN_RONLY));
00136 
00137    netsnmp_register_scalar(
00138       netsnmp_create_handler_registration(
00139          "openserSIPSummaryOutRequests",
00140          handle_openserSIPSummaryOutRequests,
00141          openserSIPSummaryOutRequests_oid,
00142          OID_LENGTH(openserSIPSummaryOutRequests_oid),
00143          HANDLER_CAN_RONLY));
00144 
00145    netsnmp_register_scalar(
00146       netsnmp_create_handler_registration(
00147          "openserSIPSummaryInResponses",
00148          handle_openserSIPSummaryInResponses,
00149          openserSIPSummaryInResponses_oid,
00150          OID_LENGTH(openserSIPSummaryInResponses_oid),
00151          HANDLER_CAN_RONLY));
00152 
00153    netsnmp_register_scalar(
00154       netsnmp_create_handler_registration(
00155          "openserSIPSummaryOutResponses",
00156          handle_openserSIPSummaryOutResponses,
00157          openserSIPSummaryOutResponses_oid,
00158          OID_LENGTH(openserSIPSummaryOutResponses_oid),
00159          HANDLER_CAN_RONLY));
00160 
00161    netsnmp_register_scalar(
00162       netsnmp_create_handler_registration(
00163          "openserSIPSummaryTotalTransactions",
00164          handle_openserSIPSummaryTotalTransactions,
00165          openserSIPSummaryTotalTransactions_oid,
00166          OID_LENGTH(openserSIPSummaryTotalTransactions_oid),
00167          HANDLER_CAN_RONLY));
00168 
00169    netsnmp_register_scalar(
00170       netsnmp_create_handler_registration(
00171          "openserSIPCurrentTransactions",
00172          handle_openserSIPCurrentTransactions,
00173          openserSIPCurrentTransactions_oid,
00174          OID_LENGTH(openserSIPCurrentTransactions_oid),
00175          HANDLER_CAN_RONLY));
00176 
00177    netsnmp_register_scalar(
00178       netsnmp_create_handler_registration(
00179          "openserSIPNumUnsupportedUris",
00180          handle_openserSIPNumUnsupportedUris,
00181          openserSIPNumUnsupportedUris_oid,
00182          OID_LENGTH(openserSIPNumUnsupportedUris_oid),
00183          HANDLER_CAN_RONLY));
00184 
00185    netsnmp_register_scalar(
00186       netsnmp_create_handler_registration(
00187          "openserSIPNumUnsupportedMethods",
00188          handle_openserSIPNumUnsupportedMethods,
00189          openserSIPNumUnsupportedMethods_oid,
00190          OID_LENGTH(openserSIPNumUnsupportedMethods_oid),
00191          HANDLER_CAN_RONLY));
00192 
00193    netsnmp_register_scalar(
00194       netsnmp_create_handler_registration(
00195          "openserSIPOtherwiseDiscardedMsgs",
00196          handle_openserSIPOtherwiseDiscardedMsgs,
00197          openserSIPOtherwiseDiscardedMsgs_oid,
00198          OID_LENGTH(openserSIPOtherwiseDiscardedMsgs_oid),
00199          HANDLER_CAN_RONLY)); 
00200 
00201 }
00202 
00203 int handle_openserSIPProtocolVersion(netsnmp_mib_handler *handler,
00204       netsnmp_handler_registration *reginfo,
00205       netsnmp_agent_request_info   *reqinfo,
00206       netsnmp_request_info         *requests)
00207 {    
00208    if (reqinfo->mode==MODE_GET) {
00209       snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
00210          (u_char *) openserVersion, 7);
00211       return SNMP_ERR_NOERROR;
00212    }
00213 
00214         return SNMP_ERR_GENERR;
00215 }
00216 
00217 /* 
00218  * The scalar represents what sysUpTime was when OpenSER first started.  This
00219  * data was stored in a file when SNMPStats first started up, as a result of a
00220  * call to spawn_sysUpTime_child() 
00221  */
00222 int handle_openserSIPServiceStartTime(netsnmp_mib_handler *handler,
00223       netsnmp_handler_registration *reginfo,
00224       netsnmp_agent_request_info   *reqinfo,
00225       netsnmp_request_info         *requests)
00226 {
00227 
00228    int elapsedTime = 0;
00229    char buffer[SNMPGET_MAX_BUFFER];
00230 
00231    FILE *theFile = fopen(SNMPGET_TEMP_FILE, "r");
00232 
00233    /* Open the file created by spawn_sysUpTime_child(), and parse our the
00234     * required data.  */
00235    if (theFile == NULL) {
00236       LM_ERR("failed to read sysUpTime file at %s\n",
00237             SNMPGET_TEMP_FILE);
00238    } else {
00239       fgets(buffer, SNMPGET_MAX_BUFFER, theFile);
00240 
00241       /* Find the positions of '(' and ')' so we can extract out the
00242        * timeticks value. */
00243       char *openBracePosition   = strchr(buffer, '(');
00244       char *closedBracePosition = strchr(buffer, ')');
00245 
00246       /* Make sure that both the '(' and ')' exist in the file, and
00247        * that '(' occurs earlier than the ')'.  If all these
00248        * conditions are true, then attempt to convert the string into
00249        * an integer. */
00250       if (openBracePosition != NULL && closedBracePosition != NULL &&
00251             openBracePosition < closedBracePosition) {
00252          elapsedTime = (int) strtol(++openBracePosition, NULL, 10);
00253       }
00254 
00255       fclose(theFile);
00256    }
00257 
00258    if (reqinfo->mode == MODE_GET) { 
00259       snmp_set_var_typed_value(requests->requestvb, ASN_TIMETICKS,
00260          (u_char *) &elapsedTime, sizeof(elapsedTime));
00261       return SNMP_ERR_NOERROR;
00262    }
00263        
00264    return SNMP_ERR_GENERR;
00265 }
00266 
00267 
00268 int handle_openserSIPEntityType(netsnmp_mib_handler *handler,
00269       netsnmp_handler_registration *reginfo,
00270       netsnmp_agent_request_info   *reqinfo,
00271       netsnmp_request_info         *requests)
00272 {
00273    if (reqinfo->mode==MODE_GET) {
00274       snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
00275          (u_char *) &openserEntityType, 1);
00276       return SNMP_ERR_NOERROR;
00277    }
00278 
00279    return SNMP_ERR_GENERR;
00280 }
00281 
00282 
00283 int handle_openserSIPSummaryInRequests(netsnmp_mib_handler *handler,
00284       netsnmp_handler_registration *reginfo,
00285       netsnmp_agent_request_info   *reqinfo,
00286       netsnmp_request_info         *requests)
00287 {
00288    int numRequests = get_statistic("rcv_requests");
00289    
00290    if (reqinfo->mode==MODE_GET) {
00291       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00292          (u_char *) &numRequests, sizeof(int));
00293       return SNMP_ERR_NOERROR;
00294    }
00295 
00296    return SNMP_ERR_GENERR;
00297 }
00298 
00299 int handle_openserSIPSummaryOutRequests(netsnmp_mib_handler *handler,
00300       netsnmp_handler_registration *reginfo,
00301       netsnmp_agent_request_info   *reqinfo,
00302       netsnmp_request_info         *requests)
00303 {
00304    int out_requests = get_statistic("fwd_requests"); 
00305    
00306    if (reqinfo->mode==MODE_GET) {
00307       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00308          (u_char *) &out_requests, sizeof(int));
00309       return SNMP_ERR_NOERROR;
00310    }
00311 
00312    return SNMP_ERR_GENERR;
00313 }
00314 
00315 int handle_openserSIPSummaryInResponses(netsnmp_mib_handler *handler,
00316       netsnmp_handler_registration *reginfo,
00317       netsnmp_agent_request_info   *reqinfo,
00318       netsnmp_request_info         *requests)
00319 {
00320    int result = get_statistic("rcv_replies"); 
00321    
00322    if (reqinfo->mode==MODE_GET) {
00323       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00324          (u_char *) &result, sizeof(int));
00325       return SNMP_ERR_NOERROR;
00326    }
00327 
00328    return SNMP_ERR_GENERR;
00329 }
00330 
00331 int handle_openserSIPSummaryOutResponses(netsnmp_mib_handler *handler,
00332       netsnmp_handler_registration *reginfo,
00333       netsnmp_agent_request_info   *reqinfo,
00334       netsnmp_request_info         *requests)
00335 {
00336    /* We can find the number of outbound responses sent by adding three
00337     * sources
00338     *
00339     *   1) fwd_replies from core_stats 
00340     *   2) local_replies and relayed_replies from the tm module 
00341     *   3) sent_replies from the sl module. 
00342     */
00343    
00344    int fwd_replies     = get_statistic("fwd_replies");
00345    int local_replies   = get_statistic("local_replies");
00346    int relayed_replies = get_statistic("relayed_replies");
00347    int sent_replies    = get_statistic("sent_replies");
00348 
00349    int result = 
00350       fwd_replies + local_replies + relayed_replies + sent_replies;
00351    
00352    if (reqinfo->mode==MODE_GET) {
00353       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00354          (u_char *) &result, sizeof(int));
00355       return SNMP_ERR_NOERROR;
00356    }
00357 
00358    return SNMP_ERR_GENERR;
00359 }
00360 
00361 int handle_openserSIPSummaryTotalTransactions(netsnmp_mib_handler *handler,
00362       netsnmp_handler_registration *reginfo,
00363       netsnmp_agent_request_info   *reqinfo,
00364       netsnmp_request_info         *requests)
00365 {
00366    /* We can find the total number of transactions by summing 
00367     * UAC_transactions and UAS_transactions.  We don't need to add
00368     * inuse_transactions because this will already be accounted for in the
00369     * two other statistics. */
00370    int result = 
00371       get_statistic("UAS_transactions") + 
00372       get_statistic("UAC_transactions");
00373 
00374    if (reqinfo->mode==MODE_GET) {
00375       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00376          (u_char *) &result, sizeof(int));
00377       return SNMP_ERR_NOERROR;
00378    }
00379 
00380    return SNMP_ERR_GENERR;
00381 }
00382 
00383 int handle_openserSIPCurrentTransactions(netsnmp_mib_handler *handler,
00384       netsnmp_handler_registration *reginfo,
00385       netsnmp_agent_request_info   *reqinfo,
00386       netsnmp_request_info         *requests)
00387 {
00388    int result = get_statistic("inuse_transactions");
00389    
00390    if (reqinfo->mode==MODE_GET) {
00391       snmp_set_var_typed_value(requests->requestvb, ASN_GAUGE,
00392          (u_char *) &result, sizeof(int));
00393       return SNMP_ERR_NOERROR;
00394    }
00395 
00396    return SNMP_ERR_GENERR;
00397 }
00398 
00399 int handle_openserSIPNumUnsupportedUris(netsnmp_mib_handler *handler,
00400       netsnmp_handler_registration *reginfo,
00401       netsnmp_agent_request_info   *reqinfo,
00402       netsnmp_request_info         *requests)
00403 {
00404    int result = get_statistic("bad_URIs_rcvd");
00405    
00406    if (reqinfo->mode==MODE_GET) {
00407       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00408          (u_char *) &result, sizeof(int));
00409       return SNMP_ERR_NOERROR;
00410    }
00411 
00412    return SNMP_ERR_GENERR;
00413 }
00414 
00415 int handle_openserSIPNumUnsupportedMethods(netsnmp_mib_handler *handler,
00416       netsnmp_handler_registration *reginfo,
00417       netsnmp_agent_request_info   *reqinfo,
00418       netsnmp_request_info         *requests)
00419 {
00420    int result = get_statistic("unsupported_methods");
00421    
00422    if (reqinfo->mode==MODE_GET) {
00423       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00424          (u_char *) &result, sizeof(int));
00425       return SNMP_ERR_NOERROR;
00426    }
00427 
00428    return SNMP_ERR_GENERR;
00429 }
00430 
00431 int handle_openserSIPOtherwiseDiscardedMsgs(netsnmp_mib_handler *handler,
00432       netsnmp_handler_registration *reginfo,
00433       netsnmp_agent_request_info   *reqinfo,
00434       netsnmp_request_info         *requests)
00435 {
00436    /* We should be able to get this number from existing stats layed out in
00437     * the following equation: */
00438    int result = 
00439       get_statistic("err_requests")  +
00440       get_statistic("err_replies")   +
00441       get_statistic("drop_requests") +
00442       get_statistic("drop_replies"); 
00443     
00444    if (reqinfo->mode==MODE_GET) {
00445       snmp_set_var_typed_value(requests->requestvb, ASN_COUNTER,
00446          (u_char *) &result, sizeof(int));
00447       return SNMP_ERR_NOERROR;
00448    }
00449 
00450    return SNMP_ERR_GENERR;
00451 }
00452 
00453 /*
00454  * Parameter Setting Functions 
00455  */
00456 
00457 /* If type==STR_PARAM and stringParam is valid, this function will overwrite
00458  * openserEntityType with a bit value corresponding to the IETF's RFC  for 
00459  * the SIP MIB.  (Textual Convention SipEntityRole).  Anything else is
00460  * considered an error.
00461  *
00462  * Returns 0 on success, -1 on failure.  
00463  */
00464 int handleSipEntityType( modparam_t type, void* val) 
00465 {
00466    
00467    /* By default we start off as "other". */
00468    static char firstTime = 1;
00469 
00470    if (!stringHandlerSanityCheck(type, val, "sipEntityType")) {
00471       return -1;
00472    }
00473 
00474    char *strEntityType = (char *)val;
00475 
00476    /* This is our first time through this function, so we need to change
00477     * openserEntityType from its default to 0, allowing our bitmasks below
00478     * to work as expected. */
00479    if (firstTime) {
00480       firstTime = 0;
00481       openserEntityType = 0;
00482    }  
00483 
00484    /* Begin our string comparison.  This isn't the most efficient approach,
00485     * but we don't expect this function to be called anywhere other than at
00486     * startup.  So our inefficiency is outweiged by simplicity 
00487     */
00488    if (strcasecmp(strEntityType, "other") == 0) {
00489       openserEntityType |= TC_SIP_ENTITY_ROLE_OTHER;
00490    }
00491    else if (strcasecmp(strEntityType, "userAgent") == 0) {
00492       openserEntityType |= TC_SIP_ENTITY_ROLE_USER_AGENT; 
00493    }
00494    else if (strcasecmp(strEntityType, "proxyServer") == 0) {
00495       openserEntityType |= TC_SIP_ENTITY_ROLE_PROXY_SERVER; 
00496    }
00497    else if (strcasecmp(strEntityType, "redirectServer") == 0) {
00498       openserEntityType |= TC_SIP_ENTITY_ROLE_REDIRECT_SERVER;
00499    }
00500    else if (strcasecmp(strEntityType, "registrarServer") == 0) {
00501       openserEntityType |= TC_SIP_ENTITY_ROLE_REGISTRAR_SERVER;
00502    }
00503    else {
00504       LM_ERR("The configuration file specified sipEntityType=%s,"
00505             " an unknown type\n", strEntityType);
00506       return -1;
00507    }
00508 
00509    return 0;
00510 }

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6