Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## controller
- def update_macs
- @device = Device.find(params[:id])
- @device.process
- if @device.errror_message
- flash[:notice] = @device.error_message
- end
- redirect_to :action => 'show_device', :id => params[:id]
- end
- ## app/models/device.rb
- class Device < ActiveRecord::Base
- def error_message
- @error_message
- end
- def process
- if Time.now - self.last_update < UpdateInterval
- @error_message = "Can't call that now."
- return
- end
- master = DeviceMaster.new
- mac_table = master.mac_table(self)
- unless mac_table.blank?
- Mac.delete_all(["device_id = ?", self.id])
- self.update_attribute!(:last_update, Time.now)
- end
- mac_port = master.regexp_mac_port(self)
- port_check = master.regexp_port_check(self)
- mac_separator = master.regexp_mac_separator(self)
- seq_type = master.regexp_mac_port_seq_type(self)
- @mac_table.split("\n").each {|line|
- if line =~ mac_port
- if seq_type == 0
- addr = $1
- port_name = $2
- else
- addr = $2
- port_name = $1
- end
- mac = Mac.new
- mac.address = addr.gsub(mac_separator, '').upcase
- # instead of using port_map, which loads all the ports from the database, we
- # only find the port we need
- mac.port = self.ports.find_by_name(port_name)
- mac.device = self
- mac.save
- end
- }
- check_idle_ports
- self
- rescue Securityerror => msg
- @error_message = msg
- rescue Timeout::Error, Errno::EHOSTUNREACH, SocketError => msg
- @error_message = "No connection to #{self.address}<br/>#{msg}"
- end
- def check_idle_ports
- self.ports.each {|port|
- new_count = port.macs.count
- # check for change
- if new_count != port.macs_count
- if new_count == 0
- port.idle_since = Time.new
- # send notifications to defined mails
- if port.send_notifications
- all_addresses = MailNotification.get_all_addresses
- Mailer.deliver_no_macs(device, port, all_addresses )
- end
- end # if
- port.macs_count = new_count
- port.save!
- end # if
- }
- end
- end
- ## lib/tasks/device_processor.rake
- # rake device_processor:process_device ID=6
- namespace :device_processor do
- task :process_device do
- # Something like this? *shrug*
- DeviceProcessor.new(ENV["ID"]).process_device
- end
- end
Add Comment
Please, Sign In to add comment