Blame local/mib2c.perl.conf

Packit fcad23
## ########################################################################
Packit fcad23
##
Packit fcad23
## Config for generating modules for use in the embedded perl environmentg
Packit fcad23
##
Packit fcad23
## Copyright Tripleplay Services Limited 2005
Packit fcad23
## All rights reserved.
Packit fcad23
##
Packit fcad23
## Use is subject to license terms specified in the COPYING file
Packit fcad23
## distributed with the Net-SNMP package.
Packit fcad23
##
Packit fcad23
## ########################################################################
Packit fcad23
##
Packit fcad23
## Gotchas.
Packit fcad23
## Any $ signs in the output will be snaffled and disappear. For this reason
Packit fcad23
## lines that need to output perl variables use a printf line instead
Packit fcad23
##
Packit fcad23
## Comments that are for the use of documenting this config file
Packit fcad23
## need to be double hashes
Packit fcad23
##
Packit fcad23
## Operation
Packit fcad23
## 1. Creates a file called output.tmp which contains the main data structures
Packit fcad23
## and a call to the agent startup function.
Packit fcad23
## 2. Creates a file called skel.tmp with skeleton accessor functions. 
Packit fcad23
## This should be copied to functions.pl and edited to actually
Packit fcad23
## do the work required for each leaf.
Packit fcad23
## 3. Creates an output file with the OID name. This contains the bolierplate
Packit fcad23
## and the data structures
Packit fcad23
## This is the file to reference in the snmpd.conf file
Packit fcad23
## 
Packit fcad23
## The user must fill in the functions.pl code as requried. This file is then
Packit fcad23
## included at run time at the top of the generated perl code 
Packit fcad23
## (do 'functions.pl)
Packit fcad23
## 
Packit fcad23
## The generated file needs the NetSNMP::agent::Support.pm module in the
Packit fcad23
## system. This module contains the run-time support for the agent.
Packit fcad23
##
Packit fcad23
## The oidtable is a hash of hashes with the top level key an OID
Packit fcad23
## There are two types of entry
Packit fcad23
## 1. Scalars have the full OID plus the .0 index string
Packit fcad23
## 2. Columnar data has the index fields set to 0. The NetSNMP::agent::Support
Packit fcad23
##    code will use zeros to locate the table specific handlers.
Packit fcad23
##
Packit fcad23
## #########################################################################
Packit fcad23
Packit fcad23
Packit fcad23
## #########################################################################
Packit fcad23
## Define the 'macros' used later in this config file
Packit fcad23
## #########################################################################
Packit fcad23
@define EMIT_INDEX_VARS@
Packit fcad23
  ##
Packit fcad23
  ## Calculate the number of index identifiers and then
Packit fcad23
  ## for each identifier work out the offset in the oid
Packit fcad23
  ##
Packit fcad23
  @eval $numindex=0@
Packit fcad23
  @eval $idxoffset = $c.oidlength@
Packit fcad23
  # The values of the oid elements for the indexes
Packit fcad23
  @foreach $i index@
Packit fcad23
##  my $$idx_$i = getOidElement($$idx, $idxoffset);
Packit fcad23
  @printf "  my %sidx_$vars{'i'} = getOidElement(%soid, $vars{'idxoffset'});\n",$,$@
Packit fcad23
     @eval $idxoffset = $idxoffset + 1@
Packit fcad23
  @end@
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@define EMIT_GETARGS@
Packit fcad23
  ##
Packit fcad23
  ## Output the code the get the args for a function
Packit fcad23
  ##
Packit fcad23
  # The OID is passed as a NetSNMP::OID object
Packit fcad23
  @printf "  my (%soid) = shift;\n",$@
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@define EMIT_LOAD_DATA@
Packit fcad23
  ## 
Packit fcad23
  ## Emit the code to load a data table
Packit fcad23
  ##
Packit fcad23
  # Load the $t table data
Packit fcad23
  load_$t();
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@define EMIT_INDEX_INFO@
Packit fcad23
##
Packit fcad23
## Emit a list of indexes for a table as perl comments
Packit fcad23
## To be used when generating the comment fields for a handler
Packit fcad23
##
Packit fcad23
# In Table: $t
Packit fcad23
@foreach $i index@
Packit fcad23
# Index: $i
Packit fcad23
@end@
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@define EMIT_INDEX_WALKER@
Packit fcad23
##
Packit fcad23
## Output a skeleton index walker and index checker
Packit fcad23
## for the table if it has not been done already
Packit fcad23
##
Packit fcad23
@if $needwalker@
Packit fcad23
## Output skeleton index validator for table
Packit fcad23
# -------------------------------------------------------
Packit fcad23
# Index validation for table $t
Packit fcad23
# Checks the supplied OID is in range
Packit fcad23
# Returns 1 if it is and 0 if out of range
Packit fcad23
@calldefine EMIT_INDEX_INFO@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
sub check_$t {
Packit fcad23
  @calldefine EMIT_GETARGS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_INDEX_VARS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_LOAD_DATA@
Packit fcad23
Packit fcad23
  # Check the index is in range and valid
Packit fcad23
  return 1;
Packit fcad23
}
Packit fcad23
Packit fcad23
# -------------------------------------------------------
Packit fcad23
# Index walker for table $t
Packit fcad23
# Given an OID for a table, returns the next OID in range, 
Packit fcad23
# or if no more OIDs it returns 0.
Packit fcad23
@calldefine EMIT_INDEX_INFO@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
sub next_$t {
Packit fcad23
  @calldefine EMIT_GETARGS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_INDEX_VARS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_LOAD_DATA@
Packit fcad23
Packit fcad23
  # Return the next OID if there is one
Packit fcad23
  # or return 0 if no more OIDs in this table
Packit fcad23
  return 0;
Packit fcad23
}
Packit fcad23
@eval $needwalker = 0@    ## Dont need this again for the current table
Packit fcad23
@end@
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@define EMIT_TABLE_LOAD@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
# Loader for table $t
Packit fcad23
# Edit this function to load the data needed for $t
Packit fcad23
# This function gets called for every request to columnar
Packit fcad23
# data in the $t table
Packit fcad23
# -------------------------------------------------------
Packit fcad23
sub load_$t { 
Packit fcad23
  
Packit fcad23
}  
Packit fcad23
@enddefine@
Packit fcad23
Packit fcad23
@eval $date=scalar localtime; @
Packit fcad23
Packit fcad23
## Open the output file and emit the perl startup bolierplate
Packit fcad23
@open output.tmp@
Packit fcad23
#!/usr/bin/perl -w
Packit fcad23
#
Packit fcad23
#
Packit fcad23
# WARNING: DO NOT EDIT THIS FILE BY HAND.
Packit fcad23
#
Packit fcad23
# This file has been generated by mib2c using the mib2c.perl.conf file
Packit fcad23
# This is intended to be used by the net-snmp agent with embedded perl
Packit fcad23
# support. See perldoc NetSNMP::agent
Packit fcad23
#
Packit fcad23
# Created on $date
Packit fcad23
#
Packit fcad23
# To load this into a running agent with embedded perl support turned
Packit fcad23
# on, simply put the following line (without the leading # mark) your
Packit fcad23
# snmpd.conf file:
Packit fcad23
#
Packit fcad23
@printf "# perl do 'path/to/agent_%s.pl'\n",$oid@
Packit fcad23
#
Packit fcad23
# You will need a copy of NetSNMP installed. This has been developed using
Packit fcad23
# NetSNMP version 5.2.2
Packit fcad23
#
Packit fcad23
Packit fcad23
Packit fcad23
Packit fcad23
##use strict;
Packit fcad23
use NetSNMP::agent::Support;
Packit fcad23
use NetSNMP::ASN (':all');
Packit fcad23
Packit fcad23
# Include the functions to handle the nodes
Packit fcad23
do 'functions.pl';
Packit fcad23
Packit fcad23
## Create the skeleton file ready for the skeleton handlers later on
Packit fcad23
@push@
Packit fcad23
@open skel.tmp@
Packit fcad23
# Skeleton accessor functions.
Packit fcad23
# DO NOT EDIT 
Packit fcad23
# This file will be overwritten next time mib2c is run.
Packit fcad23
# Copy this file to functions.pl and then edit it.
Packit fcad23
@close skel.tmp@
Packit fcad23
@pop@
Packit fcad23
Packit fcad23
## Generate the hash of hashes with the oids and handlers for the tables
Packit fcad23
# Hash for all OIDs
Packit fcad23
@printf "my %soidtable={\n", $@
Packit fcad23
# Table objects
Packit fcad23
@foreach $t table@
Packit fcad23
 @print Processing table $t@
Packit fcad23
 @push@
Packit fcad23
 @append skel.tmp@
Packit fcad23
## Output skeleton loader for this table
Packit fcad23
  @calldefine EMIT_TABLE_LOAD@
Packit fcad23
  @close skel.tmp@
Packit fcad23
  @pop@
Packit fcad23
  ## 
Packit fcad23
  @eval $needwalker = 1@   ## Need the walker and checker once this table
Packit fcad23
  @foreach $c nonindex@    
Packit fcad23
   @if $c.accessible @
Packit fcad23
    ##
Packit fcad23
    ## Generate the entry for the hash table
Packit fcad23
    ## We first calculate the number of index items for this table
Packit fcad23
    @eval $numindex = 0@
Packit fcad23
    @eval $idxelem = ""@
Packit fcad23
    @foreach $i index@
Packit fcad23
      @perleval $vars{'idxelem'} .= '.0'; 0; @
Packit fcad23
      @eval $numindex = $numindex+1@
Packit fcad23
    @end@
Packit fcad23
    "$c.objectID$idxelem"=>{func=>\&get_$c,type=>$c.type, check=>\&check_$t, nextoid=>\&next_$t, istable=>'1', next=>"", numindex=>$numindex},
Packit fcad23
    ## Output skeleton handlers for this column object
Packit fcad23
    @push@
Packit fcad23
    @append skel.tmp@
Packit fcad23
@calldefine EMIT_INDEX_WALKER@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
# Handler for columnar object '$c' 
Packit fcad23
# OID: $c.objectID
Packit fcad23
# Syntax: $c.type
Packit fcad23
# From: $c.module
Packit fcad23
@calldefine EMIT_INDEX_INFO@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
sub get_$c { 
Packit fcad23
  @calldefine EMIT_GETARGS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_INDEX_VARS@
Packit fcad23
Packit fcad23
  @calldefine EMIT_LOAD_DATA@
Packit fcad23
Packit fcad23
  # Code here to read the required variable from the loaded table
Packit fcad23
  # using whatever indexing you need.
Packit fcad23
  # The index has already been checked and found to be valid
Packit fcad23
Packit fcad23
  ## Add further types as required. 
Packit fcad23
  @if $c.type eq "ASN_INTEGER"@
Packit fcad23
  return 32;
Packit fcad23
  @end@
Packit fcad23
  @if $c.type eq "ASN_OCTET_STR"@
Packit fcad23
  return "STR";
Packit fcad23
  @end@
Packit fcad23
  @if $c.type eq "ASN_COUNTER64"@
Packit fcad23
  return 64;
Packit fcad23
  @end@
Packit fcad23
}
Packit fcad23
  @close skel.tmp@
Packit fcad23
    @pop@
Packit fcad23
  @end@
Packit fcad23
 @end@
Packit fcad23
@end@
Packit fcad23
@print Processing scalars@
Packit fcad23
## output the hash with the OIDs and handlers
Packit fcad23
## Scalars have a single index element
Packit fcad23
# Scalars
Packit fcad23
@foreach $s scalar@
Packit fcad23
 @if $s.accessible@
Packit fcad23
	'$s.objectID.0'=>{func=>\&get_$s,type=>$s.type,next=>"", numindex=>1},	
Packit fcad23
 @end@
Packit fcad23
@end@
Packit fcad23
##End of the OID hash
Packit fcad23
};
Packit fcad23
Packit fcad23
## Emit code to register the top level oid with the agent
Packit fcad23
## The $oid variable comes from mib2c as the last non option arg
Packit fcad23
# Register the top oid with the agent
Packit fcad23
@printf "registerAgent(%sagent, '$oid', %soidtable);",$,$@
Packit fcad23
Packit fcad23
## Output skeleton handlers for the scalars
Packit fcad23
@push@
Packit fcad23
@append skel.tmp@
Packit fcad23
@foreach $s scalar@
Packit fcad23
 @if $s.accessible@
Packit fcad23
# -------------------------------------------------------
Packit fcad23
# Handler for scalar object $s
Packit fcad23
# OID: $s.objectID
Packit fcad23
# Syntax: $s.type
Packit fcad23
# From: $s.module
Packit fcad23
# -------------------------------------------------------
Packit fcad23
sub get_$s { 
Packit fcad23
Packit fcad23
  # Add code here to read the value required and return it
Packit fcad23
Packit fcad23
  ## Add further types as required. 
Packit fcad23
  @if $s.type eq "ASN_INTEGER"@
Packit fcad23
  return 32;
Packit fcad23
  @end@
Packit fcad23
  @if $s.type eq "ASN_OCTET_STR"@
Packit fcad23
  return "STR";
Packit fcad23
  @end@
Packit fcad23
  @if $s.type eq "ASN_COUNTER64"@
Packit fcad23
  return 64;
Packit fcad23
  @end@
Packit fcad23
}
Packit fcad23
 @end@
Packit fcad23
@end@
Packit fcad23
@close skel.tmp@
Packit fcad23
@pop@
Packit fcad23
Packit fcad23
@close output.tmp@
Packit fcad23
##
Packit fcad23
## Now create the code file from the outputfile
Packit fcad23
##
Packit fcad23
@startperl@
Packit fcad23
my $oidname = $vars{'oid'};
Packit fcad23
my $out = "agent_" . $oidname .".pl";
Packit fcad23
system("cat output.tmp > $out");
Packit fcad23
@endperl@
Packit fcad23
@print Output code generated.@
Packit fcad23