svnno****@sourc*****
svnno****@sourc*****
2009年 11月 27日 (金) 18:32:39 JST
Revision: 44 http://sourceforge.jp/projects/ngms/svn/view?view=rev&revision=44 Author: tatsuro Date: 2009-11-27 18:32:39 +0900 (Fri, 27 Nov 2009) Log Message: ----------- 自動設定 Added Paths: ----------- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/CactiConfig.scala trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/ExecCmd.scala trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NagiosConfig.scala Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/CactiConfig.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/CactiConfig.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/CactiConfig.scala 2009-11-27 09:32:39 UTC (rev 44) @@ -0,0 +1,64 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * Cacti の自動設定を行うクラス + * + * @version $Id$ + * @author + */ + +class CactiConfig { + val cactiDir = "/usr/local/cacti" + + // php add_device.php --description=hostname --ip=address --template=1 \ + // --avail=snmp --version=2 --community=public + // + // host-template: Generic SNMP-enabled Host + + def addDevice(hostname:String, address:String) = { + val cmd = + "php %s/cli/add_device.php --description=%s --ip=%s ".format(cactiDir, hostname, address) + + "--quiet --template=1 --avail=snmp --version=2 --community=public" + val proc = new execCmd(cmd) + proc.exec + } + + def getHostID(address:String) = { + val cmd = "php %s/cli/add_graphs.php --list-hosts".format(cactiDir) + val proc = new execCmd(cmd) + val result = proc.exec + val lines = result.split("\\n").filter(_.contains(address)).toList + if (lines.isEmpty) + "" + else + lines.head.split("\\s+").toList.head + } + + //php add_graphs.php --graph-type=ds --graph-template-id=2 --host-id=?? \ + // --snmp-query-id=1 --snmp-query-type-id=13 \ + // --snmp-field=ifOperStatus --snmp-value=Up + // graph-template-id: Interface - Traffic (bits/sec) + // snmp-query-id: SNMP - Interface Statistics + // snmp-query-type-id: In/Out Bits + def addGraph(hostid:String) = { + val cmd = "php %s/cli/add_graphs.php ".format(cactiDir) + + "--graph-type=ds --graph-template-id=2 --host-id=%s ".format(hostid) + + "--quiet --snmp-query-id=1 --snmp-query-type-id=13 --snmp-field=ifOperStatus --snmp-value=Up" + val proc = new execCmd(cmd) + proc.exec + } +} + +/* +val cmd = new execCmd("date") +cmd.setEnv("LANG", "C") +cmd.setDir("/tmp") +//cmd.setEnv("LANG", "ja_JP.UTF-8") +val result = cmd.exec +println(result) +*/ + Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/ExecCmd.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/ExecCmd.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/ExecCmd.scala 2009-11-27 09:32:39 UTC (rev 44) @@ -0,0 +1,52 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * 外部コマンドを実行するクラス + * + * @version $Id$ + * @author + */ + +import scala.io.Source +import java.io.File + +class execCmd(cmd:String) { + def toJavaList[T](ary: Array[T]): java.util.List[T] = { + val lst = new java.util.ArrayList[T]; + ary.map(lst.add) + lst + } + val pb = new ProcessBuilder(toJavaList(cmd.split("\\s+"))) + + def setEnv(env:java.lang.String, value:java.lang.String) = { + val e = pb.environment() + e.put(env, value) + } + + def setDir(dir:String) = { + pb.directory(new File(dir)) + } + + def exec = { + val p = pb.start() + val ret = p.waitFor() + val is = p.getInputStream() + val result = Source.fromInputStream(is).getLines.mkString.trim + is.close + result + } +} + +/* +//val cmd = new execCmd("date") +val cmd = new execCmd("ls -lRta") +cmd.setEnv("LANG", "C") +cmd.setDir("/tmp") +//cmd.setEnv("LANG", "ja_JP.UTF-8") +val result = cmd.exec +println(result) +*/ Added: trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NagiosConfig.scala =================================================================== --- trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NagiosConfig.scala (rev 0) +++ trunk/source/NMNetWalker/src/info/ngms/nmnetwalker/NagiosConfig.scala 2009-11-27 09:32:39 UTC (rev 44) @@ -0,0 +1,112 @@ +/* + * Next Generation Management System Project + * Copyright(c) 2009, NGMS Project Team All Rights Reserved. + */ +package info.ngms.nmnetwalker + +/** + * Nagios の自動設定を行うクラス + * + * @version $Id$ + * @author + */ + +import scala.io.Source +import java.net.InetAddress + +class NagiosConfig { + val NagiosDir = "/usr/local/nagios" + + def writeConfig(hostname:String, addr:String) = { + val fmt = """define host { + use NGMS_host + host_name %s + alias %s + address %s +} + +define service { + use NGMS_service_ping + host_name %s +} +""" + printf(fmt, hostname, hostname, addr, hostname) + //val filename = "%s/etc/NGMS/%s.cfg".format(NagiosDir, hostname) + //val file = new java.io.FileWriter(filename) + //file.write(fmt.format(hostname, hostname, addr, hostname)) + //file.close + } + + def sendHUP = { + val lockFile = "%s/var/nagios.lock".format(NagiosDir) + val pid = Source.fromFile(lockFile).getLines.mkString.trim + val cmd = "kill -1 %s".format(pid) + printf("send HUP signal to Nagios: %s\n",cmd) + //val proc = execCmd(cmd) + // proc.exec + } +} + +/* +val cfg = new NagiosConfig +cfg.writeConfig("myname", "192.168.1.1") +cfg.sendHUP +*/ + +/* Template file +# +# for NGMS auto config +# + +# +# host +# +define host { + name NGMS_host_common + check_command check-host-alive + max_check_attempts 4 + contact_groups admins + notification_interval 60 + register 0 +} + +define host { + use NGMS_host_common + name NGMS_host + check_period 24x7 + notification_period 24x7 + notification_options d,u,r + register 0 +} + +define host { + use NGMS_host_common + name NGMS_switch + check_period 24x7 + notification_period 24x7 + notification_options d,r + register 0 +} + +# +# service +# +define service { + name NGMS_service_common + max_check_attempts 4 + check_interval 5 + retry_interval 2 + check_period 24x7 + notification_period 24x7 + contact_groups admins + register 0 +} + +define service { + use NGMS_service_common + name NGMS_service_ping + service_description PING + check_command check_ping!100.0,20%!500.0,60% + register 0 +} +*/