首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用带有外壳脚本的pass_persist创建可遍历的snmpd扩展

如何使用带有外壳脚本的pass_persist创建可遍历的snmpd扩展
EN

Stack Overflow用户
提问于 2015-02-04 11:59:36
回答 2查看 5.1K关注 0票数 1

我在snmpd.conf文件中添加了以下两行代码:

代码语言:javascript
复制
view all included .1.3.6.1.4.1.8072.9999.9999
pass_persist .1.3.6.1.4.1.8072.9999.9999 /root/kshtest

我已经创建了一个shell脚本(/root/kshtest),该脚本接受一个文件OIDDEFS,该文件包含具有每个OID、其类型和值(以空格分隔)的行:

代码语言:javascript
复制
.1.3.6.1.4.1.8072.9999.9999.1.1 integer 35
.1.3.6.1.4.1.8072.9999.9999.1.2 integer 21
.1.3.6.1.4.1.8072.9999.9999.1.3 integer 56
.1.3.6.1.4.1.8072.9999.9999.2.1 integer 3592
.1.3.6.1.4.1.8072.9999.9999.2.2 integer 2234
.1.3.6.1.4.1.8072.9999.9999.2.3 integer 4499
.1.3.6.1.4.1.8072.9999.9999.2.4 integer 2233
.1.3.6.1.4.1.8072.9999.9999.3.1 integer 6650
.1.3.6.1.4.1.8072.9999.9999.3.2 integer 6650
.1.3.6.1.4.1.8072.9999.9999.3.3 integer 6650
.1.3.6.1.4.1.8072.9999.9999.3.4 integer 6650
.1.3.6.1.4.1.8072.9999.9999.4.1 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.2 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.3 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.4 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.5 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.6 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.7 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.8 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.9 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.10 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.11 integer 0
.1.3.6.1.4.1.8072.9999.9999.4.12 integer 0

并在从命令行运行时提供以下交互:

代码语言:javascript
复制
<- PING 

-> PONG 

<- get 

<- .1.3.6.1.4.1.8072.9999.9999.1.1 

-> .1.3.6.1.4.1.8072.9999.9999.1.1 

-> integer 

-> 35

<- PING 

-> PONG 

<- getnext 

<- .1.3.6.1.4.1.8072.9999.9999.1.1 

-> .1.3.6.1.4.1.8072.9999.9999.1.2 

-> integer 

-> 21 

脚本的内容如下所示(但我认为您需要知道的是它的工作方式如下所示):

代码语言:javascript
复制
#!/usr/pkg/bin/ksh93

OIDDEFS=OIDDEFS
IFS=$'\n'
lines=($(< $OIDDEFS))
typeset -A gettype
typeset -A getvalue
typeset -A getnextoid
typeset -A getnexttype
typeset -A getnextvalue
for (( x=0; x<${#lines[*]}; ((x++)) ))
  do IFS=$'\t\n '
  words=($(echo ${lines[x]}))
  gettype[${words[0]}]=${words[1]}
  getvalue[${words[0]}]=$(printf "%i\n" ${words[2]})
  if [[ x -gt 0 ]]; then
    getnextoid[${lines[x-1]%% *}]=${words[0]}
    getnexttype[${lines[x-1]%% *}]=${words[1]}
    getnextvalue[${lines[x-1]%% *}]=$(printf "%i\n" ${words[2]})
  fi
done
while true
  do read
  if [[ $REPLY == "PING" ]]; then
    echo PONG
  else
    exit 1
  fi
  x=0
  while [[ x -lt 2 ]];
    do read REPLY$x
    ((x++))
  done
  if [[ $REPLY0 == get ]]; then
    echo $REPLY1
    echo ${gettype[$REPLY1]}
    echo ${getvalue[$REPLY1]}
  fi
  if [[ $REPLY0 == getnext ]]; then
    if [[ ! -v getnextoid[$REPLY1] ]]; then
      echo NONE
    else
      echo ${getnextoid[$REPLY1]}
      echo ${getnexttype[$REPLY1]}
      echo ${getnextvalue[$REPLY1]}
    fi
  fi
done

我可以运行'snmpd -f‘(尽管我不明白为什么没有snmpd标志它就不能工作),然后执行一个

代码语言:javascript
复制
snmpgetnext -On -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.8072.9999.9999.1.1

并获得响应:

代码语言:javascript
复制
.1.3.6.1.4.1.8072.9999.9999.1.2 = INTEGER: 21

我可以成功地snmpget (net-snmp命令)我的MIB中的任何oid并获得它们的值,或者snmpgetnext任何oid并获得下一个oid和值。但是,我不能随便走动我的oids。当我

代码语言:javascript
复制
snmpwalk -On -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.8072.9999.9999.1.1

我得到了

代码语言:javascript
复制
.1.3.6.1.4.1.8072.9999.9999.1.1 = INTEGER: 35

这与我从该oid上的snmpget得到的响应相同。

代码语言:javascript
复制
snmpwalk -d -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.8072.9999.9999.1.1

给了我:

代码语言:javascript
复制
Sending 48 bytes to UDP: [127.0.0.1]:161->[0.0.0.0]:0
0000: 30 2E 02 01  01 04 06 70  75 62 6C 69  63 A1 21 02    0......public.!.
0016: 04 7F 15 82  9A 02 01 00  02 01 00 30  13 30 11 06    ...........0.0..
0032: 0D 2B 06 01  04 01 BF 08  CE 0F CE 0F  01 01 05 00    .+..............


Received 49 byte packet from UDP: [127.0.0.1]:161->[0.0.0.0]:65489
0000: 30 2F 02 01  01 04 06 70  75 62 6C 69  63 A2 22 02    0/.....public.".
0016: 04 7F 15 82  9A 02 01 00  02 01 00 30  14 30 12 06    ...........0.0..
0032: 0D 2B 06 01  04 01 BF 08  CE 0F CE 0F  01 02 02 01    .+..............
0048: 15                                                    .


Sending 48 bytes to UDP: [127.0.0.1]:161->[0.0.0.0]:0
0000: 30 2E 02 01  01 04 06 70  75 62 6C 69  63 A0 21 02    0......public.!.
0016: 04 7F 15 82  9B 02 01 00  02 01 00 30  13 30 11 06    ...........0.0..
0032: 0D 2B 06 01  04 01 BF 08  CE 0F CE 0F  01 01 05 00    .+..............


Received 49 byte packet from UDP: [127.0.0.1]:161->[0.0.0.0]:65489
0000: 30 2F 02 01  01 04 06 70  75 62 6C 69  63 A2 22 02    0/.....public.".
0016: 04 7F 15 82  9B 02 01 00  02 01 00 30  14 30 12 06    ...........0.0..
0032: 0D 2B 06 01  04 01 BF 08  CE 0F CE 0F  01 01 02 01    .+..............
0048: 23                                                    #

NET-SNMP-MIB::monoCount = INTEGER: 35

它看起来是.1.3.6.1.4.1.8072.9999.9999.1.1旁边的snmpget,接着是引用.1.3.6.1.4.1.8072.9999.9999.1.2的snmpget响应,接着是.1.3.6.1.4.1.8072.9999.9999.1.1的snmpget,接着是引用.1.3.6.1.4.1.8072.9999.9999.1.1的snmpget响应。

我的NET-SNMP-MIB.txt文件,我修改该文件以添加MIB值如下:

代码语言:javascript
复制
NET-SNMP-MIB DEFINITIONS ::= BEGIN

--
-- Top-level infrastructure of the Net-SNMP project enterprise MIB tree
--

IMPORTS
    MODULE-IDENTITY, Integer32, enterprises FROM SNMPv2-SMI;

netSnmp MODULE-IDENTITY
    LAST-UPDATED "200201300000Z"
    ORGANIZATION "www.net-snmp.org"
    CONTACT-INFO    
     "postal:   Wes Hardaker
                    P.O. Box 382
                    Davis CA  95617

          email:    net-snmp-coders@lists.sourceforge.net"
    DESCRIPTION
    "Top-level infrastructure of the Net-SNMP project enterprise MIB tree"
    REVISION     "200201300000Z"
    DESCRIPTION
    "First draft"
    ::= { enterprises 8072}


--
--  Net-SNMP enterprise-specific management objects
--

netSnmpObjects              OBJECT IDENTIFIER ::= {netSnmp 1}
-- netSnmpExamples             OBJECT IDENTIFIER ::= {netSnmp 2}
netSnmpEnumerations         OBJECT IDENTIFIER ::= {netSnmp 3}
netSnmpModuleIDs            OBJECT IDENTIFIER ::= {netSnmpEnumerations 1}
netSnmpAgentOIDs            OBJECT IDENTIFIER ::= {netSnmpEnumerations 2}
netSnmpDomains              OBJECT IDENTIFIER ::= {netSnmpEnumerations 3}
netSnmpExperimental         OBJECT IDENTIFIER ::= {netSnmp 9999}

--
-- A subtree specifically designed for private testing purposes.
-- No "public" management objects should ever be defined within this tree.
--
-- It is provided for private experimentation, prior to transferring a MIB
-- structure to another part of the overall OID tree
--
netSnmpPlaypen              OBJECT IDENTIFIER ::= {netSnmpExperimental 9999}


--
--  Notifications
--

netSnmpNotificationPrefix   OBJECT IDENTIFIER ::= {netSnmp 4}
netSnmpNotifications        OBJECT IDENTIFIER ::= {netSnmpNotificationPrefix 0}
netSnmpNotificationObjects  OBJECT IDENTIFIER ::= {netSnmpNotificationPrefix 1}


--
--  Conformance
--     (No laughing at the back!)
--

netSnmpConformance          OBJECT IDENTIFIER ::= {netSnmp 5}
netSnmpCompliances          OBJECT IDENTIFIER ::= {netSnmpConformance 1}
netSnmpGroups               OBJECT IDENTIFIER ::= {netSnmpConformance 2}

-- start additions:
pageCountsVals              OBJECT IDENTIFIER ::= {netSnmpPlaypen 1}
tonerLevelsVals             OBJECT IDENTIFIER ::= {netSnmpPlaypen 2}
tonerLevelsMaxVals          OBJECT IDENTIFIER ::= {netSnmpPlaypen 3}
otherRandomVals             OBJECT IDENTIFIER ::= {netSnmpPlaypen 4}

monoCount                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {pageCountsVals 1}

colorCount                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {pageCountsVals 2}

totalCount                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {pageCountsVals 3}

cyanVal                      OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsVals 1}

magentaVal                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsVals 2}

yellowVal                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsVals 3}

blackVal                     OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsVals 4}

cyanMaxVal                      OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsMaxVals 1}

magentaMaxVal                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsMaxVals 2}

yellowMaxVal                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsMaxVals 3}

blackMaxVal                     OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {tonerLevelsMaxVals 4}

randomVal1                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 1}

randomVal2                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 2}

randomVal3                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 3}

randomVal4                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 4}

randomVal5                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 5}

randomVal6                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 6}

randomVal7                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 7}

randomVal8                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 8}

randomVal9                    OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 9}

randomVal10                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 10}

randomVal11                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 11}

randomVal12                   OBJECT-TYPE
    SYNTAX      Integer32
    MAX-ACCESS  read-write
    STATUS      current
    DEFVAL      { 0 }
    ::= {otherRandomVals 12}

END

我希望这能给一个有经验的snmp‘’er足够的信息来发现我做错了什么。如果没有,请询问。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2015-02-11 02:18:45

Snmpwalk获取树中您指定的OID下的所有OID。由于您指定了.1.3.6.1.4.1.8072.9999.9999.1.1,因此它只获取该特定的OID,因为在该OID下没有其他OID。要遍历您定义的所有OIDS,请执行以下操作:

代码语言:javascript
复制
snmpwalk -On -v 2c -c public 127.0.0.1 .1.3.6.1.4.1.8072.9999.9999
票数 0
EN

Stack Overflow用户

发布于 2016-07-20 03:14:06

@user1693487问题是你的第三行;

OIDDEFS=OIDDEFS

当作为服务/init.d运行时,这需要一个完整路径;

代码语言:javascript
复制
OIDDEFS=/usr/bla/path/OIDDEFS

不管怎样,你的帖子帮了我的忙,因为我也面临着同样的问题!所以谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28313258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档