Current File : //usr/share/doc/net-snmp/html/watched_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: watched.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">watched.c</div>  </div>
</div><!--header-->
<div class="contents">
<p>These examples creates some scalar registrations that allows some simple variables to be accessed via SNMP. In a more realistic example, it is likely that these variables would also be manipulated in other ways outside of SNMP gets/sets.</p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * watched.c</span>
<span class="comment"> * $Id: watched.c 12097 2005-04-20 18:03:47Z rstory $</span>
<span class="comment"> *</span>
<span class="comment"> */</span>
<span class="comment">/*</span>
<span class="comment"> * start by 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="keywordtype">void</span> init_watched_string(<span class="keywordtype">void</span>);

<span class="keywordtype">void</span> init_watched(<span class="keywordtype">void</span>)
{
    init_watched_string();
}

<span class="keywordtype">void</span> init_watched_string(<span class="keywordtype">void</span>)
{
    <span class="comment">/*</span>
<span class="comment">     * the storage for our string. It must be static or allocated.</span>
<span class="comment">     * we use static here for simplicity.</span>
<span class="comment">     */</span>
    <span class="keyword">static</span> <span class="keywordtype">char</span> my_string[256] = <span class="stringliteral">&quot;So long, and thanks for all the fish!&quot;</span>;

    <span class="comment">/*</span>
<span class="comment">     * the OID we want to register our string at.  This should be a</span>
<span class="comment">     * fully qualified instance.  In our case, it&#39;s a scalar at:</span>
<span class="comment">     * NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0  (note the trailing</span>
<span class="comment">     *  0 which is required for any instantiation of any scalar object) </span>
<span class="comment">     */</span>
    oid             my_registration_oid[] =
        { 1, 3, 6, 1, 4, 1, 8072, 2, 1, 3, 0 };

    <span class="comment">/*</span>
<span class="comment">     * variables needed for registration</span>
<span class="comment">     */</span>
    <a name="_a0"></a><a class="code" href="structnetsnmp__handler__registration__s.html" title="Root registration info.">netsnmp_handler_registration</a> *reginfo;
    <a name="_a1"></a><a class="code" href="structnetsnmp__watcher__info__s.html">netsnmp_watcher_info</a> *watcher_info;
    <span class="keywordtype">int</span> watcher_flags;

    <span class="comment">/*</span>
<span class="comment">     * a debugging statement.  Run the agent with -Dexample_string_instance</span>
<span class="comment">     * to see the output of this debugging statement. </span>
<span class="comment">     */</span>
    DEBUGMSGTL((<span class="stringliteral">&quot;example_string_instance&quot;</span>,
                <span class="stringliteral">&quot;Initalizing example string instance.  Default value = %s\n&quot;</span>,
                my_string));

    <span class="comment">/*</span>
<span class="comment">     * create the registration info for our string. If you want to</span>
<span class="comment">     *</span>
<span class="comment">     * If we wanted a callback when the value was retrieved or set</span>
<span class="comment">     * (even though the details of doing this are handled for you),</span>
<span class="comment">     * you could change the NULL pointer below to a valid handler</span>
<span class="comment">     * function.</span>
<span class="comment">     *</span>
<span class="comment">     * Change RWRITE to RONLY for a read-only string.</span>
<span class="comment">     */</span>
    reginfo = netsnmp_create_handler_registration(<span class="stringliteral">&quot;my example string&quot;</span>, NULL,
                                                  my_registration_oid,
                                                  OID_LENGTH(my_registration_oid),
                                                  HANDLER_CAN_RWRITE);
                                                  
    <span class="comment">/*</span>
<span class="comment">     * the two options for a string watcher are:</span>
<span class="comment">     *   fixed size string (length never changes)</span>
<span class="comment">     *   variable size (length can be 0 - MAX, for some MAX)</span>
<span class="comment">     *</span>
<span class="comment">     * we&#39;ll use a variable length string.</span>
<span class="comment">     */</span>
    watcher_flags = WATCHER_MAX_SIZE;

    <span class="comment">/*</span>
<span class="comment">     * create the watcher info for our string, and set the max size.</span>
<span class="comment">     */</span>
    watcher_info =
        netsnmp_create_watcher_info(my_string, strlen(my_string),
                                    ASN_OCTET_STR, watcher_flags);
    watcher_info-&gt;<a name="a2"></a>max_size = <span class="keyword">sizeof</span>(my_string);

    <span class="comment">/*</span>
<span class="comment">     * the line below registers our &quot;my_string&quot; variable above as</span>
<span class="comment">     * accessible and makes it writable. </span>
<span class="comment">     * </span>
<span class="comment">     * If we wanted a callback when the value was retrieved or set</span>
<span class="comment">     * (even though the details of doing this are handled for you),</span>
<span class="comment">     * you could change the NULL pointer below to a valid handler</span>
<span class="comment">     * function. </span>
<span class="comment">     */</span>
    netsnmp_register_watched_instance(reginfo, watcher_info);

    DEBUGMSGTL((<span class="stringliteral">&quot;example_string_instance&quot;</span>,
                <span class="stringliteral">&quot;Done initalizing example string instance\n&quot;</span>));
}
</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>