Class: Hotwired::CLI
- Inherits:
-
Object
- Object
- Hotwired::CLI
- Defined in:
- lib/hotwired/cli.rb
Defined Under Namespace
Classes: HotwiredError, NoConfig
Constant Summary collapse
- MAX_DELETE =
1
Instance Method Summary collapse
-
#delete_records(devs) ⇒ Object
删除某些主机数据:接收数组对象.
-
#initialize ⇒ CLI
constructor
类对象实例化入口函数.
-
#opts_parse ⇒ Object
解析命令行脚本接收参数.
-
#remove_old(days) ⇒ Object
删除历史数据 【 N*天之前 】.
-
#remove_records(name) ⇒ Object
删除表记录.
-
#run ⇒ Object
脚本调度函数入口.
Constructor Details
Instance Method Details
#delete_records(devs) ⇒ Object
删除某些主机数据:接收数组对象
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/hotwired/cli.rb', line 75 def delete_records(devs) Log.debug "delete_records #{devs.size}" max_del = @opts["max-delete"] ? @opts["max-delete"] : MAX_DELETE # 判断当前表已有数据条目和期望删除条目是否匹配 if devs.size > max_del.to_i puts "Too many matching devices:" devs.each do |dev| puts " %s (%s)" % [dev.ptr, dev.ip] end puts "Be more specific" else puts "Deleting records:" devs.each do |dev| puts " %s (%s)" % [dev.ptr, dev.ip] dev.delete unless @opts[:simulate] end end end |
#opts_parse ⇒ Object
解析命令行脚本接收参数
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hotwired/cli.rb', line 42 def opts_parse opts = Slop.parse do |o| # banner "Usage: hotwired [options] [argument]" o.on "-h", "--help", "show usage" do puts o exit end o.bool "-d", "--debug", "Debugging on" o.string "-p", "--poll", "Poll CIDR [argument]" o.bool "-r", "--remove", "Remove [argument] from DB" o.string "-m", "--max-delete", "Maximum number to delete, default #{MAX_DELETE}" o.bool "-o", "--purge-old", "Remove records order than [argument] days" o.bool "-s", "--simulate", "Simulate, do not change DB" end [opts.arguments, opts] end |
#remove_old(days) ⇒ Object
删除历史数据 【 N*天之前 】
67 68 69 70 71 72 |
# File 'lib/hotwired/cli.rb', line 67 def remove_old(days) Log.warn "Remove #{days} days ago data" old = (Time.now.utc - days.to_i * 24 * 60 * 60) DB.new delete_records DB::Device.filter { :last_seen < old }.all end |
#remove_records(name) ⇒ Object
删除表记录
60 61 62 63 64 |
# File 'lib/hotwired/cli.rb', line 60 def remove_records(name) Log.warn "Remove record #{name}" DB.new delete_records DB::Device.filter(Sequel.like(:ptr, "%#{name}%")).all end |
#run ⇒ Object
脚本调度函数入口
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hotwired/cli.rb', line 15 def run # 检查 CLI 是否携带相关参数 if @opts[:poll] Log.debug "Start hotwired with specify #{@opts[:poll]}" Hotwired.new(cidr: @opts[:poll]).run elsif @opts[:remove] Log.debug "Start hotwired remove job" remove_records @opts[:remove] elsif @opts["purge-old"] Log.debug "Start hotwired purge-old job" remove_old @opts["purge-old"] else # 缺省的执行方式 Log.debug "Start hotwired with default params!" Hotwired.new.run end end |