| 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 |
|
| 18 |
from os import sep |
| 19 |
|
| 20 |
from collections import OrderedDict |
| 21 |
|
| 22 |
from Nagstamon.config import (conf, |
| 23 |
RESOURCES) |
| 24 |
from Nagstamon.qui.constants import (COLORS, |
| 25 |
COLOR_STATE_NAMES) |
| 26 |
from Nagstamon.qui.globals import statuswindow_properties, font |
| 27 |
from Nagstamon.qui.qt import (QSizePolicy, |
| 28 |
QTimer, |
| 29 |
QWidget, |
| 30 |
Signal, |
| 31 |
Slot) |
| 32 |
from Nagstamon.qui.widgets.draggables import DraggableLabel |
| 33 |
from Nagstamon.qui.widgets.layout import HBoxLayout |
| 34 |
from Nagstamon.qui.widgets.nagstamon_logo import NagstamonLogo |
| 35 |
from Nagstamon.servers import servers, get_errors |
| 36 |
|
| 37 |
|
| 38 |
class StatusBarLabel(DraggableLabel): |
| 39 |
""" |
| 40 |
one piece of the status bar labels for one state |
| 41 |
""" |
| 42 |
|
| 43 |
# yell if statusbar is moved |
| 44 |
window_moved = Signal() |
| 45 |
|
| 46 |
# needed for popup after hover |
| 47 |
mouse_entered = Signal() |
| 48 |
|
| 49 |
# needed for popup after click |
| 50 |
mouse_pressed = Signal() |
| 51 |
mouse_released = Signal() |
| 52 |
# needed to close window in some configurations |
| 53 |
mouse_released_in_window = Signal() |
| 54 |
|
| 55 |
def __init__(self, state, parent=None): |
| 56 |
DraggableLabel.__init__(self, parent=parent) |
| 57 |
self.setStyleSheet(f'''padding-left: 1px; |
| 58 |
padding-right: 1px; |
| 59 |
color: {conf.__dict__[f'color_{state.lower()}_text']}; |
| 60 |
background-color: {conf.__dict__[f'color_{state.lower()}_background']}; |
| 61 |
''') |
| 62 |
# just let labels grow as much as they need |
| 63 |
self.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum) |
| 64 |
|
| 65 |
# hidden per default |
| 66 |
self.hide() |
| 67 |
|
| 68 |
# default text - only useful in case of OK Label |
| 69 |
self.setText(state) |
| 70 |
|
| 71 |
# number of hosts/services of this state |
| 72 |
self.number = 0 |
| 73 |
|
| 74 |
# store state of label to access long state names in .summarize_states() |
| 75 |
self.state = state |
| 76 |
|
| 77 |
@Slot() |
| 78 |
def invert(self): |
| 79 |
self.setStyleSheet(f'''padding-left: 1px; |
| 80 |
padding-right: 1px; |
| 81 |
color: {conf.__dict__[f'color_{self.state.lower()}_background']}; |
| 82 |
background-color: {conf.__dict__[f'color_{self.state.lower()}_text']}; |
| 83 |
''') |
| 84 |
|
| 85 |
@Slot() |
| 86 |
def reset(self): |
| 87 |
self.setStyleSheet(f'''padding-left: 1px; |
| 88 |
padding-right: 1px; |
| 89 |
color: {conf.__dict__[f'color_{self.state.lower()}_text']}; |
| 90 |
background-color: {conf.__dict__[f'color_{self.state.lower()}_background']}; |
| 91 |
''') |
| 92 |
|
| 93 |
|
| 94 |
class StatusBar(QWidget): |
| 95 |
""" |
| 96 |
status bar for short display of problems |
| 97 |
""" |
| 98 |
|
| 99 |
# send signal to statuswindow |
| 100 |
resize = Signal() |
| 101 |
|
| 102 |
# needed to maintain flashing labels |
| 103 |
labels_invert = Signal() |
| 104 |
labels_reset = Signal() |
| 105 |
|
| 106 |
def __init__(self, parent=None): |
| 107 |
QWidget.__init__(self, parent=parent) |
| 108 |
|
| 109 |
self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed) |
| 110 |
|
| 111 |
self.hbox = HBoxLayout(spacing=0, parent=parent) |
| 112 |
self.setLayout(self.hbox) |
| 113 |
|
| 114 |
# define labels first to get their size for svg logo dimensions |
| 115 |
self.color_labels = OrderedDict() |
| 116 |
self.color_labels['OK'] = StatusBarLabel('OK', parent=parent) |
| 117 |
|
| 118 |
for state in COLORS: |
| 119 |
self.color_labels[state] = StatusBarLabel(state, parent=parent) |
| 120 |
self.labels_invert.connect(self.color_labels[state].invert) |
| 121 |
self.labels_reset.connect(self.color_labels[state].reset) |
| 122 |
|
| 123 |
# label for error message(s) |
| 124 |
self.label_message = StatusBarLabel('error', parent=parent) |
| 125 |
self.labels_invert.connect(self.label_message.invert) |
| 126 |
self.labels_reset.connect(self.label_message.reset) |
| 127 |
|
| 128 |
# derive logo dimensions from status label |
| 129 |
self.logo = NagstamonLogo(f'{RESOURCES}{sep}nagstamon_logo_bar.svg', |
| 130 |
self.color_labels['OK'].fontMetrics().height(), |
| 131 |
self.color_labels['OK'].fontMetrics().height(), |
| 132 |
parent=parent) |
| 133 |
|
| 134 |
# add logo |
| 135 |
self.hbox.addWidget(self.logo) |
| 136 |
|
| 137 |
# label for error messages |
| 138 |
self.hbox.addWidget(self.label_message) |
| 139 |
self.label_message.hide() |
| 140 |
|
| 141 |
# add state labels |
| 142 |
self.hbox.addWidget(self.color_labels['OK']) |
| 143 |
for state in COLORS: |
| 144 |
self.hbox.addWidget(self.color_labels[state]) |
| 145 |
|
| 146 |
# timer for singleshots for flashing |
| 147 |
self.timer = QTimer() |
| 148 |
|
| 149 |
self.adjust_size() |
| 150 |
|
| 151 |
@Slot() |
| 152 |
def summarize_states(self): |
| 153 |
""" |
| 154 |
display summaries of states in statusbar |
| 155 |
""" |
| 156 |
# initial zeros |
| 157 |
for label in self.color_labels.values(): |
| 158 |
label.number = 0 |
| 159 |
|
| 160 |
# only count numbers of enabled monitor servers |
| 161 |
for server in (filter(lambda s: s.enabled, servers.values())): |
| 162 |
for state in COLORS: |
| 163 |
self.color_labels[state].number += server.__dict__[state.lower()] |
| 164 |
|
| 165 |
# summarize all numbers - if all_numbers keeps 0 everything seems to be OK |
| 166 |
all_numbers = 0 |
| 167 |
|
| 168 |
# repaint colored labels or hide them if necessary |
| 169 |
for label in self.color_labels.values(): |
| 170 |
if label.number == 0: |
| 171 |
label.hide() |
| 172 |
else: |
| 173 |
label.setText(' '.join((str(label.number), |
| 174 |
COLOR_STATE_NAMES[label.state][conf.long_display]))) |
| 175 |
label.show() |
| 176 |
label.adjustSize() |
| 177 |
all_numbers += label.number |
| 178 |
|
| 179 |
if all_numbers == 0 and not get_errors() and not self.label_message.isVisible(): |
| 180 |
self.color_labels['OK'].show() |
| 181 |
self.color_labels['OK'].adjustSize() |
| 182 |
else: |
| 183 |
self.color_labels['OK'].hide() |
| 184 |
|
| 185 |
# fix size after refresh - better done here to avoid ugly artefacts |
| 186 |
hint = self.sizeHint() |
| 187 |
self.setMaximumSize(hint) |
| 188 |
self.setMinimumSize(hint) |
| 189 |
del hint |
| 190 |
# tell statuswindow its size might be adjusted |
| 191 |
self.resize.emit() |
| 192 |
|
| 193 |
@Slot() |
| 194 |
def flash(self): |
| 195 |
""" |
| 196 |
send color inversion signal to labels |
| 197 |
""" |
| 198 |
# only if currently a notification is necessary |
| 199 |
if statuswindow_properties.is_notifying: |
| 200 |
self.labels_invert.emit() |
| 201 |
# fire up a singleshot to reset color soon |
| 202 |
self.timer.singleShot(500, self.reset) |
| 203 |
|
| 204 |
@Slot() |
| 205 |
def reset(self): |
| 206 |
""" |
| 207 |
tell labels to set original colors |
| 208 |
""" |
| 209 |
self.labels_reset.emit() |
| 210 |
# only if currently a notification is necessary |
| 211 |
if statuswindow_properties.is_notifying: |
| 212 |
# even later call itself to invert colors as flash |
| 213 |
self.timer.singleShot(500, self.flash) |
| 214 |
|
| 215 |
@Slot() |
| 216 |
def adjust_size(self): |
| 217 |
""" |
| 218 |
apply new size of widgets, especially Nagstamon logo |
| 219 |
run through all labels to the the max height in case not all labels |
| 220 |
are shown at the same time - which is very likely the case |
| 221 |
""" |
| 222 |
# take height for logo |
| 223 |
# height = 0 |
| 224 |
|
| 225 |
# run through labels to set font and get height for logo |
| 226 |
for label in self.color_labels.values(): |
| 227 |
label.setFont(font) |
| 228 |
# if label.fontMetrics().height() > height: |
| 229 |
# height = label.fontMetrics().height() |
| 230 |
|
| 231 |
self.label_message.setFont(font) |
| 232 |
height = self.label_message.sizeHint().height() |
| 233 |
|
| 234 |
# adjust logo size to fit to label size - due to being a square height and width are the same |
| 235 |
self.logo.adjust_size(height, height) |
| 236 |
|
| 237 |
# avoid flickering/artefact by updating immediately |
| 238 |
self.summarize_states() |
| 239 |
|
| 240 |
@Slot(str) |
| 241 |
def set_error(self, message): |
| 242 |
""" |
| 243 |
display error message if any error exists |
| 244 |
""" |
| 245 |
self.label_message.setText(message) |
| 246 |
self.label_message.show() |
| 247 |
|
| 248 |
@Slot() |
| 249 |
def reset_error(self): |
| 250 |
""" |
| 251 |
delete error message if there is no error |
| 252 |
""" |
| 253 |
if not get_errors(): |
| 254 |
self.label_message.setText('') |
| 255 |
self.label_message.hide() |