1 // +------------------------------------------------------------------+
2 // |             ____ _               _        __  __ _  __           |
3 // |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
4 // |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
5 // |           | |___| | | |  __/ (__|   <    | |  | | . \            |
6 // |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
7 // |                                                                  |
8 // | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
10 //
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
13 //
14 // check_mk is free software;  you can redistribute it and/or modify it
15 // under the  terms of the  GNU General Public License  as published by
16 // the Free Software Foundation in version 2.  check_mk is  distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
18 // out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
19 // PARTICULAR PURPOSE. See the  GNU General Public License for more de-
20 // ails.  You should have  received  a copy of the  GNU  General Public
21 // License along with GNU Make; see the file  COPYING.  If  not,  write
22 // to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
23 // Boston, MA 02110-1301 USA.
24 
25 #include "ServicelistStateColumn.h"
26 #include "Query.h"
27 #include "TableServices.h"
28 
29 extern TableServices *g_table_services;
30 
31 // return true if state1 is worse than state2
svcStateIsWorse(int32_t state1,int32_t state2)32 bool ServicelistStateColumn::svcStateIsWorse(int32_t state1, int32_t state2) {
33     if (state1 == 0) {
34         return false;  // OK is worse than nothing
35     }
36     if (state2 == 0) {
37         return true;  // everything else is worse then OK
38     }
39     if (state2 == 2) {
40         return false;  // nothing is worse than CRIT
41     }
42     if (state1 == 2) {
43         return true;  // state1 is CRIT, state2 not
44     }
45     return (state1 > state2);  // both or WARN or UNKNOWN
46 }
47 
getMembers(void * data)48 servicesmember *ServicelistStateColumn::getMembers(void *data) {
49     data = shiftPointer(data);
50     if (data == nullptr) {
51         return nullptr;
52     }
53 
54     return *reinterpret_cast<servicesmember **>(reinterpret_cast<char *>(data) +
55                                                 _offset);
56 }
57 
getValue(int logictype,servicesmember * mem,Query * query)58 int32_t ServicelistStateColumn::getValue(int logictype, servicesmember *mem,
59                                          Query *query) {
60     contact *auth_user = query->authUser();
61     int32_t result = 0;
62 
63     while (mem != nullptr) {
64         service *svc = mem->service_ptr;
65         if ((auth_user == nullptr) ||
66             g_table_services->isAuthorized(auth_user, svc)) {
67             int lt = logictype;
68             int state;
69             int has_been_checked;
70             if (logictype >= 60) {
71                 state = svc->last_hard_state;
72                 lt -= 64;
73             } else {
74                 state = svc->current_state;
75             }
76 
77             has_been_checked = svc->has_been_checked;
78 
79             switch (lt) {
80                 case SLSC_WORST_STATE:
81                     if (svcStateIsWorse(state, result)) {
82                         result = state;
83                     }
84                     break;
85                 case SLSC_NUM:
86                     result++;
87                     break;
88                 case SLSC_NUM_PENDING:
89                     if (has_been_checked == 0) {
90                         result++;
91                     }
92                     break;
93                 default:
94                     if ((has_been_checked != 0) && state == lt) {
95                         result++;
96                     }
97                     break;
98             }
99         }
100         mem = mem->next;
101     }
102     return result;
103 }
104 
getValue(void * data,Query * query)105 int32_t ServicelistStateColumn::getValue(void *data, Query *query) {
106     servicesmember *mem = getMembers(data);
107     return getValue(_logictype, mem, query);
108 }
109