| 1 |
#!/usr/bin/perl |
| 2 |
#!/usr/bin/perl -w |
| 3 |
|
| 4 |
# |
| 5 |
# $Id$ |
| 6 |
# |
| 7 |
# Description: |
| 8 |
# |
| 9 |
# This program, given an OID reference as an argument, creates some |
| 10 |
# template mib module files to be used with the net-snmp agent. It is |
| 11 |
# far from perfect and will not generate working modules, but it |
| 12 |
# significantly shortens development time by outlining the basic |
| 13 |
# structure. |
| 14 |
# |
| 15 |
# Its up to you to verify what it does and change the default values |
| 16 |
# it returns. |
| 17 |
# |
| 18 |
|
| 19 |
# SNMP |
| 20 |
my $havesnmp = eval {require SNMP;}; |
| 21 |
my $havenetsnmpoid = eval {require NetSNMP::OID;}; |
| 22 |
|
| 23 |
if (!$havesnmp) { |
| 24 |
print " |
| 25 |
ERROR: You don't have the SNMP perl module installed. Please obtain |
| 26 |
this by getting the latest source release of the net-snmp toolkit from |
| 27 |
http://www.net-snmp.org/download/ . Once you download the source and |
| 28 |
unpack it, the perl module is contained in the perl/SNMP directory. |
| 29 |
See the README file there for instructions. |
| 30 |
|
| 31 |
"; |
| 32 |
exit; |
| 33 |
} |
| 34 |
|
| 35 |
if ($havesnmp) { |
| 36 |
eval { import SNMP; } |
| 37 |
} |
| 38 |
if ($havenetsnmp) { |
| 39 |
eval { import NetSNMP::OID; } |
| 40 |
} |
| 41 |
use FileHandle; |
| 42 |
|
| 43 |
#use strict 'vars'; |
| 44 |
$SNMP::save_descriptions=1; |
| 45 |
$SNMP::use_long_names=1; |
| 46 |
$SNMP::use_enums=1; |
| 47 |
SNMP::initMib(); |
| 48 |
|
| 49 |
$configfile="mib2c.conf"; |
| 50 |
$debug=0; |
| 51 |
$quiet=0; |
| 52 |
$strict_unk_token = 0; |
| 53 |
$noindent = 0; |
| 54 |
$nosed = 0; |
| 55 |
$currentline = 0; |
| 56 |
$currentlevel = -1; |
| 57 |
%assignments; |
| 58 |
%outputs; |
| 59 |
@def_search_dirs = ("."); |
| 60 |
@search_dirs = (); |
| 61 |
if($ENV{MIB2C_DIR}) { |
| 62 |
push @def_search_dirs, split(/:/, $ENV{MIB2C_DIR}); |
| 63 |
} |
| 64 |
push @def_search_dirs, "/etc/snmp/"; |
| 65 |
push @def_search_dirs, "/usr/share/snmp/"; |
| 66 |
push @def_search_dirs, "/usr/share/snmp/mib2c-data"; |
| 67 |
push @def_search_dirs, "./mib2c-conf.d"; |
| 68 |
|
| 69 |
sub usage { |
| 70 |
print "$0 [-h] [-c configfile] [-f prefix] mibNode\n\n"; |
| 71 |
print " -h\t\tThis message.\n\n"; |
| 72 |
print " -c configfile\tSpecifies the configuration file to use\n\t\tthat dictates what the output of mib2c will look like.\n\n"; |
| 73 |
print " -I PATH\tSpecifies a path to look for configuration files in\n\n"; |
| 74 |
print " -f prefix\tSpecifies the output prefix to use. All code\n\t\twill be put into prefix.c and prefix.h\n\n"; |
| 75 |
print " -d\t\tdebugging output (don't do it. trust me.)\n\n"; |
| 76 |
print " -S VAR=VAL\tSet \$VAR variable to \$VAL\n\n"; |
| 77 |
print " -i\t\tDon't run indent on the resulting code\n\n"; |
| 78 |
print " -s\t\tDon't look for mibNode.sed and run sed on the resulting code\n\n"; |
| 79 |
print " mibNode\tThe name of the top level mib node you want to\n\t\tgenerate code for. By default, the code will be stored in\n\t\tmibNode.c and mibNode.h (use the -f flag to change this)\n\n"; |
| 80 |
1; |
| 81 |
} |
| 82 |
|
| 83 |
my @origargs = @ARGV; |
| 84 |
my $args_done = 0; |
| 85 |
while($#ARGV >= 0) { |
| 86 |
$_ = shift; |
| 87 |
if (/^-/) { |
| 88 |
if ($args_done != 0) { |
| 89 |
warn "all argument must be specified before the mibNode!\n"; |
| 90 |
usage; |
| 91 |
exit 1; |
| 92 |
} elsif (/^-c/) { |
| 93 |
$configfile = shift; |
| 94 |
} elsif (/^-d/) { |
| 95 |
$debug = 1; |
| 96 |
} elsif (/^-S/) { |
| 97 |
my $expr = shift; |
| 98 |
my ($var, $val) = ($expr =~ /([^=]*)=(.*)/); |
| 99 |
die "no variable specified for -S flag." if (!$var); |
| 100 |
$assignments{$var} = $val; |
| 101 |
} elsif (/^-q/) { |
| 102 |
$quiet = 1; |
| 103 |
} elsif (/^-i/) { |
| 104 |
$noindent = 1; |
| 105 |
} elsif (/^-s/) { |
| 106 |
$nosed = 1; |
| 107 |
} elsif (/^-h/) { |
| 108 |
usage && exit(1); |
| 109 |
} elsif (/^-f/) { |
| 110 |
$outputName = shift; |
| 111 |
} elsif (/^-I/) { |
| 112 |
my $dirs = shift; |
| 113 |
push @search_dirs, split(/,/,$dirs); |
| 114 |
} else { |
| 115 |
warn "Unknown option '$_'\n"; |
| 116 |
usage; |
| 117 |
exit 1; |
| 118 |
} |
| 119 |
} else { |
| 120 |
$args_done = 1; |
| 121 |
warn "Replacing previous mibNode $oid with $_\n" if ($oid); |
| 122 |
$oid = $_ ; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
# |
| 127 |
# internal conversion tables |
| 128 |
# |
| 129 |
|
| 130 |
%accessToIsWritable = qw(ReadOnly 0 ReadWrite 1 |
| 131 |
WriteOnly 1 Create 1); |
| 132 |
%perltoctypes = qw(OCTETSTR ASN_OCTET_STR |
| 133 |
INTEGER ASN_INTEGER |
| 134 |
INTEGER32 ASN_INTEGER |
| 135 |
UNSIGNED32 ASN_UNSIGNED |
| 136 |
OBJECTID ASN_OBJECT_ID |
| 137 |
COUNTER64 ASN_COUNTER64 |
| 138 |
COUNTER ASN_COUNTER |
| 139 |
NETADDR ASN_COUNTER |
| 140 |
UINTEGER ASN_UINTEGER |
| 141 |
IPADDR ASN_IPADDRESS |
| 142 |
BITS ASN_OCTET_STR |
| 143 |
TICKS ASN_TIMETICKS |
| 144 |
GAUGE ASN_GAUGE |
| 145 |
OPAQUE ASN_OPAQUE); |
| 146 |
%perltodecl = ("OCTETSTR", "char", |
| 147 |
"INTEGER", "long", |
| 148 |
"INTEGER32", "long", |
| 149 |
"UNSIGNED32", "u_long", |
| 150 |
"UINTEGER", "u_long", |
| 151 |
"OBJECTID", "oid", |
| 152 |
"COUNTER64", "struct counter64", |
| 153 |
"COUNTER", "u_long", |
| 154 |
"IPADDR", "in_addr_t", |
| 155 |
"BITS", "char", |
| 156 |
"TICKS", "u_long", |
| 157 |
"GAUGE", "u_long", |
| 158 |
"OPAQUE", "u_char"); |
| 159 |
%perltolen = ("OCTETSTR", "1", |
| 160 |
"INTEGER", "0", |
| 161 |
"INTEGER32", "0", |
| 162 |
"UNSIGNED32", "0", |
| 163 |
"UINTEGER", "0", |
| 164 |
"OBJECTID", "1", |
| 165 |
"COUNTER64", "0", |
| 166 |
"COUNTER", "0", |
| 167 |
"IPADDR", "0", |
| 168 |
"BITS", "1", |
| 169 |
"TICKS", "0", |
| 170 |
"GAUGE", "0", |
| 171 |
"OPAQUE", "1"); |
| 172 |
|
| 173 |
my $mibnode = $SNMP::MIB{$oid}; |
| 174 |
|
| 175 |
if (!$mibnode) { |
| 176 |
|
| 177 |
print STDERR " |
| 178 |
You didn't give mib2c a valid OID to start with. IE, I could not find |
| 179 |
any information about the mib node \"$oid\". This could be caused |
| 180 |
because you supplied an incorrectly node, or by the MIB that you're |
| 181 |
trying to generate code from isn't loaded. To make sure your mib is |
| 182 |
loaded, run mib2c using this as an example: |
| 183 |
|
| 184 |
env MIBS=\"+MY-PERSONAL-MIB\" mib2c " . join(" ",@origargs) . " |
| 185 |
|
| 186 |
You might wish to start by reading the MIB loading tutorial at: |
| 187 |
|
| 188 |
http://www.net-snmp.org/tutorial-5/commands/mib-options.html |
| 189 |
|
| 190 |
And making sure you can get snmptranslate to display information about |
| 191 |
your MIB node. Once snmptranslate works, then come back and try mib2c |
| 192 |
again. |
| 193 |
|
| 194 |
"; |
| 195 |
exit 1; |
| 196 |
} |
| 197 |
|
| 198 |
# setup |
| 199 |
$outputName = $mibnode->{'label'} if (!defined($outputName)); |
| 200 |
$outputName =~ s/-/_/g; |
| 201 |
$vars{'name'} = $outputName; |
| 202 |
$vars{'oid'} = $oid; |
| 203 |
$vars{'example_start'} = " /*\n" . |
| 204 |
" ***************************************************\n" . |
| 205 |
" *** START EXAMPLE CODE ***\n" . |
| 206 |
" ***---------------------------------------------***/"; |
| 207 |
$vars{'example_end'} = " /*\n" . |
| 208 |
" ***---------------------------------------------***\n" . |
| 209 |
" *** END EXAMPLE CODE ***\n" . |
| 210 |
" ***************************************************/"; |
| 211 |
|
| 212 |
# loop through mib nodes, remembering stuff. |
| 213 |
setup_data($mibnode); |
| 214 |
|
| 215 |
if(($ENV{HOME}) && (-f "$ENV{HOME}/.snmp/mib2c.conf")) { |
| 216 |
$fh = open_conf("$ENV{HOME}/.snmp/mib2c.conf"); |
| 217 |
process("-balanced"); |
| 218 |
$fh->close; |
| 219 |
} |
| 220 |
|
| 221 |
my $defaults = find_conf("default-$configfile",1); |
| 222 |
if (-f "$defaults" ) { |
| 223 |
$fh = open_conf($defaults); |
| 224 |
process("-balanced"); |
| 225 |
$fh->close; |
| 226 |
} |
| 227 |
|
| 228 |
my @theassignments = keys(%assignments); |
| 229 |
if ($#theassignments != -1) { |
| 230 |
foreach $var (@theassignments) { |
| 231 |
$vars{$var} = $assignments{$var}; |
| 232 |
} |
| 233 |
} |
| 234 |
$configfile = find_conf($configfile,0); |
| 235 |
$fh = open_conf($configfile); |
| 236 |
process("-balanced"); |
| 237 |
$fh->close; |
| 238 |
|
| 239 |
if (-f "$outputName.sed" && !$nosed) { |
| 240 |
foreach $i (keys(%written)) { |
| 241 |
next if ($i eq "-"); |
| 242 |
next if (!($i =~ /\.[ch]$/)); |
| 243 |
print STDERR "running sed --in-place=.orig --file=$outputName.sed $i\n" if (!$quiet); |
| 244 |
system("sed --in-place=.orig --file=$outputName.sed $i"); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (!$noindent) { |
| 249 |
foreach $i (keys(%written)) { |
| 250 |
next if ($i eq "-"); |
| 251 |
next if (!($i =~ /\.[ch]$/)); |
| 252 |
print STDERR "running indent on $i\n" if (!$quiet); |
| 253 |
system("indent -orig -nbc -bap -nut -nfca -T size_t -T netsnmp_mib_handler -T netsnmp_handler_registration -T netsnmp_delegated_cache -T netsnmp_mib_handler_methods -T netsnmp_old_api_info -T netsnmp_old_api_cache -T netsnmp_set_info -T netsnmp_request_info -T netsnmp_set_info -T netsnmp_tree_cache -T netsnmp_agent_request_info -T netsnmp_cachemap -T netsnmp_agent_session -T netsnmp_array_group_item -T netsnmp_array_group -T netsnmp_table_array_callbacks -T netsnmp_table_row -T netsnmp_table_data -T netsnmp_table_data_set_storage -T netsnmp_table_data_set -T netsnmp_column_info -T netsnmp_table_registration_info -T netsnmp_table_request_info -T netsnmp_iterator_info -T netsnmp_data_list -T netsnmp_oid_array_header -T netsnmp_oid_array_header_wrapper -T netsnmp_oid_stash_node -T netsnmp_pdu -T netsnmp_request_list -T netsnmp_callback_pass -T netsnmp_callback_info -T netsnmp_transport -T netsnmp_transport_list -T netsnmp_tdomain $i"); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
sub m2c_die { |
| 258 |
warn "ERROR: ". $_[0] . "\n"; |
| 259 |
die " at $currentfile:$currentline\n"; |
| 260 |
} |
| 261 |
|
| 262 |
sub tocommas { |
| 263 |
my $oid = $_[0]; |
| 264 |
$oid =~ s/\./,/g; |
| 265 |
$oid =~ s/^\s*,//; |
| 266 |
return $oid; |
| 267 |
} |
| 268 |
|
| 269 |
sub oidlength { |
| 270 |
return (scalar split(/\./, $_[0])) - 1; |
| 271 |
} |
| 272 |
|
| 273 |
# replaces $VAR type expressions and $VAR.subcomponent expressions |
| 274 |
# with data from the mib tree and loop variables. |
| 275 |
# possible uses: |
| 276 |
# |
| 277 |
# $var -- as defined by loops, etc. |
| 278 |
# ${var}otherstuff -- appending text to variable contents |
| 279 |
# $var.uc -- all upper case version of $var |
| 280 |
# |
| 281 |
# NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages |
| 282 |
# |
| 283 |
# Mib components, $var must first expand to a mib node name: |
| 284 |
# |
| 285 |
# $var.uc -- all upper case version of $var |
| 286 |
# |
| 287 |
# $var.objectID -- dotted, fully-qualified, and numeric OID |
| 288 |
# $var.commaoid -- comma separated numeric OID for array initialization |
| 289 |
# $var.oidlength -- length of the oid |
| 290 |
# $var.subid -- last number component of oid |
| 291 |
# $var.module -- MIB name that the object comes from |
| 292 |
# $var.parent -- contains the label of the parent node of $var. |
| 293 |
# |
| 294 |
# $var.isscalar -- returns 1 if var contains the name of a scalar |
| 295 |
# $var.iscolumn -- returns 1 if var contains the name of a column |
| 296 |
# $var.children -- returns 1 if var has children |
| 297 |
# |
| 298 |
# $var.perltype -- node's perl SYNTAX ($SNMP::MIB{node}{'syntax'}) |
| 299 |
# $var.type -- node's ASN_XXX type (Net-SNMP specific #define) |
| 300 |
# $var.decl -- C data type (char, u_long, ...) |
| 301 |
# |
| 302 |
# $var.readable -- 1 if an object is readable, 0 if not |
| 303 |
# $var.settable -- 1 if an object is writable, 0 if not |
| 304 |
# $var.creatable -- 1 if a column object can be created as part of a new row, 0 if not |
| 305 |
# $var.noaccess -- 1 if not-accessible, 0 if not |
| 306 |
# $var.accessible -- 1 if accessible, 0 if not |
| 307 |
# $var.storagetype -- 1 if an object is a StorageType object, 0 if not |
| 308 |
# $var.rowstatus -- 1 if an object is a RowStatus object, 0 if not |
| 309 |
# 'settable', 'creatable', 'lastchange', 'storagetype' and 'rowstatus' can |
| 310 |
# also be used with table variables to indicate whether it contains |
| 311 |
# writable, creatable, LastChange, StorageType or RowStatus column objects |
| 312 |
# |
| 313 |
# $var.hasdefval -- returns 1 if var has a DEFVAL clause |
| 314 |
# $var.defval -- node's DEFVAL |
| 315 |
# $var.hashint -- returns 1 if var has a HINT clause |
| 316 |
# $var.hint -- node's HINT |
| 317 |
# $var.ranges -- returns 1 if var has a value range defined |
| 318 |
# $var.enums -- returns 1 if var has enums defined for it. |
| 319 |
# $var.access -- node's access type |
| 320 |
# $var.status -- node's status |
| 321 |
# $var.syntax -- node's syntax |
| 322 |
# $var.reference -- node's reference |
| 323 |
# $var.description -- node's description |
| 324 |
|
| 325 |
sub process_vars { |
| 326 |
my $it = shift; |
| 327 |
|
| 328 |
# mib substitutions ($var.type -> $mibnode->{'type'}) |
| 329 |
if ( $it =~ /\$(\w+)\.(\w+)/ ) { |
| 330 |
if (!defined($vars{$1})) { |
| 331 |
m2c_die "Undefined variable \$$1"; |
| 332 |
} |
| 333 |
if ($SNMP::MIB{$vars{$1}} && defined($tables{$SNMP::MIB{$vars{$1}}{'label'}})) { |
| 334 |
$it =~ s/\$(\w+)\.(settable)/(table_is_writable($SNMP::MIB{$vars{$1}}{label}))/eg; |
| 335 |
$it =~ s/\$(\w+)\.(creatable)/(table_has_create($SNMP::MIB{$vars{$1}}{label}))/eg; |
| 336 |
$it =~ s/\$(\w+)\.(rowstatus)/(table_has_rowstatus($SNMP::MIB{$vars{$1}}{label}))/eg; |
| 337 |
$it =~ s/\$(\w+)\.(lastchange)/(table_has_lastchange($SNMP::MIB{$vars{$1}}{label}))/eg; |
| 338 |
$it =~ s/\$(\w+)\.(storagetype)/(table_has_storagetype($SNMP::MIB{$vars{$1}}{label}))/eg; |
| 339 |
} |
| 340 |
$it =~ s/\$(\w+)\.(uc)/uc($vars{$1})/eg; # make something uppercase |
| 341 |
$it =~ s/\$(\w+)\.(commaoid)/tocommas($SNMP::MIB{$vars{$1}}{objectID})/eg; |
| 342 |
$it =~ s/\$(\w+)\.(oidlength)/oidlength($SNMP::MIB{$vars{$1}}{objectID})/eg; |
| 343 |
$it =~ s/\$(\w+)\.(description)/$SNMP::MIB{$vars{$1}}{description}/g; |
| 344 |
$it =~ s/\$(\w+)\.(perltype)/$SNMP::MIB{$vars{$1}}{type}/g; |
| 345 |
$it =~ s/\$(\w+)\.(type)/$perltoctypes{$SNMP::MIB{$vars{$1}}{$2}}/g; |
| 346 |
$it =~ s/\$(\w+)\.(subid)/$SNMP::MIB{$vars{$1}}{subID}/g; |
| 347 |
$it =~ s/\$(\w+)\.(module)/$SNMP::MIB{$vars{$1}}{moduleID}/g; |
| 348 |
$it =~ s/\$(\w+)\.(settable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(ReadWrite|Create|WriteOnly)\/)?1:0)/eg; |
| 349 |
$it =~ s/\$(\w+)\.(creatable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Create)\/)?1:0)/eg; |
| 350 |
$it =~ s/\$(\w+)\.(readable)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(Read|Create)\/)?1:0)/eg; |
| 351 |
$it =~ s/\$(\w+)\.(noaccess)/(($SNMP::MIB{$vars{$1}}{access} =~ \/(NoAccess)\/)?1:0)/eg; |
| 352 |
$it =~ s/\$(\w+)\.(accessible)/(($SNMP::MIB{$vars{$1}}{access} !~ \/(NoAccess)\/)?1:0)/eg; |
| 353 |
$it =~ s/\$(\w+)\.(objectID|label|subID|access|status|syntax|reference)/$SNMP::MIB{$vars{$1}}{$2}/g; |
| 354 |
$it =~ s/\$(\w+)\.(decl)/$perltodecl{$SNMP::MIB{$vars{$1}}{type}}/g; |
| 355 |
$it =~ s/\$(\w+)\.(needlength)/$perltolen{$SNMP::MIB{$vars{$1}}{type}}/g; |
| 356 |
$it =~ s/\$(\w+)\.(iscolumn)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} =~ \/Entry$\/) ? 1 : 0/eg; |
| 357 |
$it =~ s/\$(\w+)\.(isscalar)/($SNMP::MIB{$vars{$1}}{'parent'}{'label'} !~ \/Entry$\/ && $SNMP::MIB{$vars{$1}}{access}) ? 1 : 0/eg; |
| 358 |
$it =~ s/\$(\w+)\.(parent)/$SNMP::MIB{$vars{$1}}{'parent'}{'label'}/g; |
| 359 |
$it =~ s/\$(\w+)\.(children)/($#{$SNMP::MIB{$vars{$1}}{'children'}} == 0) ? 0 : 1/eg; |
| 360 |
$it =~ s/\$(\w+)\.(hasdefval)/(length($SNMP::MIB{$vars{$1}}{'defaultValue'}) == 0) ? 0 : 1/eg; |
| 361 |
$it =~ s/\$(\w+)\.(defval)/$SNMP::MIB{$vars{$1}}{'defaultValue'}/g; |
| 362 |
$it =~ s/\$(\w+)\.(hashint)/(length($SNMP::MIB{$vars{$1}}{'hint'}) == 0) ? 0 : 1/eg; |
| 363 |
$it =~ s/\$(\w+)\.(hint)/$SNMP::MIB{$vars{$1}}{'hint'}/g; |
| 364 |
$it =~ s/\$(\w+)\.(ranges)/($#{$SNMP::MIB{$vars{$1}}{'ranges'}} == -1) ? 0 : 1/eg; |
| 365 |
# check for enums |
| 366 |
$it =~ s/\$(\w+)\.(enums)/(%{$SNMP::MIB{$vars{$1}}{'enums'}} == 0) ? 0 : 1/eg; |
| 367 |
$it =~ s/\$(\w+)\.(enumrange)/%{$SNMP::MIB{$vars{$1}}{'enums'}}/eg; |
| 368 |
$it =~ s/\$(\w+)\.(rowstatus)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(RowStatus)\/)?1:0)/eg; |
| 369 |
$it =~ s/\$(\w+)\.(storagetype)/(($SNMP::MIB{$vars{$1}}{syntax} =~ \/(StorageType)\/)?1:0)/eg; |
| 370 |
if ( $it =~ /\$(\w+)\.(\w+)/ ) { |
| 371 |
warn "Possible unknown variable attribute \$$1.$2 at $currentfile:$currentline\n"; |
| 372 |
} |
| 373 |
} |
| 374 |
# normal variable substitions |
| 375 |
$it =~ s/\$\{(\w+)\}/$vars{$1}/g; |
| 376 |
$it =~ s/\$(\w+)/$vars{$1}/g; |
| 377 |
# use $@var to put literal '$var' |
| 378 |
$it =~ s/\$\@(\w+)/\$$1/g; |
| 379 |
return $it; |
| 380 |
} |
| 381 |
|
| 382 |
# process various types of statements |
| 383 |
# |
| 384 |
# NOTE: THESE ARE AUTO-EXTRACTED/PROCESSED BY ../mib2c.extract.pl for man pages |
| 385 |
# which include: |
| 386 |
# @open FILE@ |
| 387 |
# writes generated output to FILE |
| 388 |
# note that for file specifications, opening '-' will print to stdout. |
| 389 |
# @append FILE@ |
| 390 |
# appends the given FILE |
| 391 |
# @close FILE@ |
| 392 |
# closes the given FILE |
| 393 |
# @push@ |
| 394 |
# save the current outputs, then clear outputs. Use with @open@ |
| 395 |
# and @pop@ to write to a new file without interfering with current |
| 396 |
# outputs. |
| 397 |
# @pop@ |
| 398 |
# pop up the process() stack one level. Use after a @push@ to return to |
| 399 |
# the previous set of open files. |
| 400 |
# @foreach $VAR scalar@ |
| 401 |
# repeat iterate over code until @end@ setting $VAR to all known scalars |
| 402 |
# @foreach $VAR table@ |
| 403 |
# repeat iterate over code until @end@ setting $VAR to all known tables |
| 404 |
# @foreach $VAR column@ |
| 405 |
# repeat iterate over code until @end@ setting $VAR to all known |
| 406 |
# columns within a given table. Obviously this must be called |
| 407 |
# within a foreach-table clause. |
| 408 |
# @foreach $VAR nonindex@ |
| 409 |
# repeat iterate over code until @end@ setting $VAR to all known |
| 410 |
# non-index columns within a given table. Obviously this must be called |
| 411 |
# within a foreach-table clause. |
| 412 |
# @foreach $VAR internalindex@ |
| 413 |
# repeat iterate over code until @end@ setting $VAR to all known internal |
| 414 |
# index columns within a given table. Obviously this must be called |
| 415 |
# within a foreach-table clause. |
| 416 |
# @foreach $VAR externalindex@ |
| 417 |
# repeat iterate over code until @end@ setting $VAR to all known external |
| 418 |
# index columns within a given table. Obviously this must be called |
| 419 |
# within a foreach-table clause. |
| 420 |
# @foreach $VAR index@ |
| 421 |
# repeat iterate over code until @end@ setting $VAR to all known |
| 422 |
# indexes within a given table. Obviously this must be called |
| 423 |
# within a foreach-table clause. |
| 424 |
# @foreach $VAR notifications@ |
| 425 |
# repeat iterate over code until @end@ setting $VAR to all known notifications |
| 426 |
# @foreach $VAR varbinds@ |
| 427 |
# repeat iterate over code until @end@ setting $VAR to all known varbinds |
| 428 |
# Obviously this must be called within a foreach-notifications clause. |
| 429 |
# @foreach $LABEL, $VALUE enum@ |
| 430 |
# repeat iterate over code until @end@ setting $LABEL and $VALUE |
| 431 |
# to the label and values from the enum list. |
| 432 |
# @foreach $RANGE_START, $RANGE_END range NODE@ |
| 433 |
# repeat iterate over code until @end@ setting $RANGE_START and $RANGE_END |
| 434 |
# to the legal accepted range set for a given mib NODE. |
| 435 |
# @foreach $var stuff a b c d@ |
| 436 |
# repeat iterate over values a, b, c, d as assigned generically |
| 437 |
# (ie, the values are taken straight from the list with no |
| 438 |
# mib-expansion, etc). |
| 439 |
# @while expression@ |
| 440 |
# repeat iterate over code until the expression is false |
| 441 |
# @eval $VAR = expression@ |
| 442 |
# evaluates expression and assigns the results to $VAR. This is |
| 443 |
# not a full perl eval, but sort of a "psuedo" eval useful for |
| 444 |
# simple expressions while keeping the same variable name space. |
| 445 |
# See below for a full-blown export to perl. |
| 446 |
# @perleval STUFF@ |
| 447 |
# evaluates STUFF directly in perl. Note that all mib2c variables |
| 448 |
# interpereted within .conf files are in $vars{NAME} and that |
| 449 |
# a warning will be printed if STUFF does not return 0. (adding a |
| 450 |
# 'return 0;' at the end of STUFF is a workaround. |
| 451 |
# @startperl@ |
| 452 |
# @endperl@ |
| 453 |
# treats everything between these tags as perl code, and evaluates it. |
| 454 |
# @next@ |
| 455 |
# restart foreach; should only be used inside a conditional. |
| 456 |
# skips out of current conditional, then continues to skip to |
| 457 |
# end for the current foreach clause. |
| 458 |
# @if expression@ |
| 459 |
# evaluates expression, and if expression is true processes |
| 460 |
# contained part until appropriate @end@ is reached. If the |
| 461 |
# expression is false, the next @elsif expression@ expression |
| 462 |
# (if it exists) will be evaluated, until an expression is |
| 463 |
# true. If no such expression exists and an @else@ |
| 464 |
# clause is found, it will be evaluated. |
| 465 |
# @ifconf file@ |
| 466 |
# If the specified file can be found in the conf file search path, |
| 467 |
# and if found processes contained part until an appropriate @end@ is |
| 468 |
# found. As with a regular @if expression@, @elsif expression@ and |
| 469 |
# @else@ can be used. |
| 470 |
# @ifdir dir@ |
| 471 |
# If the specified directory exists, process contained part until an |
| 472 |
# appropriate @end@ is found. As with a regular @if expression@, |
| 473 |
# @elsif expression@ and @else@ can be used. |
| 474 |
# @define NAME@ |
| 475 |
# @enddefine@ |
| 476 |
# Memorizes "stuff" between the define and enddefine tags for |
| 477 |
# later calling as NAME by @calldefine NAME@. |
| 478 |
# @calldefine NAME@ |
| 479 |
# Executes stuff previously memorized as NAME. |
| 480 |
# @printf "expression" stuff1, stuff2, ...@ |
| 481 |
# Like all the other printf's you know and love. |
| 482 |
# @run FILE@ |
| 483 |
# Sources the contents of FILE as a mib2c file, |
| 484 |
# but does not affect current files opened. |
| 485 |
# @include FILE@ |
| 486 |
# Sources the contents of FILE as a mib2c file and appends its |
| 487 |
# output to the current output. |
| 488 |
# @prompt $var QUESTION@ |
| 489 |
# Presents the user with QUESTION, expects a response and puts it in $var |
| 490 |
# @print STUFF@ |
| 491 |
# Prints stuff directly to the users screen (ie, not to where |
| 492 |
# normal mib2c output goes) |
| 493 |
# @quit@ |
| 494 |
# Bail out (silently) |
| 495 |
# @exit@ |
| 496 |
# Bail out! |
| 497 |
# |
| 498 |
sub skippart { |
| 499 |
my $endcount = 1; |
| 500 |
my $arg = shift; |
| 501 |
my $rtnelse = 0; |
| 502 |
while ($arg =~ s/-(\w+)\s*//) { |
| 503 |
$rtnelse = 1 if ($1 eq "else"); |
| 504 |
} |
| 505 |
while(get_next_line()) { |
| 506 |
$currentline++; |
| 507 |
$_ = process_vars($_) if ($debug); |
| 508 |
print "$currentfile.$currentline:P$currentlevel:S$endcount.$rtnelse:$_" if ($debug); |
| 509 |
next if ( /^\s*\#\#/ ); # noop, it's a comment |
| 510 |
next if (! /^\s*\@/ ); # output |
| 511 |
if (! /^\s*\@.*\@/ ) { |
| 512 |
warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n"; |
| 513 |
warn "(maybe missing the trailing @?)\n"; |
| 514 |
warn "$currentfile:$currentline [$_]\n"; |
| 515 |
} |
| 516 |
elsif (/\@\s*end\@/) { |
| 517 |
return "end" if ($endcount == 1); |
| 518 |
$endcount--; |
| 519 |
} |
| 520 |
elsif (/\@\s*elseif.*\@/) { |
| 521 |
m2c_die "use 'elsif' instead of 'elseif'\n"; |
| 522 |
} |
| 523 |
elsif (/\@\s*else\@/) { |
| 524 |
return "else" if (($endcount == 1) && ($rtnelse == 1)); |
| 525 |
} |
| 526 |
elsif (/\@\s*elsif\s+([^\@]+)\@/) { |
| 527 |
return "else" if (($endcount == 1) && ($rtnelse == 1) && (eval(process_vars($1)))); |
| 528 |
} |
| 529 |
elsif (/\@\s*(foreach|if|while)/) { |
| 530 |
$endcount++; |
| 531 |
} |
| 532 |
} |
| 533 |
print "skippart EOF\n"; |
| 534 |
m2c_die "unbalanced code detected in skippart: EOF when $endcount levels deep" if($endcount != 1); |
| 535 |
return "eof"; |
| 536 |
} |
| 537 |
|
| 538 |
sub close_file { |
| 539 |
my $name = shift; |
| 540 |
if (!$name) { |
| 541 |
print "close_file w/out name!\n"; |
| 542 |
return; |
| 543 |
} |
| 544 |
if(!$outputs{$name}) { |
| 545 |
print "no handle for $name\n"; |
| 546 |
return; |
| 547 |
} |
| 548 |
$outputs{$name}->close(); |
| 549 |
delete $outputs{$name}; |
| 550 |
# print STDERR "closing $name\n" if (!$quiet); |
| 551 |
} |
| 552 |
|
| 553 |
sub close_files { |
| 554 |
foreach $name (keys(%outputs)) { |
| 555 |
close_file($name); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
sub open_file { |
| 560 |
my $multiple = shift; |
| 561 |
my $spec = shift; |
| 562 |
my $name = $spec; |
| 563 |
$name =~ s/>//; |
| 564 |
if ($multiple == 0) { |
| 565 |
close_files(); |
| 566 |
} |
| 567 |
return if ($outputs{$name}); |
| 568 |
$outputs{$name} = new IO::File; |
| 569 |
$outputs{$name}->open(">$spec") || m2c_die "failed to open $name"; |
| 570 |
print STDERR "writing to $name\n" if (!$quiet && !$written{$name}); |
| 571 |
$written{$name} = '1'; |
| 572 |
} |
| 573 |
|
| 574 |
sub process_file { |
| 575 |
my ($file, $missingok, $keepvars) = (@_); |
| 576 |
my $oldfh = $fh; |
| 577 |
my $oldfile = $currentfile; |
| 578 |
my $oldline = $currentline; |
| 579 |
# keep old copy of @vars and just build on it. |
| 580 |
my %oldvars; |
| 581 |
|
| 582 |
%oldvars = %vars if ($keepvars != 1); |
| 583 |
|
| 584 |
$file = find_conf($file,$missingok); |
| 585 |
return if (! $file); |
| 586 |
|
| 587 |
$fh = open_conf($file); |
| 588 |
$currentline = 0; |
| 589 |
process("-balanced"); |
| 590 |
$fh->close(); |
| 591 |
|
| 592 |
$fh = $oldfh; |
| 593 |
$currentfile = $oldfile; |
| 594 |
$currentline = $oldline; |
| 595 |
|
| 596 |
# don't keep values in replaced vars. Revert to ours. |
| 597 |
%vars = %oldvars if ($keepvars != 1); |
| 598 |
} |
| 599 |
|
| 600 |
sub get_next_line { |
| 601 |
if ($#process_lines > -1) { |
| 602 |
return $_ = shift @process_lines; |
| 603 |
} |
| 604 |
return $_ = <$fh>; |
| 605 |
} |
| 606 |
|
| 607 |
sub do_tell { |
| 608 |
my $stash; |
| 609 |
$stash->{'startpos'} = $fh->tell(); |
| 610 |
$stash->{'startline'} = $currentline; |
| 611 |
@{$stash->{'lines'}} = @process_lines; |
| 612 |
return $stash; |
| 613 |
} |
| 614 |
|
| 615 |
sub do_seek { |
| 616 |
my $stash = shift; |
| 617 |
|
| 618 |
# save current line number |
| 619 |
$currentline = $stash->{'startline'}; |
| 620 |
$fh->seek($stash->{'startpos'}, 0); # go to top of section. |
| 621 |
|
| 622 |
# save current process_lines state. |
| 623 |
@process_lines = @{$stash->{'lines'}}; |
| 624 |
|
| 625 |
# save state of a number of variables (references), and new assignments |
| 626 |
for (my $i = 0; $i <= $#_; $i += 2) { |
| 627 |
push @{$stash->{'vars'}}, $_[$i], ${$_[$i]}; |
| 628 |
${$_[$i]} = $_[$i+1]; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
sub do_unseek { |
| 633 |
my $stash = shift; |
| 634 |
for (my $i = 0; $i <= $#{$stash->{'vars'}}; $i += 2) { |
| 635 |
${$stash->{'vars'}[$i]} = $stash->{'vars'}[$i+1]; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
sub do_a_loop { |
| 640 |
my $stash = shift; |
| 641 |
do_seek($stash, @_); |
| 642 |
my $return = process(); |
| 643 |
do_unseek($stash); |
| 644 |
return $return; |
| 645 |
} |
| 646 |
|
| 647 |
sub process { |
| 648 |
my $arg = shift; |
| 649 |
my $elseok = 0; |
| 650 |
my $balanced = 0; |
| 651 |
my $startlevel; |
| 652 |
my $return = "eof"; |
| 653 |
while ($arg =~ s/-(\w+)\s*//) { |
| 654 |
$elseok = 1 if ($1 eq "elseok"); |
| 655 |
$balanced = 1 if ($1 eq "balanced"); |
| 656 |
} |
| 657 |
|
| 658 |
$currentlevel++; |
| 659 |
$startlevel = $currentlevel; |
| 660 |
if($balanced) { |
| 661 |
$balanced = $currentlevel; |
| 662 |
} |
| 663 |
while(get_next_line()) { |
| 664 |
$currentline++; |
| 665 |
if ($debug) { |
| 666 |
# my $line = process_vars($_); |
| 667 |
# chop $line; |
| 668 |
print "$currentfile.$currentline:P$currentlevel.$elseok:$return:$_"; |
| 669 |
} |
| 670 |
|
| 671 |
next if (/^\s*\#\#/); # noop, it's a comment |
| 672 |
if (! /^\s*\@/ ) { # output |
| 673 |
my $line = process_vars($_); |
| 674 |
foreach $file (values(%outputs)) { |
| 675 |
print $file "$line"; |
| 676 |
} |
| 677 |
} #################################################################### |
| 678 |
elsif (/\@\s*exit\@/) { # EXIT |
| 679 |
close_files; |
| 680 |
die "exiting at conf file ($currentfile:$currentline) request\n"; |
| 681 |
} elsif (/\@\s*quit\@/) { # QUIT |
| 682 |
close_files; |
| 683 |
exit; |
| 684 |
} elsif (/\@\s*debug\s+([^\@]+)\@/) { # DEBUG |
| 685 |
if ($1 eq "on") { |
| 686 |
$debug = 1; |
| 687 |
} |
| 688 |
else { |
| 689 |
$debug = 0; |
| 690 |
} |
| 691 |
} elsif (/\@\s*strict token\s+([^\@]+)\@/) { # STRICT |
| 692 |
if ($1 eq "on") { |
| 693 |
$strict_unk_token = 1; |
| 694 |
} |
| 695 |
else { |
| 696 |
$strict_unk_token = 0; |
| 697 |
} |
| 698 |
} elsif (/\@\s*balanced\@/) { # BALANCED |
| 699 |
$balanced = $currentlevel; |
| 700 |
} elsif (/\@\s*open\s+([^\@]+)\@/) { # OPEN |
| 701 |
my $arg = $1; |
| 702 |
my ($multiple) = (0); |
| 703 |
while ($arg =~ s/-(\w+)\s+//) { |
| 704 |
$multiple = 1 if ($1 eq 'multiple'); |
| 705 |
} |
| 706 |
my $spec = process_vars($arg); |
| 707 |
open_file($multiple, $spec); |
| 708 |
} elsif (/\@\s*close\s+([^\@]+)\@/) { # CLOSE |
| 709 |
my $spec = process_vars($1); |
| 710 |
close_file($spec); |
| 711 |
} elsif (/\@\s*append\s+([^\@]+)\@/) { # APPEND |
| 712 |
my $arg = $1; |
| 713 |
my ($multiple) = (0); |
| 714 |
while ($arg =~ s/-(\w+)\s+//) { |
| 715 |
$multiple = 1 if ($1 eq 'multiple'); |
| 716 |
} |
| 717 |
my $spec = process_vars($arg); |
| 718 |
$spec=">$spec"; |
| 719 |
open_file($multiple,$spec); |
| 720 |
} elsif (/\@\s*define\s*(.*)\@/) { # DEFINE |
| 721 |
my $it = $1; |
| 722 |
while (<$fh>) { |
| 723 |
last if (/\@\s*enddefine\s*@/); |
| 724 |
push @{$defines{$it}}, $_; |
| 725 |
} |
| 726 |
} elsif (/\@\s*calldefine\s+(\w+)@/) { |
| 727 |
if ($#{$defines{$1}} == -1) { |
| 728 |
warn "called a define of $1 which didn't exist\n"; |
| 729 |
warn "$currentfile:$currentline [$_]\n"; |
| 730 |
} else { |
| 731 |
unshift @process_lines, @{$defines{$1}}; |
| 732 |
} |
| 733 |
} elsif (/\@\s*run (.*)\@/) { # RUN |
| 734 |
my $arg = $1; |
| 735 |
my ($again) = (0); |
| 736 |
while ($arg =~ s/-(\w+)\s+//) { |
| 737 |
$again = 1 if ($1 eq 'again'); |
| 738 |
# if ($1 eq 'file') { |
| 739 |
# my ($filearg) = ($arg =~ s/^(\w+)//); |
| 740 |
# } |
| 741 |
} |
| 742 |
my $spec = process_vars($arg); |
| 743 |
next if (!$again && $ranalready{$spec}); |
| 744 |
$ranalready{$spec} = 1; |
| 745 |
my %oldout = %outputs; |
| 746 |
my %emptyarray; |
| 747 |
%outputs = %emptyoutputs; |
| 748 |
process_file($spec,0,0); |
| 749 |
close_files; |
| 750 |
%outputs = %oldout; |
| 751 |
} elsif (/\@\s*push\@/) { # PUSH |
| 752 |
my %oldout = %outputs; |
| 753 |
my %emptyarray; |
| 754 |
%outputs = %emptyoutputs; |
| 755 |
process($arg); |
| 756 |
close_files; |
| 757 |
%outputs = %oldout; |
| 758 |
} elsif (/\@\s*pop\s*\@/) { # POP |
| 759 |
$return = "pop"; |
| 760 |
last; |
| 761 |
} elsif (/\@\s*include (.*)\@/) { # INCLUDE |
| 762 |
my $arg = $1; |
| 763 |
my ($missingok) = (0); |
| 764 |
while ($arg =~ s/-(\w+)\s+//) { |
| 765 |
$missingok = 1 if ($1 eq 'ifexists'); |
| 766 |
} |
| 767 |
my $spec = process_vars($arg); |
| 768 |
process_file($spec,$missingok,1); |
| 769 |
} elsif (/\@\s*if([a-z]*)\s+([^@]+)\@/) { # IF |
| 770 |
my ($type,$arg,$ok) = ($1,$2,0); |
| 771 |
# check condition based on type |
| 772 |
if (! $type) { |
| 773 |
$ok = eval(process_vars($arg)); |
| 774 |
} elsif ($type eq conf) { |
| 775 |
my $file = find_conf(process_vars($arg),1); # missingok |
| 776 |
$ok = (-f $file); |
| 777 |
} elsif ($type eq dir) { |
| 778 |
$ok = (-d $arg); |
| 779 |
} else { |
| 780 |
m2c_die "unknown if modifier ($type)\n"; |
| 781 |
} |
| 782 |
# act on condition |
| 783 |
if ($ok) { |
| 784 |
$return = process("-elseok"); |
| 785 |
} else { |
| 786 |
$return = skippart("-else"); |
| 787 |
$return = process("-elseok") if ($return eq "else"); |
| 788 |
} |
| 789 |
if ($return eq "next") { |
| 790 |
$return = skippart(); |
| 791 |
m2c_die("unbalanced code detected while exiting next/2 (returned $return)") if ($return ne "end"); |
| 792 |
# $return = "next"; |
| 793 |
last; |
| 794 |
} |
| 795 |
if (($return ne "end") && ($return ne "else")) { |
| 796 |
m2c_die "unbalanced if / return $return\n"; |
| 797 |
} |
| 798 |
} elsif (/\@\s*elseif.*\@/) { # bogus elseif |
| 799 |
m2c_die "error: use 'elsif' instead of 'elseif'\n"; |
| 800 |
} elsif (/\@\s*els(e|if).*\@/) { # ELSE/ELSIF |
| 801 |
if ($elseok != 1) { |
| 802 |
chop $_; |
| 803 |
m2c_die "unexpected els$1\n"; |
| 804 |
} |
| 805 |
$return = skippart(); |
| 806 |
if ($return ne "end") { |
| 807 |
m2c_die "unbalanced els$1 / rtn $rtn\n"; |
| 808 |
} |
| 809 |
$return = "else"; |
| 810 |
last; |
| 811 |
} elsif (/\@\s*next\s*\@/) { # NEXT |
| 812 |
$return = skippart(); |
| 813 |
m2c_die "unbalanced code detected while exiting next/1 (returned $return)" if ($return ne "end"); |
| 814 |
$return = "next"; |
| 815 |
last; |
| 816 |
} elsif (/\@\s*end\@/) { # END |
| 817 |
$return = "end"; |
| 818 |
last; |
| 819 |
} elsif (/\@\s*eval\s+\$(\w+)\s*=\s*([^\@]*)/) { # EVAL |
| 820 |
my ($v, $e) = ($1, $2); |
| 821 |
# print STDERR "eval: $e\n"; |
| 822 |
my $e = process_vars($e); |
| 823 |
$vars{$v} = eval($e); |
| 824 |
if (!defined($vars{$v})) { |
| 825 |
warn "$@"; |
| 826 |
warn "$currentfile:$currentline [$_]\n"; |
| 827 |
} |
| 828 |
} elsif (/\@\s*perleval\s*(.*)\@/) { # PERLEVAL |
| 829 |
# print STDERR "perleval: $1\n"; |
| 830 |
my $res = eval($1); |
| 831 |
if ($res) { |
| 832 |
warn "$@"; |
| 833 |
warn "$currentfile:$currentline [$_]\n"; |
| 834 |
} |
| 835 |
} elsif (/\@\s*startperl\s*\@/) { # STARTPERL |
| 836 |
my $text; |
| 837 |
while (get_next_line()) { |
| 838 |
last if (/\@\s*endperl\s*\@/); |
| 839 |
$text .= $_; |
| 840 |
} |
| 841 |
my $res = eval($text); |
| 842 |
if ($res) { |
| 843 |
warn "$@"; |
| 844 |
warn "$currentfile:$currentline [$_]\n"; |
| 845 |
} |
| 846 |
# print STDERR "perleval: $1\n"; |
| 847 |
} elsif (/\@\s*printf\s+(\"[^\"]+\")\s*,?(.*)\@/) { # PRINTF |
| 848 |
my ($f, $rest) = ($1, $2); |
| 849 |
$rest = process_vars($rest); |
| 850 |
my @args = split(/\s*,\s*/,$rest); |
| 851 |
$f = eval $f; |
| 852 |
# print STDERR "printf: $f, ", join(", ",@args),"\n"; |
| 853 |
foreach $file (values(%outputs)) { |
| 854 |
printf $file (eval {$f}, @args); |
| 855 |
} |
| 856 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+scalars*\s*\@/) { # SCALARS |
| 857 |
my $var = $1; |
| 858 |
my $stash = do_tell(); |
| 859 |
my $scalar; |
| 860 |
my @thekeys = keys(%scalars); |
| 861 |
if ($#thekeys == -1) { |
| 862 |
$return = skippart(); |
| 863 |
} else { |
| 864 |
if ($havenetsnmpoid) { |
| 865 |
@thekeys = sort { |
| 866 |
new NetSNMP::OID($a) <=> |
| 867 |
new NetSNMP::OID($b) } @thekeys; |
| 868 |
} |
| 869 |
foreach $scalar (@thekeys) { |
| 870 |
$return = do_a_loop($stash, \$vars{$var}, $scalar, |
| 871 |
\$currentscalar, $scalar, |
| 872 |
\$currentvar, $scalar); |
| 873 |
} |
| 874 |
} |
| 875 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 876 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+notifications*\s*\@/) { |
| 877 |
my $var = $1; |
| 878 |
my $stash = do_tell(); |
| 879 |
my $notify; |
| 880 |
my @thekeys = keys(%notifications); |
| 881 |
if ($#thekeys == -1) { |
| 882 |
$return = skippart(); |
| 883 |
} else { |
| 884 |
if ($havenetsnmpoid) { |
| 885 |
@thekeys = sort { |
| 886 |
new NetSNMP::OID($a) <=> |
| 887 |
new NetSNMP::OID($b) } @thekeys; |
| 888 |
} |
| 889 |
foreach $notify (@thekeys) { |
| 890 |
$return = do_a_loop($stash, \$vars{$var}, $notify, |
| 891 |
\$currentnotify, $notify); |
| 892 |
} |
| 893 |
} |
| 894 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 895 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+varbinds\s*\@/) { |
| 896 |
my $var = $1; |
| 897 |
my $stash = do_tell(); |
| 898 |
my $varbind; |
| 899 |
if ($#{$notifyvars{$currentnotify}} == -1) { |
| 900 |
$return = skippart(); |
| 901 |
} else { |
| 902 |
foreach $varbind (@{$notifyvars{$currentnotify}}) { |
| 903 |
# print "looping on $var for $varbind\n"; |
| 904 |
$return = do_a_loop($stash, \$vars{$var}, $varbind, |
| 905 |
\$currentvarbind, $varbind, |
| 906 |
\$currentvar, $varbind); |
| 907 |
} |
| 908 |
} |
| 909 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 910 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+tables*\s*\@/) { |
| 911 |
my $var = $1; |
| 912 |
my $stash = do_tell(); |
| 913 |
my $table; |
| 914 |
my @thekeys = keys(%tables); |
| 915 |
if ($#thekeys == -1) { |
| 916 |
$return = skippart(); |
| 917 |
} else { |
| 918 |
if ($havenetsnmpoid) { |
| 919 |
@thekeys = sort { |
| 920 |
new NetSNMP::OID($a) <=> |
| 921 |
new NetSNMP::OID($b) } @thekeys; |
| 922 |
} |
| 923 |
foreach $table (@thekeys) { |
| 924 |
$return = do_a_loop($stash, \$vars{$var}, $table, |
| 925 |
\$currenttable, $table); |
| 926 |
} |
| 927 |
} |
| 928 |
m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end"); |
| 929 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+stuff\s*(.*)\@/) { |
| 930 |
my $var = $1; |
| 931 |
my $stuff = $2; |
| 932 |
my @stuff = split(/[,\s]+/, $stuff); |
| 933 |
my $stash = do_tell(); |
| 934 |
if ($#stuff == -1) { |
| 935 |
$return = skippart(); |
| 936 |
} else { |
| 937 |
foreach $st (@stuff) { |
| 938 |
$return = do_a_loop($stash, \$vars{$var}, $st, |
| 939 |
\$currentstuff, $st); |
| 940 |
} |
| 941 |
} |
| 942 |
m2c_die("foreach did not end with \@end@ ($return)") if($return ne "end"); |
| 943 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+(column|index|internalindex|externalindex|nonindex)\s*\@/) { |
| 944 |
my ($var, $type) = ($1, $2); |
| 945 |
my $stash = do_tell(); |
| 946 |
my $column; |
| 947 |
if ($#{$tables{$currenttable}{$type}} == -1) { |
| 948 |
$return = skippart(); |
| 949 |
} else { |
| 950 |
foreach $column (@{$tables{$currenttable}{$type}}) { |
| 951 |
# print "looping on $var for $type -> $column\n"; |
| 952 |
$return = do_a_loop($stash, \$vars{$var}, $column, |
| 953 |
\$currentcolumn, $column, |
| 954 |
\$currentvar, $column); |
| 955 |
} |
| 956 |
} |
| 957 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 958 |
} elsif (/\@\s*foreach\s+\$([^\@]+)\s+\$([^\@]+)\s+range\s+([^\@]+)\@/) { |
| 959 |
my ($svar, $evar, $node) = ($1, $2, $3); |
| 960 |
$svar =~ s/,$//; |
| 961 |
my $stash = do_tell(); |
| 962 |
my $range; |
| 963 |
$node = $currentcolumn if (!$node); |
| 964 |
my $mibn = $SNMP::MIB{process_vars($node)}; |
| 965 |
die "no such mib node: $node" if (!$mibn); |
| 966 |
my @ranges = @{$mibn->{'ranges'}}; |
| 967 |
if ($#ranges > -1) { |
| 968 |
foreach $range (@ranges) { |
| 969 |
$return = do_a_loop($stash, \$vars{$svar}, $range->{'low'}, |
| 970 |
\$vars{$evar}, $range->{'high'}); |
| 971 |
} |
| 972 |
} else { |
| 973 |
$return = skippart(); |
| 974 |
} |
| 975 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 976 |
} elsif (/\@\s*foreach\s+\$([^\@,]+)\s*,*\s+\$([^\@]+)\s+(enums*)\s*\@/) { |
| 977 |
my ($varvar, $varval, $type) = ($1, $2, $3); |
| 978 |
my $stash = do_tell(); |
| 979 |
my $enum, $enum2; |
| 980 |
|
| 981 |
my @keys = sort { $SNMP::MIB{$currentvar}{'enums'}{$a} <=> |
| 982 |
$SNMP::MIB{$currentvar}{'enums'}{$b} } (keys(%{$SNMP::MIB{$currentvar}{'enums'}})); |
| 983 |
if ($#keys > -1) { |
| 984 |
foreach $enum (@keys) { |
| 985 |
($enum2 = $enum) =~ s/-/_/g; |
| 986 |
$return = do_a_loop($stash, \$vars{$varvar}, $enum2, |
| 987 |
\$vars{$varval}, |
| 988 |
$SNMP::MIB{$currentvar}{'enums'}{$enum}); |
| 989 |
} |
| 990 |
} else { |
| 991 |
$return = skippart(); |
| 992 |
} |
| 993 |
m2c_die("foreach did not end with \@end@") if($return ne "end"); |
| 994 |
} elsif (/\@\s*while([a-z]*)\s+([^@]+)\@/) { # WHILE |
| 995 |
my ($type,$arg,$ok) = ($1,$2,0); |
| 996 |
my $stash = do_tell(); |
| 997 |
my $loop = 1; |
| 998 |
my $everlooped = 0; |
| 999 |
|
| 1000 |
while ($loop) { |
| 1001 |
# check condition based on type |
| 1002 |
if (! $type) { |
| 1003 |
$ok = eval(process_vars($arg)); |
| 1004 |
} elsif ($type eq conf) { |
| 1005 |
my $file = find_conf(process_vars($arg),1); # missingok |
| 1006 |
$ok = (-f $file); |
| 1007 |
} elsif ($type eq dir) { |
| 1008 |
$ok = (-d $arg); |
| 1009 |
} else { |
| 1010 |
m2c_die "unknown while modifier ($type)\n"; |
| 1011 |
} |
| 1012 |
|
| 1013 |
# act on condition |
| 1014 |
if ($ok) { |
| 1015 |
$return = do_a_loop($stash, \$vars{$type}, $ok, \$vars{$args}); |
| 1016 |
$everlooped = 1; |
| 1017 |
} else { |
| 1018 |
if (!$everlooped) { |
| 1019 |
$return = skippart(); |
| 1020 |
} |
| 1021 |
$loop = 0; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
} elsif (/\@\s*prompt\s+\$(\S+)\s*(.*)\@/) { # PROMPT |
| 1025 |
my ($var, $prompt) = ($1, $2); |
| 1026 |
if (!$term) { |
| 1027 |
my $haveit = eval { require Term::ReadLine }; |
| 1028 |
if ($haveit) { |
| 1029 |
$term = new Term::ReadLine 'mib2c'; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
if ($term) { |
| 1033 |
$vars{$var} = $term->readline(process_vars($prompt)); |
| 1034 |
} |
| 1035 |
} elsif (/\@\s*print\s+([^@]*)\@/) { # PRINT |
| 1036 |
my $line = process_vars($1); |
| 1037 |
print "$line\n"; |
| 1038 |
} else { |
| 1039 |
my $line = process_vars($_); |
| 1040 |
mib2c_output($line); |
| 1041 |
chop $_; |
| 1042 |
warn "$currentfile:$currentline contained a line that started with a @ but did not match any mib2c configuration tokens.\n"; |
| 1043 |
warn "(maybe missing the trailing @?)\n"; |
| 1044 |
warn "$currentfile:$currentline [$_]\n"; |
| 1045 |
m2c_die if ($strict_unk_token == 1); |
| 1046 |
} |
| 1047 |
# $return = "eof"; |
| 1048 |
} |
| 1049 |
print "< Balanced $balanced / level $currentlevel / rtn $return / $_\n" if($debug); |
| 1050 |
if((!$_) && ($return ne "eof")) { |
| 1051 |
# warn "switching return of '$return' to EOF\n" if($debug); |
| 1052 |
$return = "eof"; |
| 1053 |
} |
| 1054 |
if ($balanced) { |
| 1055 |
if(($balanced != $currentlevel) || ($return ne "eof")) { |
| 1056 |
m2c_die "\@balanced@ specified, but processing terminated with '$return' before EOF!"; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
$currentlevel--; |
| 1060 |
return $return; |
| 1061 |
} |
| 1062 |
|
| 1063 |
sub mib2c_output { |
| 1064 |
my $line = shift; |
| 1065 |
foreach $file (values(%outputs)) { |
| 1066 |
print $file "$line"; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
sub setup_data { |
| 1072 |
my $mib = shift; |
| 1073 |
if ($mib->{label} =~ /Table$/ && $#{$mib->{children}} != -1) { |
| 1074 |
my $tablename = $mib->{label}; |
| 1075 |
my $entry = $mib->{children}; |
| 1076 |
my $columns = $entry->[0]{children}; |
| 1077 |
my $augments = $entry->[0]{'augments'}; |
| 1078 |
foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) { |
| 1079 |
# store by numeric key so we can sort them later |
| 1080 |
push @{$tables{$tablename}{'column'}}, $col->{'label'}; |
| 1081 |
} |
| 1082 |
if ($augments) { |
| 1083 |
my $mib = $SNMP::MIB{$augments} || |
| 1084 |
die "can't find info about augmented table $augments in table $tablename\n"; |
| 1085 |
$mib = $mib->{parent} || |
| 1086 |
die "can't find info about augmented table $augments in table $tablename\n"; |
| 1087 |
my $entry = $mib->{children}; |
| 1088 |
foreach my $index (@{$entry->[0]{'indexes'}}) { |
| 1089 |
my $node = $SNMP::MIB{$index} || |
| 1090 |
die "can't find info about index $index in table $tablename\n"; |
| 1091 |
push @{$tables{$tablename}{'index'}}, $index; |
| 1092 |
push @{$tables{$tablename}{'externalindex'}}, $index; |
| 1093 |
} |
| 1094 |
my $columns = $entry->[0]{children}; |
| 1095 |
} |
| 1096 |
else { |
| 1097 |
foreach my $index (@{$entry->[0]{'indexes'}}) { |
| 1098 |
my $node = $SNMP::MIB{$index} || |
| 1099 |
die "can't find info about index $index in table $tablename\n"; |
| 1100 |
push @{$tables{$tablename}{'index'}}, $index; |
| 1101 |
if("@{$tables{$tablename}{'column'}}" =~ /$index\b/ ) { |
| 1102 |
# print "idx INT $index\n"; |
| 1103 |
push @{$tables{$tablename}{'internalindex'}}, $index; |
| 1104 |
} else { |
| 1105 |
# print "idx EXT $index\n"; |
| 1106 |
push @{$tables{$tablename}{'externalindex'}}, $index; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
foreach my $col (sort { $a->{'subID'} <=> $b->{'subID'} } @$columns) { |
| 1111 |
next if ( "@{$tables{$tablename}{'index'}}" =~ /$col->{'label'}\b/ ); |
| 1112 |
push @{$tables{$tablename}{'nonindex'}}, $col->{'label'}; |
| 1113 |
} |
| 1114 |
# print "indexes: @{$tables{$tablename}{'index'}}\n"; |
| 1115 |
# print "internal indexes: @{$tables{$tablename}{'internalindex'}}\n"; |
| 1116 |
# print "external indexes: @{$tables{$tablename}{'externalindex'}}\n"; |
| 1117 |
# print "non-indexes: @{$tables{$tablename}{'nonindex'}}\n"; |
| 1118 |
} else { |
| 1119 |
my $children = $mib->{children}; |
| 1120 |
if ($#children == -1 && $mib->{type}) { |
| 1121 |
# scalar |
| 1122 |
if ($mib->{type} eq "NOTIF" || |
| 1123 |
$mib->{type} eq "TRAP") { |
| 1124 |
my $notifyname = $mib->{label}; |
| 1125 |
my @varlist = (); |
| 1126 |
$notifications{$notifyname} = 1; |
| 1127 |
$notifyvars{$notifyname} = $mib->{varbinds}; |
| 1128 |
} else { |
| 1129 |
$scalars{$mib->{label}} = 1 if ($mib->{'access'} ne 'Notify'); |
| 1130 |
} |
| 1131 |
} else { |
| 1132 |
my $i; |
| 1133 |
for($i = 0; $i <= $#$children; $i++) { |
| 1134 |
setup_data($children->[$i]); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
sub min { |
| 1141 |
return $_[0] if ($_[0] < $_[1]); |
| 1142 |
return $_[1]; |
| 1143 |
} |
| 1144 |
|
| 1145 |
sub max { |
| 1146 |
return $_[0] if ($_[0] > $_[1]); |
| 1147 |
return $_[1]; |
| 1148 |
} |
| 1149 |
|
| 1150 |
sub find_conf { |
| 1151 |
my ($configfile, $missingok) = (@_); |
| 1152 |
|
| 1153 |
foreach my $d (@search_dirs, @def_search_dirs) { |
| 1154 |
# print STDERR "using $d/$configfile" if (-f "$d/$configfile"); |
| 1155 |
return "$d/$configfile" if (-f "$d/$configfile"); |
| 1156 |
} |
| 1157 |
return $configfile if (-f "$configfile"); |
| 1158 |
return if ($missingok); |
| 1159 |
|
| 1160 |
print STDERR "Can't find a configuration file called $configfile\n"; |
| 1161 |
print STDERR "(referenced at $currentfile:$currentline)\n" if ($currentfile); |
| 1162 |
print STDERR "I looked in:\n"; |
| 1163 |
print " " . join("\n ", @search_dirs, @def_search_dirs), "\n"; |
| 1164 |
exit 1; |
| 1165 |
} |
| 1166 |
|
| 1167 |
sub open_conf { |
| 1168 |
my $configfile = shift; |
| 1169 |
# process .conf file |
| 1170 |
if (! -f "$configfile") { |
| 1171 |
print STDERR "Can't find a configuration file called $configfile\n"; |
| 1172 |
exit 1; |
| 1173 |
} |
| 1174 |
$currentfile = $configfile; |
| 1175 |
my $fh = new IO::File; |
| 1176 |
$fh->open("$configfile"); |
| 1177 |
return $fh; |
| 1178 |
} |
| 1179 |
|
| 1180 |
sub count_scalars { |
| 1181 |
my @k = keys(%scalars); |
| 1182 |
return $#k + 1; |
| 1183 |
} |
| 1184 |
|
| 1185 |
sub count_tables { |
| 1186 |
my @k = keys(%tables); |
| 1187 |
return $#k + 1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
sub count_columns { |
| 1191 |
my $table = shift; |
| 1192 |
return $#{$tables{$table}{'column'}} + 1; |
| 1193 |
} |
| 1194 |
|
| 1195 |
sub table_is_writable { |
| 1196 |
my $table = shift; |
| 1197 |
my $column; |
| 1198 |
my $result = 0; |
| 1199 |
foreach $column (@{$tables{$table}{'column'}}) { |
| 1200 |
if($SNMP::MIB{$column}{access} =~ /(ReadWrite|Create|WriteOnly)/) { |
| 1201 |
$result = 1; |
| 1202 |
last; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
return $result; |
| 1206 |
} |
| 1207 |
|
| 1208 |
sub table_has_create { |
| 1209 |
my $table = shift; |
| 1210 |
my $column; |
| 1211 |
my $result = 0; |
| 1212 |
foreach $column (@{$tables{$table}{'column'}}) { |
| 1213 |
if($SNMP::MIB{$column}{access} =~ /(Create)/) { |
| 1214 |
$result = 1; |
| 1215 |
last; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
return $result; |
| 1219 |
} |
| 1220 |
|
| 1221 |
sub table_has_rowstatus { |
| 1222 |
my $table = shift; |
| 1223 |
my $column; |
| 1224 |
my $result = 0; |
| 1225 |
foreach $column (@{$tables{$table}{'column'}}) { |
| 1226 |
if($SNMP::MIB{$column}{syntax} =~ /(RowStatus)/) { |
| 1227 |
$result = 1; |
| 1228 |
last; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
return $result; |
| 1232 |
} |
| 1233 |
|
| 1234 |
sub table_has_lastchange { |
| 1235 |
my $table = shift; |
| 1236 |
my $column; |
| 1237 |
my $result = 0; |
| 1238 |
foreach $column (@{$tables{$table}{'column'}}) { |
| 1239 |
if(($SNMP::MIB{$column}{syntax} =~ /(TimeStamp)/) && |
| 1240 |
($SNMP::MIB{$column}{label} =~ /(LastChange)/)) { |
| 1241 |
$result = 1; |
| 1242 |
last; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
return $result; |
| 1246 |
} |
| 1247 |
|
| 1248 |
sub table_has_storagetype { |
| 1249 |
my $table = shift; |
| 1250 |
my $column; |
| 1251 |
my $result = 0; |
| 1252 |
foreach $column (@{$tables{$table}{'column'}}) { |
| 1253 |
if($SNMP::MIB{$column}{syntax} =~ /(StorageType)/) { |
| 1254 |
$result = 1; |
| 1255 |
last; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
return $result; |
| 1259 |
} |
| 1260 |
|
| 1261 |
sub count_indexes { |
| 1262 |
my $table = shift; |
| 1263 |
return $#{$tables{$table}{'index'}} + 1; |
| 1264 |
} |
| 1265 |
|
| 1266 |
sub count_external_indexes { |
| 1267 |
my $table = shift; |
| 1268 |
return $#{$tables{$table}{'externalindex'}} + 1; |
| 1269 |
} |
| 1270 |
|
| 1271 |
sub count_notifications { |
| 1272 |
my @k = keys(%notifications); |
| 1273 |
return $#k + 1; |
| 1274 |
} |
| 1275 |
|
| 1276 |
sub count_varbinds { |
| 1277 |
my $notify = shift; |
| 1278 |
return $#{$notifyvars{$notify}} + 1; |
| 1279 |
} |