/usr/lib/perl5/SNMP.pm is in libsnmp-perl 5.4.3~dfsg-2.4ubuntu1.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 | # SNMP.pm -- Perl 5 interface to the Net-SNMP toolkit
#
# written by G. S. Marzot (marz@users.sourceforge.net)
#
# Copyright (c) 1995-2006 G. S. Marzot. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
package SNMP;
$VERSION = '5.0403'; # current release version number
use strict;
use warnings;
require Exporter;
require DynaLoader;
require AutoLoader;
use NetSNMP::default_store (':all');
@SNMP::ISA = qw(Exporter AutoLoader DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@SNMP::EXPORT = qw(
RECEIVED_MESSAGE
SNMPERR_BAD_ADDRESS
SNMPERR_BAD_LOCPORT
SNMPERR_BAD_SESSION
SNMPERR_GENERR
SNMPERR_TOO_LONG
SNMP_DEFAULT_ADDRESS
SNMP_DEFAULT_COMMUNITY_LEN
SNMP_DEFAULT_ENTERPRISE_LENGTH
SNMP_DEFAULT_ERRINDEX
SNMP_DEFAULT_ERRSTAT
SNMP_DEFAULT_PEERNAME
SNMP_DEFAULT_REMPORT
SNMP_DEFAULT_REQID
SNMP_DEFAULT_RETRIES
SNMP_DEFAULT_TIME
SNMP_DEFAULT_TIMEOUT
SNMP_DEFAULT_VERSION
TIMED_OUT
snmp_get
snmp_getnext
snmp_set
snmp_trap
);
sub AUTOLOAD {
no strict;
# This AUTOLOAD is used to 'autoload' constants from the constant()
# XS function. If a constant is not found then control is passed
# to the AUTOLOAD in AutoLoader.
my($val,$pack,$file,$line);
my $constname;
($constname = $AUTOLOAD) =~ s/.*:://;
# croak "&$module::constant not defined" if $constname eq 'constant';
$val = constant($constname, @_ ? $_[0] : 0);
if ($! != 0) {
if ($! =~ /Invalid/) {
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
}
else {
($pack,$file,$line) = caller;
die "Your vendor has not defined SNMP macro $constname, used at $file line $line.
";
}
}
eval "sub $AUTOLOAD { $val }";
goto &$AUTOLOAD;
}
bootstrap SNMP;
# Preloaded methods go here.
# Package variables
tie $SNMP::debugging, 'SNMP::DEBUGGING';
tie $SNMP::debug_internals, 'SNMP::DEBUG_INTERNALS';
tie $SNMP::dump_packet, 'SNMP::DUMP_PACKET';
tie %SNMP::MIB, 'SNMP::MIB';
tie $SNMP::save_descriptions, 'SNMP::MIB::SAVE_DESCR';
tie $SNMP::replace_newer, 'SNMP::MIB::REPLACE_NEWER';
tie $SNMP::mib_options, 'SNMP::MIB::MIB_OPTIONS';
%SNMP::V3_SEC_LEVEL_MAP = (noAuthNoPriv => 1, authNoPriv => 2, authPriv =>3);
use vars qw(
$auto_init_mib $use_long_names $use_sprint_value $use_enums
$use_numeric %MIB $verbose $debugging $dump_packet $save_descriptions
$best_guess $non_increasing $replace_newer %session_params
$debug_internals $mib_options
);
$auto_init_mib = 1; # enable automatic MIB loading at session creation time
$use_long_names = 0; # non-zero to prefer longer mib textual identifiers rather
# than just leaf indentifiers (see translateObj)
# may also be set on a per session basis(see UseLongNames)
$use_sprint_value = 0; # non-zero to enable formatting of response values
# using the snmp libraries "snprint_value"
# may also be set on a per session basis(see UseSprintValue)
# note: returned values not suitable for 'set' operations
$use_enums = 0; # non-zero to return integers as enums and allow sets
# using enums where appropriate - integer data will
# still be accepted for set operations
# may also be set on a per session basis (see UseEnums)
$use_numeric = 0; # non-zero to return object tags as numeric OID's instead
# of converting to textual representations. use_long_names,
# if non-zero, returns the entire OID, otherwise, return just
# the label portion. use_long_names is also set if the
# use_numeric variable is set.
%MIB = (); # tied hash to access libraries internal mib tree structure
# parsed in from mib files
$verbose = 0; # controls warning/info output of SNMP module,
# 0 => no output, 1 => enables warning and info
# output from SNMP module itself (is also controlled
# by SNMP::debugging)
$debugging = 0; # non-zero to globally enable libsnmp do_debugging output
# set to >= 2 to enabling packet dumping (see below)
$dump_packet = 0; # non-zero to globally enable libsnmp dump_packet output.
# is also enabled when $debugging >= 2
$save_descriptions = 0; #tied scalar to control saving descriptions during
# mib parsing - must be set prior to mib loading
$best_guess = 0; # determine whether or not to enable best-guess regular
# expression object name translation. 1 = Regex (-Ib),
# 2 = random (-IR)
$non_increasing = 0; # stop polling with an "OID not increasing"-error
# when an OID does not increases in bulkwalk.
$replace_newer = 0; # determine whether or not to tell the parser to replace
# older MIB modules with newer ones when loading MIBs.
# WARNING: This can cause an incorrect hierarchy.
sub setMib {
# loads mib from file name provided
# setting second arg to true causes currently loaded mib to be replaced
# otherwise mib file will be added to existing loaded mib database
# NOTE: now deprecated in favor of addMibFiles and new module based funcs
my $file = shift;
my $force = shift || '0';
return 0 if $file and not (-r $file);
SNMP::_read_mib($file,$force);
}
sub initMib {
# eqivalent to calling the snmp library init_mib if Mib is NULL
# if Mib is already loaded this function does nothing
# Pass a zero valued argument to get minimal mib tree initialzation
# If non zero agrgument or no argument then full mib initialization
SNMP::init_snmp("perl");
return;
if (defined $_[0] and $_[0] == 0) {
SNMP::_init_mib_internals();
} else {
SNMP::_read_mib("");
}
}
sub addMibDirs {
# adds directories to search path when a module is requested to be loaded
SNMP::init_snmp("perl");
foreach (@_) {
SNMP::_add_mib_dir($_) or return undef;
}
return 1;
}
sub addMibFiles {
# adds mib definitions to currently loaded mib database from
# file(s) supplied
SNMP::init_snmp("perl");
foreach (@_) {
SNMP::_read_mib($_) or return undef;
}
return 1;
}
sub loadModules {
# adds mib module definitions to currently loaded mib database.
# Modules will be searched from previously defined mib search dirs
# Passing and arg of 'ALL' will cause all known modules to be loaded
SNMP::init_snmp("perl");
foreach (@_) {
SNMP::_read_module($_) or return undef;
}
return 1;
}
sub unloadModules {
# causes modules to be unloaded from mib database
# Passing and arg of 'ALL' will cause all known modules to be unloaded
warn("SNMP::unloadModules not implemented! (yet)");
}
sub translateObj {
# Translate object identifier(tag or numeric) into alternate representation
# (i.e., sysDescr => '.1.3.6.1.2.1.1.1' and '.1.3.6.1.2.1.1.1' => sysDescr)
# when $SNMP::use_long_names or second arg is non-zero the translation will
# return longer textual identifiers (e.g., system.sysDescr). An optional
# third argument of non-zero will cause the module name to be prepended
# to the text name (e.g. 'SNMPv2-MIB::sysDescr'). If no Mib is loaded
# when called and $SNMP::auto_init_mib is enabled then the Mib will be
# loaded. Will return 'undef' upon failure.
SNMP::init_snmp("perl");
my $obj = shift;
my $temp = shift;
my $include_module_name = shift || "0";
my $long_names = $temp || $SNMP::use_long_names;
return undef if not defined $obj;
my $res;
if ($obj =~ /^\.?(\d+\.)*\d+$/) {
$res = SNMP::_translate_obj($obj,1,$long_names,$SNMP::auto_init_mib,0,$include_module_name);
} elsif ($obj =~ /(\.\d+)*$/ && $SNMP::best_guess == 0) {
$res = SNMP::_translate_obj($`,0,$long_names,$SNMP::auto_init_mib,0,$include_module_name);
$res .= $& if defined $res and defined $&;
} elsif ($SNMP::best_guess) {
$res = SNMP::_translate_obj($obj,0,$long_names,$SNMP::auto_init_mib,$SNMP::best_guess,$include_module_name);
}
return($res);
}
sub getType {
# return SNMP data type for given textual identifier
# OBJECTID, OCTETSTR, INTEGER, NETADDR, IPADDR, COUNTER
# GAUGE, TIMETICKS, OPAQUE, or undef
my $tag = shift;
SNMP::_get_type($tag, $SNMP::best_guess);
}
sub mapEnum {
# return the corresponding integer value *or* tag for a given MIB attribute
# and value. The function will sense which direction to perform the conversion
# various arg formats are supported
# $val = SNMP::mapEnum($varbind); # note: will update $varbind
# $val = SNMP::mapEnum('ipForwarding', 'forwarding');
# $val = SNMP::mapEnum('ipForwarding', 1);
#
my $var = shift;
my ($tag, $val, $update);
if (ref($var) =~ /ARRAY/ or ref($var) =~ /Varbind/) {
$tag = $var->[$SNMP::Varbind::tag_f];
$val = $var->[$SNMP::Varbind::val_f];
$update = 1;
} else {
$tag = $var;
$val = shift;
}
my $iflag = $val =~ /^\d+$/;
my $res = SNMP::_map_enum($tag, $val, $iflag, $SNMP::best_guess);
if ($update and defined $res) { $var->[$SNMP::Varbind::val_f] = $res; }
return($res);
}
%session_params = (DestHost => 1,
Community => 1,
Version => 1,
Timeout => 1,
Retries => 1,
RemotePort => 1,
LocalPort => 1);
sub strip_session_params {
my @params;
my @args;
my $param;
while ($param = shift) {
push(@params,$param, shift), next
if $session_params{$param};
push(@args,$param);
}
@_ = @args;
@params;
}
sub snmp_get {
# procedural form of 'get' method. sometimes quicker to code
# but is less efficient since the Session is created and destroyed
# with each call. Takes all the parameters of both SNMP::Session::new and
# SNMP::Session::get (*NOTE*: this api does not support async callbacks)
my @sess_params = &strip_session_params;
my $sess = new SNMP::Session(@sess_params);
$sess->get(@_);
}
sub snmp_getnext {
# procedural form of 'getnext' method. sometimes quicker to code
# but is less efficient since the Session is created and destroyed
# with each call. Takes all the parameters of both SNMP::Session::new and
# SNMP::Session::getnext (*NOTE*: this api does not support async callbacks)
my @sess_params = &strip_session_params;
my $sess = new SNMP::Session(@sess_params);
$sess->getnext(@_);
}
sub snmp_set {
# procedural form of 'set' method. sometimes quicker to code
# but is less efficient since the Session is created and destroyed
# with each call. Takes all the parameters of both SNMP::Session::new and
# SNMP::Session::set (*NOTE*: this api does not support async callbacks)
my @sess_params = &strip_session_params;
my $sess = new SNMP::Session(@sess_params);
$sess->set(@_);
}
sub snmp_trap {
# procedural form of 'trap' method. sometimes quicker to code
# but is less efficient since the Session is created and destroyed
# with each call. Takes all the parameters of both SNMP::TrapSession::new and
# SNMP::TrapSession::trap
my @sess_params = &strip_session_params;
my $sess = new SNMP::TrapSession(@sess_params);
$sess->trap(@_);
}
sub MainLoop {
my $time = shift;
my $callback = shift;
my $time_sec = ($time ? int $time : 0);
my $time_usec = ($time ? int(($time-$time_sec)*1000000) : 0);
SNMP::_main_loop($time_sec,$time_usec,$callback);
}
sub finish {
SNMP::_mainloop_finish();
}
sub reply_cb {
# callback function for async snmp calls
# when triggered, will do a SNMP read on the
# given fd
my $fd = shift;
SNMP::_read_on_fd($fd);
}
sub select_info {
# retrieves SNMP used fd's and timeout info
# calculates timeout in fractional seconds
# ( easy to use with select statement )
my($block, $to_sec, $to_usec, @fd_set)=SNMP::_get_select_info();
my $time_sec_dec = ($block? 0 : $to_sec + $to_usec * 1e-6);
#print "fd's for snmp -> ", @fd_set, "\n";
#print "block -> ", $block, "\n";
#print "timeout_sec -> ", $to_sec, "\n";
#print "timeout_usec -> ", $to_usec, "\n";
#print "timeout dec -> ", $time_sec_dec, "\n";
return ($time_sec_dec,@fd_set);
}
sub check_timeout {
# check to see if a snmp session
# timed out, and if so triggers
# the callback function
SNMP::_check_timeout();
# check to see when have to check again
my($block, $to_sec, $to_usec, @fd_set)=SNMP::_get_select_info();
my $time_sec_dec = ($block? 0 : $to_sec + $to_usec * 1e-6);
#print "fd's for snmp -> ", @fd_set, "\n";
#print "block -> ", $block, "\n";
#print "timeout_sec -> ", $to_sec, "\n";
#print "timeout_usec -> ", $to_usec, "\n";
#print "timeout dec -> ", $time_sec_dec, "\n";
return ($time_sec_dec);
}
sub _tie {
# this is a little implementation hack so ActiveState can access pp_tie
# thru perl code. All other environments allow the calling of pp_tie from
# XS code but AS was not exporting it when PERL_OBJECT was used.
#
# short term solution was call this perl func which calls 'tie'
#
# longterm fix is to supply a patch which allows AS to export pp_tie in
# such a way that it can be called from XS code. gsarathy says:
# a patch to util.c is needed to provide access to PL_paddr
# so it is possible to call PL_paddr[OP_TIE] as the compiler does
tie($_[0],$_[1],$_[2],$_[3]);
}
sub split_vars {
# This sub holds the regex that is used throughout this module
# to parse the base part of an OID from the IID.
# eg: portName.9.30 -> ['portName','9.30']
my $vars = shift;
# The regex was changed to this simple form by patch 722075 for some reason.
# Testing shows now (2/05) that it is not needed, and that the long expression
# works fine. AB
# my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/);
# These following two are the same. Broken down for easier maintenance
# my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|(?:\w+(?:\-*\w+)+))\.?(.*)$/);
my ($tag, $iid) =
($vars =~ /^( # Capture $1
# 1. either this 5.5.5.5
(?:\.\d+)+ # for grouping, won't increment $1
|
# 2. or asdf-asdf-asdf-asdf
(?: # grouping again
\w+ # needs some letters followed by
(?:\-*\w+)+ # zero or more dashes, one or more letters
)
)
\.? # optionally match a dot
(.*) # whatever is left in the string is our iid ($2)
$/x
);
return [$tag,$iid];
}
package SNMP::Session;
sub new {
my $type = shift;
my $this = {};
my ($name, $aliases, $host_type, $len, $thisaddr);
SNMP::init_snmp("perl");
%$this = @_;
$this->{ErrorStr} = ''; # if methods return undef check for expln.
$this->{ErrorNum} = 0; # contains SNMP error return
$this->{Version} ||=
NetSNMP::default_store::netsnmp_ds_get_int(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID,
NetSNMP::default_store::NETSNMP_DS_LIB_SNMPVERSION) ||
SNMP::SNMP_DEFAULT_VERSION();
if ($this->{Version} eq 128) {
# special handling of the bogus v1 definition.
$this->{Version} = 1;
}
# allow override of local SNMP port
$this->{LocalPort} ||= 0;
# destination host defaults to localhost
$this->{DestHost} ||= 'localhost';
# community defaults to public
$this->{Community} ||= NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_COMMUNITY()) || 'public';
# number of retries before giving up, defaults to SNMP_DEFAULT_RETRIES
$this->{Retries} = SNMP::SNMP_DEFAULT_RETRIES() unless defined($this->{Retries});
# timeout before retry, defaults to SNMP_DEFAULT_TIMEOUT
$this->{Timeout} = SNMP::SNMP_DEFAULT_TIMEOUT() unless defined($this->{Timeout});
# flag to enable fixing pdu and retrying with a NoSuch error
$this->{RetryNoSuch} ||= 0;
# backwards compatibility. Make host = host:port
if ($this->{RemotePort} && $this->{DestHost} !~ /:/) {
$this->{DestHost} = $this->{DestHost} . ":" . $this->{RemotePort};
}
if ($this->{Version} eq '1' or $this->{Version} eq '2'
or $this->{Version} eq '2c') {
$this->{SessPtr} = SNMP::_new_session($this->{Version},
$this->{Community},
$this->{DestHost},
$this->{LocalPort},
$this->{Retries},
$this->{Timeout},
);
} elsif ($this->{Version} eq '3' ) {
$this->{SecName} ||=
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_SECNAME()) ||
'initial';
if (!$this->{SecLevel}) {
$this->{SecLevel} =
NetSNMP::default_store::netsnmp_ds_get_int(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_SECLEVEL()) ||
$SNMP::V3_SEC_LEVEL_MAP{'noAuthNoPriv'};
} elsif ($this->{SecLevel} !~ /^\d+$/) {
$this->{SecLevel} = $SNMP::V3_SEC_LEVEL_MAP{$this->{SecLevel}};
}
$this->{SecEngineId} ||= '';
$this->{ContextEngineId} ||= $this->{SecEngineId};
$this->{Context} ||=
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_CONTEXT()) || '';
$this->{AuthProto} ||= 'DEFAULT'; # defaults to the library's default
$this->{AuthPass} ||=
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_AUTHPASSPHRASE()) ||
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_PASSPHRASE()) || '';
$this->{AuthMasterKey} ||= '';
$this->{PrivMasterKey} ||= '';
$this->{AuthLocalizedKey} ||= '';
$this->{PrivLocalizedKey} ||= '';
$this->{PrivProto} ||= 'DEFAULT'; # defaults to hte library's default
$this->{PrivPass} ||=
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_PRIVPASSPHRASE()) ||
NetSNMP::default_store::netsnmp_ds_get_string(NetSNMP::default_store::NETSNMP_DS_LIBRARY_ID(),
NetSNMP::default_store::NETSNMP_DS_LIB_PASSPHRASE()) || '';
$this->{EngineBoots} = 0 if not defined $this->{EngineBoots};
$this->{EngineTime} = 0 if not defined $this->{EngineTime};
$this->{SessPtr} = SNMP::_new_v3_session($this->{Version},
$this->{DestHost},
$this->{Retries},
$this->{Timeout},
$this->{SecName},
$this->{SecLevel},
$this->{SecEngineId},
$this->{ContextEngineId},
$this->{Context},
$this->{AuthProto},
$this->{AuthPass},
$this->{PrivProto},
$this->{PrivPass},
$this->{EngineBoots},
$this->{EngineTime},
$this->{AuthMasterKey},
length($this->{AuthMasterKey}),
$this->{PrivMasterKey},
length($this->{PrivMasterKey}),
$this->{AuthLocalizedKey},
length($this->{AuthLocalizedKey}),
$this->{PrivLocalizedKey},
length($this->{PrivLocalizedKey}),
);
}
unless ($this->{SessPtr}) {
warn("unable to create session") if $SNMP::verbose;
return undef;
}
SNMP::initMib($SNMP::auto_init_mib); # ensures that *some* mib is loaded
$this->{UseLongNames} = $SNMP::use_long_names
unless exists $this->{UseLongNames};
$this->{UseSprintValue} = $SNMP::use_sprint_value
unless exists $this->{UseSprintValue};
$this->{BestGuess} = $SNMP::best_guess unless exists $this->{BestGuess};
$this->{NonIncreasing} ||= $SNMP::non_increasing;
$this->{UseEnums} = $SNMP::use_enums unless exists $this->{UseEnums};
$this->{UseNumeric} = $SNMP::use_numeric unless exists $this->{UseNumeric};
# Force UseLongNames if UseNumeric is in use.
$this->{UseLongNames}++ if $this->{UseNumeric};
bless $this, $type;
}
sub update {
# *Not Implemented*
# designed to update the fields of session to allow retargetting to different
# host, community name change, timeout, retry changes etc. Unfortunately not
# working yet because some updates (the address in particular) need to be
# done on the internal session pointer which cannot be fetched w/o touching
# globals at this point which breaks win32. A patch to the net-snmp toolkit
# is needed
my $this = shift;
my ($name, $aliases, $host_type, $len, $thisaddr);
my %new_fields = @_;
@$this{keys %new_fields} = values %new_fields;
$this->{UseLongNames} = $SNMP::use_long_names
unless exists $this->{UseLongNames};
$this->{UseSprintValue} = $SNMP::use_sprint_value
unless exists $this->{UseSprintValue};
$this->{BestGuess} = $SNMP::best_guess unless exists $this->{BestGuess};
$this->{NonIncreasing} ||= $SNMP::non_increasing;
$this->{UseEnums} = $SNMP::use_enums unless exists $this->{UseEnums};
$this->{UseNumeric} = $SNMP::use_numeric unless exists $this->{UseNumeric};
# Force UseLongNames if UseNumeric is in use.
$this->{UseLongNames}++ if $this->{UseNumeric};
SNMP::_update_session($this->{Version},
$this->{Community},
$this->{DestHost},
$this->{RemotePort},
$this->{LocalPort},
$this->{Retries},
$this->{Timeout},
);
}
sub set {
my $this = shift;
my $vars = shift;
my $varbind_list_ref;
my $res = 0;
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
#$varbind_list_ref = [[$tag, $iid, $val]];
my $split_vars = SNMP::split_vars($vars);
my $val = shift;
push @$split_vars,$val;
$varbind_list_ref = [$split_vars];
}
my $cb = shift;
$res = SNMP::_set($this, $varbind_list_ref, $cb);
}
sub get {
my $this = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
$varbind_list_ref = [SNMP::split_vars($vars)];
}
my $cb = shift;
@res = SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb);
return(wantarray() ? @res : $res[0]);
}
my $have_netsnmp_oid = eval { require NetSNMP::OID; };
sub gettable {
#
# getTable
# --------
#
# Get OIDs starting at $table_oid, and continue down the tree
# until we get to an OID which does not start with $table_oid,
# i.e. we have reached the end of this table.
#
my $state;
my ($this, $root_oid, @options) = @_;
$state->{'options'} = {@options};
my ($textnode, $varbinds, $vbl, $res, $repeat);
# translate the OID into numeric form if its not
if ($root_oid !~ /^[\.0-9]+$/) {
$textnode = $root_oid;
$root_oid = SNMP::translateObj($root_oid);
} else {
$textnode = SNMP::translateObj($root_oid);
}
# bail if we don't have a valid oid.
return if (!$root_oid);
# deficed if we're going to parse indexes
my $parse_indexes = (defined($state->{'options'}{'noindexes'})) ?
0 : $have_netsnmp_oid;
# get the list of columns we should look at.
my @columns;
if (!$state->{'options'}{'columns'}) {
if ($textnode) {
my %indexes;
if ($parse_indexes) {
# get indexes
my @indexes =
@{$SNMP::MIB{$textnode}{'children'}[0]{'indexes'} || [] };
# quick translate into a hash
map { $indexes{$_} = 1; } @indexes;
}
# calculate the list of accessible columns that aren't indexes
my $children = $SNMP::MIB{$textnode}{'children'}[0]{'children'};
foreach my $c (@$children) {
push @{$state->{'columns'}},
$root_oid . ".1." . $c->{'subID'}
if (!$indexes{$c->{'label'}});
}
if ($#{$state->{'columns'}} == -1) {
# some tables are only indexes, and we need to walk at
# least one column. We pick the last.
push @{$state->{'columns'}}, $root_oid . ".1." .
$children->[$#$children]{'subID'}
if ref($state) eq 'HASH' and ref($children) eq 'HASH';
}
}
} else {
# XXX: requires specification in numeric OID... ack.!
@{$state->{'columns'}} = @{$state->{'options'}{'columns'}};
# if the columns aren't numeric, we need to turn them into
# numeric columns...
map {
if ($_ !~ /\.1\.3/) {
$_ = $SNMP::MIB{$_}{'objectID'};
}
} @{$state->{'columns'}};
}
# create the initial walking info.
foreach my $c (@{$state->{'columns'}}) {
push @{$state->{'varbinds'}}, [$c];
push @{$state->{'stopconds'}}, $c;
}
if ($#{$state->{'varbinds'}} == -1) {
print STDERR "ack: gettable failed to find any columns to look for.\n";
return;
}
$vbl = $state->{'varbinds'};
my $repeatcount;
if ($this->{Version} eq '1' || $state->{'options'}{nogetbulk}) {
$state->{'repeatcount'} = 1;
} elsif ($state->{'options'}{'repeat'}) {
$state->{'repeatcount'} = $state->{'options'}{'repeat'};
} elsif ($#{$state->{'varbinds'}} == -1) {
$state->{'repeatcount'} = 1;
} else {
# experimentally determined maybe guess at a best repeat value
# 1000 bytes max (safe), 30 bytes average for encoding of the
# varbind (experimentally determined to be closer to
# 26. Again, being safe. Then devide by the number of
# varbinds.
$state->{'repeatcount'} = int(1000 / 36 / ($#{$state->{'varbinds'}} + 1));
}
#
# if we've been configured with a callback, then call the
# sub-functions with a callback to our own "next" processing
# function (_gettable_do_it). or else call the blocking method and
# call the next processing function ourself.
#
if ($state->{'options'}{'callback'}) {
if ($this->{Version} ne '1' && !$state->{'options'}{'nogetbulk'}) {
$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl,
[\&_gettable_do_it, $this, $vbl,
$parse_indexes, $textnode, $state]);
} else {
$res = $this->getnext($vbl,
[\&_gettable_do_it, $this, $vbl,
$parse_indexes, $textnode, $state]);
}
} else {
if ($this->{Version} ne '1' && !$state->{'options'}{'nogetbulk'}) {
$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl);
} else {
$res = $this->getnext($vbl);
}
return $this->_gettable_do_it($vbl, $parse_indexes, $textnode, $state);
}
return 0;
}
sub _gettable_do_it() {
my ($this, $vbl, $parse_indexes, $textnode, $state) = @_;
my ($res);
$vbl = $_[$#_] if ($state->{'options'}{'callback'});
while ($#$vbl > -1 && !$this->{ErrorNum}) {
if (($#$vbl + 1) % ($#{$state->{'stopconds'}} + 1) != 0) {
if ($vbl->[$#$vbl][2] ne 'ENDOFMIBVIEW') {
# unless it's an end of mib view we didn't get the
# proper number of results back.
print STDERR "ack: gettable results not appropriate\n";
}
my @k = keys(%{$state->{'result_hash'}});
last if ($#k > -1); # bail with what we have
return;
}
$state->{'varbinds'} = [];
my $newstopconds;
my $lastsetstart = ($state->{'repeatcount'}-1) * ($#{$state->{'stopconds'}}+1);
for (my $i = 0; $i <= $#$vbl; $i++) {
my $row_oid = SNMP::translateObj($vbl->[$i][0]);
my $row_text = $vbl->[$i][0];
my $row_index = $vbl->[$i][1];
my $row_value = $vbl->[$i][2];
my $row_type = $vbl->[$i][3];
if ($row_oid =~
/^$state->{'stopconds'}[$i % ($#{$state->{'stopconds'}}+1)]/ &&
$row_value ne 'ENDOFMIBVIEW' ){
if ($row_type eq "OBJECTID") {
# If the value returned is an OID, translate this
# back in to a textual OID
$row_value = SNMP::translateObj($row_value);
}
# Place the results in a hash
$state->{'result_hash'}{$row_index}{$row_text} = $row_value;
# continue past this next time
if ($i >= $lastsetstart) {
push @$newstopconds,
$state->{'stopconds'}->[$i%($#{$state->{'stopconds'}}+1)];
push @{$state->{'varbinds'}},[$vbl->[$i][0],$vbl->[$i][1]];
}
}
}
if ($#$newstopconds == -1) {
last;
}
if ($#{$state->{'varbinds'}} == -1) {
print "gettable ack. shouldn't get here\n";
}
$vbl = $state->{'varbinds'};
$state->{'stopconds'} = $newstopconds;
#
# if we've been configured with a callback, then call the
# sub-functions with a callback to our own "next" processing
# function (_gettable_do_it). or else call the blocking method and
# call the next processing function ourself.
#
if ($state->{'options'}{'callback'}) {
if ($this->{Version} ne '1' && !$state->{'options'}{'nogetbulk'}) {
$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl,
[\&_gettable_do_it, $this, $vbl,
$parse_indexes, $textnode, $state]);
} else {
$res = $this->getnext($vbl,
[\&_gettable_do_it, $this, $vbl,
$parse_indexes, $textnode, $state]);
}
return;
} else {
if ($this->{Version} ne '1' && !$state->{'options'}{'nogetbulk'}) {
$res = $this->getbulk(0, $state->{'repeatcount'}, $vbl);
} else {
$res = $this->getnext($vbl);
}
}
}
# finish up
_gettable_end_routine($state, $parse_indexes, $textnode);
# return the hash if no callback was specified
if (!$state->{'options'}{'callback'}) {
return($state->{'result_hash'});
}
#
# if they provided a callback, call it
# (if an array pass the args as well)
#
if (ref($state->{'options'}{'callback'}) eq 'ARRAY') {
my $code = shift @{$state->{'options'}{'callback'}};
$code->(@{$state->{'options'}{'callback'}}, $state->{'result_hash'});
} else {
$state->{'options'}{'callback'}->($state->{'result_hash'});
}
}
sub _gettable_end_routine {
my ($state, $parse_indexes, $textnode) = @_;
if ($parse_indexes) {
my @indexes = @{$SNMP::MIB{$textnode}{'children'}[0]{'indexes'}};
my $i;
foreach my $trow (keys(%{$state->{'result_hash'}})) {
my $noid = new NetSNMP::OID($state->{'columns'}[0] . "." . $trow);
if (!$noid) {
print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB OID\n";
next;
}
my $nindexes = $noid->get_indexes();
if (!$nindexes || ref($nindexes) ne 'ARRAY' ||
$#indexes != $#$nindexes) {
print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB indexes:\n $noid => " . ref($nindexes) . "\n [should be an ARRAY]\n expended # indexes = $#indexes\n";
if (ref($nindexes) eq 'ARRAY') {
print STDERR "***** ERROR parsing $state->{'columns'}[0].$trow MIB indexes: " . ref($nindexes) . " $#indexes $#$nindexes\n";
}
next;
}
for ($i = 0; $i <= $#indexes; $i++) {
$state->{'result_hash'}{$trow}{$indexes[$i]} = $nindexes->[$i];
}
}
}
}
sub fget {
my $this = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
$varbind_list_ref = [SNMP::split_vars($vars)];
}
my $cb = shift;
SNMP::_get($this, $this->{RetryNoSuch}, $varbind_list_ref, $cb);
foreach my $varbind (@$varbind_list_ref) {
my $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||
$this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};
&$sub($varbind) if defined $sub;
push(@res, $varbind->[$SNMP::Varbind::val_f]);
}
return(wantarray() ? @res : $res[0]);
}
sub getnext {
my $this = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
$varbind_list_ref = [SNMP::split_vars($vars)];
}
my $cb = shift;
@res = SNMP::_getnext($this, $varbind_list_ref, $cb);
return(wantarray() ? @res : $res[0]);
}
sub fgetnext {
my $this = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
$varbind_list_ref = [SNMP::split_vars($vars)];
}
my $cb = shift;
SNMP::_getnext($this, $varbind_list_ref, $cb);
foreach my $varbind (@$varbind_list_ref) {
my $sub = $this->{VarFormats}{$varbind->[$SNMP::Varbind::tag_f]} ||
$this->{TypeFormats}{$varbind->[$SNMP::Varbind::type_f]};
&$sub($varbind) if defined $sub;
push(@res, $varbind->[$SNMP::Varbind::val_f]);
}
return(wantarray() ? @res : $res[0]);
}
sub getbulk {
my $this = shift;
my $nonrepeaters = shift;
my $maxrepetitions = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
$varbind_list_ref = [SNMP::split_vars($vars)];
}
my $cb = shift;
@res = SNMP::_getbulk($this, $nonrepeaters, $maxrepetitions, $varbind_list_ref, $cb);
return(wantarray() ? @res : $res[0]);
}
sub bulkwalk {
my $this = shift;
my $nonrepeaters = shift;
my $maxrepetitions = shift;
my $vars = shift;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
} else {
# my ($tag, $iid) = ($vars =~ /^((?:\.\d+)+|\w+)\.?(.*)$/);
my ($tag, $iid) = ($vars =~ /^(.*?)\.?(\d+)+$/);
$varbind_list_ref = [[$tag, $iid]];
}
if (scalar @$varbind_list_ref == 0) {
$this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);
$this->{ErrorStr} = "cannot bulkwalk() empty variable list";
return undef;
}
if (scalar @$varbind_list_ref < $nonrepeaters) {
$this->{ErrorNum} = SNMP::constant("SNMPERR_GENERR", 0);
$this->{ErrorStr} = "bulkwalk() needs at least $nonrepeaters varbinds";
return undef;
}
my $cb = shift;
@res = SNMP::_bulkwalk($this, $nonrepeaters, $maxrepetitions,
$varbind_list_ref, $cb);
# Return, in list context, a copy of the array of arrays of Varbind refs.
# In scalar context, return either a reference to the array of arrays of
# Varbind refs, or the request ID for an asynchronous bulkwalk. This is
# a compromise between the getbulk()-ish return, and the more useful array
# of arrays of Varbinds return from the synchronous bulkwalk().
#
return @res if (wantarray());
return defined($cb) ? $res[0] : \@res;
}
my %trap_type = (coldStart => 0, warmStart => 1, linkDown => 2, linkUp => 3,
authFailure => 4, egpNeighborLoss => 5, specific => 6 );
sub trap {
# (v1) enterprise, agent, generic, specific, uptime, <vars>
# $sess->trap(enterprise=>'.1.3.6.1.4.1.2021', # or 'ucdavis' [default]
# agent => '127.0.0.1', # or 'localhost',[default 1st intf on host]
# generic => specific, # can be omitted if 'specific' supplied
# specific => 5, # can be omitted if 'generic' supplied
# uptime => 1234, # default to localhost uptime (0 on win32)
# [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars
# # always last
# (v2) oid, uptime, <vars>
# $sess->trap(uptime => 1234,
# oid => 'snmpRisingAlarm',
# [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars
# # always last
# # always last
my $this = shift;
my $vars = pop if ref($_[$#_]); # last arg may be varbind or varlist
my %param = @_;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
}
if ($this->{Version} eq '1') {
my $enterprise = $param{enterprise} || 'ucdavis';
$enterprise = SNMP::translateObj($enterprise)
unless $enterprise =~ /^[\.\d]+$/;
my $agent = $param{agent} || '';
my $generic = $param{generic} || 'specific';
$generic = $trap_type{$generic} || $generic;
my $uptime = $param{uptime} || SNMP::_sys_uptime();
my $specific = $param{specific} || 0;
@res = SNMP::_trapV1($this, $enterprise, $agent, $generic, $specific,
$uptime, $varbind_list_ref);
} elsif (($this->{Version} eq '2')|| ($this->{Version} eq '2c')) {
my $trap_oid = $param{oid} || $param{trapoid} || '.0.0';
my $uptime = $param{uptime} || SNMP::_sys_uptime();
@res = SNMP::_trapV2($this, $uptime, $trap_oid, $varbind_list_ref);
}
return(wantarray() ? @res : $res[0]);
}
sub inform {
# (v3) oid, uptime, <vars>
# $sess->inform(uptime => 1234,
# oid => 'coldStart',
# [[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars
# # then callback
# always last
my $this = shift;
my $vars;
my $cb;
$cb = pop if ref($_[$#_]) eq 'CODE'; # last arg may be code
$vars = pop if ref($_[$#_]); # varbind or varlist
my %param = @_;
my ($varbind_list_ref, @res);
if (ref($vars) =~ /SNMP::VarList/) {
$varbind_list_ref = $vars;
} elsif (ref($vars) =~ /SNMP::Varbind/) {
$varbind_list_ref = [$vars];
} elsif (ref($vars) =~ /ARRAY/) {
$varbind_list_ref = [$vars];
$varbind_list_ref = $vars if ref($$vars[0]) =~ /ARRAY/;
}
my $trap_oid = $param{oid} || $param{trapoid};
my $uptime = $param{uptime} || SNMP::_sys_uptime();
if($this->{Version} eq '3') {
@res = SNMP::_inform($this, $uptime, $trap_oid, $varbind_list_ref, $cb);
} else {
warn("error:inform: This version doesn't support the command\n");
}
return(wantarray() ? @res : $res[0]);
}
package SNMP::TrapSession;
@SNMP::TrapSession::ISA = ('SNMP::Session');
sub new {
my $type = shift;
# allow override of remote SNMP trap port
unless (grep(/RemotePort/, @_)) {
push(@_, 'RemotePort', 162); # push on new default for trap session
}
SNMP::Session::new($type, @_);
}
package SNMP::Varbind;
my $tag_f = 0;
my $iid_f = 1;
my $val_f = 2;
my $type_f = 3;
my $time_f = 4;
sub new {
my $type = shift;
my $this = shift;
$this ||= [];
bless $this;
}
sub tag {
$_[0]->[$tag_f];
}
sub iid {
$_[0]->[$iid_f];
}
sub val {
$_[0]->[$val_f];
}
sub type {
$_[0]->[$type_f];
}
sub name {
if (defined($_[0]->[$iid_f]) && ($_[0]->[$iid_f] =~ m/^[0-9]+$/)) {
return $_[0]->[$tag_f] . "." . $_[0]->[$iid_f];
}
return $_[0]->[$tag_f];
}
sub fmt {
my $self = shift;
return $self->name . " = \"" . $self->val . "\" (" . $self->type . ")";
}
#sub DESTROY {
# print "SNMP::Varbind::DESTROY($_[0])\n";
#}
package SNMP::VarList;
sub new {
my $type = shift;
my $this = [];
my $varb;
foreach $varb (@_) {
$varb = new SNMP::Varbind($varb) unless ref($varb) =~ /SNMP::Varbind/;
push(@{$this}, $varb);
}
bless $this;
}
#sub DESTROY {
# print "SNMP::VarList::DESTROY($_[0])\n";
#}
package SNMP::DEBUGGING;
# controls info/debugging output from SNMP module and libsnmp
# $SNMP::debugging == 1 => enables general info and warning output
# (eqiv. to setting $SNMP::verbose)
# $SNMP::debugging == 2 => enables do_debugging from libsnmp as well
# $SNMP::debugging == 3 => enables packet_dump from libsnmp as well
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE {
$SNMP::verbose = $_[1];
SNMP::_set_debugging($_[1]>1);
$SNMP::dump_packet = ($_[1]>2);
${$_[0]} = $_[1];
}
sub DELETE {
$SNMP::verbose = 0;
SNMP::_set_debugging(0);
$SNMP::dump_packet = 0;
${$_[0]} = undef;
}
package SNMP::DEBUG_INTERNALS; # Controls SNMP.xs debugging.
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE {
SNMP::_debug_internals($_[1]);
${$_[0]} = $_[1];
}
sub DELETE {
SNMP::_debug_internals(0);
${$_[0]} = undef;
}
package SNMP::DUMP_PACKET;
# controls packet dump output from libsnmp
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE { SNMP::_dump_packet($_[1]); ${$_[0]} = $_[1]; }
sub DELETE { SNMP::_dump_packet(0); ${$_[0]} = 0; }
package SNMP::MIB;
sub TIEHASH {
bless {};
}
sub FETCH {
my $this = shift;
my $key = shift;
if (!defined $this->{$key}) {
tie(%{$this->{$key}}, 'SNMP::MIB::NODE', $key) or return undef;
}
$this->{$key};
}
sub STORE {
warn "STORE(@_) : write access to the MIB not implemented\n";
}
sub DELETE {
delete $_[0]->{$_[1]}; # just delete cache entry
}
sub FIRSTKEY { return '.1'; } # this should actually start at .0 but
# because nodes are not stored in lexico
# order in ucd-snmp node tree walk will
# miss most of the tree
sub NEXTKEY { # this could be sped up by using an XS __get_next_oid maybe
my $node = $_[0]->FETCH($_[1])->{nextNode};
$node->{objectID};
}
sub EXISTS { exists $_[0]->{$_[1]} || $_[0]->FETCH($_[1]); }
sub CLEAR { undef %{$_[0]}; } # clear the cache
package SNMP::MIB::NODE;
my %node_elements =
(
objectID => 0, # dotted decimal fully qualified OID
label => 0, # leaf textual identifier (e.g., 'sysDescr')
subID => 0, # leaf numeric OID component of objectID (e.g., '1')
moduleID => 0, # textual identifier for module (e.g., 'RFC1213-MIB')
parent => 0, # parent node
children => 0, # array reference of children nodes
indexes => 0, # returns array of column labels
varbinds => 0, # returns array of trap/notification varbinds
nextNode => 0, # next lexico node (BUG! does not return in lexico order)
type => 0, # returns simple type (see getType for values)
access => 0, # returns ACCESS (ReadOnly, ReadWrite, WriteOnly,
# NoAccess, Notify, Create)
status => 0, # returns STATUS (Mandatory, Optional, Obsolete,
# Deprecated)
syntax => 0, # returns 'textualConvention' if defined else 'type'
textualConvention => 0, # returns TEXTUAL-CONVENTION
units => 0, # returns UNITS
hint => 0, # returns HINT
enums => 0, # returns hash ref {tag => num, ...}
ranges => 0, # returns array ref of hash ref [{low => num, high => num}]
defaultValue => 0, # returns default value
description => 0, # returns DESCRIPTION ($SNMP::save_descriptions must
# be set prior to MIB initialization/parsing
augments => 0, # textual identifier of augmented object
);
# sub TIEHASH - implemented in SNMP.xs
# sub FETCH - implemented in SNMP.xs
sub STORE {
warn "STORE(@_): write access to MIB node not implemented\n";
}
sub DELETE {
warn "DELETE(@_): write access to MIB node not implemented\n";
}
sub FIRSTKEY { my $k = keys %node_elements; (each(%node_elements))[0]; }
sub NEXTKEY { (each(%node_elements))[0]; }
sub EXISTS { exists($node_elements{$_[1]}); }
sub CLEAR {
warn "CLEAR(@_): write access to MIB node not implemented\n";
}
#sub DESTROY {
# warn "DESTROY(@_): write access to MIB node not implemented\n";
# # print "SNMP::MIB::NODE::DESTROY : $_[0]->{label} ($_[0])\n";
#}
package SNMP::MIB::SAVE_DESCR;
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE { SNMP::_set_save_descriptions($_[1]); ${$_[0]} = $_[1]; }
sub DELETE { SNMP::_set_save_descriptions(0); ${$_[0]} = 0; }
package SNMP::MIB::REPLACE_NEWER; # Controls MIB parsing
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE {
SNMP::_set_replace_newer($_[1]);
${$_[0]} = $_[1];
}
sub DELETE {
SNMP::_set_replace_newer(0);
${$_[0]} = 0;
}
package SNMP::MIB::MIB_OPTIONS;
sub TIESCALAR { my $class = shift; my $val; bless \$val, $class; }
sub FETCH { ${$_[0]}; }
sub STORE { SNMP::_mib_toggle_options($_[1]); ${$_[0]} = $_[1]; }
sub DELETE { SNMP::_mib_toggle_options(0); ${$_[0]} = ''; }
package SNMP;
END{SNMP::_sock_cleanup() if defined &SNMP::_sock_cleanup;}
# Autoload methods go after __END__, and are processed by the autosplit prog.
1;
__END__
=head1 NAME
SNMP - The Perl5 'SNMP' Extension Module for the Net-SNMP SNMP package.
=head1 SYNOPSIS
use SNMP;
...
$sess = new SNMP::Session(DestHost => localhost, Community => public);
$val = $sess->get('sysDescr.0');
...
$vars = new SNMP::VarList([sysDescr,0], [sysContact,0], [sysLocation,0]);
@vals = $sess->get($vars);
...
$vb = new SNMP::Varbind();
do {
$val = $sess->getnext($vb);
print "@{$vb}\n";
} until ($sess->{ErrorNum});
...
$SNMP::save_descriptions = 1;
SNMP::initMib(); # assuming mib is not already loaded
print "$SNMP::MIB{sysDescr}{description}\n";
=head1 DESCRIPTION
Note: The perl SNMP 5.0 module which comes with net-snmp 5.0 and
higher is different than previous versions in a number of ways. Most
importantly, it behaves like a proper net-snmp application and calls
init_snmp properly, which means it will read configuration files and
use those defaults where appropriate automatically parse MIB files,
etc. This will likely affect your perl applications if you have, for
instance, default values set up in your snmp.conf file (as the perl
module will now make use of those defaults). The docmuentation,
however, has sadly not been updated yet (aside from this note), nor is
the read_config default usage implementation fully complete.
The basic operations of the SNMP protocol are provided by this module
through an object oriented interface for modularity and ease of use.
The primary class is SNMP::Session which encapsulates the persistent
aspects of a connection between the management application and the
managed agent. Internally the class is implemented as a blessed hash
reference. This class supplies 'get', 'getnext', 'set', 'fget', and
'fgetnext' method calls. The methods take a variety of input argument
formats and support both syncronous and asyncronous operation through
a polymorphic API (i.e., method behaviour varies dependent on args
passed - see below).
=head1 SNMP::Session
$sess = new SNMP::Session(DestHost => 'host', ...)
The following arguments may be passed to new as a hash.
=over 4
=item DestHost
default 'localhost', hostname or ip addr of SNMP agent
=item Community
default 'public', SNMP community string (used for both R/W)
=item Version
default taken from library configuration - probably 3 [1, 2 (same as 2c), 2c, 3]
=item RemotePort
default '161', allow remote UDP port to be overriden
=item Timeout
default '1000000', micro-seconds before retry
=item Retries
default '5', retries before failure
=item RetryNoSuch
default '0', if enabled NOSUCH errors in 'get' pdus will
be repaired, removing the varbind in error, and resent -
undef will be returned for all NOSUCH varbinds, when set
to '0' this feature is disabled and the entire get request
will fail on any NOSUCH error (applies to v1 only)
=item SecName
default 'initial', security name (v3)
=item SecLevel
default 'noAuthNoPriv', security level [noAuthNoPriv,
authNoPriv, authPriv] (v3)
=item SecEngineId
default <none>, security engineID, will be probed if not
supplied (v3)
=item ContextEngineId
default <SecEngineId>, context engineID, will be
probed if not supplied (v3)
=item Context
default '', context name (v3)
=item AuthProto
default 'MD5', authentication protocol [MD5, SHA] (v3)
=item AuthPass
default <none>, authentication passphrase
=item PrivProto
default 'DES', privacy protocol [DES, AES] (v3)
=item PrivPass
default <none>, privacy passphrase (v3)
=item AuthMasterKey
=item PrivMasterKey
=item AuthLocalizedKey
=item PrivLocalizedKey
Directly specified SNMPv3 USM user keys (used if you want to specify
the keys instead of deriving them from a password as above).
=item VarFormats
default 'undef', used by 'fget[next]', holds an hash
reference of output value formatters, (e.g., {<obj> =>
<sub-ref>, ... }, <obj> must match the <obj> and format
used in the get operation. A special <obj>, '*', may be
used to apply all <obj>s, the supplied sub is called to
translate the value to a new format. The sub is called
passing the Varbind as the arg
=item TypeFormats
default 'undef', used by 'fget[next]', holds an hash
reference of output value formatters, (e.g., {<type> =>
<sub-ref>, ... }, the supplied sub is called to translate
the value to a new format, unless a VarFormat mathces first
(e.g., $sess->{TypeFormats}{INTEGER} = \&mapEnum();
although this can be done more efficiently by enabling
$SNMP::use_enums or session creation param 'UseEnums')
=item UseLongNames
defaults to the value of SNMP::use_long_names at time
of session creation. set to non-zero to have <tags>
for 'getnext' methods generated preferring longer Mib name
convention (e.g., system.sysDescr vs just sysDescr)
=item UseSprintValue
defaults to the value of SNMP::use_sprint_value at time
of session creation. set to non-zero to have return values
for 'get' and 'getnext' methods formatted with the libraries
snprint_value function. This will result in certain data types
being returned in non-canonical format Note: values returned
with this option set may not be appropriate for 'set' operations
(see discussion of value formats in <vars> description section)
=item UseEnums
defaults to the value of SNMP::use_enums at time of session
creation. set to non-zero to have integer return values
converted to enumeration identifiers if possible, these values
will also be acceptable when supplied to 'set' operations
=item UseNumeric
defaults to the value of SNMP::use_numeric at time of session
creation. set to non-zero to have <tags> for get methods returned
as numeric OID's rather than descriptions. UseLongNames will be
set so that the full OID is returned to the caller.
=item BestGuess
defaults to the value of SNMP::best_guess at time of session
creation. this setting controls how <tags> are parsed. setting to
0 causes a regular lookup. setting to 1 causes a regular expression
match (defined as -Ib in snmpcmd) and setting to 2 causes a random
access lookup (defined as -IR in snmpcmd).
=item NonIncreasing
defaults to the value of SNMP::non_increasing at time of session
creation. this setting controls if a non-increasing OID during
bulkwalk will causes an error. setting to 0 causes the default
behaviour (which may, in very badly performing agents, result in a never-ending loop).
setting to 1 causes an error (OID not increasing) when this error occur.
=item ErrorStr
read-only, holds the error message assoc. w/ last request
=item ErrorNum
read-only, holds the snmp_err or staus of last request
=item ErrorInd
read-only, holds the snmp_err_index when appropriate
=back
Private variables:
=over
=item DestAddr
internal field used to hold the translated DestHost field
=item SessPtr
internal field used to cache a created session structure
=back
=head2 SNMP::Session methods
=over
=item $sess->update(E<lt>fieldsE<gt>)
Updates the SNMP::Session object with the values fields
passed in as a hash list (similar to new(E<lt>fieldsE<gt>))
B<(WARNING! not fully implemented)>
=item $sess->get(E<lt>varsE<gt> [,E<lt>callbackE<gt>])
do SNMP GET, multiple <vars> formats accepted.
for syncronous operation <vars> will be updated
with value(s) and type(s) and will also return
retrieved value(s). If <callback> supplied method
will operate asyncronously
=item $sess->fget(E<lt>varsE<gt> [,E<lt>callbackE<gt>])
do SNMP GET like 'get' and format the values according
the handlers specified in $sess->{VarFormats} and
$sess->{TypeFormats}
=item $sess->getnext(E<lt>varsE<gt> [,E<lt>callbackE<gt>])
do SNMP GETNEXT, multiple <vars> formats accepted,
returns retrieved value(s), <vars> passed as arguments are
updated to indicate next lexicographical <obj>,<iid>,<val>,
and <type>
Note: simple string <vars>,(e.g., 'sysDescr.0')
form is not updated. If <callback> supplied method
will operate asyncronously
=item $sess->fgetnext(E<lt>varsE<gt> [,E<lt>callbackE<gt>])
do SNMP GETNEXT like getnext and format the values according
the handlers specified in $sess->{VarFormats} and
$sess->{TypeFormats}
=item $sess->set(E<lt>varsE<gt> [,E<lt>callbackE<gt>])
do SNMP SET, multiple <vars> formats accepted.
the value field in all <vars> formats must be in a canonical
format (i.e., well known format) to ensure unambiguous
translation to SNMP MIB data value (see discussion of
canonical value format <vars> description section),
returns snmp_errno. If <callback> supplied method
will operate asyncronously
=item $sess->getbulk(E<lt>non-repeatersE<gt>, E<lt>max-repeatersE<gt>, E<lt>varsE<gt>)
do an SNMP GETBULK, from the list of Varbinds, the single
next lexico instance is fetched for the first n Varbinds
as defined by <non-repeaters>. For remaining Varbinds,
the m lexico instances are retrieved each of the remaining
Varbinds, where m is <max-repeaters>.
=item $sess->bulkwalk(E<lt>non-repeatersE<gt>, E<lt>max-repeatersE<gt>, E<lt>varsE<gt> [,E<lt>callbackE<gt>])
Do a "bulkwalk" of the list of Varbinds. This is done by
sending a GETBULK request (see getbulk() above) for the
Varbinds. For each requested variable, the response is
examined to see if the next lexico instance has left the
requested sub-tree. Any further instances returned for
this variable are ignored, and the walk for that sub-tree
is considered complete.
If any sub-trees were not completed when the end of the
responses is reached, another request is composed, consisting
of the remaining variables. This process is repeated until
all sub-trees have been completed, or too many packets have
been exchanged (to avoid loops).
The bulkwalk() method returns an array containing an array of
Varbinds, one for each requested variable, in the order of the
variable requests. Upon error, bulkwalk() returns undef and
sets $sess->ErrorStr and $sess->ErrorNum. If a callback is
supplied, bulkwalk() returns the SNMP request id, and returns
immediately. The callback will be called with the supplied
argument list and the returned variables list.
Note: Because the client must "discover" that the tree is
complete by comparing the returned variables with those that
were requested, there is a potential "gotcha" when using the
max-repeaters value. Consider the following code to print a
list of interfaces and byte counts:
$numInts = $sess->get('ifNumber.0');
($desc, $in, $out) = $sess->bulkwalk(0, $numInts,
[['ifDescr'], ['ifInOctets'], ['ifOutOctets']]);
for $i (0..($numInts - 1)) {
printf "Interface %4s: %s inOctets, %s outOctets\n",
$$desc[$i]->val, $$in[$i]->val, $$out[$i]->val;
}
This code will produce *two* requests to the agent -- the first
to get the interface values, and the second to discover that all
the information was in the first packet. To get around this,
use '$numInts + 1' for the max_repeaters value. This asks the
agent to include one additional (unrelated) variable that signals
the end of the sub-tree, allowing bulkwalk() to determine that
the request is complete.
=item $results = $sess->gettable(E<lt>TABLE OIDE<gt>, E<lt>OPTIONS<gt>)
This will retrieve an entire table of data and return a hash reference
to that data. The returned hash reference will have indexes of the
OID suffixes for the index data as the key. The value for each entry
will be another hash containing the data for a given row. The keys to
that hash will be the column names, and the values will be the data.
Example:
#!/usr/bin/perl
use SNMP;
use Data::Dumper;
my $s = new SNMP::Session(DestHost => 'localhost');
print Dumper($s->gettable('ifTable'));
On my machine produces:
$VAR1 = {
'6' => {
'ifMtu' => '1500',
'ifPhysAddress' => 'PV',
# ...
'ifInUnknownProtos' => '0'
},
'4' => {
'ifMtu' => '1480',
'ifPhysAddress' => '',
# ...
'ifInUnknownProtos' => '0'
},
# ...
};
By default, it will try to do as optimized retrieval as possible.
It'll request multiple columns at once, and use GETBULK if possible.
A few options may be specified by passing in an I<OPTIONS> hash
containing various parameters:
=over
=item noindexes => 1
Instructs the code not to parse the indexes and place the results in
the second hash. If you don't need the index data, this will be
faster.
=item columns => [ colname1, ... ]
This specifies which columns to collect. By default, it will try to
collect all the columns defined in the MIB table.
=item repeat => I<COUNT>
Specifies a GETBULK repeat I<COUNT>. IE, it will request this many
varbinds back per column when using the GETBULK operation. Shortening
this will mean smaller packets which may help going through some
systems. By default, this value is calculated and attepmts to guess
at what will fit all the results into 1000 bytes. This calculation is
fairly safe, hopefully, but you can either raise or lower the number
using this option if desired. In lossy networks, you want to make
sure that the packets don't get fragmented and lowering this value is
one way to help that.
=item nogetbulk => 1
Force the use of GETNEXT rather than GETBULK. (always true for
SNMPv1, as it doesn't have GETBULK anyway). Some agents are great
implementers of GETBULK and this allows you to force the use of
GETNEXT oprations instead.
=item callback => \&subroutine
=item callback => [\&subroutine, optarg1, optarg2, ...]
If a callback is specified, gettable will return quickly without
returning results. When the results are finally retrieved the
callback subroutine will be called (see the other sections defining
callback behaviour and how to make use of SNMP::MainLoop which is
required fro this to work). An additional argument of the normal hash
result will be added to the callback subroutine arguments.
Note 1: internally, the gettable function uses it's own callbacks
which are passed to getnext/getbulk as appropriate.
Note 2: callback support is only available in the SNMP module version
5.04 and above. To test for this in code intending to support both
versions prior to 5.04 and and 5.04 and up, the following should work:
if ($response = $sess->gettable('ifTable', callback => \&my_sub)) {
# got a response, gettable doesn't support callback
my_sub($response);
$no_mainloop = 1;
}
Deciding on whether to use SNMP::MainLoop is left as an excersize to
the reader since it depends on whether your code uses other callbacks
as well.
=back
=back
=head1 SNMP::TrapSession
$sess = new SNMP::Session(DestHost => 'host', ...)
supports all applicable fields from SNMP::Session
(see above)
=head2 SNMP::TrapSession methods
=over
=item $sess->trap(enterprise, agent, generic, specific, uptime, <vars>)
$sess->trap(enterprise=>'.1.3.6.1.4.1.2021', # or 'ucdavis' [default]
agent => '127.0.0.1', # or 'localhost',[dflt 1st intf on host]
generic => specific, # can be omitted if 'specific' supplied
specific => 5, # can be omitted if 'generic' supplied
uptime => 1234, # dflt to localhost uptime (0 on win32)
[[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars
# always last
=item trap(oid, uptime, <vars>) - v2 format
$sess->trap(oid => 'snmpRisingAlarm',
uptime => 1234,
[[ifIndex, 1, 1],[sysLocation, 0, "here"]]); # optional vars
# always last
=back
=head1 Acceptable variable formats:
<vars> may be one of the following forms:
=over
=item SNMP::VarList
represents an array of MIB objects to get or set,
implemented as a blessed reference to an array of
SNMP::Varbinds, (e.g., [<varbind1>, <varbind2>, ...])
=item SNMP::Varbind
represents a single MIB object to get or set, implemented as
a blessed reference to a 4 element array;
[<obj>, <iid>, <val>, <type>].
=over
=item <obj>
one of the following forms:
=over
=item 1)
leaf identifier (e.g., 'sysDescr') assumed to be
unique for practical purposes
=item 2)
fully qualified identifier (e.g.,
'.iso.org.dod.internet.mgmt.mib-2.system.sysDescr')
=item 3)
fully qualified, dotted-decimal, numeric OID (e.g.,
'.1.3.6.1.2.1.1.1')
=back
=item <iid>
the dotted-decimal, instance identifier. for
scalar MIB objects use '0'
=item <val>
the SNMP data value retrieved from or being set
to the agents MIB. for (f)get(next) operations
<val> may have a variety of formats as determined by
session and package settings. However for set
operations the <val> format must be canonical to
ensure unambiguous translation. The canonical forms
are as follows:
=over
=item OBJECTID
dotted-decimal (e.g., .1.3.6.1.2.1.1.1)
=item OCTETSTR
perl scalar containing octets
=item INTEGER
decimal signed integer (or enum)
=item NETADDR
dotted-decimal
=item IPADDR
dotted-decimal
=item COUNTER
decimal unsigned integer
=item COUNTER64
decimal unsigned integer
=item GAUGE
decimal unsigned integer
=item UINTEGER
decimal unsigned integer
=item TICKS
decimal unsigned integer
=item OPAQUE
perl scalar containing octets
=item NULL
perl scalar containing nothing
=back
=item <type>
SNMP data type (see list above), this field is
populated by 'get' and 'getnext' operations. In
some cases the programmer needs to populate this
field when passing to a 'set' operation. this
field need not be supplied when the attribute
indicated by <tag> is already described by loaded
Mib modules. for 'set's, if a numeric OID is used
and the object is not currently in the loaded Mib,
the <type> field must be supplied
=back
=item simple string
light weight form of <var> used to 'set' or 'get' a
single attribute without constructing an SNMP::Varbind.
stored in a perl scalar, has the form '<tag>.<iid>',
(e.g., 'sysDescr.0'). for 'set' operations the value
is passed as a second arg. Note: This argument form is
not updated in get[next] operations as are the other forms.
=back
=head1 Acceptable callback formats
<callback> may be one of the following forms:
=over
=item without arguments
=over
=item \&subname
=item sub { ... }
=back
=item or with arguments
=over
=item [ \&subname, $arg1, ... ]
=item [ sub { ... }, $arg1, ... ]
=item [ "method", $obj, $arg1, ... ]
=back
=back
callback will be called when response is received or timeout
occurs. the last argument passed to callback will be a
SNMP::VarList reference. In case of timeout the last argument
will be undef.
=over
=item &SNMP::MainLoop([<timeout>, [<callback>]])
to be used with async SNMP::Session
calls. MainLoop must be called after initial async calls
so return packets from the agent will not be processed.
If no args suplied this function enters an infinite loop
so program must be exited in a callback or externally
interupted. If <timeout(sic)
=item &SNMP::finish()
This function, when called from an SNMP::MainLoop() callback
function, will cause the current SNMP::MainLoop() to return
after the callback is completed. finish() can be used to
terminate an otherwise-infinite MainLoop. A new MainLoop()
instance can then be started to handle further requests.
=back
=head1 SNMP package variables and functions
=over
=item $SNMP::VERSION
the current version specifier (e.g., 3.1.0)
=item $SNMP::auto_init_mib
default '1', set to 0 to disable automatic reading
of the MIB upon session creation. set to non-zero
to call initMib at session creation which will result
in MIB loading according to Net-SNMP env. variables (see
man mib_api)
=item $SNMP::verbose
default '0', controls warning/info output of
SNMP module, 0 => no output, 1 => enables warning/info
output from SNMP module itself (is also controlled
by SNMP::debugging - see below)
=item $SNMP::use_long_names
default '0', set to non-zero to enable the use of
longer Mib identifiers. see translateObj. will also
influence the formatting of <tag> in varbinds returned
from 'getnext' operations. Can be set on a per session
basis (UseLongNames)
=item $SNMP::use_sprint_value
default '0', set to non-zero to enable formatting of
response values using the snmp libraries snprint_value
function. can also be set on a per session basis (see
UseSprintValue) Note: returned values may not be
suitable for 'set' operations
=item $SNMP::use_enums
default '0',set non-zero to return values as enums and
allow sets using enums where appropriate. integer data
will still be accepted for set operations. can also be
set on a per session basis (see UseEnums)
=item $SNMP::use_numeric
default to '0',set to non-zero to have <tags> for 'get'
methods returned as numeric OID's rather than descriptions.
UseLongNames will be set so that the entire OID will be
returned. Set on a per-session basis (see UseNumeric).
=item $SNMP::best_guess
default '0'. This setting controls how <tags> are
parsed. Setting to 0 causes a regular lookup. Setting
to 1 causes a regular expression match (defined as -Ib
in snmpcmd) and setting to 2 causes a random access
lookup (defined as -IR in snmpcmd). Can also be set
on a per session basis (see BestGuess)
=item $SNMP::save_descriptions
default '0',set non-zero to have mib parser save
attribute descriptions. must be set prior to mib
initialization
=item $SNMP::debugging
default '0', controlls debugging output level
within SNMP module and libsnmp
=over
=item 1
enables 'SNMP::verbose' (see above)
=item 2
level 1 plus snmp_set_do_debugging(1)
=item 3
level 2 plus snmp_set_dump_packet(1)
=back
=item $SNMP::dump_packet
default '0', set [non-]zero to independently set
snmp_set_dump_packet()
=back
=head1 %SNMP::MIB
a tied hash to access parsed MIB information. After
the MIB has been loaded this hash allows access to
to the parsed in MIB meta-data(the structure of the
MIB (i.e., schema)). The hash returns blessed
references to SNMP::MIB::NODE objects which represent
a single MIB attribute. The nodes can be fetched with
multiple 'key' formats - the leaf name (e.g.,sysDescr)
or fully/partially qualified name (e.g.,
system.sysDescr) or fully qualified numeric OID. The
returned node object supports the following fields:
=over
=item objectID
dotted decimal fully qualified OID
=item label
leaf textual identifier (e.g., 'sysDescr')
=item subID
leaf numeric OID component of objectID (e.g., '1')
=item moduleID
textual identifier for module (e.g., 'RFC1213-MIB')
=item parent
parent node
=item children
array reference of children nodes
=item nextNode
next lexico node B<(BUG!does not return in lexico order)>
=item type
returns application type (see getType for values)
=item access
returns ACCESS (ReadOnly, ReadWrite, WriteOnly,
NoAccess, Notify, Create)
=item status
returns STATUS (Mandatory, Optional, Obsolete,
Deprecated)
=item syntax
returns 'textualConvention' if defined else 'type'
=item textualConvention
returns TEXTUAL-CONVENTION
=item TCDescription
returns the TEXTUAL-CONVENTION's DESCRIPTION field.
=item units
returns UNITS
=item hint
returns HINT
=item enums
returns hash ref {tag => num, ...}
=item ranges
returns array ref of hash ref [{low => num, high => num}, ...]
=item description
returns DESCRIPTION ($SNMP::save_descriptions must
be set prior to MIB initialization/parsing)
=item reference
returns the REFERENCE clause
=back
=head1 MIB Functions
=over
=item &SNMP::setMib(<file>)
allows dynamic parsing of the mib and explicit
specification of mib file independent of enviroment
variables. called with no args acts like initMib,
loading MIBs indicated by environment variables (see
Net-SNMP mib_api docs). passing non-zero second arg
forces previous mib to be freed and replaced
B<(Note: second arg not working since freeing previous
Mib is more involved than before)>.
=item &SNMP::initMib()
calls library init_mib function if Mib not already
loaded - does nothing if Mib already loaded. will
parse directories and load modules according to
environment variables described in Net-SNMP documentations.
(see man mib_api, MIBDIRS, MIBS, MIBFILE(S), etc.)
=item &SNMP::addMibDirs(<dir>,...)
calls library add_mibdir for each directory
supplied. will cause directory(s) to be added to
internal list and made available for searching in
subsequent loadModules calls
=item &SNMP::addMibFiles(<file>,...)
calls library read_mib function. The file(s)
supplied will be read and all Mib module definitions
contained therein will be added to internal mib tree
structure
=item &SNMP::loadModules(<mod>,...)
calls library read_module function. The
module(s) supplied will be searched for in the
current mibdirs and and added to internal mib tree
structure. Passing special <mod>, 'ALL', will cause
all known modules to be loaded.
=item &SNMP::unloadModules(<mod>,...)
B<*Not Implemented*>
=item &SNMP::translateObj(<var>[,arg,[arg]])
will convert a text obj tag to an OID and vice-versa.
Any iid suffix is retained numerically. Default
behaviour when converting a numeric OID to text
form is to return leaf identifier only
(e.g.,'sysDescr') but when $SNMP::use_long_names
is non-zero or a non-zero second arg is supplied it
will return a longer textual identifier. An optional
third argument of non-zero will cause the module name
to be prepended to the text name (e.g.
'SNMPv2-MIB::sysDescr'). When converting a text obj,
the $SNMP::best_guess option is used. If no Mib is
loaded when called and $SNMP::auto_init_mib is enabled
then the Mib will be loaded. Will return 'undef' upon
failure.
=item &SNMP::getType(<var>)
return SNMP data type for given textual identifier
OBJECTID, OCTETSTR, INTEGER, NETADDR, IPADDR, COUNTER
GAUGE, TIMETICKS, OPAQUE, or undef
=item &SNMP::mapEnum(<var>)
converts integer value to enumertion tag defined
in Mib or converts tag to integer depending on
input. the function will return the corresponding
integer value *or* tag for a given MIB attribute
and value. The function will sense which direction
to perform the conversion. Various arg formats are
supported
=over
=item $val = SNMP::mapEnum($varbind);
where $varbind is SNMP::Varbind or equiv.
note: $varbind will be updated
=item $val = SNMP::mapEnum('ipForwarding', 'forwarding');
=item $val = SNMP::mapEnum('ipForwarding', 1);
=back
=back
=head1 Exported SNMP utility functions
Note: utility functions do not support async operation yet.
=over
=item &snmp_get()
takes args of SNMP::Session::new followed by those of
SNMP::Session::get
=item &snmp_getnext()
takes args of SNMP::Session::new followed by those of
SNMP::Session::getnext
=item &snmp_set()
takes args of SNMP::Session::new followed by those of
SNMP::Session::set
=item &snmp_trap()
takes args of SNMP::TrapSession::new followed by those of
SNMP::TrapSession::trap
=back
=head1 Trouble Shooting
If problems occur there are number areas to look at to narrow down the
possibilities.
The first step should be to test the Net-SNMP installation
independently from the Perl5 SNMP interface.
Try running the apps from the Net-SNMP distribution.
Make sure your agent (snmpd) is running and properly configured with
read-write access for the community you are using.
Ensure that your MIBs are installed and enviroment variables are set
appropriately (see man mib_api)
Be sure to remove old net-snmp installations and ensure headers and
libraries from old CMU installations are not being used by mistake.
If the problem occurs during compilation/linking check that the snmp
library being linked is actually the Net-SNMP library (there have been
name conflicts with existing snmp libs).
Also check that the header files are correct and up to date.
Sometimes compiling the Net-SNMP library with
'position-independent-code' enabled is required (HPUX specifically).
If you cannot resolve the problem you can post to
comp.lang.perl.modules or
net-snmp-users@net-snmp-users@lists.sourceforge.net
please give sufficient information to analyze the problem (OS type,
versions for OS/Perl/Net-SNMP/compiler, complete error output, etc.)
=head1 Acknowledgements
Many thanks to all those who supplied patches, suggestions and
feedback.
Joe Marzot (the original author)
Wes Hardaker and the net-snmp-coders
Dave Perkins
Marcel Wiget
David Blackburn
John Stofell
Gary Hayward
Claire Harrison
Achim Bohnet
Doug Kingston
Jacques Vidrine
Carl Jacobsen
Wayne Marquette
Scott Schumate
Michael Slifcak
Srivathsan Srinivasagopalan
Bill Fenner
Jef Peeraer
Daniel Hagerty
Karl "Rat" Schilke and Electric Lightwave, Inc.
Perl5 Porters
Alex Burger
Apologies to any/all who's patch/feature/request was not mentioned or
included - most likely it was lost when paying work intruded on my
fun. Please try again if you do not see a desired feature. This may
actually turn out to be a decent package with such excellent help and
the fact that I have more time to work on it than in the past.
=head1 AUTHOR
bugs, comments, questions to net-snmp-users@lists.sourceforge.net
=head1 Copyright
Copyright (c) 1995-2000 G. S. Marzot. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
Copyright (c) 2001-2002 Networks Associates Technology, Inc. All
Rights Reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl
itself.
=cut
|