file_monitoring 0.0.4 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/bin/file_monitoring CHANGED
@@ -34,18 +34,20 @@
34
34
 
35
35
  require 'rubygems'
36
36
  require 'daemons'
37
+
37
38
  require 'file_monitoring/file_monitoring.rb'
39
+ require 'log'
38
40
 
39
41
  conf_file_path = File.expand_path('~/.bbfs/etc/file_monitoring.yml')
40
42
  if ARGV.length > 1
41
43
  conf_file_path = File.expand_path(ARGV[1])
42
44
  end
43
45
 
44
- puts "Taking config from:" + conf_file_path
46
+ Log.info "Taking config from:" + conf_file_path
45
47
  YAML::load_file(conf_file_path) rescue abort("Can't load %s" % conf_file_path)
46
48
  pid_dir = File.expand_path('~/.bbfs/run/')
47
49
  FileUtils.mkdir_p(pid_dir)
48
- puts "pid dir:" + pid_dir
50
+ Log.info "pid dir:" + pid_dir
49
51
 
50
52
  Daemons.run_proc(
51
53
  'file_monitoring', # name of daemon
@@ -0,0 +1,29 @@
1
+ require 'win32/daemon'
2
+ include Win32
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require "./file_monitoring/file_monitoring.rb"
7
+
8
+ conf_file_path = (ARGV.length > 0 ? "#{ARGV[0]}" : '~/.bbfs/etc/file_monitoring.yml')
9
+ conf_file_path = File.expand_path(conf_file_path)
10
+
11
+ CONFIG_FILE_PATH = "#{conf_file_path}"
12
+
13
+ class Daemon
14
+ def service_main
15
+ while running?
16
+ monitor_files(CONFIG_FILE_PATH)
17
+ end
18
+ end
19
+
20
+ def service_stop
21
+ exit!
22
+ end
23
+ end
24
+
25
+ Daemon.mainloop
26
+
27
+ rescue Exception => err
28
+ raise
29
+ end
@@ -1,14 +1,15 @@
1
1
  require 'algorithms'
2
2
  require 'fileutils'
3
+ require 'log'
3
4
  require 'params'
4
5
  require 'yaml'
5
6
 
6
- require_relative 'monitor_path'
7
+ require 'file_monitoring/monitor_path'
7
8
 
8
9
  module BBFS
9
10
  module FileMonitoring
10
11
 
11
- PARAMS.parameter('default_log_path', File.expand_path('~/.bbfs/log/file_monitoring.log'),
12
+ Params.string('default_log_path', File.expand_path('~/.bbfs/log/file_monitoring.log'),
12
13
  'Default path for log file.')
13
14
 
14
15
  class FileMonitoring
@@ -31,34 +32,34 @@ module BBFS
31
32
  priority = (Time.now + elem['scan_period']).to_i
32
33
  dir_stat = DirStat.new(elem['path'], elem['stable_state'])
33
34
  dir_stat.set_event_queue(@event_queue) if @event_queue
35
+ Log.info [priority, elem, dir_stat]
34
36
  pq.push([priority, elem, dir_stat], -priority)
35
37
  }
36
38
 
37
- log_path = PARAMS.default_log_path
39
+ log_path = Params['default_log_path']
38
40
  if config_yml.key?('log_path')
39
41
  log_path = File.expand_path(config_yml['log_path'])
40
42
  end
41
43
 
42
- puts 'Log path:' + log_path
44
+ Log.info 'Log path:' + log_path
43
45
  FileUtils.mkdir_p(File.dirname(log_path))
44
46
  log = File.open(log_path, 'w')
45
47
  FileStat.set_log(log)
46
48
 
47
49
  while true do
48
50
  time, conf, dir_stat = pq.pop
49
- #puts 'time:' + time.to_s()
50
- #puts 'now:' + Time.now.to_i.to_s()
51
- #puts conf
51
+ #Log.info 'time:' + time.to_s()
52
+ #Log.info 'now:' + Time.now.to_i.to_s()
53
+ #Log.info conf
52
54
 
53
55
  time_span = time - Time.now.to_i
54
56
  if (time_span > 0)
55
57
  sleep(time_span)
56
58
  end
57
-
58
59
  dir_stat.monitor
59
60
 
60
- #puts conf['path']
61
- #puts conf['scan_period']
61
+ #Log.info conf['path']
62
+ #Log.info conf['scan_period']
62
63
  priority = (Time.now + conf['scan_period']).to_i
63
64
  pq.push([priority, conf, dir_stat], -priority)
64
65
  end
@@ -1,3 +1,6 @@
1
+ require 'log'
2
+ require 'params'
3
+
1
4
  module BBFS
2
5
  module FileMonitoring
3
6
  # Path monitoring.
@@ -32,6 +35,7 @@ module BBFS
32
35
  def initialize(path, stable_state = DEFAULT_STABLE_STATE)
33
36
  @path ||= path
34
37
  @size = nil
38
+ @creation_time = nil
35
39
  @modification_time = nil
36
40
  @cycles = 0 # number of iterations from the last file modification
37
41
  @state = FileStatEnum::NON_EXISTING
@@ -39,8 +43,8 @@ module BBFS
39
43
  @stable_state = stable_state # number of iteration to move unchanged file to stable state
40
44
  end
41
45
 
42
- def set_output_queue event_queue
43
- @event_queue
46
+ def set_output_queue(event_queue)
47
+ @event_queue = event_queue
44
48
  end
45
49
 
46
50
  # Sets a log file to report changes
@@ -59,16 +63,19 @@ module BBFS
59
63
  if file_stats == nil
60
64
  new_state = FileStatEnum::NON_EXISTING
61
65
  @size = nil
66
+ @creation_time = nil
62
67
  @modification_time = nil
63
68
  @cycles = 0
64
69
  elsif @size == nil
65
70
  new_state = FileStatEnum::NEW
66
71
  @size = file_stats.size
72
+ @creation_time = file_stats.ctime.utc
67
73
  @modification_time = file_stats.mtime.utc
68
74
  @cycles = 0
69
75
  elsif changed?(file_stats)
70
76
  new_state = FileStatEnum::CHANGED
71
77
  @size = file_stats.size
78
+ @creation_time = file_stats.ctime.utc
72
79
  @modification_time = file_stats.mtime.utc
73
80
  @cycles = 0
74
81
  else
@@ -85,7 +92,9 @@ module BBFS
85
92
 
86
93
  # Checks that stored file attributes are the same as file attributes taken from file system.
87
94
  def changed?(file_stats)
88
- not (file_stats.size == size and file_stats.mtime.utc == modification_time.utc)
95
+ not (file_stats.size == @size &&
96
+ file_stats.ctime.utc == @creation_time.utc &&
97
+ file_stats.mtime.utc == @modification_time.utc)
89
98
  end
90
99
 
91
100
  def set_event_queue(queue)
@@ -100,9 +109,9 @@ module BBFS
100
109
  @@log.puts(cur_stat)
101
110
  @@log.flush #Ruby1.9.3: note that this is Ruby internal buffering only; the OS may buffer the data as well
102
111
  end
103
- if (@event_queue && !self.instance_of?(DirStat))
104
- p "Writing to event queue [#{self.state}, #{self.path}]"
105
- @event_queue.push([self.state, self.path])
112
+ if (!@event_queue.nil?)
113
+ Log.info "Writing to event queue [#{self.state}, #{self.path}]"
114
+ @event_queue.push([self.state, self.instance_of?(DirStat), self.path])
106
115
  end
107
116
  end
108
117
  end
@@ -175,10 +184,10 @@ module BBFS
175
184
  res = super
176
185
  @files.each_value do |file|
177
186
  res += "\n" + file.to_s(child_ident)
178
- end
187
+ end if @files
179
188
  @dirs.each_value do |dir|
180
189
  res += "\n" + dir.to_s(child_ident)
181
- end
190
+ end if @dirs
182
191
  res
183
192
  end
184
193
 
@@ -256,7 +265,7 @@ module BBFS
256
265
  # newly added directories have to remain with NEW state
257
266
  was_changed = true
258
267
  ds = DirStat.new(file, self.stable_state)
259
- ds.set_event_queue(@event_queue) if @event_queue
268
+ ds.set_event_queue(@event_queue) unless @event_queue.nil?
260
269
  ds.monitor
261
270
  add_dir(ds)
262
271
  end
@@ -266,7 +275,7 @@ module BBFS
266
275
  # newly added directories have to remain with NEW state
267
276
  was_changed = true
268
277
  fs = FileStat.new(file, self.stable_state)
269
- fs.set_event_queue(@event_queue) if @event_queue
278
+ fs.set_event_queue(@event_queue) unless @event_queue.nil?
270
279
  fs.monitor
271
280
  add_file(fs)
272
281
  end
@@ -0,0 +1,5 @@
1
+ module BBFS
2
+ module FileMonitoring
3
+ VERSION = "0.0.8"
4
+ end
5
+ end
@@ -1,15 +1,8 @@
1
- require 'params'
2
-
3
- require_relative 'file_monitoring/file_monitoring'
1
+ require 'file_monitoring/file_monitoring'
4
2
 
5
3
  # TODO add description
6
4
  module BBFS
7
5
  module FileMonitoring
8
- VERSION = "0.0.3"
9
-
10
- PARAMS.parameter("config_path","~/.bbfs/etc/file_monitoring.yml",
11
- "Configuration file for monitoring.")
12
-
13
6
  # The main method. Loops on all paths each time span and monitors them.
14
7
  def monitor_files
15
8
  fm = FileMonitoring.new
@@ -0,0 +1,4 @@
1
+ paths:
2
+ - path: /Users/kolman/Dev/git/bbfs/resources
3
+ scan_period: 1
4
+ stable_state: 5
@@ -0,0 +1,5 @@
1
+ log_path: C:\Kab.Tech\bbfs\resources\file_monitoring\log
2
+ paths:
3
+ - path: C:/Kab.Tech/bbfs/resources/path_monitor_test
4
+ scan_period: 1
5
+ stable_state: 5
@@ -0,0 +1,56 @@
1
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000
2
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.0
3
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
4
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
5
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500
6
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.0
7
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.1
8
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
9
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500
10
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.0
11
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.1
12
+ 2012-02-15 07:20:47 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test
13
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500
14
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.0
15
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.1
16
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000
17
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.0
18
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
19
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
20
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500
21
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.0
22
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.1
23
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
24
+ 2012-02-15 07:20:48 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test
25
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500
26
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.0
27
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/test_file.500.1
28
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000
29
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.0
30
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
31
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
32
+ 2012-02-15 07:20:52 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500
33
+ 2012-02-15 07:20:53 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.0
34
+ 2012-02-15 07:20:53 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1500.1
35
+ 2012-02-15 07:20:53 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
36
+ 2012-02-15 07:20:53 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test
37
+ 2012-02-15 07:21:31 UTC : NON_EXISTING : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
38
+ 2012-02-15 07:21:31 UTC : CHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
39
+ 2012-02-15 07:21:31 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1000.1
40
+ 2012-02-15 07:21:31 UTC : CHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
41
+ 2012-02-15 07:21:32 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
42
+ 2012-02-15 07:21:32 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1000.1
43
+ 2012-02-15 07:21:32 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
44
+ 2012-02-15 07:21:36 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
45
+ 2012-02-15 07:21:37 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1000.1
46
+ 2012-02-15 07:21:37 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
47
+ 2012-02-15 07:22:12 UTC : NEW : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
48
+ 2012-02-15 07:22:12 UTC : CHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
49
+ 2012-02-15 07:22:12 UTC : NON_EXISTING : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500/test_file.1000.1
50
+ 2012-02-15 07:22:12 UTC : CHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
51
+ 2012-02-15 07:22:13 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
52
+ 2012-02-15 07:22:13 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
53
+ 2012-02-15 07:22:13 UTC : UNCHANGED : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
54
+ 2012-02-15 07:22:17 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000/test_file.1000.1
55
+ 2012-02-15 07:22:17 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1000
56
+ 2012-02-15 07:22:17 UTC : STABLE : C:/Kab.Tech/bbfs/resources/path_monitor_test/dir1500
File without changes