lib/rubysermon.rb
271b1a48
 require_relative "rubysermon/version"
def91ab1
 require_relative "rubysermon/mod_loader"
 require_relative "rubysermon/mod_template"
c61af953
 require_relative "rubysermon/configurator"
958dd062
 require_relative "rubysermon/db"
271b1a48
 
fb5f2bce
 require "date"
 
271b1a48
 module Rubysermon
8cbb7fe5
 	LIB_PATH = File.expand_path(File.dirname(__FILE__))
 	APP_PATH = "#{LIB_PATH}/.."
f0d38088
 	MOD_PATH = "#{LIB_PATH}/rubysermon/mod"
8cbb7fe5
 
def91ab1
 	class App
 		def initialize
c8be27d8
 			@config 		= { repeat: 60,
 								config_path: "#{LIB_PATH}/rubysermon/config.json",
 								db_path: "/tmp/rubysermon_history.db"
 			}
 
71bcfce4
 			@running_mods	= []
 			@enabled_mods	= []
958dd062
 			@db				= nil
e5c1300f
 
92546c22
 			check_args()
71bcfce4
 		end
fb5f2bce
 
71bcfce4
 		def run
 			read_config()
 			enable_and_start_mods()
 
 			if (msg = cannot_start_fetch_process_sleep_cycle?)
 				abort(msg)
 			end
fb5f2bce
 
92546c22
 			#start_db()
fb5f2bce
 			start_fetch_process_sleep_cycle()
def91ab1
 		end
 
 		private
c61af953
 
92546c22
 		def check_args
 			if not ARGV.empty?
 				config_path = ARGV.first
 				if File.exists?(config_path)
 					@config[:config_path] = config_path
 				else
 					abort("Wrong config file path. Exiting.")
 				end
 			end
 		end
 
71bcfce4
 		def read_config
c8be27d8
 			configurator = Configurator.new(@config[:config_path])
e9310bd3
 			config = configurator.get_settings()
c196a26b
 			@config.merge!(config)
c61af953
 		end
 
71bcfce4
 		def enable_and_start_mods
 			@enabled_mods = @config[:modules].to_a
fb5f2bce
 			@enabled_mods.each do |mod| load_mod(mod) end
 		end
 
def91ab1
 		def load_mod(mod_name)
 			begin
fb5f2bce
 				mod = ModLoader.load(mod_name)
 				@running_mods.push(mod)
def91ab1
 			rescue ModLoaderException => e
71bcfce4
 				$stderr.puts e.message
def91ab1
 			end
 		end
fb5f2bce
 
71bcfce4
 		def cannot_start_fetch_process_sleep_cycle?
 			if @config[:repeat].to_i < 1
 				return "Repeat cycle is too short"
 			elsif @running_mods.empty?
 				return "There are no enabled modules"
fb5f2bce
 			end
 
71bcfce4
 			false
 		end
 
958dd062
 		def start_db
c8be27d8
 			@db = DB.new(@config[:db_path])
958dd062
 		end
 
71bcfce4
 		def start_fetch_process_sleep_cycle
fb5f2bce
 			while true
 				current_time = DateTime.now()
 				@running_mods.each do |mod|
 					result = mod.results
 					notify = mod.notify?
 				end
 				sleep @config[:repeat]
 			end
 		end
def91ab1
 	end
271b1a48
 end