svnno****@sourc*****
svnno****@sourc*****
2009年 11月 18日 (水) 15:57:16 JST
Revision: 40 http://sourceforge.jp/projects/ngms/svn/view?view=rev&revision=40 Author: takayah Date: 2009-11-18 15:57:16 +0900 (Wed, 18 Nov 2009) Log Message: ----------- make initial library and NMNetWalker source file Modified Paths: -------------- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/Main.scala Added Paths: ----------- trunk/lib/ trunk/lib/log4j/ trunk/lib/log4j/1.2.15/ trunk/lib/log4j/1.2.15/log4j-1.2.15.jar trunk/lib/scalatest/ trunk/lib/scalatest/1.0/ trunk/lib/scalatest/1.0/scalatest-1.0.jar trunk/lib/snmp4j/ trunk/lib/snmp4j/1.10.2/ trunk/lib/snmp4j/1.10.2/snmp4j-1.10.2.jar trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/AutoConfig.scala trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NMNetWalker.scala trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/SNMPNetWalker.scala Added: trunk/lib/log4j/1.2.15/log4j-1.2.15.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/log4j/1.2.15/log4j-1.2.15.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/lib/scalatest/1.0/scalatest-1.0.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/scalatest/1.0/scalatest-1.0.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/lib/snmp4j/1.10.2/snmp4j-1.10.2.jar =================================================================== (Binary files differ) Property changes on: trunk/lib/snmp4j/1.10.2/snmp4j-1.10.2.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/AutoConfig.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/AutoConfig.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/AutoConfig.scala 2009-11-18 06:57:16 UTC (rev 40) @@ -0,0 +1,44 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * 自動設定を行うクラス + * + * @version $Id$ + * @author + */ +class AutoConfig { + import scala.io.Source + import scala.collection.Map + import scala.collection.mutable.HashMap + + def getConfig(path : String) : Map[String, String] = { + val config = new HashMap[String, String] + for (line <- Source.fromFile(path).getLines) { + val index = line.indexOf(":"); + val key = line.substring(0, index); + val value = line.substring(index + 1); + config(key.trim) = value.trim + } + config + } + + def createConfig(confPath : String, tempPath : String) : String = { + var dtConfig = getConfig(confPath); + + var temp : String = "" + for (line <- Source.fromFile(tempPath).getLines) { + temp += line + } + + var config : String = temp + for ((key, value) <- dtConfig) { + config = config.replaceAll("%" + key + "%", value); + } + config + } +} + Property changes on: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/AutoConfig.scala ___________________________________________________________________ Added: svn:keywords + Date Id Modified: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/Main.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/Main.scala 2009-11-17 04:23:42 UTC (rev 39) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/Main.scala 2009-11-18 06:57:16 UTC (rev 40) @@ -1,3 +1,7 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ package info.ngms.nmnetwalker /** @@ -6,10 +10,15 @@ * @author */ object Main { - /** - * メイン関数 - */ - def main(args : Array[String]) : Unit = { - println("this is netwalker") - } + /** + * メイン関数 + */ + def main(args : Array[String]) : Unit = { + val addr = "192.168.0.1" + + val result = NMNetWalker.find(addr); + for ((key, value) <- result) { + printf("key: %s, value: %s\n", key, value); + } + } } Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NMNetWalker.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NMNetWalker.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NMNetWalker.scala 2009-11-18 06:57:16 UTC (rev 40) @@ -0,0 +1,26 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * 自動取得を行うクラス + * + * @version $Id$ + * @author + */ +object NMNetWalker { + def find(addr: String): List[(String, String)] = { + val parsedAddr = String.format("udp:%s/161", addr); + val oids = List("1.3.6.1.2.1.1.2.0", // sysObjectID + "1.3.6.1.2.1.1.5.0", // sysName + "1.3.6.1.2.1.2.1", // ifNumber + "1.3.6.1.2.1.2.2.1.6.0") // ifPhysAddress.0 + + val walker = new SNMPNetWalker() + + walker.get(parsedAddr, oids) + } +} + Property changes on: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NMNetWalker.scala ___________________________________________________________________ Added: svn:keywords + Date Id Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/SNMPNetWalker.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/SNMPNetWalker.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/SNMPNetWalker.scala 2009-11-18 06:57:16 UTC (rev 40) @@ -0,0 +1,113 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * SNMP + * + * @version $Id$ + * @author + */ +class SNMPNetWalker { + import org.snmp4j.CommunityTarget + import org.snmp4j.MessageDispatcher + import org.snmp4j.MessageDispatcherImpl + import org.snmp4j.PDU + import org.snmp4j.Snmp + import org.snmp4j.TransportMapping + import org.snmp4j.event.ResponseEvent + import org.snmp4j.mp.MPv1 + import org.snmp4j.mp.MPv2c + import org.snmp4j.mp.MPv3 + import org.snmp4j.mp.SnmpConstants + import org.snmp4j.smi.Address + import org.snmp4j.smi.GenericAddress + import org.snmp4j.smi.OID + import org.snmp4j.smi.OctetString + import org.snmp4j.smi.VariableBinding + import org.snmp4j.transport.DefaultUdpTransportMapping + + def get(addr: String, oids: List[String]): List[(String, String)] = { + get(addr, oids, SnmpConstants.version2c, 5, 1000L, "public") + } + + def get(addr: String, oids: List[String], version: Int, retry: Int, + timeout: Long, community: String): List[(String, String)] = { + // check param + require((addr != null) && (addr.length() >= 1), "address is null") + require((oids != null) && (oids.length >= 1), "OIDs is null") + require((version >= 0) && (version <= 3), "invalid version") + require(retry >= 0, "retry is negative value") + require(timeout >= 0, "timeout is negative value") + require((community != null) && (community.length() >= 1), "community is null") + + val snmp = createSnmp() + var result = List[(String, String)]() + try { + val pdu = createPDU(oids) + val target = createCommunityTarget(addr, version, retry, timeout, community) + + val resEvent = snmp.get(pdu, target) + + if (resEvent != null && resEvent.getResponse() != null) { + val vbs = resEvent.getResponse().getVariableBindings() + + for (i <- (0 to (vbs.size() - 1))) { + val vb = vbs.elementAt(i); + val resultStr = vb.toString() + val index = resultStr.indexOf("=") + result = (resultStr.substring(0, index).trim, + resultStr.substring(index + 1).trim) :: result + } + } + } finally { + if (snmp != null) { + snmp.close() + } + } + + result.reverse + } + + private def createSnmp(): Snmp = { + val msgDispatcher = new MessageDispatcherImpl() + msgDispatcher.addMessageProcessingModel(new MPv1()) + msgDispatcher.addMessageProcessingModel(new MPv2c()) + msgDispatcher.addMessageProcessingModel(new MPv3()) +// SecurityProtocols.getInstance().addDefaultProtocols() + val transport = new DefaultUdpTransportMapping() + val snmp = new Snmp(msgDispatcher, transport) + + transport.listen() + + snmp + } + + private def createCommunityTarget(addr: String, version: Int, + retry: Int, timeout: Long, + community: String): CommunityTarget = { + val targetAddress = GenericAddress.parse(addr) + + val target = new CommunityTarget() + target.setCommunity(new OctetString(community)) + target.setAddress(targetAddress) + target.setRetries(retry) + target.setTimeout(timeout) + target.setVersion(version) + + target + } + + private def createPDU(oids: List[String]): PDU = { + val pdu = new PDU() + pdu.setType(PDU.GET) + for (oid <- oids) { + pdu.add(new VariableBinding(new OID(oid))) + } + + pdu + } +} + Property changes on: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/SNMPNetWalker.scala ___________________________________________________________________ Added: svn:keywords + Date Id