Groovy后处理snmpget结果?

问题描述

我在groovy中有一个脚本,它实际上是在轮询某些VLAN STP的最新拓扑更改。如果我执行普通的SNMP退出脚本,它将以滴答为单位返回值:

enter image description here

但是,当我在Groovy上执行完全相同的snmpget时,它将以可读格式返回结果:

enter image description here

我正在使用一个监视工具,该工具仅期望以刻度为单位的值(而不是实际的可读时间)。因此,我想了解为什么要对结果进行“处理”。

MyScript:

import com.santaba.agent.groovyapi.expect.Expect;
import com.santaba.agent.groovyapi.snmp.Snmp;
import com.santaba.agent.groovyapi.http.*;
import com.santaba.agent.groovyapi.jmx.*;
import org.xbill.DNS.*;
import com.santaba.agent.groovyapi.snmp.Snmp;

//array to store the interfaces
array_vlans = [];

//OID variables
stpTCN=".1.3.6.1.2.1.17.2.3.0" //get the actual TCN for the STP (for each VLAN)
vlan_indexes = ".1.3.6.1.4.1.9.9.46.1.3.1.1.3" //CISCO VLAN MIB

//device variables
deviceIP = hostProps.get("system.hostname");
snmpCommunity_raw = hostProps.get("snmp.community");
snmpVersion = hostProps.get("snmp.version");

interfaceWalk = Snmp.walk(deviceIP,vlan_indexes)
interfaceWalk.eachLine() {
    line ->
        vlanType= line.minus(vlan_indexes + ".").split(" = ")[1] //get the content of the specific OID
        vlanNumber_raw= line.minus(vlan_indexes + ".").split(" = ")[0] //get the actual extra OID portion that was added (since that's the actual VLAN ID)
        vlanNumber = vlanNumber_raw.replace("1.",""); //remove the WEIRD '1.' that get's added to the OID

        if(vlanType != "1"){
            //doNothing (since we only care about type==1 [ethernet])
        }else{
            //DEBUG print
            /*println "####"
            println "VLAN Type -> " +vlanType
            println "VLAN Number -> " +vlanNumber*/ 
            array_vlans.push(vlanNumber); //add VLANS(s) to the array
        }
}

//iterate over the VLAN array & do the SNMP GET per vlan (to retrieve the TopologyChange metric)
array_vlans.each(){
    vlanID=it
    snmpCommunity=snmpCommunity_raw+"@"+vlanID
    getVlan_TCN= Snmp.get(deviceIP,snmpCommunity,snmpVersion,stpTCN);
    println vlanID+".LastTopologyChange="+getVlan_TCN
}

感谢帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...