| 1 |
# Nagstamon - Nagios status monitor for your desktop |
| 2 |
# Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al. |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 |
from copy import deepcopy |
| 18 |
from datetime import datetime |
| 19 |
from subprocess import Popen |
| 20 |
from sys import stdout |
| 21 |
from traceback import print_exc |
| 22 |
from urllib.parse import quote |
| 23 |
|
| 24 |
from Nagstamon.config import conf |
| 25 |
from Nagstamon.helpers import (is_found_by_re, |
| 26 |
STATES, |
| 27 |
SORT_COLUMNS_FUNCTIONS, |
| 28 |
urlify, |
| 29 |
webbrowser_open) |
| 30 |
from Nagstamon.qui.constants import (COLORS, |
| 31 |
HEADERS, |
| 32 |
SORT_COLUMNS_INDEX, |
| 33 |
SORT_ORDER) |
| 34 |
from Nagstamon.qui.dialogs.weblogin import DialogWebLogin |
| 35 |
from Nagstamon.qui.globals import (clipboard, |
| 36 |
font, |
| 37 |
qbrushes, |
| 38 |
statuswindow_properties) |
| 39 |
from Nagstamon.qui.qt import (get_sort_order_value, |
| 40 |
QAbstractItemView, |
| 41 |
QAction, |
| 42 |
QColor, |
| 43 |
QHeaderView, |
| 44 |
QKeySequence, |
| 45 |
QMenu, |
| 46 |
QObject, |
| 47 |
QSignalMapper, |
| 48 |
QSizePolicy, |
| 49 |
Qt, |
| 50 |
QThread, |
| 51 |
QTimer, |
| 52 |
QTreeView, |
| 53 |
Signal, |
| 54 |
Slot) |
| 55 |
from Nagstamon.qui.widgets.app import app |
| 56 |
from Nagstamon.qui.widgets.menu import MenuAtCursor |
| 57 |
from Nagstamon.qui.widgets.model import Model |
| 58 |
from Nagstamon.servers import SERVER_TYPES, servers |
| 59 |
|
| 60 |
|
| 61 |
class TreeView(QTreeView): |
| 62 |
""" |
| 63 |
attempt to get a less resource-hungry table/tree |
| 64 |
""" |
| 65 |
|
| 66 |
# tell global window that it should be resized |
| 67 |
ready_to_resize = Signal() |
| 68 |
|
| 69 |
# sent by refresh() for statusbar |
| 70 |
refreshed = Signal() |
| 71 |
|
| 72 |
# tell worker to get status after a recheck has been solicited |
| 73 |
recheck = Signal(dict) |
| 74 |
|
| 75 |
# tell notification that status of server has changed |
| 76 |
status_changed = Signal(str, str, str) |
| 77 |
|
| 78 |
# action to be executed by worker |
| 79 |
# 2 values: action and host/service info |
| 80 |
request_action = Signal(dict, dict) |
| 81 |
|
| 82 |
# tell worker it should sort columns after someone pressed the column header |
| 83 |
sort_data_array_for_columns = Signal(int, int, bool) |
| 84 |
|
| 85 |
# mouse clicked on cell |
| 86 |
mouse_released = Signal() |
| 87 |
|
| 88 |
# action menu option was selected |
| 89 |
action_menu_clicked = Signal() |
| 90 |
|
| 91 |
# action to edit actions in settings dialog |
| 92 |
action_edit_triggered = Signal(int) |
| 93 |
|
| 94 |
# action acknowledge triggered, needs to be initialized and shown |
| 95 |
action_acknowledge_triggered_initialize = Signal(object, list, list) |
| 96 |
action_acknowledge_triggered_show = Signal() |
| 97 |
|
| 98 |
# action downtime triggered, needs to be initialized and shown |
| 99 |
action_downtime_triggered_initialize = Signal(object, list, list) |
| 100 |
action_downtime_triggered_show = Signal() |
| 101 |
|
| 102 |
# action submit check result triggered, needs to be initialized and shown |
| 103 |
action_submit_triggered_initialize = Signal(object, str, str) |
| 104 |
action_submit_triggered_show = Signal() |
| 105 |
|
| 106 |
def __init__(self, columncount, rowcount, sort_column, sort_order, server, parent=None): |
| 107 |
QTreeView.__init__(self, parent=parent) |
| 108 |
|
| 109 |
self.parent_statuswindow = self.parentWidget() |
| 110 |
|
| 111 |
self.sort_column = sort_column |
| 112 |
self.sort_order = sort_order |
| 113 |
self.server = server |
| 114 |
|
| 115 |
# no handling of selection by treeview |
| 116 |
self.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows) |
| 117 |
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) |
| 118 |
|
| 119 |
# disable space on the left side |
| 120 |
self.setRootIsDecorated(False) |
| 121 |
self.setIndentation(0) |
| 122 |
|
| 123 |
self.setUniformRowHeights(True) |
| 124 |
|
| 125 |
# no scrollbars at tables because they will be scrollable by the global vertical scrollbar |
| 126 |
self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) |
| 127 |
self.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) |
| 128 |
self.setAutoScroll(False) |
| 129 |
self.setSortingEnabled(True) |
| 130 |
|
| 131 |
self.sortByColumn(0, Qt.SortOrder.AscendingOrder) |
| 132 |
|
| 133 |
self.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Expanding) |
| 134 |
|
| 135 |
self.header().setSectionResizeMode(QHeaderView.ResizeMode.ResizeToContents) |
| 136 |
self.header().setSortIndicatorShown(True) |
| 137 |
self.header().setStretchLastSection(True) |
| 138 |
|
| 139 |
self.header().setSortIndicator(sort_column, SORT_ORDER[self.sort_order]) |
| 140 |
|
| 141 |
# small method needed to tell worker which column and sort order to use |
| 142 |
self.header().sortIndicatorChanged.connect(self.sort_columns) |
| 143 |
|
| 144 |
# set overall margin and hover colors - to be refined |
| 145 |
self.setStyleSheet('''QTreeView::item {margin: 5px;} |
| 146 |
QTreeView::item:hover {margin: 0px; |
| 147 |
padding: 5px; |
| 148 |
color: white; |
| 149 |
background-color: dimgrey;} |
| 150 |
QTreeView::item:selected {margin: 0px; |
| 151 |
padding: 5px; |
| 152 |
color: white; |
| 153 |
background-color: grey;} |
| 154 |
''') |
| 155 |
|
| 156 |
# set application font |
| 157 |
self.set_font() |
| 158 |
|
| 159 |
# change font if it has been changed by settings |
| 160 |
self.parent_statuswindow.injected_dialogs.settings.changed.connect(self.set_font) |
| 161 |
|
| 162 |
# create brushes if colors have been changed |
| 163 |
self.parent_statuswindow.injected_dialogs.settings.changed.connect(self.create_brushes) |
| 164 |
|
| 165 |
# create brushes for treeview |
| 166 |
self.create_brushes() |
| 167 |
|
| 168 |
# action context menu |
| 169 |
self.action_menu = MenuAtCursor(parent=self) |
| 170 |
# signalmapper for getting triggered actions |
| 171 |
self.signalmapper_action_menu = QSignalMapper() |
| 172 |
# connect menu to responder |
| 173 |
self.signalmapper_action_menu.mappedString[str].connect(self.action_menu_custom_response) |
| 174 |
|
| 175 |
# clipboard actions |
| 176 |
self.clipboard_menu = QMenu('Copy to clipboard', self) |
| 177 |
|
| 178 |
self.clipboard_action_host = QAction('Host', self) |
| 179 |
self.clipboard_action_host.triggered.connect(self.action_clipboard_action_host) |
| 180 |
self.clipboard_menu.addAction(self.clipboard_action_host) |
| 181 |
|
| 182 |
self.clipboard_action_service = QAction('Service', self) |
| 183 |
self.clipboard_action_service.triggered.connect(self.action_clipboard_action_service) |
| 184 |
self.clipboard_menu.addAction(self.clipboard_action_service) |
| 185 |
|
| 186 |
self.clipboard_action_statusinformation = QAction('Status information', self) |
| 187 |
self.clipboard_action_statusinformation.triggered.connect(self.action_clipboard_action_statusinformation) |
| 188 |
self.clipboard_menu.addAction(self.clipboard_action_statusinformation) |
| 189 |
|
| 190 |
self.clipboard_action_all = QAction('All information', self) |
| 191 |
self.clipboard_action_all.triggered.connect(self.action_clipboard_action_all) |
| 192 |
self.clipboard_menu.addAction(self.clipboard_action_all) |
| 193 |
|
| 194 |
self.setModel(Model(server=self.server, parent=self)) |
| 195 |
self.model().model_data_array_filled.connect(self.adjust_table) |
| 196 |
self.model().hosts_flags_column_needed.connect(self.show_hosts_flags_column) |
| 197 |
self.model().services_flags_column_needed.connect(self.show_services_flags_column) |
| 198 |
|
| 199 |
# a thread + worker is necessary to get new monitor server data in the background and |
| 200 |
# to refresh the table cell by cell after new data is available |
| 201 |
self.worker_thread = QThread(parent=self) |
| 202 |
self.worker = self.Worker(server=server, sort_column=self.sort_column, sort_order=self.sort_order, status_window=self.parent_statuswindow) |
| 203 |
self.worker.moveToThread(self.worker_thread) |
| 204 |
|
| 205 |
# if worker got new status data from monitor server get_status |
| 206 |
# the treeview model has to be updated |
| 207 |
self.worker.worker_data_array_filled.connect(self.model().fill_data_array) |
| 208 |
|
| 209 |
# fill array again if data has been sorted after a header column click |
| 210 |
self.worker.data_array_sorted.connect(self.model().fill_data_array) |
| 211 |
|
| 212 |
# tell worker to sort data_array depending on sort_column and sort_order |
| 213 |
self.sort_data_array_for_columns.connect(self.worker.sort_data_array) |
| 214 |
|
| 215 |
# if worker got new status data from monitor server get_status the table should be refreshed |
| 216 |
self.worker.new_status.connect(self.refresh) |
| 217 |
|
| 218 |
# quit thread if worker has finished |
| 219 |
self.worker.finish.connect(self.finish_worker_thread) |
| 220 |
|
| 221 |
# get status if started |
| 222 |
self.worker_thread.started.connect(self.worker.get_status) |
| 223 |
# start with priority 0 = lowest |
| 224 |
self.worker_thread.start() |
| 225 |
|
| 226 |
# connect signal for acknowledge |
| 227 |
self.parent_statuswindow.injected_dialogs.acknowledge.acknowledge.connect(self.worker.acknowledge) |
| 228 |
|
| 229 |
# connect signal to get start end time for downtime from worker |
| 230 |
self.parent_statuswindow.injected_dialogs.downtime.get_start_end.connect(self.worker.get_start_end) |
| 231 |
self.worker.set_start_end.connect(self.parent_statuswindow.injected_dialogs.downtime.set_start_end) |
| 232 |
|
| 233 |
# connect signal for downtime |
| 234 |
self.parent_statuswindow.injected_dialogs.downtime.downtime.connect(self.worker.downtime) |
| 235 |
|
| 236 |
# connect signal for submit check result |
| 237 |
self.parent_statuswindow.injected_dialogs.submit.submit.connect(self.worker.submit) |
| 238 |
|
| 239 |
# connect signal for recheck action |
| 240 |
self.recheck.connect(self.worker.recheck) |
| 241 |
|
| 242 |
# execute action by worker |
| 243 |
self.request_action.connect(self.worker.execute_action) |
| 244 |
|
| 245 |
# hide status window if mouse was clicked ant it is configured to do so |
| 246 |
self.mouse_released.connect(self.parent_statuswindow.hide_window) |
| 247 |
|
| 248 |
# option of action menu was selected |
| 249 |
self.action_menu_clicked.connect(self.parent_statuswindow.hide_window) |
| 250 |
|
| 251 |
# edit actions in settings dialog in tab #3 |
| 252 |
self.action_edit_triggered.connect(self.parent_statuswindow.injected_dialogs.settings.show) |
| 253 |
|
| 254 |
# intialize and open acknowledge dialog |
| 255 |
self.action_acknowledge_triggered_initialize.connect(self.parent_statuswindow.injected_dialogs.acknowledge.initialize) |
| 256 |
self.action_acknowledge_triggered_show.connect(self.parent_statuswindow.injected_dialogs.acknowledge.show) |
| 257 |
|
| 258 |
# intialize and open downtime dialog |
| 259 |
self.action_downtime_triggered_initialize.connect(self.parent_statuswindow.injected_dialogs.downtime.initialize) |
| 260 |
self.action_downtime_triggered_show.connect(self.parent_statuswindow.injected_dialogs.downtime.show) |
| 261 |
|
| 262 |
# intialize and open submit check result dialog |
| 263 |
self.action_submit_triggered_initialize.connect(self.parent_statuswindow.injected_dialogs.submit.initialize) |
| 264 |
self.action_submit_triggered_show.connect(self.parent_statuswindow.injected_dialogs.submit.show) |
| 265 |
|
| 266 |
@Slot() |
| 267 |
def set_font(self): |
| 268 |
""" |
| 269 |
change font if it has been changed by settings |
| 270 |
""" |
| 271 |
self.setFont(font) |
| 272 |
|
| 273 |
@Slot() |
| 274 |
def create_brushes(self): |
| 275 |
""" |
| 276 |
fill static brushes with current colors for treeview |
| 277 |
""" |
| 278 |
# if not customized, use default intensity |
| 279 |
if conf.grid_use_custom_intensity: |
| 280 |
intensity = 100 + conf.grid_alternation_intensity |
| 281 |
else: |
| 282 |
intensity = 115 |
| 283 |
|
| 284 |
# every state has 2 labels in both alteration levels 0 and 1 |
| 285 |
for state in STATES[1:]: |
| 286 |
for role in ('text', 'background'): |
| 287 |
qbrushes[0][COLORS[state] + role] = QColor(conf.__dict__[COLORS[state] + role]) |
| 288 |
# if the background is too dark to be litten split it into RGB values |
| 289 |
# and increase them separately |
| 290 |
# light/darkness spans from 0 to 255 - 30 is just a guess |
| 291 |
if role == 'background' and conf.show_grid: |
| 292 |
if qbrushes[0][COLORS[state] + role].lightness() < 30: |
| 293 |
r, g, b, a = (qbrushes[0][COLORS[state] + role].getRgb()) |
| 294 |
r += 30 |
| 295 |
g += 30 |
| 296 |
b += 30 |
| 297 |
qbrushes[1][COLORS[state] + role] = QColor(r, g, b).lighter(intensity) |
| 298 |
else: |
| 299 |
# otherwise just make it a little bit darker |
| 300 |
qbrushes[1][COLORS[state] + role] = QColor(conf.__dict__[COLORS[state] + |
| 301 |
role]).darker(intensity) |
| 302 |
else: |
| 303 |
# only make the background darker; the text should stay as it is |
| 304 |
qbrushes[1][COLORS[state] + role] = qbrushes[0][COLORS[state] + role] |
| 305 |
|
| 306 |
@Slot(bool) |
| 307 |
def show_hosts_flags_column(self, value): |
| 308 |
""" |
| 309 |
show hosts flags column if needed |
| 310 |
'value' is True if there is a need so it has to be converted |
| 311 |
""" |
| 312 |
self.setColumnHidden(1, not value) |
| 313 |
|
| 314 |
@Slot(bool) |
| 315 |
def show_services_flags_column(self, value): |
| 316 |
""" |
| 317 |
show service flags column if needed |
| 318 |
'value' is True if there is a need so it has to be converted |
| 319 |
""" |
| 320 |
self.setColumnHidden(3, not value) |
| 321 |
|
| 322 |
def get_real_height(self): |
| 323 |
""" |
| 324 |
calculate real table height as there is no method included |
| 325 |
""" |
| 326 |
height = 0 |
| 327 |
|
| 328 |
# only count if there is anything to display - there is no use of the headers only |
| 329 |
if self.model().rowCount(self) > 0: |
| 330 |
# height summary starts with headers' height |
| 331 |
# apparently height works better/without scrollbar if some pixels are added |
| 332 |
height = self.header().sizeHint().height() + 2 |
| 333 |
|
| 334 |
# maybe simply take nagitems_filtered_count? |
| 335 |
height += self.indexRowSizeHint(self.model().index(0, 0)) * self.model().rowCount(self) |
| 336 |
|
| 337 |
return height |
| 338 |
|
| 339 |
def get_real_width(self): |
| 340 |
width = 0 |
| 341 |
# avoid the last dummy column to be counted |
| 342 |
for column in range(len(HEADERS) - 1): |
| 343 |
width += self.columnWidth(column) |
| 344 |
return width |
| 345 |
|
| 346 |
@Slot() |
| 347 |
def adjust_table(self): |
| 348 |
""" |
| 349 |
adjust table dimensions after filling it |
| 350 |
""" |
| 351 |
# force table to its maximal height, calculated by .get_real_height() |
| 352 |
self.setMinimumHeight(self.get_real_height()) |
| 353 |
self.setMaximumHeight(self.get_real_height()) |
| 354 |
self.setSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Maximum) |
| 355 |
# after setting table whole window can be repainted |
| 356 |
self.ready_to_resize.emit() |
| 357 |
|
| 358 |
def count_selected_rows(self): |
| 359 |
""" |
| 360 |
find out if rows are selected and return their number |
| 361 |
""" |
| 362 |
rows = [] |
| 363 |
for index in self.selectedIndexes(): |
| 364 |
if index.row() not in rows: |
| 365 |
rows.append(index.row()) |
| 366 |
return len(rows) |
| 367 |
|
| 368 |
def mouseReleaseEvent(self, event): |
| 369 |
""" |
| 370 |
forward clicked cell info from event |
| 371 |
""" |
| 372 |
# special treatment if window should be closed when left-clicking somewhere |
| 373 |
# it is important to check if CTRL or SHIFT key is presses while clicking to select lines |
| 374 |
modifiers = event.modifiers() |
| 375 |
if conf.close_details_clicking_somewhere: |
| 376 |
if event.button() == Qt.MouseButton.LeftButton: |
| 377 |
# count selected rows - if more than 1 do not close popwin |
| 378 |
if modifiers or self.count_selected_rows() > 1: |
| 379 |
super(TreeView, self).mouseReleaseEvent(event) |
| 380 |
else: |
| 381 |
self.mouse_released.emit() |
| 382 |
return |
| 383 |
elif event.button() == Qt.MouseButton.RightButton: |
| 384 |
self.cell_clicked() |
| 385 |
return |
| 386 |
elif not modifiers or \ |
| 387 |
event.button() == Qt.MouseButton.RightButton: |
| 388 |
self.cell_clicked() |
| 389 |
return |
| 390 |
else: |
| 391 |
super(TreeView, self).mouseReleaseEvent(event) |
| 392 |
|
| 393 |
def wheelEvent(self, event): |
| 394 |
""" |
| 395 |
avoid scrollable single treeview in Linux and GNOME3 by simply do nothing when getting a wheel event |
| 396 |
""" |
| 397 |
event.ignore() |
| 398 |
|
| 399 |
def keyPressEvent(self, event): |
| 400 |
""" |
| 401 |
Use to handle copy from keyboard |
| 402 |
""" |
| 403 |
if event.matches(QKeySequence.StandardKey.Copy): |
| 404 |
self.action_clipboard_action_all() |
| 405 |
return |
| 406 |
super(TreeView, self).keyPressEvent(event) |
| 407 |
|
| 408 |
@Slot() |
| 409 |
def cell_clicked(self): |
| 410 |
""" |
| 411 |
Windows reacts differently to clicks into table cells than Linux and MacOSX |
| 412 |
Therefore the .available flag is necessary |
| 413 |
""" |
| 414 |
# empty the menu |
| 415 |
self.action_menu.clear() |
| 416 |
|
| 417 |
# clear signal mappings |
| 418 |
self.signalmapper_action_menu.removeMappings(self.signalmapper_action_menu) |
| 419 |
|
| 420 |
# add custom actions |
| 421 |
actions_list = list(conf.actions) |
| 422 |
actions_list.sort(key=str.lower) |
| 423 |
|
| 424 |
# How many rows do we have |
| 425 |
list_rows = [] |
| 426 |
for index in self.selectedIndexes(): |
| 427 |
if index.row() not in list_rows: |
| 428 |
list_rows.append(index.row()) |
| 429 |
|
| 430 |
# dummy definition to avoid crash if no actions are enabled - asked for some lines later |
| 431 |
miserable_service = None |
| 432 |
|
| 433 |
# Add custom actions if all selected rows want them, one per one |
| 434 |
for a in actions_list: |
| 435 |
# shortcut for next lines |
| 436 |
action = conf.actions[a] |
| 437 |
|
| 438 |
# check if current monitor server type is in action |
| 439 |
# second check for server type is legacy-compatible with older settings |
| 440 |
if action.enabled is True and (action.monitor_type in ['', self.server.TYPE] or |
| 441 |
action.monitor_type not in SERVER_TYPES): |
| 442 |
|
| 443 |
# menu item visibility flag |
| 444 |
item_visible = None |
| 445 |
|
| 446 |
for lrow in list_rows: |
| 447 |
# temporary menu item visibility flag to collect all visibility info |
| 448 |
item_visible_temporary = False |
| 449 |
# take data from model data_array |
| 450 |
miserable_host = self.model().data_array[lrow][0] |
| 451 |
miserable_service = self.model().data_array[lrow][2] |
| 452 |
miserable_duration = self.model().data_array[lrow][6] |
| 453 |
miserable_attempt = self.model().data_array[lrow][7] |
| 454 |
miserable_status_information = self.model().data_array[lrow][8] |
| 455 |
# check if clicked line is a service or host |
| 456 |
# it is checked if the action is targeted on hosts or services |
| 457 |
if miserable_service: |
| 458 |
if action.filter_target_service is True: |
| 459 |
# only check if there is some to check |
| 460 |
if action.re_host_enabled is True: |
| 461 |
if is_found_by_re(miserable_host, |
| 462 |
action.re_host_pattern, |
| 463 |
action.re_host_reverse): |
| 464 |
item_visible_temporary = True |
| 465 |
# dito |
| 466 |
if action.re_service_enabled is True: |
| 467 |
if is_found_by_re(miserable_service, |
| 468 |
action.re_service_pattern, |
| 469 |
action.re_service_reverse): |
| 470 |
item_visible_temporary = True |
| 471 |
# dito |
| 472 |
if action.re_status_information_enabled is True: |
| 473 |
if is_found_by_re(miserable_status_information, |
| 474 |
action.re_status_information_pattern, |
| 475 |
action.re_status_information_reverse): |
| 476 |
item_visible_temporary = True |
| 477 |
# dito |
| 478 |
if action.re_duration_enabled is True: |
| 479 |
if is_found_by_re(miserable_duration, |
| 480 |
action.re_duration_pattern, |
| 481 |
action.re_duration_reverse): |
| 482 |
item_visible_temporary = True |
| 483 |
|
| 484 |
# dito |
| 485 |
if action.re_attempt_enabled is True: |
| 486 |
if is_found_by_re(miserable_attempt, |
| 487 |
action.re_attempt_pattern, |
| 488 |
action.re_attempt_reverse): |
| 489 |
item_visible_temporary = True |
| 490 |
|
| 491 |
# dito - how is this supposed to work? |
| 492 |
if action.re_groups_enabled is True: |
| 493 |
if is_found_by_re(miserable_service, |
| 494 |
action.re_groups_pattern, |
| 495 |
action.re_groups_reverse): |
| 496 |
item_visible_temporary = True |
| 497 |
|
| 498 |
# fallback if no regexp is selected |
| 499 |
if action.re_host_enabled == action.re_service_enabled == \ |
| 500 |
action.re_status_information_enabled == action.re_duration_enabled == \ |
| 501 |
action.re_attempt_enabled == action.re_groups_enabled is False: |
| 502 |
item_visible_temporary = True |
| 503 |
|
| 504 |
else: |
| 505 |
# hosts should only care about host specific actions, no services |
| 506 |
if action.filter_target_host is True: |
| 507 |
if action.re_host_enabled is True: |
| 508 |
if is_found_by_re(miserable_host, |
| 509 |
action.re_host_pattern, |
| 510 |
action.re_host_reverse): |
| 511 |
item_visible_temporary = True |
| 512 |
else: |
| 513 |
# a non-specific action will be displayed per default |
| 514 |
item_visible_temporary = True |
| 515 |
|
| 516 |
# when item_visible has never been set it shall be false |
| 517 |
# also if at least one row leads to not-showing the item it will be false |
| 518 |
if item_visible_temporary and item_visible is None: |
| 519 |
item_visible = True |
| 520 |
if not item_visible_temporary: |
| 521 |
item_visible = False |
| 522 |
|
| 523 |
else: |
| 524 |
item_visible = False |
| 525 |
|
| 526 |
# populate context menu with service actions |
| 527 |
if item_visible: |
| 528 |
# create action |
| 529 |
action_menuentry = QAction(a, self) |
| 530 |
# add action |
| 531 |
self.action_menu.addAction(action_menuentry) |
| 532 |
# action to signalmapper |
| 533 |
self.signalmapper_action_menu.setMapping(action_menuentry, a) |
| 534 |
action_menuentry.triggered.connect(self.signalmapper_action_menu.map) |
| 535 |
|
| 536 |
del action, item_visible |
| 537 |
|
| 538 |
# create and add default actions |
| 539 |
action_edit_actions = QAction('Edit actions...', self) |
| 540 |
action_edit_actions.triggered.connect(self.action_edit_actions) |
| 541 |
self.action_menu.addAction(action_edit_actions) |
| 542 |
|
| 543 |
# put actions into menu after separator |
| 544 |
self.action_menu.addSeparator() |
| 545 |
if 'Monitor' in self.server.MENU_ACTIONS and len(list_rows) == 1: |
| 546 |
action_monitor = QAction('Monitor', self) |
| 547 |
action_monitor.triggered.connect(self.action_monitor) |
| 548 |
self.action_menu.addAction(action_monitor) |
| 549 |
|
| 550 |
if 'Recheck' in self.server.MENU_ACTIONS: |
| 551 |
action_recheck = QAction('Recheck', self) |
| 552 |
action_recheck.triggered.connect(self.action_recheck) |
| 553 |
self.action_menu.addAction(action_recheck) |
| 554 |
|
| 555 |
if 'Acknowledge' in self.server.MENU_ACTIONS: |
| 556 |
action_acknowledge = QAction('Acknowledge', self) |
| 557 |
action_acknowledge.triggered.connect(self.action_acknowledge) |
| 558 |
self.action_menu.addAction(action_acknowledge) |
| 559 |
|
| 560 |
if 'Downtime' in self.server.MENU_ACTIONS: |
| 561 |
action_downtime = QAction('Downtime', self) |
| 562 |
action_downtime.triggered.connect(self.action_downtime) |
| 563 |
self.action_menu.addAction(action_downtime) |
| 564 |
|
| 565 |
# special menu entry for Checkmk Multisite for archiving events |
| 566 |
if self.server.type == 'Checkmk Multisite' and len(list_rows) == 1: |
| 567 |
if miserable_service == 'Events': |
| 568 |
action_archive_event = QAction('Archive event', self) |
| 569 |
action_archive_event.triggered.connect(self.action_archive_event) |
| 570 |
self.action_menu.addAction(action_archive_event) |
| 571 |
|
| 572 |
# not all servers allow to submit fake check results |
| 573 |
if 'Submit check result' in self.server.MENU_ACTIONS and len(list_rows) == 1: |
| 574 |
action_submit = QAction('Submit check result', self) |
| 575 |
action_submit.triggered.connect(self.action_submit) |
| 576 |
self.action_menu.addAction(action_submit) |
| 577 |
|
| 578 |
# experimental clipboard submenu |
| 579 |
self.action_menu.addMenu(self.clipboard_menu) |
| 580 |
|
| 581 |
# show menu |
| 582 |
self.action_menu.show_at_cursor() |
| 583 |
|
| 584 |
@Slot(str) |
| 585 |
def action_menu_custom_response(self, action): |
| 586 |
# How many rows do we have |
| 587 |
list_rows = [] |
| 588 |
for index in self.selectedIndexes(): |
| 589 |
if index.row() not in list_rows: |
| 590 |
list_rows.append(index.row()) |
| 591 |
|
| 592 |
for lrow in list_rows: |
| 593 |
miserable_host = self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole) |
| 594 |
miserable_service = self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole) |
| 595 |
miserable_status_info = self.model().data(self.model().createIndex(lrow, 8), Qt.ItemDataRole.DisplayRole) |
| 596 |
|
| 597 |
# get data to send to action |
| 598 |
server = self.server.get_name() |
| 599 |
address = self.server.get_host(miserable_host).result |
| 600 |
monitor = self.server.monitor_url |
| 601 |
monitor_cgi = self.server.monitor_cgi_url |
| 602 |
username = self.server.username |
| 603 |
password = self.server.password |
| 604 |
comment_ack = conf.defaults_acknowledge_comment |
| 605 |
comment_down = conf.defaults_downtime_comment |
| 606 |
comment_submit = conf.defaults_submit_check_result_comment |
| 607 |
|
| 608 |
# send dict with action info and dict with host/service info |
| 609 |
self.request_action.emit(conf.actions[action].__dict__, |
| 610 |
{'server': server, |
| 611 |
'host': miserable_host, |
| 612 |
'service': miserable_service, |
| 613 |
'status-info': miserable_status_info, |
| 614 |
'address': address, |
| 615 |
'monitor': monitor, |
| 616 |
'monitor-cgi': monitor_cgi, |
| 617 |
'username': username, |
| 618 |
'password': password, |
| 619 |
'comment-ack': comment_ack, |
| 620 |
'comment-down': comment_down, |
| 621 |
'comment-submit': comment_submit |
| 622 |
} |
| 623 |
) |
| 624 |
|
| 625 |
# if action wants a closed status window it should be closed now |
| 626 |
if conf.actions[action].close_popwin and not conf.fullscreen and not conf.windowed: |
| 627 |
self.action_menu_clicked.emit() |
| 628 |
|
| 629 |
# clean up |
| 630 |
del list_rows |
| 631 |
|
| 632 |
@Slot() |
| 633 |
def action_response_decorator(method): |
| 634 |
""" |
| 635 |
decorate repeatedly called stuff |
| 636 |
""" |
| 637 |
|
| 638 |
def decoration_function(self): |
| 639 |
# run decorated method |
| 640 |
method(self) |
| 641 |
# default actions need closed statuswindow to display own dialogs |
| 642 |
if not conf.fullscreen and not conf.windowed and \ |
| 643 |
not method.__name__ == 'action_recheck' and \ |
| 644 |
not method.__name__ == 'action_archive_event': |
| 645 |
self.action_menu_clicked.emit() |
| 646 |
|
| 647 |
return decoration_function |
| 648 |
|
| 649 |
@action_response_decorator |
| 650 |
def action_edit_actions(self): |
| 651 |
# buttons in toparee |
| 652 |
if not conf.fullscreen and not conf.windowed: |
| 653 |
self.action_menu_clicked.emit() |
| 654 |
# open actions tab (#3) of settings dialog |
| 655 |
self.action_edit_triggered.emit(3) |
| 656 |
|
| 657 |
@action_response_decorator |
| 658 |
def action_monitor(self): |
| 659 |
# only on 1 row |
| 660 |
indexes = self.selectedIndexes() |
| 661 |
if len(indexes) > 0: |
| 662 |
index = indexes[0] |
| 663 |
miserable_host = self.model().data(self.model().createIndex(index.row(), 0), Qt.ItemDataRole.DisplayRole) |
| 664 |
miserable_service = self.model().data(self.model().createIndex(index.row(), 2), Qt.ItemDataRole.DisplayRole) |
| 665 |
|
| 666 |
# open host/service monitor in browser |
| 667 |
self.server.open_monitor(miserable_host, miserable_service) |
| 668 |
|
| 669 |
@action_response_decorator |
| 670 |
def action_recheck(self): |
| 671 |
# How many rows we have |
| 672 |
list_rows = [] |
| 673 |
for index in self.selectedIndexes(): |
| 674 |
if index.row() not in list_rows: |
| 675 |
list_rows.append(index.row()) |
| 676 |
|
| 677 |
for lrow in list_rows: |
| 678 |
miserable_host = self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole) |
| 679 |
miserable_service = self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole) |
| 680 |
|
| 681 |
# send signal to worker recheck slot |
| 682 |
self.recheck.emit({'host': miserable_host, |
| 683 |
'service': miserable_service}) |
| 684 |
|
| 685 |
@action_response_decorator |
| 686 |
def action_acknowledge(self): |
| 687 |
list_host = [] |
| 688 |
list_service = [] |
| 689 |
|
| 690 |
# How many rows we have |
| 691 |
list_rows = [] |
| 692 |
for index in self.selectedIndexes(): |
| 693 |
if index.row() not in list_rows: |
| 694 |
list_rows.append(index.row()) |
| 695 |
|
| 696 |
for lrow in list_rows: |
| 697 |
list_host.append(self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole)) |
| 698 |
list_service.append(self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole)) |
| 699 |
|
| 700 |
# running worker method is left to OK button of dialog |
| 701 |
self.action_acknowledge_triggered_initialize.emit(self.server, list_host, list_service) |
| 702 |
self.action_acknowledge_triggered_show.emit() |
| 703 |
|
| 704 |
@action_response_decorator |
| 705 |
def action_downtime(self): |
| 706 |
list_host = [] |
| 707 |
list_service = [] |
| 708 |
|
| 709 |
# How many rows we have |
| 710 |
list_rows = [] |
| 711 |
for index in self.selectedIndexes(): |
| 712 |
if index.row() not in list_rows: |
| 713 |
list_rows.append(index.row()) |
| 714 |
|
| 715 |
for lrow in list_rows: |
| 716 |
list_host.append(self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole)) |
| 717 |
list_service.append(self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole)) |
| 718 |
|
| 719 |
# running worker method is left to OK button of dialog |
| 720 |
self.action_downtime_triggered_initialize.emit(self.server, list_host, list_service) |
| 721 |
self.action_downtime_triggered_show.emit() |
| 722 |
|
| 723 |
@action_response_decorator |
| 724 |
def action_archive_event(self): |
| 725 |
""" |
| 726 |
archive events in Checkmk Multisite Event Console |
| 727 |
""" |
| 728 |
|
| 729 |
# fill action and info dict for thread-safe action request |
| 730 |
action = { |
| 731 |
'string': '$MONITOR$/view.py?_transid=$TRANSID$&_do_actions=yes&_do_confirm=Yes!&output_format=python&view_name=ec_events_of_monhost&host=$HOST$&_mkeventd_comment=archived&_mkeventd_acknowledge=on&_mkeventd_state=2&_delete_event=Archive Event&event_first_from=&event_first_until=&event_last_from=&event_last_until=', |
| 732 |
'type': 'url', 'recheck': True} |
| 733 |
|
| 734 |
list_host = [] |
| 735 |
list_service = [] |
| 736 |
list_status = [] |
| 737 |
|
| 738 |
# How many rows we have |
| 739 |
list_rows = [] |
| 740 |
indexes = self.selectedIndexes() |
| 741 |
for index in indexes: |
| 742 |
if index.row() not in list_rows: |
| 743 |
list_rows.append(index.row()) |
| 744 |
|
| 745 |
for lrow in list_rows: |
| 746 |
list_host.append(self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole)) |
| 747 |
list_service.append(self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole)) |
| 748 |
list_status.append(self.model().data(self.model().createIndex(lrow, 8), Qt.ItemDataRole.DisplayRole)) |
| 749 |
|
| 750 |
for line_number in range(len(list_host)): |
| 751 |
host = list_host[line_number] |
| 752 |
service = list_service[line_number] |
| 753 |
status = list_status[line_number] |
| 754 |
|
| 755 |
info = {'server': self.server.get_name(), |
| 756 |
'host': host, |
| 757 |
'service': service, |
| 758 |
'status-info': status, |
| 759 |
'address': self.server.get_host(host).result, |
| 760 |
'monitor': self.server.monitor_url, |
| 761 |
'monitor-cgi': self.server.monitor_cgi_url, |
| 762 |
'username': self.server.username, |
| 763 |
'password': self.server.password, |
| 764 |
'comment-ack': conf.defaults_acknowledge_comment, |
| 765 |
'comment-down': conf.defaults_downtime_comment, |
| 766 |
'comment-submit': conf.defaults_submit_check_result_comment |
| 767 |
} |
| 768 |
|
| 769 |
# tell worker to do the action |
| 770 |
self.request_action.emit(action, info) |
| 771 |
|
| 772 |
# clean up |
| 773 |
del index, indexes, list_rows, list_host, list_service, list_status |
| 774 |
|
| 775 |
@action_response_decorator |
| 776 |
def action_submit(self): |
| 777 |
# only on 1 row |
| 778 |
indexes = self.selectedIndexes() |
| 779 |
index = indexes[0] |
| 780 |
miserable_host = self.model().data(self.model().createIndex(index.row(), 0), Qt.ItemDataRole.DisplayRole) |
| 781 |
miserable_service = self.model().data(self.model().createIndex(index.row(), 2), Qt.ItemDataRole.DisplayRole) |
| 782 |
|
| 783 |
# running worker method is left to OK button of dialog |
| 784 |
self.action_submit_triggered_initialize.emit(self.server, miserable_host, miserable_service) |
| 785 |
self.action_submit_triggered_show.emit() |
| 786 |
|
| 787 |
@Slot() |
| 788 |
def action_clipboard_action_host(self): |
| 789 |
""" |
| 790 |
copy host name to clipboard |
| 791 |
""" |
| 792 |
list_host = [] |
| 793 |
text = '' |
| 794 |
|
| 795 |
# How many rows we have |
| 796 |
list_rows = [] |
| 797 |
for index in self.selectedIndexes(): |
| 798 |
if index.row() not in list_rows: |
| 799 |
list_rows.append(index.row()) |
| 800 |
|
| 801 |
for lrow in list_rows: |
| 802 |
list_host.append(self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole)) |
| 803 |
|
| 804 |
for line_number in range(len(list_host)): |
| 805 |
text = text + list_host[line_number] |
| 806 |
if line_number + 1 < len(list_host): |
| 807 |
text += '\n' |
| 808 |
|
| 809 |
clipboard.setText(text) |
| 810 |
|
| 811 |
@Slot() |
| 812 |
def action_clipboard_action_service(self): |
| 813 |
""" |
| 814 |
copy service name to clipboard |
| 815 |
""" |
| 816 |
list_service = [] |
| 817 |
text = '' |
| 818 |
|
| 819 |
# How many rows we have |
| 820 |
list_rows = [] |
| 821 |
for index in self.selectedIndexes(): |
| 822 |
if index.row() not in list_rows: |
| 823 |
list_rows.append(index.row()) |
| 824 |
|
| 825 |
for lrow in list_rows: |
| 826 |
list_service.append(self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole)) |
| 827 |
|
| 828 |
for line_number in range(len(list_service)): |
| 829 |
text = text + list_service[line_number] |
| 830 |
if line_number + 1 < len(list_service): |
| 831 |
text += '\n' |
| 832 |
|
| 833 |
clipboard.setText(text) |
| 834 |
|
| 835 |
@Slot() |
| 836 |
def action_clipboard_action_statusinformation(self): |
| 837 |
""" |
| 838 |
copy status information to clipboard |
| 839 |
""" |
| 840 |
list_status = [] |
| 841 |
text = '' |
| 842 |
|
| 843 |
# How many rows we have |
| 844 |
list_rows = [] |
| 845 |
for index in self.selectedIndexes(): |
| 846 |
if index.row() not in list_rows: |
| 847 |
list_rows.append(index.row()) |
| 848 |
|
| 849 |
for lrow in list_rows: |
| 850 |
list_status.append(self.model().data(self.model().createIndex(lrow, 8), Qt.ItemDataRole.DisplayRole)) |
| 851 |
|
| 852 |
for line_number in range(len(list_status)): |
| 853 |
text = text + list_status[line_number] |
| 854 |
if line_number + 1 < len(list_status): |
| 855 |
text += '\n' |
| 856 |
|
| 857 |
clipboard.setText(text) |
| 858 |
|
| 859 |
@Slot() |
| 860 |
def action_clipboard_action_all(self): |
| 861 |
""" |
| 862 |
copy all information to clipboard |
| 863 |
""" |
| 864 |
list_host = [] |
| 865 |
list_service = [] |
| 866 |
text = '' |
| 867 |
|
| 868 |
# How many rows we have |
| 869 |
list_rows = [] |
| 870 |
for index in self.selectedIndexes(): |
| 871 |
if index.row() not in list_rows: |
| 872 |
list_rows.append(index.row()) |
| 873 |
|
| 874 |
for lrow in list_rows: |
| 875 |
list_host.append(self.model().data(self.model().createIndex(lrow, 0), Qt.ItemDataRole.DisplayRole)) |
| 876 |
list_service.append(self.model().data(self.model().createIndex(lrow, 2), Qt.ItemDataRole.DisplayRole)) |
| 877 |
|
| 878 |
for line_number in range(len(list_host)): |
| 879 |
host = list_host[line_number] |
| 880 |
service = list_service[line_number] |
| 881 |
|
| 882 |
# item to access all properties of host/service object |
| 883 |
# defaults to host |
| 884 |
item = self.server.hosts[host] |
| 885 |
text += f'Host: {host}\n' |
| 886 |
# if it is a service switch to service object |
| 887 |
if service != '': |
| 888 |
if item.services.get(service): |
| 889 |
item = item.services[service] |
| 890 |
text += f'Service: {service}\n' |
| 891 |
# finally solve https://github.com/HenriWahl/Nagstamon/issues/1024 |
| 892 |
elif self.server.TYPE == 'Zabbix': |
| 893 |
for service_item in item.services.values(): |
| 894 |
if service_item.name == service: |
| 895 |
item = service_item |
| 896 |
text += f'Service: {service}\n' |
| 897 |
break |
| 898 |
|
| 899 |
# the other properties belong to both hosts and services |
| 900 |
text += 'Status: {0}\n'.format(item.status) |
| 901 |
text += 'Last check: {0}\n'.format(item.last_check) |
| 902 |
text += 'Duration: {0}\n'.format(item.duration) |
| 903 |
text += 'Attempt: {0}\n'.format(item.attempt) |
| 904 |
text += 'Status information: {0}\n'.format(item.status_information) |
| 905 |
if line_number + 1 < len(list_host): |
| 906 |
text += '\n' |
| 907 |
|
| 908 |
# copy text to clipboard |
| 909 |
clipboard.setText(text) |
| 910 |
|
| 911 |
@Slot() |
| 912 |
def refresh(self): |
| 913 |
""" |
| 914 |
refresh status display |
| 915 |
""" |
| 916 |
# avoid race condition when waiting for password dialog |
| 917 |
if self.parent_statuswindow is not None: |
| 918 |
# do nothing if window is moving to avoid lagging movement |
| 919 |
if not statuswindow_properties.moving: |
| 920 |
# tell statusbar it should update |
| 921 |
self.refreshed.emit() |
| 922 |
|
| 923 |
# check if status changed and notification is necessary |
| 924 |
# send signal because there are unseen events |
| 925 |
# status has changed if there are unseen events in the list OR (current status is up AND has been changed since last time) |
| 926 |
if (self.server.get_events_history_count() > 0) or \ |
| 927 |
((self.server.worst_status_current == 'UP') and ( |
| 928 |
self.server.worst_status_current != self.server.worst_status_last)): |
| 929 |
self.status_changed.emit(self.server.name, self.server.worst_status_diff, |
| 930 |
self.server.worst_status_current) |
| 931 |
|
| 932 |
@Slot(int, Qt.SortOrder) |
| 933 |
def sort_columns(self, sort_column, sort_order): |
| 934 |
""" |
| 935 |
forward sorting task to worker |
| 936 |
""" |
| 937 |
# better int() the Qt.* values because they partly seem to be |
| 938 |
# intransmissible |
| 939 |
# get_sort_order_value() cures the differences between Qt5 and Qt6 |
| 940 |
self.sort_data_array_for_columns.emit(int(sort_column), int(get_sort_order_value(sort_order)), True) |
| 941 |
|
| 942 |
@Slot() |
| 943 |
def finish_worker_thread(self): |
| 944 |
""" |
| 945 |
attempt to shut down thread cleanly |
| 946 |
""" |
| 947 |
# tell thread to quit |
| 948 |
self.worker_thread.quit() |
| 949 |
# wait until thread is really stopped |
| 950 |
self.worker_thread.wait() |
| 951 |
|
| 952 |
class Worker(QObject): |
| 953 |
""" |
| 954 |
attempt to run a server status update thread - only needed by table so it is defined here inside table |
| 955 |
""" |
| 956 |
|
| 957 |
# send signal if monitor server has new status data |
| 958 |
new_status = Signal() |
| 959 |
get_status_successful = Signal(str) |
| 960 |
|
| 961 |
# send signal if next cell can be filled |
| 962 |
next_cell = Signal(int, int, str, str, str, list, str) |
| 963 |
|
| 964 |
# send signal if all cells are filled and table can be adjusted |
| 965 |
table_ready = Signal() |
| 966 |
|
| 967 |
# send signal if ready to stop |
| 968 |
finish = Signal() |
| 969 |
|
| 970 |
# send start and end of downtime |
| 971 |
set_start_end = Signal(str, str) |
| 972 |
|
| 973 |
# try to stop thread by evaluating this flag |
| 974 |
running = True |
| 975 |
|
| 976 |
# signal to be sent to slot "change" of ServerStatusLabel |
| 977 |
change_label_status = Signal(str, str) |
| 978 |
|
| 979 |
# signal to be sent to slot "restore" of ServerStatusLabel |
| 980 |
restore_label_status = Signal() |
| 981 |
|
| 982 |
# send notification a stop message if problems vanished without being noticed |
| 983 |
problems_vanished = Signal() |
| 984 |
|
| 985 |
# flag to keep recheck_all from being started more than once |
| 986 |
rechecking_all = False |
| 987 |
|
| 988 |
# signals to control error message in statusbar |
| 989 |
show_error = Signal(str) |
| 990 |
hide_error = Signal() |
| 991 |
|
| 992 |
# signal to request authentication |
| 993 |
authentication_needed = Signal() |
| 994 |
|
| 995 |
# sent to treeview with new data_array |
| 996 |
worker_data_array_filled = Signal(list, dict) |
| 997 |
|
| 998 |
# sendt to treeview if data has been sorted by click on column header |
| 999 |
data_array_sorted = Signal(list, dict) |
| 1000 |
|
| 1001 |
# keep track of last sorting column and order to pre-sort by it |
| 1002 |
# start with sorting by host |
| 1003 |
last_sort_column_cached = 0 |
| 1004 |
last_sort_column_real = 0 |
| 1005 |
last_sort_order = 0 |
| 1006 |
|
| 1007 |
# keep track of action menu being shown or not to avoid refresh while selecting multiple items |
| 1008 |
# action_menu_shown = False |
| 1009 |
|
| 1010 |
def __init__(self, parent=None, server=None, sort_column=0, sort_order=0, status_window=None): |
| 1011 |
QObject.__init__(self) |
| 1012 |
self.server = server |
| 1013 |
# needed for update interval |
| 1014 |
self.timer = QTimer(self) |
| 1015 |
self.server.init_config() |
| 1016 |
|
| 1017 |
self.sort_column = sort_column |
| 1018 |
self.sort_order = sort_order |
| 1019 |
|
| 1020 |
self.parent_statuswindow = status_window |
| 1021 |
|
| 1022 |
self.get_status_successful.connect(self.parent_statuswindow.injected_dialogs.weblogin.close_browser) |
| 1023 |
|
| 1024 |
@Slot() |
| 1025 |
def get_status(self): |
| 1026 |
""" |
| 1027 |
check every second if thread still has to run |
| 1028 |
if interval time is reached get status |
| 1029 |
""" |
| 1030 |
# if counter is at least update interval get status or the weblogin dialog triggered this |
| 1031 |
if self.server.thread_counter >= conf.update_interval_seconds or \ |
| 1032 |
type(self.sender()) is DialogWebLogin: |
| 1033 |
# only if no multiple selection is done at the moment and no context action menu is open |
| 1034 |
if not app.keyboardModifiers() and app.activePopupWidget() is None: |
| 1035 |
# reflect status retrieval attempt on server vbox label |
| 1036 |
self.change_label_status.emit('Refreshing...', '') |
| 1037 |
|
| 1038 |
status = self.server.get_status() |
| 1039 |
|
| 1040 |
# all is OK if no error info came back |
| 1041 |
if self.server.status_description == '' and \ |
| 1042 |
self.server.status_code < 400 and \ |
| 1043 |
not self.server.refresh_authentication and \ |
| 1044 |
not self.server.tls_error: |
| 1045 |
# show last update time |
| 1046 |
self.change_label_status.emit(f"Last updated at {datetime.now().strftime('%X')}", '') |
| 1047 |
|
| 1048 |
self.get_status_successful.emit(self.server.name) |
| 1049 |
|
| 1050 |
# reset server error flag, needed for error label in statusbar |
| 1051 |
self.server.has_error = False |
| 1052 |
|
| 1053 |
# tell statusbar there is no error |
| 1054 |
self.hide_error.emit() |
| 1055 |
else: |
| 1056 |
# try to display some more user-friendly error description |
| 1057 |
if self.server.status_code == 404: |
| 1058 |
self.change_label_status.emit('Monitor URL not valid', 'critical') |
| 1059 |
elif status.error.startswith('requests.exceptions.ConnectTimeout'): |
| 1060 |
self.change_label_status.emit('Connection timeout', 'error') |
| 1061 |
elif status.error.startswith('requests.exceptions.ConnectionError'): |
| 1062 |
self.change_label_status.emit('Connection error', 'error') |
| 1063 |
elif status.error.startswith('requests.exceptions.ReadTimeout'): |
| 1064 |
self.change_label_status.emit('Connection timeout', 'error') |
| 1065 |
elif status.error.startswith('requests.exceptions.ProxyError'): |
| 1066 |
self.change_label_status.emit('Proxy error', 'error') |
| 1067 |
elif status.error.startswith('requests.exceptions.MaxRetryError'): |
| 1068 |
self.change_label_status.emit('Max retry error', 'error') |
| 1069 |
elif self.server.tls_error: |
| 1070 |
self.change_label_status.emit('SSL/TLS problem', 'critical') |
| 1071 |
elif self.server.status_code in self.server.STATUS_CODES_NO_AUTH or \ |
| 1072 |
self.server.refresh_authentication: |
| 1073 |
self.change_label_status.emit('Authentication problem', 'critical') |
| 1074 |
self.authentication_needed.emit() |
| 1075 |
elif self.server.status_code == 503: |
| 1076 |
self.change_label_status.emit('Service unavailable', 'error') |
| 1077 |
else: |
| 1078 |
# kick out line breaks to avoid broken status window |
| 1079 |
if self.server.status_description == '': |
| 1080 |
self.server.status_description = 'Unknown error' |
| 1081 |
self.change_label_status.emit(self.server.status_description.replace('\n', ''), 'error') |
| 1082 |
|
| 1083 |
# set server error flag, needed for error label in statusbar |
| 1084 |
self.server.has_error = True |
| 1085 |
|
| 1086 |
# tell statusbar there is some error to display |
| 1087 |
self.show_error.emit('ERROR') |
| 1088 |
|
| 1089 |
# reset counter for this thread |
| 1090 |
self.server.thread_counter = 0 |
| 1091 |
|
| 1092 |
# if failures have gone and nobody took notice switch notification off again |
| 1093 |
if len([k for k, v in self.server.events_history.items() if v is True]) == 0 and \ |
| 1094 |
self.parent_statuswindow and \ |
| 1095 |
statuswindow_properties.is_notifying is True and \ |
| 1096 |
statuswindow_properties.notifying_server == self.server.name: |
| 1097 |
# tell notification that unnoticed problems are gone |
| 1098 |
self.problems_vanished.emit() |
| 1099 |
|
| 1100 |
# stuff data into array and sort it |
| 1101 |
self.fill_data_array(self.sort_column, self.sort_order) |
| 1102 |
|
| 1103 |
# tell news about new status available |
| 1104 |
self.new_status.emit() |
| 1105 |
|
| 1106 |
# increase thread counter |
| 1107 |
self.server.thread_counter += 1 |
| 1108 |
|
| 1109 |
# if running flag is still set call myself after 1 second |
| 1110 |
if self.running: |
| 1111 |
self.timer.singleShot(1000, self.get_status) |
| 1112 |
else: |
| 1113 |
# tell treeview to finish worker_thread |
| 1114 |
self.finish.emit() |
| 1115 |
|
| 1116 |
@Slot(int, int) |
| 1117 |
def fill_data_array(self, sort_column, sort_order): |
| 1118 |
""" |
| 1119 |
let worker do the dirty job of filling the array |
| 1120 |
""" |
| 1121 |
|
| 1122 |
# data_array to be evaluated in data() of model |
| 1123 |
# first 9 items per row come from current status information |
| 1124 |
self.data_array = list() |
| 1125 |
|
| 1126 |
# dictionary containing extra info about data_array |
| 1127 |
self.info = {'hosts_flags_column_needed': False, |
| 1128 |
'services_flags_column_needed': False, } |
| 1129 |
|
| 1130 |
# only refresh table if there is no popup opened |
| 1131 |
if not app.activePopupWidget(): |
| 1132 |
# avoid race condition when waiting for password dialog |
| 1133 |
if len(qbrushes[0]) > 0: |
| 1134 |
# cruising the whole nagitems structure |
| 1135 |
for category in ('hosts', 'services'): |
| 1136 |
for state in self.server.nagitems_filtered[category].values(): |
| 1137 |
for item in state: |
| 1138 |
self.data_array.append(list(item.get_columns(HEADERS))) |
| 1139 |
|
| 1140 |
# hash for freshness comparison |
| 1141 |
hash = item.get_hash() |
| 1142 |
|
| 1143 |
if item.is_host(): |
| 1144 |
if hash in self.server.events_history and \ |
| 1145 |
self.server.events_history[hash] is True: |
| 1146 |
# second item in last data_array line is host flags |
| 1147 |
self.data_array[-1][1] += 'N' |
| 1148 |
else: |
| 1149 |
if hash in self.server.events_history and \ |
| 1150 |
self.server.events_history[hash] is True: |
| 1151 |
# fourth item in last data_array line is service flags |
| 1152 |
self.data_array[-1][3] += 'N' |
| 1153 |
# add text color as QBrush from status |
| 1154 |
self.data_array[-1].append( |
| 1155 |
qbrushes[len(self.data_array) % 2][COLORS[item.status] + 'text']) |
| 1156 |
# add background color as QBrush from status |
| 1157 |
self.data_array[-1].append( |
| 1158 |
qbrushes[len(self.data_array) % 2][COLORS[item.status] + 'background']) |
| 1159 |
# add text color name for sorting data |
| 1160 |
self.data_array[-1].append(COLORS[item.status] + 'text') |
| 1161 |
# add background color name for sorting data |
| 1162 |
self.data_array[-1].append(COLORS[item.status] + 'background') |
| 1163 |
|
| 1164 |
# check if hosts and services flags should be shown |
| 1165 |
if self.data_array[-1][1] != '': |
| 1166 |
self.info['hosts_flags_column_needed'] = True |
| 1167 |
if self.data_array[-1][3] != '': |
| 1168 |
self.info['services_flags_column_needed'] = True |
| 1169 |
|
| 1170 |
self.data_array[-1].append('X') |
| 1171 |
|
| 1172 |
# sort data before it gets transmitted to treeview model |
| 1173 |
self.sort_data_array(self.sort_column, self.sort_order, False) |
| 1174 |
|
| 1175 |
# give sorted data to model |
| 1176 |
self.worker_data_array_filled.emit(self.data_array, self.info) |
| 1177 |
|
| 1178 |
@Slot(int, int, bool) |
| 1179 |
def sort_data_array(self, sort_column, sort_order, header_clicked=False): |
| 1180 |
""" |
| 1181 |
sort list of lists in data_array depending on sort criteria |
| 1182 |
used from fill_data_array() and when clicked on table headers |
| 1183 |
""" |
| 1184 |
# store current sort_column and sort_data for next sort actions |
| 1185 |
self.sort_column = sort_column |
| 1186 |
self.sort_order = sort_order |
| 1187 |
|
| 1188 |
# to keep GTK Treeview sort behaviour first by hosts |
| 1189 |
first_sort = sorted(self.data_array, |
| 1190 |
key=lambda row: SORT_COLUMNS_FUNCTIONS[self.last_sort_column_real]( |
| 1191 |
row[SORT_COLUMNS_INDEX[self.last_sort_column_real]]), |
| 1192 |
reverse=self.last_sort_order) |
| 1193 |
|
| 1194 |
# use SORT_COLUMNS from Helpers to sort column accordingly |
| 1195 |
self.data_array = sorted(first_sort, |
| 1196 |
key=lambda row: SORT_COLUMNS_FUNCTIONS[self.sort_column]( |
| 1197 |
row[SORT_COLUMNS_INDEX[self.sort_column]]), |
| 1198 |
reverse=self.sort_order) |
| 1199 |
|
| 1200 |
# fix alternating colors |
| 1201 |
for count, row in enumerate(self.data_array): |
| 1202 |
# change text color of sorted rows |
| 1203 |
row[10] = qbrushes[count % 2][row[12]] |
| 1204 |
# change background color of sorted rows |
| 1205 |
row[11] = qbrushes[count % 2][row[13]] |
| 1206 |
|
| 1207 |
# if header was clicked tell model to use new data_array |
| 1208 |
if header_clicked: |
| 1209 |
self.data_array_sorted.emit(self.data_array, self.info) |
| 1210 |
|
| 1211 |
# store last sorting column for next sorting only if header was clicked |
| 1212 |
if header_clicked: |
| 1213 |
# last sorting column needs to be cached to avoid losing it |
| 1214 |
# effective last column is self.last_sort_column_real |
| 1215 |
if self.last_sort_column_cached != self.sort_column: |
| 1216 |
self.last_sort_column_real = self.last_sort_column_cached |
| 1217 |
self.last_sort_order = self.sort_order |
| 1218 |
|
| 1219 |
self.last_sort_column_cached = self.sort_column |
| 1220 |
|
| 1221 |
@Slot(dict) |
| 1222 |
def acknowledge(self, info_dict): |
| 1223 |
""" |
| 1224 |
slot waiting for 'acknowledge' signal from ok button from acknowledge dialog |
| 1225 |
all information about target server, host, service and flags is contained |
| 1226 |
in dictionary 'info_dict' |
| 1227 |
""" |
| 1228 |
# because all monitors are connected to this slot we must check which one sent the signal, |
| 1229 |
# otherwise there are several calls and not only one as wanted |
| 1230 |
if self.server == info_dict['server']: |
| 1231 |
# pass dictionary to server's acknowledge machinery |
| 1232 |
self.server.set_acknowledge(info_dict) |
| 1233 |
|
| 1234 |
@Slot(dict) |
| 1235 |
def downtime(self, info_dict): |
| 1236 |
""" |
| 1237 |
slot waiting for 'downtime' signal from ok button from downtime dialog |
| 1238 |
all information about target server, host, service and flags is contained |
| 1239 |
in dictionary 'info_dict' |
| 1240 |
""" |
| 1241 |
# because all monitors are connected to this slot we must check which one sent the signal, |
| 1242 |
# otherwise there are several calls and not only one as wanted |
| 1243 |
if self.server == info_dict['server']: |
| 1244 |
# pass dictionary to server's downtime machinery |
| 1245 |
self.server.set_downtime(info_dict) |
| 1246 |
|
| 1247 |
@Slot(dict) |
| 1248 |
def submit(self, info_dict): |
| 1249 |
""" |
| 1250 |
slot waiting for 'submit' signal from ok button from submit dialog |
| 1251 |
all information about target server, host, service and flags is contained |
| 1252 |
in dictionary 'info_dict' |
| 1253 |
""" |
| 1254 |
# because all monitors are connected to this slot we must check which one sent the signal, |
| 1255 |
# otherwise there are several calls and not only one as wanted |
| 1256 |
if self.server == info_dict['server']: |
| 1257 |
# pass dictionary to server's downtime machinery |
| 1258 |
self.server.set_submit_check_result(info_dict) |
| 1259 |
|
| 1260 |
@Slot(dict) |
| 1261 |
def recheck(self, info_dict): |
| 1262 |
""" |
| 1263 |
Slot to start server recheck method, getting signal from TableWidget context menu |
| 1264 |
""" |
| 1265 |
if conf.debug_mode: |
| 1266 |
# host |
| 1267 |
if info_dict['service'] == '': |
| 1268 |
self.server.debug(server=self.server.name, debug='Rechecking host {0}'.format(info_dict['host'])) |
| 1269 |
else: |
| 1270 |
self.server.debug(server=self.server.name, |
| 1271 |
debug='Rechecking service {0} on host {1}'.format(info_dict['service'], |
| 1272 |
info_dict['host'])) |
| 1273 |
|
| 1274 |
# call server recheck method |
| 1275 |
self.server.set_recheck(info_dict) |
| 1276 |
|
| 1277 |
@Slot() |
| 1278 |
def recheck_all(self): |
| 1279 |
""" |
| 1280 |
call server.set_recheck for every single host/service |
| 1281 |
""" |
| 1282 |
# only if no already rechecking |
| 1283 |
if self.rechecking_all is False: |
| 1284 |
# block rechecking |
| 1285 |
self.rechecking_all = True |
| 1286 |
# change label of server vbox |
| 1287 |
self.change_label_status.emit('Rechecking all...', '') |
| 1288 |
if conf.debug_mode: |
| 1289 |
self.server.debug(server=self.server.name, debug='Start rechecking all') |
| 1290 |
# make a copy to preserve hosts/service to recheck - just in case something changes meanwhile |
| 1291 |
nagitems_filtered = deepcopy(self.server.nagitems_filtered) |
| 1292 |
for status in nagitems_filtered['hosts'].items(): |
| 1293 |
for host in status[1]: |
| 1294 |
if conf.debug_mode: |
| 1295 |
self.server.debug(server=self.server.name, |
| 1296 |
debug='Rechecking host {0}'.format(host.name)) |
| 1297 |
# call server recheck method |
| 1298 |
self.server.set_recheck({'host': host.name, 'service': ''}) |
| 1299 |
for status in nagitems_filtered['services'].items(): |
| 1300 |
for service in status[1]: |
| 1301 |
if conf.debug_mode: |
| 1302 |
self.server.debug(server=self.server.name, |
| 1303 |
debug='Rechecking service {0} on host {1}'.format( |
| 1304 |
service.get_service_name(), |
| 1305 |
service.host)) |
| 1306 |
# call server recheck method |
| 1307 |
self.server.set_recheck({'host': service.host, 'service': service.name}) |
| 1308 |
del nagitems_filtered, status |
| 1309 |
# release rechecking lock |
| 1310 |
self.rechecking_all = False |
| 1311 |
# restore server status label |
| 1312 |
self.restore_label_status.emit() |
| 1313 |
else: |
| 1314 |
if conf.debug_mode: |
| 1315 |
self.server.debug(server=self.server.name, debug='Already rechecking all') |
| 1316 |
|
| 1317 |
@Slot(str, str) |
| 1318 |
def get_start_end(self, server_name, host): |
| 1319 |
""" |
| 1320 |
Investigates start and end time of a downtime asynchronously |
| 1321 |
""" |
| 1322 |
# because every server listens to this signal the name has to be filtered |
| 1323 |
if server_name == self.server.name: |
| 1324 |
start, end = self.server.get_start_end(host) |
| 1325 |
# send start/end time to slot |
| 1326 |
self.set_start_end.emit(start, end) |
| 1327 |
|
| 1328 |
@Slot(dict, dict) |
| 1329 |
def execute_action(self, action, info): |
| 1330 |
""" |
| 1331 |
runs action, may it be custom or included like the Checkmk Multisite actions |
| 1332 |
""" |
| 1333 |
# first replace placeholder variables in string with actual values |
| 1334 |
# |
| 1335 |
# Possible values for variables: |
| 1336 |
# $HOST$ - host as in monitor |
| 1337 |
# $SERVICE$ - service as in monitor |
| 1338 |
# $MONITOR$ - monitor address - not yet clear what exactly for |
| 1339 |
# $MONITOR-CGI$ - monitor CGI address - not yet clear what exactly for |
| 1340 |
# $ADDRESS$ - address of host, investigated by Server.GetHost() |
| 1341 |
# $STATUS-INFO$ - status information |
| 1342 |
# $USERNAME$ - username on monitor |
| 1343 |
# $PASSWORD$ - username's password on monitor - whatever for |
| 1344 |
# $COMMENT-ACK$ - default acknowledge comment |
| 1345 |
# $COMMENT-DOWN$ - default downtime comment |
| 1346 |
# $COMMENT-SUBMIT$ - default submit check result comment |
| 1347 |
|
| 1348 |
try: |
| 1349 |
# used for POST request |
| 1350 |
if 'cgi_data' in action: |
| 1351 |
cgi_data = action['cgi_data'] |
| 1352 |
else: |
| 1353 |
cgi_data = '' |
| 1354 |
|
| 1355 |
# mapping of variables and values |
| 1356 |
mapping = {'$HOST$': info['host'], |
| 1357 |
'$SERVICE$': info['service'], |
| 1358 |
'$ADDRESS$': info['address'], |
| 1359 |
'$MONITOR$': info['monitor'], |
| 1360 |
'$MONITOR-CGI$': info['monitor-cgi'], |
| 1361 |
'$STATUS-INFO$': info['status-info'], |
| 1362 |
'$USERNAME$': info['username'], |
| 1363 |
'$PASSWORD$': info['password'], |
| 1364 |
'$COMMENT-ACK$': info['comment-ack'], |
| 1365 |
'$COMMENT-DOWN$': info['comment-down'], |
| 1366 |
'$COMMENT-SUBMIT$': info['comment-submit']} |
| 1367 |
|
| 1368 |
# take string form action |
| 1369 |
string = action['string'] |
| 1370 |
|
| 1371 |
# mapping mapping |
| 1372 |
for i in mapping: |
| 1373 |
# mapping with urllib.quote |
| 1374 |
string = string.replace("$" + i + "$", quote(mapping[i])) |
| 1375 |
# normal mapping |
| 1376 |
string = string.replace(i, mapping[i]) |
| 1377 |
|
| 1378 |
# see what action to take |
| 1379 |
if action['type'] == 'browser': |
| 1380 |
# debug |
| 1381 |
if conf.debug_mode is True: |
| 1382 |
self.server.debug(server=self.server.name, host=info['host'], service=info['service'], |
| 1383 |
debug='ACTION: BROWSER ' + string) |
| 1384 |
webbrowser_open(string) |
| 1385 |
elif action['type'] == 'command': |
| 1386 |
# debug |
| 1387 |
if conf.debug_mode is True: |
| 1388 |
self.server.debug(server=self.server.name, host=info['host'], service=info['service'], |
| 1389 |
debug='ACTION: COMMAND ' + string) |
| 1390 |
Popen(string, shell=True) |
| 1391 |
elif action['type'] == 'url': |
| 1392 |
# Checkmk uses transids - if this occurs in URL its very likely that a Checkmk-URL is called |
| 1393 |
if '$TRANSID$' in string: |
| 1394 |
transid = servers[info['server']]._get_transid(info['host'], info['service']) |
| 1395 |
string = string.replace('$TRANSID$', transid).replace(' ', '+') |
| 1396 |
else: |
| 1397 |
# make string ready for URL |
| 1398 |
string = urlify(string) |
| 1399 |
# debug |
| 1400 |
if conf.debug_mode is True: |
| 1401 |
self.server.debug(server=self.server.name, host=info['host'], service=info['service'], |
| 1402 |
debug='ACTION: URL in background ' + string) |
| 1403 |
servers[info['server']].fetch_url(string) |
| 1404 |
# used for example by Op5Monitor.py |
| 1405 |
elif action['type'] == 'url-post': |
| 1406 |
# make string ready for URL |
| 1407 |
string = urlify(string) |
| 1408 |
# debug |
| 1409 |
if conf.debug_mode is True: |
| 1410 |
self.server.debug(server=self.server.name, host=info['host'], service=info['service'], |
| 1411 |
debug='ACTION: URL-POST in background ' + string) |
| 1412 |
servers[info['server']].fetch_url(string, cgi_data=cgi_data, multipart=True) |
| 1413 |
|
| 1414 |
if action['recheck']: |
| 1415 |
self.recheck(info) |
| 1416 |
|
| 1417 |
except Exception: |
| 1418 |
print_exc(file=stdout) |
| 1419 |
|
| 1420 |
@Slot() |
| 1421 |
def unfresh_event_history(self): |
| 1422 |
# set all flagged-as-fresh-events to un-fresh |
| 1423 |
for event in self.server.events_history.keys(): |
| 1424 |
self.server.events_history[event] = False |