Current File : //usr/share/doc/net-snmp/html/notification_8c-example.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>net-snmp: notification.c</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">net-snmp
   &#160;<span id="projectnumber">5.4.1</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">notification.c</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example shows how to send a notification from inside the agent. In this case we do something really boring to decide whether to send a notification or not: we simply sleep for 30 seconds and send it, then we sleep for 30 more and send it again. We do this through the <a class="el" href="structsnmp__alarm.html">snmp_alarm</a> mechanisms (which are safe to use within the agent. Don't use the system alarm() call, it won't work properly). Normally, you would probably want to do something to test whether or not to send an alarm, based on the type of mib module you were creating.</p>
<p>When this module is compiled into the agent (run configure with --with-mib-modules="examples/notification") then it should send out traps, which when received by the snmptrapd demon will look roughly like:</p>
<p>2002-05-08 08:57:05 localhost.localdomain [udp:127.0.0.1:32865]: sysUpTimeInstance = Timeticks: (3803) 0:00:38.03 snmpTrapOID.0 = OID: netSnmpExampleNotification</p>
<div class="fragment"><pre class="fragment">
<span class="comment">/*</span>
<span class="comment"> * start be including the appropriate header files </span>
<span class="comment"> */</span>
<span class="preprocessor">#include &lt;net-snmp/net-snmp-config.h&gt;</span>
<span class="preprocessor">#include &lt;net-snmp/net-snmp-includes.h&gt;</span>
<span class="preprocessor">#include &lt;net-snmp/agent/net-snmp-agent-includes.h&gt;</span>

<span class="comment">/*</span>
<span class="comment"> * contains prototypes </span>
<span class="comment"> */</span>
<span class="preprocessor">#include &quot;notification.h&quot;</span>

<span class="comment">/*</span>
<span class="comment"> * our initialization routine</span>
<span class="comment"> * (to get called, the function name must match init_FILENAME() </span>
<span class="comment"> */</span>
<span class="keywordtype">void</span>
init_notification(<span class="keywordtype">void</span>)
{
    DEBUGMSGTL((<span class="stringliteral">&quot;example_notification&quot;</span>,
                <span class="stringliteral">&quot;initializing (setting callback alarm)\n&quot;</span>));
    <a name="a0"></a><a class="code" href="group__snmp__alarm.html#ga6eb45344f1145c3e323824411089cefe" title="This function registers function callbacks to occur at a speciifc time in the future.">snmp_alarm_register</a>(30,     <span class="comment">/* seconds */</span>
                        SA_REPEAT,      <span class="comment">/* repeat (every 30 seconds). */</span>
                        send_example_notification,      <span class="comment">/* our callback */</span>
                        NULL    <span class="comment">/* no callback data needed */</span>
        );
}

<span class="keywordtype">void</span>
send_example_notification(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> clientreg, <span class="keywordtype">void</span> *clientarg)
{
    <span class="comment">/*</span>
<span class="comment">     * define the OID for the notification we&#39;re going to send</span>
<span class="comment">     * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification </span>
<span class="comment">     */</span>
    oid             notification_oid[] =
        { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 0, 1 };
    <span class="keywordtype">size_t</span>          notification_oid_len = OID_LENGTH(notification_oid);
    <span class="keyword">static</span> u_long count = 0;

    <span class="comment">/*</span>
<span class="comment">     * In the notification, we have to assign our notification OID to</span>
<span class="comment">     * the snmpTrapOID.0 object. Here is it&#39;s definition. </span>
<span class="comment">     */</span>
    oid             objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
    <span class="keywordtype">size_t</span>          objid_snmptrap_len = OID_LENGTH(objid_snmptrap);

    <span class="comment">/*</span>
<span class="comment">     * define the OIDs for the varbinds we&#39;re going to include</span>
<span class="comment">     *  with the notification -</span>
<span class="comment">     * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate  and</span>
<span class="comment">     * NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatName </span>
<span class="comment">     */</span>
    oid      hbeat_rate_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 1, 0 };
    <span class="keywordtype">size_t</span>   hbeat_rate_oid_len = OID_LENGTH(hbeat_rate_oid);
    oid      hbeat_name_oid[]   = { 1, 3, 6, 1, 4, 1, 8072, 2, 3, 2, 2, 0 };
    <span class="keywordtype">size_t</span>   hbeat_name_oid_len = OID_LENGTH(hbeat_name_oid);

    <span class="comment">/*</span>
<span class="comment">     * here is where we store the variables to be sent in the trap </span>
<span class="comment">     */</span>
    <a name="_a1"></a><a class="code" href="structvariable__list.html" title="The netsnmp variable list binding structure, it&#39;s typedef&#39;d to netsnmp_variable_list.">netsnmp_variable_list</a> *notification_vars = NULL;
    <span class="keyword">const</span> <span class="keywordtype">char</span> *heartbeat_name = <span class="stringliteral">&quot;A girl named Maria&quot;</span>;
<span class="preprocessor">#ifdef  RANDOM_HEARTBEAT</span>
<span class="preprocessor"></span>    <span class="keywordtype">int</span>  heartbeat_rate = rand() % 60;
<span class="preprocessor">#else</span>
<span class="preprocessor"></span>    <span class="keywordtype">int</span>  heartbeat_rate = 30;
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
    DEBUGMSGTL((<span class="stringliteral">&quot;example_notification&quot;</span>, <span class="stringliteral">&quot;defining the trap\n&quot;</span>));

    <span class="comment">/*</span>
<span class="comment">     * add in the trap definition object </span>
<span class="comment">     */</span>
    snmp_varlist_add_variable(&amp;notification_vars,
                              <span class="comment">/*</span>
<span class="comment">                               * the snmpTrapOID.0 variable </span>
<span class="comment">                               */</span>
                              objid_snmptrap, objid_snmptrap_len,
                              <span class="comment">/*</span>
<span class="comment">                               * value type is an OID </span>
<span class="comment">                               */</span>
                              ASN_OBJECT_ID,
                              <span class="comment">/*</span>
<span class="comment">                               * value contents is our notification OID </span>
<span class="comment">                               */</span>
                              (u_char *) notification_oid,
                              <span class="comment">/*</span>
<span class="comment">                               * size in bytes = oid length * sizeof(oid) </span>
<span class="comment">                               */</span>
                              notification_oid_len * <span class="keyword">sizeof</span>(oid));

    <span class="comment">/*</span>
<span class="comment">     * add in the additional objects defined as part of the trap</span>
<span class="comment">     */</span>

    snmp_varlist_add_variable(&amp;notification_vars,
                               hbeat_rate_oid, hbeat_rate_oid_len,
                               ASN_INTEGER,
                              (u_char *)&amp;heartbeat_rate,
                                  <span class="keyword">sizeof</span>(heartbeat_rate));

    <span class="comment">/*</span>
<span class="comment">     * if we want to insert additional objects, we do it here </span>
<span class="comment">     */</span>
    <span class="keywordflow">if</span> (heartbeat_rate &lt; 30 ) {
        snmp_varlist_add_variable(&amp;notification_vars,
                               hbeat_name_oid, hbeat_name_oid_len,
                               ASN_OCTET_STR,
                               heartbeat_name, strlen(heartbeat_name));
    }

    <span class="comment">/*</span>
<span class="comment">     * send the trap out.  This will send it to all registered</span>
<span class="comment">     * receivers (see the &quot;SETTING UP TRAP AND/OR INFORM DESTINATIONS&quot;</span>
<span class="comment">     * section of the snmpd.conf manual page. </span>
<span class="comment">     */</span>
    ++count;
    DEBUGMSGTL((<span class="stringliteral">&quot;example_notification&quot;</span>, <span class="stringliteral">&quot;sending trap %ld\n&quot;</span>,count));
    <a name="a2"></a><a class="code" href="group__agent__trap.html#gac9fa0228d574f715b60cd93b87968a15" title="Uses the supplied list of variable bindings to form an SNMPv2 trap, which is sent to SNMPv2-capable s...">send_v2trap</a>(notification_vars);

    <span class="comment">/*</span>
<span class="comment">     * free the created notification variable list </span>
<span class="comment">     */</span>
    DEBUGMSGTL((<span class="stringliteral">&quot;example_notification&quot;</span>, <span class="stringliteral">&quot;cleaning up\n&quot;</span>));
    snmp_free_varbind(notification_vars);
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>