Guest User

Untitled

a guest
Jul 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. ## controller
  2. def update_macs
  3. @device = Device.find(params[:id])
  4. @device.process
  5.  
  6. if @device.errror_message
  7. flash[:notice] = @device.error_message
  8. end
  9.  
  10. redirect_to :action => 'show_device', :id => params[:id]
  11. end
  12.  
  13. ## app/models/device.rb
  14. class Device < ActiveRecord::Base
  15. def error_message
  16. @error_message
  17. end
  18.  
  19. def process
  20. if Time.now - self.last_update < UpdateInterval
  21. @error_message = "Can't call that now."
  22. return
  23. end
  24.  
  25. master = DeviceMaster.new
  26. mac_table = master.mac_table(self)
  27. unless mac_table.blank?
  28. Mac.delete_all(["device_id = ?", self.id])
  29. self.update_attribute!(:last_update, Time.now)
  30. end
  31.  
  32. mac_port = master.regexp_mac_port(self)
  33. port_check = master.regexp_port_check(self)
  34. mac_separator = master.regexp_mac_separator(self)
  35. seq_type = master.regexp_mac_port_seq_type(self)
  36.  
  37. @mac_table.split("\n").each {|line|
  38. if line =~ mac_port
  39. if seq_type == 0
  40. addr = $1
  41. port_name = $2
  42. else
  43. addr = $2
  44. port_name = $1
  45. end
  46.  
  47. mac = Mac.new
  48. mac.address = addr.gsub(mac_separator, '').upcase
  49. # instead of using port_map, which loads all the ports from the database, we
  50. # only find the port we need
  51. mac.port = self.ports.find_by_name(port_name)
  52. mac.device = self
  53. mac.save
  54. end
  55. }
  56.  
  57. check_idle_ports
  58.  
  59. self
  60. rescue Securityerror => msg
  61. @error_message = msg
  62. rescue Timeout::Error, Errno::EHOSTUNREACH, SocketError => msg
  63. @error_message = "No connection to #{self.address}<br/>#{msg}"
  64. end
  65.  
  66. def check_idle_ports
  67. self.ports.each {|port|
  68. new_count = port.macs.count
  69.  
  70. # check for change
  71. if new_count != port.macs_count
  72. if new_count == 0
  73. port.idle_since = Time.new
  74.  
  75. # send notifications to defined mails
  76. if port.send_notifications
  77. all_addresses = MailNotification.get_all_addresses
  78. Mailer.deliver_no_macs(device, port, all_addresses )
  79. end
  80.  
  81. end # if
  82.  
  83. port.macs_count = new_count
  84. port.save!
  85. end # if
  86. }
  87. end
  88. end
  89.  
  90. ## lib/tasks/device_processor.rake
  91. # rake device_processor:process_device ID=6
  92. namespace :device_processor do
  93. task :process_device do
  94. # Something like this? *shrug*
  95. DeviceProcessor.new(ENV["ID"]).process_device
  96. end
  97. end
Add Comment
Please, Sign In to add comment