hq-log-monitor-server 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,12 @@ Feature: Log monitor server submit event via HTTP
8
8
  <log-monitor-server-config>
9
9
  <server port="${port}"/>
10
10
  <db host="${db-host}" port="${db-port}" name="${db-name}"/>
11
+ <icinga command-file="${command-file}">
12
+ <service name="service" icinga-host="host" icinga-service="service">
13
+ <type name="warning" level="warning"/>
14
+ <type name="critical" level="critical"/>
15
+ </service>
16
+ </icinga>
11
17
  </log-monitor-server-config>
12
18
  """
13
19
 
@@ -39,3 +45,7 @@ Feature: Log monitor server submit event via HTTP
39
45
  },
40
46
  }
41
47
  """
48
+ And icinga should receive:
49
+ """
50
+ PROCESS_SERVICE_CHECK_RESULT;host;service;1;WARNING 1 warning
51
+ """
@@ -57,3 +57,11 @@ at_exit do
57
57
  end
58
58
 
59
59
  end
60
+
61
+ Before do
62
+ @command_file = Tempfile.new "cuke-"
63
+ end
64
+
65
+ After do
66
+ @command_file.unlink
67
+ end
@@ -14,6 +14,7 @@ Given /^the log monitor server config:$/ do
14
14
  config_string.gsub! "${db-host}", "localhost"
15
15
  config_string.gsub! "${db-port}", "27017"
16
16
  config_string.gsub! "${db-name}", mongo_db_name("logMonitorServer")
17
+ config_string.gsub! "${command-file}", @command_file.path
17
18
 
18
19
  @log_monitor_server_config.write config_string
19
20
  @log_monitor_server_config.flush
@@ -137,3 +138,14 @@ Then /^the (\d+(?:st|nd|rd|th)) summary should be:$/ do
137
138
  end
138
139
 
139
140
  end
141
+
142
+ Then /^icinga should receive:$/ do
143
+ |expected_command|
144
+
145
+ command_contents =
146
+ File.new(@command_file).to_a.map { |line| line.strip }
147
+
148
+ command_contents.should \
149
+ include expected_command
150
+
151
+ end
@@ -18,6 +18,7 @@ class Script
18
18
 
19
19
  def initialize
20
20
  @status = 0
21
+ @mutex = Mutex.new
21
22
  end
22
23
 
23
24
  def main
@@ -25,12 +26,14 @@ class Script
25
26
  trap "INT" do
26
27
  @web_server.shutdown
27
28
  end
29
+ Thread.new { background }
28
30
  run
29
31
  end
30
32
 
31
33
  def start
32
34
  setup
33
35
  Thread.new { run }
36
+ Thread.new { background }
34
37
  end
35
38
 
36
39
  def stop
@@ -48,6 +51,118 @@ class Script
48
51
  @web_server.start
49
52
  end
50
53
 
54
+ def background
55
+
56
+ @next_check = Time.now
57
+
58
+ loop do
59
+ sleep 1 while Time.now < @next_check
60
+ do_checks
61
+ end
62
+
63
+ end
64
+
65
+ def do_checks
66
+ @mutex.synchronize do
67
+
68
+ File.open @icinga_elem["command-file"], "a" do
69
+ |command_io|
70
+
71
+ summaries_by_service =
72
+ get_summaries_by_service
73
+
74
+ @icinga_elem.find("service").each do
75
+ |service_elem|
76
+
77
+ critical_count = 0
78
+ warning_count = 0
79
+ unknown_count = 0
80
+
81
+ summaries_by_service[service_elem["name"]]["types"].each do
82
+ |type_name, type_info|
83
+
84
+ type_elem =
85
+ service_elem.find_first("
86
+ type [@name = #{esc_xp type_name}]
87
+ ")
88
+
89
+ if ! type_elem
90
+ unknown_count += 1
91
+ elsif type_elem["level"] == "critical"
92
+ critical_count += 1
93
+ elsif type_elem["level"] == "warning"
94
+ warning_count += 1
95
+ else
96
+ unknown_count += 1
97
+ end
98
+
99
+ status_int =
100
+ if critical_count > 0
101
+ 2
102
+ elsif warning_count > 0
103
+ 1
104
+ elsif unknown_count > 0
105
+ 3
106
+ else
107
+ 0
108
+ end
109
+
110
+ status_str =
111
+ if critical_count > 0
112
+ "CRITICAL"
113
+ elsif warning_count > 0
114
+ "WARNING"
115
+ elsif unknown_count > 0
116
+ "UNKNOWN"
117
+ else
118
+ "OK"
119
+ end
120
+
121
+ parts = []
122
+
123
+ if critical_count > 0
124
+ parts << "%d critical" % critical_count
125
+ end
126
+
127
+ if warning_count > 0
128
+ parts << "%d warning" % warning_count
129
+ end
130
+
131
+ if unknown_count > 0
132
+ parts << "%d unknown" % unknown_count
133
+ end
134
+
135
+ if parts.empty?
136
+ parts << "no new events"
137
+ end
138
+
139
+ message = "%s %s" % [
140
+ status_str,
141
+ parts.join(", "),
142
+ ]
143
+
144
+ command = [
145
+ "PROCESS_SERVICE_CHECK_RESULT",
146
+ service_elem["icinga-host"],
147
+ service_elem["icinga-service"],
148
+ status_int,
149
+ message,
150
+ ].join ";"
151
+
152
+ command_io.print "%s\n" % command
153
+
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ @next_check = Time.now + 60
161
+
162
+ end
163
+
164
+ end
165
+
51
166
  def process_args
52
167
 
53
168
  @opts, @args =
@@ -80,6 +195,9 @@ class Script
80
195
  @db_elem =
81
196
  @config_elem.find_first("db")
82
197
 
198
+ @icinga_elem =
199
+ @config_elem.find_first("icinga")
200
+
83
201
  end
84
202
 
85
203
  def connect_db
@@ -183,13 +301,17 @@ class Script
183
301
 
184
302
  @db["summaries"].save summary
185
303
 
304
+ # perform checks
305
+
306
+ do_checks
307
+
186
308
  # respond successfully
187
309
 
188
310
  return 202, {}, []
189
311
 
190
312
  end
191
313
 
192
- def overview_page env
314
+ def get_summaries_by_service
193
315
 
194
316
  summaries_by_service = {}
195
317
 
@@ -231,6 +353,15 @@ class Script
231
353
 
232
354
  end
233
355
 
356
+ return summaries_by_service
357
+
358
+ end
359
+
360
+ def overview_page env
361
+
362
+ summaries_by_service =
363
+ get_summaries_by_service
364
+
234
365
  headers = {}
235
366
  html = []
236
367
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-log-monitor-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-02 00:00:00.000000000 Z
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext