File: NagVisStatefulObject.js

package info (click to toggle)
nagvis 1%3A1.9.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,556 kB
  • sloc: php: 28,986; javascript: 16,395; sh: 1,696; makefile: 16; xml: 8
file content (468 lines) | stat: -rw-r--r-- 17,297 bytes parent folder | download | duplicates (3)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*****************************************************************************
 *
 * NagVisObject.js - This class handles the visualisation of statefull objects
 *
 * Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org)
 *
 * License:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *****************************************************************************/

/**
 * @author	Lars Michelsen <lm@larsmichelsen.com>
 */

var NagVisStatefulObject = NagVisObject.extend({
    has_state: true,

    // Stores the information from last refresh (Needed for change detection)
    last_state: null,
    // Array of member objects like services for hosts etc.
    members: [],
    // Timestamps for last event handling events if repeated events are enabled
    event_time_first: null,
    event_time_last: null,

    constructor: function(oConf) {
        this.base(oConf);
    },

    getMembers: function() {
        // Clear member array on every launch
        this.members = [];

        if(this.conf && this.conf.members && this.conf.members.length > 0) {
            for(var i = 0, len = this.conf.members.length; i < len; i++) {
                var oMember = this.conf.members[i];
                var oObj;

                switch (oMember.type) {
                    case 'host':
                        oObj = new NagVisHost(oMember);
                    break;
                    case 'service':
                        oObj = new NagVisService(oMember);
                    break;
                    case 'hostgroup':
                        oObj = new NagVisHostgroup(oMember);
                    break;
                    case 'servicegroup':
                        oObj = new NagVisServicegroup(oMember);
                    break;
                    case 'dyngroup':
                        oObj = new NagVisDynGroup(oMember);
                    break;
                    case 'aggr':
                        oObj = new NagVisAggr(oMember);
                    break;
                    case 'map':
                        oObj = new NagVisMap(oMember);
                    break;
                    case 'textbox':
                        oObj = new NagVisTextbox(oMember);
                    break;
                    case 'container':
                        oObj = new NagVisContainer(oMember);
                    break;
                    case 'shape':
                        oObj = new NagVisShape(oMember);
                    break;
                    case 'line':
                        oObj = new NagVisLine(oMember);
                    break;
                    default:
                        alert('Error: Unknown member object type ('+oMember.type+')');
                    break;
                }

                if(oObj !== null) {
                    this.members.push(oObj);
                }

                oObj = null;
                oMember = null;
            }
        }
    },

    getStatefulMembers: function() {
        var stateful = [];
        for (var i = 0, len = this.members.length; i < len; i++) {
            if (this.members[i].has_state) {
                stateful.push(this.members[i]);
            }
        }
        return stateful;
    },

    /**
     * PUBLIC saveLastState()
     *
     * Saves the current state in last state array for later change detection
     *
     * @author	Lars Michelsen <lm@larsmichelsen.com>
     */
    saveLastState: function() {
        this.last_state = {
          'summary_state': this.conf.summary_state,
            'summary_in_downtime': this.conf.summary_in_downtime,
            'summary_stale': this.conf.summary_stale,
            'summary_problem_has_been_acknowledged': this.conf.summary_problem_has_been_acknowledged,
            'output': this.conf.output,
            'perfdata': this.conf.perfdata
        };
    },

    /**
     * PUBLIC stateChanged()
     *
     * Check if a state change occured since last refresh
     *
     * @author	Lars Michelsen <lm@larsmichelsen.com>
     */
    stateChanged: function() {
        if(this.conf.summary_state != this.last_state.summary_state ||
           this.conf.summary_problem_has_been_acknowledged != this.last_state.summary_problem_has_been_acknowledged ||
           this.conf.summary_stale != this.last_state.summary_stale ||
           this.conf.summary_in_downtime != this.last_state.summary_in_downtime) {
            return true;
        } else {
            return false;
        }
    },

    /**
     * PUBLIC stateChangedToWorse()
     *
     * Check if a state change occured to a worse state
     *
     * @author	Lars Michelsen <lm@larsmichelsen.com>
     */
    stateChangedToWorse: function() {
        var lastSubState = 'normal';
        if(this.last_state.summary_problem_has_been_acknowledged && this.last_state.summary_problem_has_been_acknowledged == 1) {
            lastSubState = 'ack';
        } else if(this.last_state.summary_in_downtime && this.last_state.summary_in_downtime == 1) {
            lastSubState = 'downtime';
        } else if(this.last_state.summary_stale) {
            lastSubState = 'stale';
        }

        // If there is no "last state" return true here
        if(!this.last_state.summary_state) {
            return true;
        }

        var lastWeight = oStates[this.last_state.summary_state][lastSubState];

        var subState = 'normal';
        if(this.conf.summary_problem_has_been_acknowledged && this.conf.summary_problem_has_been_acknowledged == 1) {
            subState = 'ack';
        } else if(this.conf.summary_in_downtime && this.conf.summary_in_downtime == 1) {
            subState = 'downtime';
        } else if(this.conf.summary_stale) {
            subState = 'stale';
        }

        var weight = oStates[this.conf.summary_state][subState];

        return lastWeight < weight;
    },

    /**
     * Returns true if the object is in non acked problem state
     */
    hasProblematicState: function() {
        // In case of acked/downtimed states this is no problematic state
        if(this.conf.summary_problem_has_been_acknowledged && this.conf.summary_problem_has_been_acknowledged == 1) {
            return false;
        } else if(this.conf.summary_in_downtime && this.conf.summary_in_downtime == 1) {
            return false;
        } else if(this.conf.summary_stale && this.conf.summary_stale) {
            return false;
        }
        
        var weight = oStates[this.conf.summary_state]['normal'];
        return weight > oStates['UP']['normal'];
    },

    /**
     * PUBLIC outputChanged()
     *
     * Check if an output/perfdata change occured since last refresh
     *
     * @author	Lars Michelsen <lm@larsmichelsen.com>
     */
    outputOrPerfdataChanged: function() {
        return this.conf.output != this.last_state.output || this.conf.perfdata != this.last_state.perfdata;
    },

    // Updates the logical elements of the object
    update: function () {
        this.getMembers();
        this.replaceMacros();

        if (g_view.type === 'map') {
            switch (this.conf.view_type) {
                case 'line':
                    new ElementLine(this).addTo(this);
                break;
                case 'gadget':
                    new ElementGadget(this).addTo(this);
                break;
                default:
                    new ElementIcon(this).addTo(this);
                break;
            }
        }

        this.base();
    },

    updateAttrs: function(attrs, only_state) {
        // Save old state for later "change detection"
        this.saveLastState();

        this.base(attrs, only_state);
        this.updateMemberAttrs(attrs, only_state);

        if (!only_state) {
            this.replaceMacros();
        }

        // When the config has not changed, but the state, rerender the whole object
        // the config update is handled within NagVisObject()
        if (only_state && this.stateChanged())
            this.render(); // erase, rerender and draw object

        if (this.stateChanged()) {
            /**
             * Additional eventhandling
             *
             * event_log=1/0
             * event_highlight=1/0
             * event_scroll=1/0
             * event_sound=1/0
             */

            // Only do eventhandling when object state changed to a worse state
            if (this.stateChangedToWorse()) {
                this.raiseEvents(true);
                this.initRepeatedEvents();
            }
            return true;
        }
        else {
            return false;
        }
    },

    transformAttributes: function() {
        this.replaceMacros();
    },

    updateMemberAttrs: function(attrs, only_state) {
        if (!this.members || !attrs.members)
            return;

        // Update already existing objects
        for (var i = 0, len = attrs.members.length; i < len; i++) {
            var member_attrs = attrs.members[i],
                updated = false;

            for (var a = 0, len2 = this.members.length; a < len2; a++) {
                var member = this.members[a];
                if (member_attrs.object_id == member.conf.object_id) {
                    member.updateAttrs(member_attrs, only_state);
                    break;
                }
            }
        }

        this.getMembers();
    },

    /**
     * Raise enabled frontend events for the object with the given object id
     */
    raiseEvents: function (stateChanged) {
        // - Highlight (Flashing)
        if (oPageProperties.event_highlight === '1') {
            if (this.conf.view_type && this.conf.view_type === 'icon') {
                // Detach the handler
                setTimeout(function(obj_id) {
                    return function() {
                        flashIcon(obj_id, oPageProperties.event_highlight_duration,
                                  oPageProperties.event_highlight_interval);
                    };
                }(this.conf.object_id), 0);
            } else {
                // FIXME: Atm only flash icons, not lines or gadgets
            }
        }
    
        // - Scroll to object
        if (oPageProperties.event_scroll === '1') {
            setTimeout(function(x, y) {
                return function() {
                    scrollSlow(x, y, 1);
                };
            }(this.parsedX(), this.parsedY()), 0);
        }
    
        // - Eventlog
        if (this.conf.type == 'service') {
            if (stateChanged) {
                eventlog("state-change", "info", this.conf.type+" "+this.conf.name+" "+this.conf.service_description+": Old: "+this.last_state.summary_state+"/"+this.last_state.summary_problem_has_been_acknowledged+"/"+this.last_state.summary_in_downtime+" New: "+this.conf.summary_state+"/"+this.conf.summary_problem_has_been_acknowledged+"/"+this.conf.summary_in_downtime);
            }
            else {
                eventlog("state-log", "info", this.conf.type+" "+this.conf.name+" "+this.conf.service_description+": State: "+this.conf.summary_state+"/"+this.conf.summary_problem_has_been_acknowledged+"/"+this.conf.summary_in_downtime);
            }
        }
        else {
            if (stateChanged) {
                eventlog("state-change", "info", this.conf.type+" "+this.conf.name+": Old: "+this.last_state.summary_state+"/"+this.last_state.summary_problem_has_been_acknowledged+"/"+this.last_state.summary_in_downtime+" New: "+this.conf.summary_state+"/"+this.conf.summary_problem_has_been_acknowledged+"/"+this.conf.summary_in_downtime);
            }
            else {
                eventlog("state-log", "info", this.conf.type+" "+this.conf.name+": State: "+this.conf.summary_state+"/"+this.conf.summary_problem_has_been_acknowledged+"/"+this.conf.summary_in_downtime);
            }
        }
    
        // - Sound
        if (oPageProperties.event_sound === '1') {
            setTimeout(function(obj_id) {
                return function() {
                    playSound(obj_id, 1);
                };
            }(this.conf.object_id), 0);
        }
    },

    // Initializes repeated events (if configured to do so) after first event handling
    initRepeatedEvents: function () {
        // Are the events configured to be re-raised?
        if(isset(oViewProperties.event_repeat_interval)
           && oViewProperties.event_repeat_interval != 0) {
            this.event_time_first = iNow;
            this.event_time_last  = iNow;
        }
    },

    /**
     * Checks wether or not repeated events need to be re-raised and re-raises
     * them if the time has come
     */
    checkRepeatEvents: function () {
        // Terminate repeated events after the state has changed to OK state
        if (!this.hasProblematicState()) {
            // Terminate, reset vars
            this.event_time_first = null;
            this.event_time_last  = null;
            return;
        }
    
        // Terminate repeated events after duration has been reached when
        // a limited duration has been configured
        if (oViewProperties.event_repeat_duration != -1 
           && this.event_time_first 
              + oViewProperties.event_repeat_duration < iNow) {
            // Terminate, reset vars
            this.event_time_first = null;
            this.event_time_last  = null;
            return;
        }
        
        // Time for next event interval?
        if (this.event_time_last
           + oViewProperties.event_repeat_interval >= iNow) {
            this.raiseEvents(false);
            this.event_time_last = iNow;
        }
    },

    /**
     * Replaces macros of urls and hover_urls
     *
     * @author 	Lars Michelsen <lm@larsmichelsen.com>
     */
    replaceMacros: function () {
        var name = '';
        if(this.conf.type == 'service') {
            name = 'host_name';
        } else {
            name = this.conf.type + '_name';
        }

        if(this.conf.url && this.conf.url !== '') {
            if(this.conf.htmlcgi && this.conf.htmlcgi !== '') {
                this.conf.url = this.conf.url.replace(getRegEx('htmlcgi', '\\[htmlcgi\\]', 'g'), this.conf.htmlcgi);
            } else {
                this.conf.url = this.conf.url.replace(getRegEx('htmlcgi', '\\[htmlcgi\\]', 'g'), oGeneralProperties.path_cgi);
            }

            this.conf.url = this.conf.url.replace(getRegEx('htmlbase', '\\[htmlbase\\]', 'g'), oGeneralProperties.path_base);

            this.conf.url = this.conf.url.replace(getRegEx(name, '\\['+name+'\\]', 'g'), this.conf.name);
            if(this.conf.type == 'service') {
                this.conf.url = this.conf.url.replace(getRegEx('service_description', '\\[service_description\\]', 'g'), this.conf.service_description);
            }

            if(this.conf.type != 'map') {
                this.conf.url = this.conf.url.replace(getRegEx('backend_id', '\\[backend_id\\]', 'g'), this.conf.backend_id);
            }
        }
    },

    highlight: function(show) {
        // FIXME: Highlight lines in the future too
        if(this.conf.view_type !== 'icon')
            return;

        var oObjIcon = document.getElementById(this.conf.object_id + '-icon');
        var oObjIconDiv = document.getElementById(this.conf.object_id + '-icondiv');

        var sColor = oStates[this.conf.summary_state].color;

        // Use these classes (icon-flashing-<state>) in your custom css to customise flashing icons
        // e.g. icon-flashing-critical, icondiv-flashing-critical
        // Note you may need to use '!important' to override the inline default
        // 
        var sFlashingClass = 'icon-flashing-' + this.conf.summary_state.toLowerCase();
        var sFlashingDivClass = 'icondiv-flashing-' + this.conf.summary_state.toLowerCase();

        this.bIsFlashing = show;
        if(show) {
            oObjIcon.style.border  = "5px solid " + sColor;
            oObjIcon.classList.add(sFlashingClass);
            oObjIconDiv.style.top  = (this.parseCoord(this.conf.y, 'y') - 5) + 'px';
            oObjIconDiv.style.left = (this.parseCoord(this.conf.x, 'x') - 5) + 'px';
            oObjIconDiv.classList.add(sFlashingDivClass);
        } else {
            oObjIcon.style.border  = "none";
            oObjIcon.classList.remove(sFlashingClass);
            oObjIconDiv.style.top  = this.parseCoord(this.conf.y, 'y') + 'px';
            oObjIconDiv.style.left = this.parseCoord(this.conf.x, 'x') + 'px';
            oObjIconDiv.classList.remove(sFlashingDivClass);
        }

        sColor            = null;
        sFlashingClass    = null;
        sFlashingDivClass = null;
        oObjIconDiv       = null;
        oObjIcon          = null;
    }

});