Class: Qnotifier::MainProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/qnotifier.rb

Instance Method Summary collapse

Constructor Details

#initializeMainProcess

Returns a new instance of MainProcess.



32
33
34
35
36
# File 'lib/qnotifier.rb', line 32

def initialize
  Qnotifier::Storage.restore
  
  @alert_count = 0
end

Instance Method Details

#alert(key, message, priority) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/qnotifier.rb', line 168

def alert(key, message, priority)
  start_logging
  load_config
  
  alerts = {"User" => [[key, message, priority]]}
  webservice = Qnotifier::WebService.new
  webservice.config = @config
  webservice.send_alerts(alerts)
end

#load_configObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/qnotifier.rb', line 189

def load_config
  # Load from /var/lib/qnotifier/qnotifier_config.yml if the file is there
  begin
    @config = YAML::load(File.open('/var/lib/qnotifier/qnotifier_config.yml'))
  rescue Exception => e
    puts "Can't open /var/lib/qnotifier/qnotifier_config.yml"
    Qnotifier.log.error "Can't open /var/lib/qnotifier/qnotifier_config.yml"
    exit
  end
  
  unless @config["api_key"]
    begin
      api_key_file = File.open("/var/lib/qnotifier/api_key")
      while (line = api_key_file.readline)
        @config["api_key"] = line
      end
    rescue EOFError
    rescue Exception => e
      Qnotifier.log.debug "Can't get API key, have you registered?"
    end
  end
end

#load_plugin_file(file) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/qnotifier.rb', line 103

def load_plugin_file(file)
  return unless file =~ /\.rb/
  require file
  class_name = "Qnotifier::#{File.basename(file, ".rb").split(/[^a-z0-9]/i).map{|w| w.capitalize}.join}"
  begin
    plugin = eval(class_name).new
  rescue
    Qnotifier.log.error "Couldn't process the plugin file #{file}"
    return
  end
  plugin.class_name = class_name
  plugin.config = @config
  unless plugin.class.superclass.name == "Qnotifier::Plugin"
    Qnotifier.log.fatal "Attempted to load a non Qnotifier::Plugin plugin subclass at #{file} - make sure this directory only includes Qnotifier::Plugin subclasses"
    return
  end
  @plugins << plugin
end

#register(code) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/qnotifier.rb', line 138

def register(code)
  
  start_logging
  load_config
        
  if @config["api_key"]
    Qnotifier.log.error "Server was already registered. Re-registering."
  end
  
  Qnotifier::Storage.wipe
      
  webservice = Qnotifier::WebService.new
  webservice.config = @config
  api_key = webservice.register_server(code)
        
  if api_key
    save_api_key(api_key)
    return api_key
  else
    return nil
  end
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/qnotifier.rb', line 47

def run
  start_logging
  load_config
  
  Qnotifier.log.info "QNotifier Starting"

  if Qnotifier::Storage.get("server_disabled_pings")
    Qnotifier.log.fatal "Disabled by the Qnotifer API, please re-register (qnotifier --register key) this agent to enable. Exiting."
    exit
  end

  unless @config["api_key"]
    error_message =  "Missing API Key, please register this agent first."
    puts error_message
    Qnotifier.log.fatal error_message
    exit
  end

  # Our Plugins
  @plugins = []

  # Load the built in plugins
  Dir[File.dirname(__FILE__) + '/../plugins/*.rb'].each do |file| 
    load_plugin_file(file)
  end

  # Load the user plugins
  Dir['/var/lib/qnotifier/plugins/*.rb'].each do |file| 
    load_plugin_file(file)
  end

  # Load the web service
  webservice = Qnotifier::WebService.new
  webservice.config = @config

  loop do
    @alerts = {}
    @reports = {}
    @stats = {}
    @report_count = 0
    
    for plugin in @plugins
      run_plugin(plugin)
    end

    # Send the alerts, reports and stats to the API
    webservice.send_alerts(@alerts)
    webservice.send_reports_stats(@reports, @stats, @alert_count)

    Qnotifier::Storage.save
    
    Qnotifier.log.debug "QNotifier Check Complete"
    sleep 30
  end
end

#run_plugin(plugin) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/qnotifier.rb', line 122

def run_plugin(plugin)
  begin
    Timeout.timeout 30 do
      plugin.run
    end
  rescue RuntimeError => e 
    Qnotifier.log.error "#{plugin.class_name} Plugin Error: #{e.message}"
  rescue Errno::ENOENT => e
    Qnotifier.log.error "#{plugin.class_name} Plugin Error: #{e.message}" 
  end
  @alerts.merge!(plugin.alerts) if plugin.alerts
  @reports.merge!(plugin.reports) if plugin.reports
  @stats.merge!(plugin.stats) if plugin.stats
  @alert_count += plugin.alert_count
end

#save_api_key(api_key) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/qnotifier.rb', line 178

def save_api_key(api_key)
  return unless api_key
  @config["api_key"] = api_key
  begin
    File.open("/var/lib/qnotifier/api_key", 'w') {|f| f.write(api_key) }
    File.chmod(0600, "/var/lib/qnotifier/api_key")
  rescue Exception => e
    Qnotifier.log.fatal "Can't save API Key #{e.message}"
  end
end

#start_loggingObject



38
39
40
41
42
43
44
45
# File 'lib/qnotifier.rb', line 38

def start_logging
  # Logging
  if $qnotifier_debug
    Qnotifier.log.level = Logger::DEBUG 
  else
    Qnotifier.log.level = Logger::INFO 
  end
end

#wipeObject



161
162
163
164
165
166
# File 'lib/qnotifier.rb', line 161

def wipe
  start_logging
  load_config
  
  Qnotifier::Storage.wipe
end