Network Monitor as a Windows Service in Ruby
In reading thru the Ruby Google Group, there seems to be a lot of misconceptions about what should be done inside
of a Rails process and what should be done outside it. Generally if it take any time at all, you should be it outside
the Rails Web Server. In windows you can do a Windows Service, in Linux/Unix you would do a daemon.
This is a example of a windows service that scans cisco switches, finds new nodes, and keep track of there mac
address and there port. Even builds a network map.
The app consists of a Rails app to display the data, and take in configuration information. Which is put in a database,
such as sqlite3, and updated by this Windows Service.
As long as the Service is running, the data will be updated. If I was doing it again, I'd use rufus-scheduler to
wrap the scanning and the mapping.
require 'rubygems'
require 'win32/daemon'
include Win32
require 'logger'
require 'win32/process'
require 'net/ping'
#Note that most of your requires need to go in the service init
class IPAddr
def succ()
return self.clone.set(@addr + 1)
end
end
class AirstateDiscovery
def doscan()
ProcessNetworks()
ProcessDiscovery()
return
end
def ProcessDiscovery()
Mac.find(:all).each do |mymac|
mydevice = mymac.device
if mydevice
mynet = Network.find_network_by_ip(mymac.ip)
if mynet #Ok we have a valid network
Device.find_device_type(mymac.ip,mydevice)
end
end
end
end
def ProcessSwitches()
Switch.find(:all).each do |myswitch|
if myswitch.enabled
if myswitch.switch_type == "cisco"
cisco = CiscoSwitch.new(myswitch)
#begin
cisco.update_macs()
#rescue
# end # Rescue
cisco = nil
end #if myswitch.switch_type == "cisco"
end # myswitch.enabled
thegraph = SwitchGraph.new(myswitch)
thegraph.graph()
thegraph = nil
end # find do
end
def ProcessNetworks()
ProcessSwitches()
return
# Brute Force Scan
Network.find(:all).each do |mynet|
ProcessSwitches() # Networks is "long" running, Switch is "fast"
if mynet.enable
startip = mynet.ip_start
endip = mynet.ip_end
ip = IPAddr.new(startip)
while ip.to_s != endip
ProcessWmi(mynet,ip.to_s)
ip = ip.succ()
end
end
end
end # def Process Networks
end # class AirStateDiscovery
class Daemon
def service_init
end
def service_main
require 'ipaddr'
Dir.chdir("\\projects\\airstate")
require File.dirname(__FILE__) + '/../config/environment.rb'
require 'lib/cisco_phone'
require 'lib/managepc'
require 'lib/switchgraph'
@mywork = AirstateDiscovery.new
mylog = Syslog.new
mylog.whom = "AirStateDiscovery"
mylog.logmessage = "Discovery Starting"
mylog.save
while running?
@mywork.doscan()
sleep 3
end
end
end
# Test Code
# require 'ipaddr'
# Dir.chdir("\\projects\\airstate")
# require File.dirname(__FILE__) + '/../config/environment.rb'
# require 'lib/managepc'
# require 'lib/cisco'
# require 'lib/switchgraph'
# require 'lib/cisco_phone'
# @mywork = AirstateDiscovery.new
# while 1
# @mywork.doscan()
# end
# exit
Daemon.mainloop
- Company:
- Operating System:
- Person:
- Technology:


Recent comments
1 year 23 weeks ago
1 year 23 weeks ago
1 year 25 weeks ago
1 year 27 weeks ago
1 year 42 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 46 weeks ago
1 year 48 weeks ago