eyes / eyeswebapp / core / tests / monitor.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
This file stores all the tests related to the Monitor model
"""
from django.test import TestCase
from core.models import Monitor
from core.models import Datastore
from core.models import Host
from eyeswebapp.util.monitor import ArgSet
import datetime
import simplejson
from core.tests.util import create_unsaved_minimum_monitor
from core.tests.util import create_unsaved_minimum_host
from core.tests.util import mock_nagios_results
from core.tests.util import clear_all_rrd_files
from pyrrd.rrd import RRD


class MonitorTests(TestCase):
    """ test cases for the Monitor model object"""

    def setUp(self):
        """testing default monitor model data"""
        clear_all_rrd_files()

    def test_reverse_url_setup(self):
        """testing reverse url lookup for a model object"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        management.call_command('loaddata', 'modeltestdata.json', verbosity=0)
        xmon = Monitor.objects.get(pk=1)
        self.assertEquals(xmon.get_absolute_url(), '/core/monitor/1/')

    def test_host_monitor_linkages(self):
        # pylint: disable=E1101
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        management.call_command('loaddata', 'modeltestdata.json', verbosity=0)
        xmon = Monitor.objects.get(pk=1)
        self.assertEquals(xmon.get_absolute_url(), '/core/monitor/1/')
        newhost = create_unsaved_minimum_host()
        newhost.save()
        xmon.host = newhost
        xmon.save()
        self.assertEquals(xmon.__unicode__(), u'Monitor selfping (check_ping) against localhost')
        self.assertEquals(xmon.host.hostname, "localhost")
        self.assertEquals(newhost.monitor_set.count(), 1)

    def test_default_data(self):
        """testing default monitor model data"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.save()
        self.assertEquals(newmonitor.lastupdate, None)

    def test_default_passive_attribute(self):
        """testing default monitor model data - passive attribute"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.save()
        self.assertEquals(newmonitor.passive, False)

    def test_default_unicodevalue(self):
        """testing default monitor model data"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.save()
        self.assertEquals(newmonitor.__unicode__(), u'Monitor newmonitor (check_ping) against None')

    def test_pending_query(self):
        """testing the manager's pending monitors method to get monitors with nextupdate set to NULL"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        oldmonitor = create_unsaved_minimum_monitor()
        oldmonitor.name = "foo"
        oldmonitor.nextupdate = None
        oldmonitor.save()
        pending_query_set = Monitor.objects.pending_update()
        self.assertEquals(len(pending_query_set), 2)

    def test_pending_query_with_passive(self):
        """testing the manager's pending monitors method to get monitors with nextupdate set to NULL"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        oldmonitor = create_unsaved_minimum_monitor()
        oldmonitor.name = "foo"
        oldmonitor.nextupdate = None
        oldmonitor.save()
        mon2 = create_unsaved_minimum_monitor()
        mon2.name = "passivetest"
        mon2.nextupdate = None
        mon2.passive = True
        mon2.save()
        pending_query_set = Monitor.objects.pending_update()
        self.assertEquals(len(pending_query_set), 2)

    def test_pending_query2(self):
        """testing the manager's pending monitors method"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        oldmonitor = create_unsaved_minimum_monitor()
        oldmonitor.name = "old"
        oldmonitor.poll_frequency = 10

        futuremonitor = create_unsaved_minimum_monitor()
        futuremonitor.name = "future"
        futuremonitor.poll_frequency = 10

        now = datetime.datetime.now()
        yesterday = now - datetime.timedelta(days=1)
        tomorrow = now + datetime.timedelta(days=1)
        oldmonitor.lastupdate = yesterday
        futuremonitor.lastupdate = tomorrow
        oldmonitor.nextupdate = yesterday
        futuremonitor.nextupdate = tomorrow
        oldmonitor.save()
        futuremonitor.save()
        # get the details of the test - pending monitors
        pending_query_set = Monitor.objects.pending_update()
        self.assertEquals(len(pending_query_set), 2)
        # update the pending monitors
        for mon in pending_query_set:
            mon.state = 1
            mon.save()
        # check again - pending monitors
        pending_query_set = Monitor.objects.pending_update()
        # for mon in pending_query_set:
        #     print mon.name, mon.nextupdate
        self.assertEquals(len(pending_query_set), 0)

    def test_pending_query_with_passive_and_update(self):
        """testing the manager's pending monitors method"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        oldmonitor = create_unsaved_minimum_monitor()
        oldmonitor.name = "old"
        oldmonitor.poll_frequency = 10

        futuremonitor = create_unsaved_minimum_monitor()
        futuremonitor.name = "future"
        futuremonitor.poll_frequency = 10

        passivemonitor = create_unsaved_minimum_monitor()
        passivemonitor.name = "passive"
        passivemonitor.poll_frequency = 10
        passivemonitor.passive = True

        now = datetime.datetime.now()
        yesterday = now - datetime.timedelta(days=1)
        tomorrow = now + datetime.timedelta(days=1)
        oldmonitor.lastupdate = yesterday
        futuremonitor.lastupdate = tomorrow
        passivemonitor.lastupdate = yesterday
        oldmonitor.nextupdate = yesterday
        futuremonitor.nextupdate = tomorrow
        oldmonitor.save()
        futuremonitor.save()
        passivemonitor.save()
        # get the details of the test - pending monitors
        pending_query_set = Monitor.objects.pending_update()
        self.assertEquals(len(pending_query_set), 2)
        # update the pending monitors
        for mon in pending_query_set:
            mon.state = 1
            mon.save()
        # check again - pending monitors
        pending_query_set = Monitor.objects.pending_update()
        # for mon in pending_query_set:
        #     print mon.name, mon.nextupdate
        self.assertEquals(len(pending_query_set), 0)

    def test_passive_update(self):
        """testing the manager's pending monitors method"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        passivemonitor = create_unsaved_minimum_monitor()
        passivemonitor.name = "passive"
        passivemonitor.poll_frequency = 10
        passivemonitor.passive = True

        now = datetime.datetime.now()
        yesterday = now - datetime.timedelta(days=1)
        tomorrow = now + datetime.timedelta(days=1)
        passivemonitor.lastupdate = yesterday
        passivemonitor.save()

        passive_mon_list = Monitor.objects.filter(passive__exact=True)
        self.assertEquals(len(passive_mon_list), 1)
        for mon in passive_mon_list:
            mon.state = 1
            mon.save()
        the_passive_mon = passive_mon_list[0]
        self.assertTrue(the_passive_mon.lastupdate > yesterday)
        self.assertTrue(the_passive_mon.nextupdate is None)

    def test_setting_state1(self):
        """testing setting state & default value"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "testsettingstate"
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 0)
        self.assertEquals(newmonitor.state, 0)
        self.assertEquals(newmonitor.state_display, u'ok')

    def test_setting_state2(self):
        """testing setting state & default value"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "testsettingstate"
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 0)
        self.assertEquals(newmonitor.state, 0)
        self.assertEquals(newmonitor.state_display, u'ok')
        point_in_time = datetime.datetime.now()
        newmonitor.state = 1
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 1)
        self.assertEquals(newmonitor.state, 1)
        self.assertEquals(newmonitor.state_display, u'warning')
        self.assertTrue(newmonitor.lastupdate > point_in_time)
        self.assertTrue(newmonitor.nextupdate > point_in_time)

    def test_setting_state3(self):
        """testing setting state & display value"""
        clear_all_rrd_files()
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "testsettingstate"
        newmonitor.state = 1
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 1)
        self.assertEquals(newmonitor.state, 1)
        self.assertEquals(newmonitor.state_display, u'warning')
        newmonitor.state = 2
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 2)
        self.assertEquals(newmonitor.state, 2)
        self.assertEquals(newmonitor.state_display, u'error')
        newmonitor.state = 3
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 3)
        self.assertEquals(newmonitor.state, 3)
        self.assertEquals(newmonitor.state_display, u'unknown')
        newmonitor.state = 4
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 3)
        self.assertEquals(newmonitor.state, 3)
        self.assertEquals(newmonitor.state_display, u'unknown')
        newmonitor.state = -1
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 3)
        self.assertEquals(newmonitor.state, 3)
        self.assertEquals(newmonitor.state_display, u'unknown')
        newmonitor.state = None
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 3)
        self.assertEquals(newmonitor.state, 3)
        self.assertEquals(newmonitor.state_display, u'unknown')
        newmonitor.state = 0
        newmonitor.save()
        self.assertEquals(newmonitor.latest_state, 0)
        self.assertEquals(newmonitor.state, 0)
        self.assertEquals(newmonitor.state_display, u'ok')

    def test_default_argset(self):
        """testing default monitor model json data for argset"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.save()
        self.assertEquals(newmonitor.arg_set, None)

    def test_setting_argset_none(self):
        """testing setting an argset to None"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        xargset = ArgSet()
        xargset.add_argument('--help')
        newmonitor.arg_set = xargset
        newmonitor.save()
        newmonitor.arg_set = None
        newmonitor.save()
        self.assertEquals(newmonitor.json_argset, '')

    def test_setting_argset_none2(self):
        """testing setting an argset to None"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.json_argset = None
        self.assertEquals(newmonitor.arg_set, None)

    def test_setting_argset(self):
        """testing setting an argset into a model"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.save()
        xargset = ArgSet()
        xargset.add_argument('--help')
        newmonitor.arg_set = xargset
        zed1 = newmonitor.arg_set.json()
        zed2 = xargset.json()
        self.assertEquals(zed1, zed2)
        argjson = xargset.json()
        self.assertEquals(newmonitor.json_argset, argjson)

    def test_setting_argset2(self):
        """testing setting an argset into a model, make sure we can get the json back out again"""
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "testsettingargset"
        xargset = ArgSet()
        xargset.add_argument('--help')
        newmonitor.arg_set = xargset
        newmonitor.save()
        mon_list = Monitor.objects.filter(name='testsettingargset')
        self.assertEquals(len(mon_list), 1)
        that_monitor = mon_list[0]
        self.assertEquals(xargset.json(), that_monitor.json_argset)

    def test_verify_mock(self):
        """ verifying mock objects return something appropriate """
        self.assertTrue(mock_nagios_results())
        self.assertTrue(mock_nagios_results(type='http'))
        pingdict = simplejson.loads(mock_nagios_results(type='ping'))
        httpdict = simplejson.loads(mock_nagios_results(type='http'))
        self.assertEquals(pingdict['decoded']['human'], "PING OK - Packet loss = 0%, RTA = 0.11 ms")
        self.assertEquals(httpdict['decoded']['human'], "HTTP OK HTTP/1.0 200 OK - 5825 bytes in 2.158 seconds ")

    def test_functional_storing_results(self):
        """testing storing monitoring data and verifying underlying files"""
        # pylint: disable=E1101
        clear_all_rrd_files()
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "test monitor"
        newmonitor.save()
        datastore_list = newmonitor.datastore_set.all()
        self.assertEquals(len(datastore_list), 0)
        # create mock results - for a ping - 2 values
        mock_result_json = mock_nagios_results(type='ping')
        newmonitor.store_json_results(mock_result_json, debug=False)
        # store and verify new datastore files are created
        datastore_list = newmonitor.datastore_set.all()
        self.assertEquals(len(datastore_list), 2)

        # get individial datastores and verify aspects of them - 'pl' and 'rta'
        (datastore1, created_newds) = Datastore.objects.get_or_create(monitor=newmonitor, name='pl')
        self.assertFalse(created_newds)
        self.assertTrue(datastore1)
        self.assertEquals(datastore1.name, 'pl')
        self.assertTrue(datastore1.rrd_exists)
        #mode='r' is a slow method - loads data from the RRD file
        rrd1 = RRD(datastore1.location, mode="r")
        self.assertTrue(rrd1)
        # self.assertEquals(len(rrd1.ds), 1)
        # data_dict1 = rrd1.ds[0].getData()
        # self.assertEquals(data_dict1['name'], 'pl')

        # get data for 'rta'
        (datastore2, created_newds) = Datastore.objects.get_or_create(monitor=newmonitor, name='rta')
        self.assertFalse(created_newds)
        self.assertTrue(datastore2)
        self.assertEquals(datastore2.name, 'rta')
        self.assertTrue(datastore2.rrd_exists)
        #mode='r' is a slow method - loads data from the RRD file
        rrd2 = RRD(datastore2.location, mode="r")
        self.assertTrue(rrd2)
        # self.assertEquals(len(rrd2.ds), 1)
        # data_dict2 = rrd2.ds[0].getData()
        # self.assertEquals(data_dict2['name'], 'rta')

        # wipe out the RRD files and verify property is working
        clear_all_rrd_files()
        self.assertFalse(datastore1.rrd_exists)
        self.assertFalse(datastore2.rrd_exists)
        #from pyrrd.rrd import RRD, RRA, DS

    def test_failed_store_dict_results(self):
        """ verify failure mode for invalid dict structure"""
        clear_all_rrd_files()
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "test monitor"
        newmonitor.save()

        bad_result_dict = {'a': 1}
        store_result = newmonitor.store_dict_results(bad_result_dict)
        self.assertFalse(store_result)

    def test_failed_store_dict_results_debug(self):
        """ verify failure mode for invalid dict structure"""
        clear_all_rrd_files()
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)
        newmonitor = create_unsaved_minimum_monitor()
        newmonitor.name = "test monitor"
        newmonitor.save()

        bad_result_dict = {'a': 1}
        store_result = newmonitor.store_dict_results(bad_result_dict, debug=True)
        self.assertFalse(store_result)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.