def get_item_text(item): """ Return item text depends if it's a host or service :param item: host or service item :type item: alignak_app.items.host.Host | alignak_app.items.service.Service :return: text of item :rtype: str """ hostname = '' if 'host' in item.data: host = data_manager.get_item('host', '_id', item.data['host']) if host: hostname = host.get_display_name() service_name = item.get_display_name() else: hostname = item.get_display_name() service_name = '' if service_name: text = _('%s is %s (Attached to %s)') % (service_name, item.data['ls_state'], hostname) else: text = _('%s is %s') % (hostname, item.data['ls_state']) return text
def add_spy_host(self, host_id): """ Add a host to spied list and create corresponding EventItem() :param host_id: "_id" of host to spy :type host_id: str """ if self.parent(): self.parent().host_services_lbl.setText(_('Select spy host to display its problems...')) if not self.initialized: # Remove Hint item self.takeItem(0) self.initialized = True if host_id not in self.spied_hosts: self.spied_hosts.append(host_id) host = data_manager.get_item('host', '_id', host_id) if host: event_item = EventItem() event_item.initialize( EventItem.get_event_type(host.data), _('Host %s, current state: %s (new !)') % ( host.get_display_name(), host.data['ls_state']), host=host.item_id ) self.insertItem(0, event_item) logger.info('Spy a new host: %s', host.name) logger.debug('... with id: %s', host_id)
def query_history(self, hostname=None, host_id=None): """ Launch request on "history" endpoint but only for hosts in "data_manager" :param hostname: name of host we want history :type hostname: str :param host_id: id of host for history :type host_id: str """ request_model = History.get_request_model() if hostname and host_id: request_model['params']['where'] = json.dumps({'host': host_id}) request_model['params']['max_results'] = 25 request = self.get(request_model['endpoint'], request_model['params'], request_model['projection'], all_items=False) if request: logger.debug('Add / Update history for %s (%s)', hostname, host_id) if data_manager.get_item('history', host_id): data_manager.update_item_data('history', host_id, request['_items']) else: host_history = History() host_history.create( host_id, request['_items'], hostname, ) data_manager.database['history'].append(host_history) else: # pragma: no cover, too long to test history_list = [] for history in data_manager.database['history']: request_model['params']['where'] = json.dumps( {'host': history.item_id}) request_model['params']['max_results'] = 25 request = self.get(request_model['endpoint'], request_model['params'], request_model['projection'], all_items=False) if request: host_history = History() host_history.create( history.item_id, request['_items'], history.name, ) history_list.append(host_history) if history_list: data_manager.update_database('history', history_list)
def acknowledge(self, item, sticky, notify, comment): # pragma: no cover """ Prepare data for acknowledge and POST on backend API or WS if available :param item: item to acknowledge: host | service :type item: alignak_app.items.host.Host | alignak_app.items.service.Service :param sticky: define if ack is sticky or not :type sticky: bool :param notify: define if ack should notify user or not :type notify: bool :param comment: comment of ack :type comment: str :return: request response :rtype: dict """ user = data_manager.database['user'] if self.ws_client.auth: if item.item_type == 'service': command = 'ACKNOWLEDGE_SVC_PROBLEM' host = data_manager.get_item('host', '_id', item.data['host']) element = host.name else: command = 'ACKNOWLEDGE_HOST_PROBLEM' element = item.name item_name = item.name if sticky: sticky = '2' else: sticky = '1' notify = str(int(notify)) persistent = '0' parameters = ';'.join( [item_name, sticky, notify, persistent, user.name, comment]) data = { 'command': command, 'element': element, 'parameters': parameters } request = self.ws_client.post('command', params=data) else: data = { 'action': 'add', 'user': user.item_id, 'comment': comment, 'notify': notify, 'sticky': sticky } if item.item_type == 'service': data['host'] = item.data['host'] data['service'] = item.item_id else: data['host'] = item.item_id data['service'] = None request = self.post('actionacknowledge', data) return request
def acknowledge(self, item, sticky, notify, comment): # pragma: no cover """ Prepare data for acknowledge and POST on backend API or WS if available :param item: item to acknowledge: host | service :type item: alignak_app.items.host.Host | alignak_app.items.service.Service :param sticky: define if ack is sticky or not :type sticky: bool :param notify: define if ack should notify user or not :type notify: bool :param comment: comment of ack :type comment: str :return: request response :rtype: dict """ user = data_manager.database['user'] if self.ws_client.auth: if item.item_type == 'service': command = 'ACKNOWLEDGE_SVC_PROBLEM' host = data_manager.get_item('host', '_id', item.data['host']) element = host.name else: command = 'ACKNOWLEDGE_HOST_PROBLEM' element = item.name item_name = item.name if sticky: sticky = '2' else: sticky = '1' notify = str(int(notify)) persistent = '0' parameters = ';'.join([item_name, sticky, notify, persistent, user.name, comment]) data = { 'command': command, 'element': element, 'parameters': parameters } request = self.ws_client.post('command', params=data) else: data = { 'action': 'add', 'user': user.item_id, 'comment': comment, 'notify': notify, 'sticky': sticky } if item.item_type == 'service': data['host'] = item.data['host'] data['service'] = item.item_id else: data['host'] = item.item_id data['service'] = None request = self.post('actionacknowledge', data) return request
def query_services(self, host_id=None): """ Launch request for "service" endpoint. If ``host_id`` is given, only services related to host are added / updated :param host_id: "_id" of host :type host_id: str """ request_model = Service.get_request_model(host_id) request = self.get( request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True ) if request: services_list = [] for backend_item in request['_items']: service = Service() service.create( backend_item['_id'], backend_item, backend_item['name'], ) # Add / update only services of host "if host_id" if host_id: if not data_manager.get_item('service', service.item_id): logger.debug('Add item data in database[service]') data_manager.database['service'].append(service) else: data_manager.update_item_data('service', service.item_id, service.data) # If not item ID, update all database if services_list and not host_id: data_manager.update_database('service', services_list) if host_id: host = data_manager.get_item('host', '_id', host_id) if host: logger.info('Update database[service] for %s', host.name) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def query_services(self, host_id=None): """ Launch request for "service" endpoint. If ``host_id`` is given, only services related to host are added / updated :param host_id: "_id" of host :type host_id: str """ request_model = Service.get_request_model(host_id) request = self.get(request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True) if request: services_list = [] for backend_item in request['_items']: service = Service() service.create( backend_item['_id'], backend_item, backend_item['name'], ) # Add / update only services of host "if host_id" if host_id: if not data_manager.get_item('service', service.item_id): logger.debug('Add item data in database[service]') data_manager.database['service'].append(service) else: data_manager.update_item_data('service', service.item_id, service.data) # If not item ID, update all database if services_list and not host_id: data_manager.update_database('service', services_list) if host_id: host = data_manager.get_item('host', '_id', host_id) if host: logger.info('Update database[service] for %s', host.name) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def update_service_data(self): # pragma: no cover """ Update ServiceDataqWidget """ service_item = self.sender().currentItem() if isinstance(service_item, (ServiceTreeItem, QListWidgetItem)): service = None # Get service if isinstance(service_item, ServiceTreeItem): service = data_manager.get_item('service', '_id', service_item.service_id) elif isinstance(service_item, QListWidgetItem): service = data_manager.get_item('service', '_id', service_item.data(Qt.UserRole)) if not service: service = self.service_data_widget.service_item # Update QWidgets self.services_tree_widget.setMaximumWidth(self.width() * 0.5) self.services_list_widget.setMaximumWidth(self.width() * 0.5) self.service_data_widget.setMaximumWidth(self.width() * 0.5) self.service_data_widget.update_widget(service) self.services_dashboard.update_widget(self.services) self.service_data_widget.show() # Update Service Items (ServiceTreeItem, QListWidgetItem) if isinstance(service_item, ServiceTreeItem): service_item.update_item() else: monitored = \ service.data['passive_checks_enabled'] + service.data['active_checks_enabled'] icon_name = get_icon_name('service', service.data['ls_state'], service.data['ls_acknowledged'], service.data['ls_downtimed'], monitored) service_item.setData(Qt.DecorationRole, QIcon(settings.get_image(icon_name))) service_item.setData(Qt.DisplayRole, service.get_display_name()) service_item.setToolTip(service.get_tooltip())
def update_service_data(self): # pragma: no cover """ Update ServiceDataqWidget """ service_item = self.sender().currentItem() if isinstance(service_item, (ServiceTreeItem, QListWidgetItem)): service = None # Get service if isinstance(service_item, ServiceTreeItem): service = data_manager.get_item('service', '_id', service_item.service_id) elif isinstance(service_item, QListWidgetItem): service = data_manager.get_item('service', '_id', service_item.data(Qt.UserRole)) if not service: service = self.service_data_widget.service_item # Update QWidgets self.services_tree_widget.setMaximumWidth(self.width() * 0.5) self.services_list_widget.setMaximumWidth(self.width() * 0.5) self.service_data_widget.setMaximumWidth(self.width() * 0.5) self.service_data_widget.update_widget(service) self.services_dashboard.update_widget(self.services) self.service_data_widget.show() # Update Service Items (ServiceTreeItem, QListWidgetItem) if isinstance(service_item, ServiceTreeItem): service_item.update_item() else: monitored = \ service.data['passive_checks_enabled'] + service.data['active_checks_enabled'] icon_name = get_icon_name( 'service', service.data['ls_state'], service.data['ls_acknowledged'], service.data['ls_downtimed'], monitored ) service_item.setData(Qt.DecorationRole, QIcon(settings.get_image(icon_name))) service_item.setData(Qt.DisplayRole, service.get_display_name()) service_item.setToolTip(service.get_tooltip())
def periodic_refresh(self): """ Refresh QWidget periodically """ if self.service_item: updated_service = data_manager.get_item('service', '_id', self.service_item.item_id) if updated_service: self.service_item = updated_service self.update_widget(self.service_item)
def periodic_refresh(self): """ Refresh QWidget periodically """ if self.service_item: updated_service = data_manager.get_item('service', '_id', self.service_item.item_id) if updated_service: self.service_item = updated_service self.update_widget(self.service_item)
def get_current_host(self): """ Return current Host item with name in QLineEdit :return: current host :rtype: alignak_app.items.host.Host """ current_host = data_manager.get_item( 'host', self.synthesis_widget.line_search.text().rstrip()) return current_host
def get_current_host(self): """ Return current Host item with name in QLineEdit :return: current host :rtype: alignak_app.items.host.Host """ current_host = data_manager.get_item( 'host', self.synthesis_widget.line_search.text().rstrip() ) return current_host
def send_spy_events(self): """ Send event messages for all hosts who are spied """ if self.spy_list_widget.spied_hosts: for host_id in self.spy_list_widget.spied_hosts: host = data_manager.get_item('host', host_id) get_events_widget().add_event( EventItem.get_event_type(host.data), _('Host %s, current state: %s') % (host.get_display_name(), host.data['ls_state']), host=host.item_id)
def dropEvent(self, event): # pragma: no cover """ Override dropEvent. Get dropped item data, create a new one, and delete the one who is in EventsQWidget :param event: event triggered when something is dropped """ host = data_manager.get_item('host', '_id', event.source().currentItem().data(Qt.UserRole)) logger.debug('Drag and drop host in Panel: %s', host.name) logger.debug('... with current item: %s', event.source().currentItem()) self.synthesis_widget.line_search.setText(host.name) self.tab_widget.setCurrentIndex(self.tab_widget.indexOf(self.synthesis_widget)) self.display_host()
def send_spy_events(self): """ Send event messages for all hosts who are spied """ if self.spy_list_widget.spied_hosts: for host_id in self.spy_list_widget.spied_hosts: host = data_manager.get_item('host', host_id) get_events_widget().add_event( EventItem.get_event_type(host.data), _('Host %s, current state: %s') % ( host.get_display_name(), host.data['ls_state']), host=host.item_id )
def set_host_from_problems(self): """ Set line search if ``sender()`` is instance of QPushButton from :class:`Problems <alignak_app.qobjects.alignak.problems.ProblemsQWidget>` QWidget """ item = self.problems_widget.get_current_user_role_item() if item: if 'service' in item.item_type: hostname = data_manager.get_item('host', item.data['host']).name else: hostname = item.name if hostname in self.hostnames_list: self.synthesis_widget.line_search.setText(hostname) self.tab_widget.setCurrentIndex(self.tab_widget.indexOf(self.synthesis_widget))
def query_services_problems(self, state): """ Launch requests on "service" endpoint to get items with "ls_state = state" Wanted states are: ``WARNING``, ``CRITICAL`` and ``UNKNOWN`` :param state: state of service :type state: str """ # Services services_projection = [ 'name', 'host', 'alias', 'ls_state', 'ls_output', 'ls_acknowledged', 'ls_downtimed', 'passive_checks_enabled', 'active_checks_enabled' ] params = { 'where': json.dumps({ '_is_template': False, 'ls_state': state }) } request = self.get('service', params, services_projection, all_items=True) if request: for backend_item in request['_items']: if data_manager.is_problem('service', backend_item): service = Service() service.create(backend_item['_id'], backend_item, backend_item['name']) if data_manager.get_item('problems', service.item_id): data_manager.update_item_data('problems', service.item_id, service.data) else: data_manager.database['problems'].append(service) # Problems state is ready data_manager.db_is_ready['problems'][state] = True logger.info("Update database[problems] for %s services...", state)
def dropEvent(self, event): # pragma: no cover """ Override dropEvent. Get dropped item data, create a new one, and delete the one who is in EventsQWidget :param event: event triggered when something is dropped """ host = data_manager.get_item( 'host', '_id', event.source().currentItem().data(Qt.UserRole)) logger.debug('Drag and drop host in Panel: %s', host.name) logger.debug('... with current item: %s', event.source().currentItem()) self.synthesis_widget.line_search.setText(host.name) self.tab_widget.setCurrentIndex( self.tab_widget.indexOf(self.synthesis_widget)) self.display_host()
def set_host_from_problems(self): """ Set line search if ``sender()`` is instance of QPushButton from :class:`Problems <alignak_app.qobjects.alignak.problems.ProblemsQWidget>` QWidget """ item = self.problems_widget.get_current_user_role_item() if item: if 'service' in item.item_type: hostname = data_manager.get_item('host', item.data['host']).name else: hostname = item.name if hostname in self.hostnames_list: self.synthesis_widget.line_search.setText(hostname) self.tab_widget.setCurrentIndex( self.tab_widget.indexOf(self.synthesis_widget))
def query_services_problems(self, state): """ Launch requests on "service" endpoint to get items with "ls_state = state" Wanted states are: ``WARNING``, ``CRITICAL`` and ``UNKNOWN`` :param state: state of service :type state: str """ # Services services_projection = [ 'name', 'host', 'alias', 'ls_state', 'ls_output', 'ls_acknowledged', 'ls_downtimed', 'passive_checks_enabled', 'active_checks_enabled' ] params = {'where': json.dumps({'_is_template': False, 'ls_state': state})} request = self.get( 'service', params, services_projection, all_items=True ) if request: for backend_item in request['_items']: if data_manager.is_problem('service', backend_item): service = Service() service.create( backend_item['_id'], backend_item, backend_item['name'] ) if data_manager.get_item('problems', service.item_id): data_manager.update_item_data('problems', service.item_id, service.data) else: data_manager.database['problems'].append(service) # Problems state is ready data_manager.db_is_ready['problems'][state] = True logger.info("Update database[problems] for %s services...", state)
def query_hosts(self): """ Launch request on "host" endpoint, add hosts in problems if needed """ request_model = Host.get_request_model() request = self.get( request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True ) if request: hosts_list = [] for backend_item in request['_items']: host = Host() host.create( backend_item['_id'], backend_item, backend_item['name'], ) hosts_list.append(host) # If host is a problem, add / update it if data_manager.is_problem('host', backend_item): if data_manager.get_item('problems', host.item_id): data_manager.update_item_data('problems', host.item_id, host.data) else: data_manager.database['problems'].append(host) data_manager.db_is_ready['problems']['host'] = True if hosts_list: data_manager.update_database('host', hosts_list) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def query_hosts(self): """ Launch request on "host" endpoint, add hosts in problems if needed """ request_model = Host.get_request_model() request = self.get(request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True) if request: hosts_list = [] for backend_item in request['_items']: host = Host() host.create( backend_item['_id'], backend_item, backend_item['name'], ) hosts_list.append(host) # If host is a problem, add / update it if data_manager.is_problem('host', backend_item): if data_manager.get_item('problems', host.item_id): data_manager.update_item_data('problems', host.item_id, host.data) else: data_manager.database['problems'].append(host) data_manager.db_is_ready['problems']['host'] = True if hosts_list: data_manager.update_database('host', hosts_list) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def update_widget(self): """ Update QWidget """ self.item = data_manager.get_item(self.item.item_type, '_id', self.item.item_id) if self.item.data['ls_acknowledged'] or \ self.item.data['ls_state'] == 'OK' or \ self.item.data['ls_state'] == 'UP' or \ not data_manager.database['user'].data['can_submit_commands'] or \ not self.can_submit_actions(): self.acknowledge_btn.setEnabled(False) else: self.acknowledge_btn.setEnabled(True) if self.item.data['ls_downtimed'] or \ not data_manager.database['user'].data['can_submit_commands'] or \ not self.can_submit_actions(): self.downtime_btn.setEnabled(False) else: self.downtime_btn.setEnabled(True)
def update_item(self): """ Update QIcon of service """ service = data_manager.get_item('service', '_id', self.service_id) monitored = self.service_item.data[ 'passive_checks_enabled'] + self.service_item.data[ 'active_checks_enabled'] icon_name = get_icon_name('service', self.service_item.data['ls_state'], self.service_item.data['ls_acknowledged'], self.service_item.data['ls_downtimed'], monitored) self.setData(0, 0, service.get_display_name()) self.setData(0, 1, QIcon(settings.get_image(icon_name))) self.service_item = service assert service.item_id == self.service_id
def update_widget(self): """ Update QWidget """ self.item = data_manager.get_item(self.item.item_type, '_id', self.item.item_id) if self.item.data['ls_acknowledged'] or \ self.item.data['ls_state'] == 'OK' or \ self.item.data['ls_state'] == 'UP' or \ not data_manager.database['user'].data['can_submit_commands'] or \ not self.can_submit_actions(): self.acknowledge_btn.setEnabled(False) else: self.acknowledge_btn.setEnabled(True) if self.item.data['ls_downtimed'] or \ not data_manager.database['user'].data['can_submit_commands'] or \ not self.can_submit_actions(): self.downtime_btn.setEnabled(False) else: self.downtime_btn.setEnabled(True)
def update_host(self, host_item=None): """ Update HostQWidget data and QLabels :param host_item: the Host item :type host_item: alignak_app.items.host.Host """ if self.host_item and not host_item: self.set_data(self.host_item) if host_item: self.set_data(host_item) if self.host_item or host_item: # Update host services self.refresh_counter += 1 if self.refresh_counter > 10: thread_manager.add_high_priority_thread('service', self.host_item.item_id) self.refresh_counter = 0 # Update host icon_name = get_overall_state_icon( self.service_items, self.host_item.data['_overall_state_id'] ) icon_pixmap = QPixmap(settings.get_image(icon_name)) self.labels['host_icon'].setPixmap(QPixmap(icon_pixmap)) self.labels['host_icon'].setToolTip( self.host_item.get_overall_tooltip(self.service_items) ) self.labels['host_name'].setText('%s' % self.host_item.get_display_name()) monitored = self.host_item.data[ 'passive_checks_enabled'] + self.host_item.data['active_checks_enabled'] icon_name = get_icon_name( 'host', self.host_item.data['ls_state'], self.host_item.data['ls_acknowledged'], self.host_item.data['ls_downtimed'], monitored ) pixmap_icon = QPixmap(settings.get_image(icon_name)) final_icon = pixmap_icon.scaled(32, 32, Qt.KeepAspectRatio) self.labels['state_icon'].setPixmap(final_icon) self.labels['state_icon'].setToolTip(self.host_item.get_tooltip()) since_last_check = get_diff_since_last_timestamp( self.host_item.data['ls_last_check'] ) last_check_tooltip = get_date_fromtimestamp(self.host_item.data['ls_last_check']) self.labels['ls_last_check'].setText(since_last_check) self.labels['ls_last_check'].setToolTip(last_check_tooltip) self.labels['ls_output'].setText(self.host_item.data['ls_output']) self.labels['realm'].setText( data_manager.get_realm_name(self.host_item.data['_realm']) ) self.labels['address'].setText(self.host_item.data['address']) self.labels['business_impact'].setText(str(self.host_item.data['business_impact'])) self.labels['notes'].setText(self.host_item.data['notes']) self.actions_widget.item = self.host_item self.actions_widget.update_widget() self.activecheck_btn.update_btn_state(self.host_item.data['active_checks_enabled']) self.passivecheck_btn.update_btn_state(self.host_item.data['passive_checks_enabled']) self.customs_btn.setEnabled(bool(self.host_item.data['customs'])) # Update host history self.host_history = data_manager.get_item('history', self.host_item.item_id) if self.host_history: self.history_btn.setEnabled(True) self.history_btn.setToolTip(_('History is available')) else: self.history_btn.setToolTip(_('History is not available, please wait...')) self.history_btn.setEnabled(False) if app_backend.connected: thread_manager.add_high_priority_thread( 'history', {'hostname': self.host_item.name, 'host_id': self.host_item.item_id} ) else: thread_manager.stop_threads()
def manage_host_events(self, row): """ Manage spy events for a host, defined by current row of "spy_list_widget" :param row: current row of "spy_list_widget" :type row: int """ # Clear QListWidget self.host_list_widget.clear() # Get Host and its services if row < 0: item = None else: item = self.spy_list_widget.item(row) if item: host = data_manager.get_item('host', item.host) if _('(new !)') in item.data(Qt.DisplayRole): item.setData(Qt.DisplayRole, item.data(Qt.DisplayRole).replace(_('(new !)'), '')) item.setToolTip(item.toolTip().replace(_('(new !)'), '')) self.host_services_lbl.setText(_('Problems found for %s:') % host.get_display_name()) services = data_manager.get_host_services(host.item_id) if services: problems = False for service in services: if data_manager.is_problem('service', service.data): problems = True svc_state = _('Service %s is %s') % ( service.get_display_name(), service.data['ls_state'] ) event = EventItem() event.initialize( service.data['ls_state'], svc_state, host=host.item_id ) tooltip = 'Output: %s (%s)' % ( service.data['ls_output'], get_date_fromtimestamp(service.data['ls_last_check']) ) event.setToolTip(tooltip) self.host_list_widget.insertItem(0, event) if not problems: event = EventItem() event.initialize( host.data['ls_state'], _('%s is %s. Services of host seems managed.') % ( host.get_display_name(), host.data['ls_state']), host=host.item_id ) self.host_list_widget.insertItem(0, event) else: no_service_event = EventItem() no_service_event.initialize( host.data['ls_state'], _('%s is %s. No services.') % (host.get_display_name(), host.data['ls_state']) ) self.host_list_widget.insertItem(0, no_service_event)
def downtime(self, item, fixed, duration, start_stamp, end_stamp, comment): # pragma: no cover """ Prepare data for downtime and POST on backend API or WS if available :param item: item to downtime: host | service :type item: alignak_app.items.host.Host | alignak_app.items.service.Service :param fixed: define if donwtime is fixed or not :type fixed: bool :param duration: duration timestamp of downtime :type duration: int :param start_stamp: start timestamp of downtime :type start_stamp: int :param end_stamp: end timestamp of downtime :type end_stamp: int :param comment: comment of downtime :type comment: str :return: request response :rtype: dict """ if self.ws_client.auth: if item.item_type == 'service': host = data_manager.get_item('host', '_id', item.data['host']) element = host.name else: element = item.name fixed = str(int(fixed)) item_name = item.name trigger_id = '0' parameters = ';'.join( [item_name, str(start_stamp), str(end_stamp), fixed, trigger_id, str(duration), data_manager.database['user'].name, comment] ) data = { 'command': 'SCHEDULE_SVC_DOWNTIME' if item.item_type == 'service' else 'SCHEDULE_HOST_DOWNTIME', 'element': element, 'parameters': parameters } request = self.ws_client.post('command', params=data) else: data = { 'action': 'add', 'user': data_manager.database['user'].item_id, 'fixed': fixed, 'duration': duration, 'start_time': start_stamp, 'end_time': end_stamp, 'comment': comment, } if item.item_type == 'service': data['host'] = item.data['host'] data['service'] = item.item_id else: data['host'] = item.item_id data['service'] = None request = app_backend.post('actiondowntime', data) return request
def query_history(self, hostname=None, host_id=None): """ Launch request on "history" endpoint but only for hosts in "data_manager" :param hostname: name of host we want history :type hostname: str :param host_id: id of host for history :type host_id: str """ request_model = History.get_request_model() if hostname and host_id: request_model['params']['where'] = json.dumps({ 'host': host_id}) request_model['params']['max_results'] = 25 request = self.get( request_model['endpoint'], request_model['params'], request_model['projection'], all_items=False ) if request: logger.debug('Add / Update history for %s (%s)', hostname, host_id) if data_manager.get_item('history', host_id): data_manager.update_item_data('history', host_id, request['_items']) else: host_history = History() host_history.create( host_id, request['_items'], hostname, ) data_manager.database['history'].append(host_history) else: # pragma: no cover, too long to test history_list = [] for history in data_manager.database['history']: request_model['params']['where'] = json.dumps({ 'host': history.item_id}) request_model['params']['max_results'] = 25 request = self.get( request_model['endpoint'], request_model['params'], request_model['projection'], all_items=False ) if request: host_history = History() host_history.create( history.item_id, request['_items'], history.name, ) history_list.append(host_history) if history_list: data_manager.update_database('history', history_list)
def manage_host_events(self, row): """ Manage spy events for a host, defined by current row of "spy_list_widget" :param row: current row of "spy_list_widget" :type row: int """ # Clear QListWidget self.host_list_widget.clear() # Get Host and its services if row < 0: item = None else: item = self.spy_list_widget.item(row) if item: host = data_manager.get_item('host', item.host) if _('(new !)') in item.data(Qt.DisplayRole): item.setData( Qt.DisplayRole, item.data(Qt.DisplayRole).replace(_('(new !)'), '')) item.setToolTip(item.toolTip().replace(_('(new !)'), '')) self.host_services_lbl.setText( _('Problems found for %s:') % host.get_display_name()) services = data_manager.get_host_services(host.item_id) if services: problems = False for service in services: if data_manager.is_problem('service', service.data): problems = True svc_state = _('Service %s is %s') % ( service.get_display_name(), service.data['ls_state']) event = EventItem() event.initialize(service.data['ls_state'], svc_state, host=host.item_id) tooltip = 'Output: %s (%s)' % ( service.data['ls_output'], get_date_fromtimestamp( service.data['ls_last_check'])) event.setToolTip(tooltip) self.host_list_widget.insertItem(0, event) if not problems: event = EventItem() event.initialize( host.data['ls_state'], _('%s is %s. Services of host seems managed.') % (host.get_display_name(), host.data['ls_state']), host=host.item_id) self.host_list_widget.insertItem(0, event) else: no_service_event = EventItem() no_service_event.initialize( host.data['ls_state'], _('%s is %s. No services.') % (host.get_display_name(), host.data['ls_state'])) self.host_list_widget.insertItem(0, no_service_event)
def downtime(self, item, fixed, duration, start_stamp, end_stamp, comment): # pragma: no cover """ Prepare data for downtime and POST on backend API or WS if available :param item: item to downtime: host | service :type item: alignak_app.items.host.Host | alignak_app.items.service.Service :param fixed: define if donwtime is fixed or not :type fixed: bool :param duration: duration timestamp of downtime :type duration: int :param start_stamp: start timestamp of downtime :type start_stamp: int :param end_stamp: end timestamp of downtime :type end_stamp: int :param comment: comment of downtime :type comment: str :return: request response :rtype: dict """ if self.ws_client.auth: if item.item_type == 'service': host = data_manager.get_item('host', '_id', item.data['host']) element = host.name else: element = item.name fixed = str(int(fixed)) item_name = item.name trigger_id = '0' parameters = ';'.join([ item_name, str(start_stamp), str(end_stamp), fixed, trigger_id, str(duration), data_manager.database['user'].name, comment ]) data = { 'command': 'SCHEDULE_SVC_DOWNTIME' if item.item_type == 'service' else 'SCHEDULE_HOST_DOWNTIME', 'element': element, 'parameters': parameters } request = self.ws_client.post('command', params=data) else: data = { 'action': 'add', 'user': data_manager.database['user'].item_id, 'fixed': fixed, 'duration': duration, 'start_time': start_stamp, 'end_time': end_stamp, 'comment': comment, } if item.item_type == 'service': data['host'] = item.data['host'] data['service'] = item.item_id else: data['host'] = item.item_id data['service'] = None request = app_backend.post('actiondowntime', data) return request
def update_host(self, host_item=None): """ Update HostQWidget data and QLabels :param host_item: the Host item :type host_item: alignak_app.items.host.Host """ if self.host_item and not host_item: self.set_data(self.host_item) if host_item: self.set_data(host_item) if self.host_item or host_item: # Update host services self.refresh_counter += 1 if self.refresh_counter > 10: thread_manager.add_high_priority_thread( 'service', self.host_item.item_id) self.refresh_counter = 0 # Update host icon_name = get_overall_state_icon( self.service_items, self.host_item.data['_overall_state_id']) icon_pixmap = QPixmap(settings.get_image(icon_name)) self.labels['host_icon'].setPixmap(QPixmap(icon_pixmap)) self.labels['host_icon'].setToolTip( self.host_item.get_overall_tooltip(self.service_items)) self.labels['host_name'].setText('%s' % self.host_item.get_display_name()) monitored = self.host_item.data[ 'passive_checks_enabled'] + self.host_item.data[ 'active_checks_enabled'] icon_name = get_icon_name('host', self.host_item.data['ls_state'], self.host_item.data['ls_acknowledged'], self.host_item.data['ls_downtimed'], monitored) pixmap_icon = QPixmap(settings.get_image(icon_name)) final_icon = pixmap_icon.scaled(32, 32, Qt.KeepAspectRatio) self.labels['state_icon'].setPixmap(final_icon) self.labels['state_icon'].setToolTip(self.host_item.get_tooltip()) since_last_check = get_diff_since_last_timestamp( self.host_item.data['ls_last_check']) last_check_tooltip = get_date_fromtimestamp( self.host_item.data['ls_last_check']) self.labels['ls_last_check'].setText(since_last_check) self.labels['ls_last_check'].setToolTip(last_check_tooltip) self.labels['ls_output'].setText(self.host_item.data['ls_output']) self.labels['realm'].setText( data_manager.get_realm_name(self.host_item.data['_realm'])) self.labels['address'].setText(self.host_item.data['address']) self.labels['business_impact'].setText( str(self.host_item.data['business_impact'])) self.labels['notes'].setText(self.host_item.data['notes']) self.actions_widget.item = self.host_item self.actions_widget.update_widget() self.activecheck_btn.update_btn_state( self.host_item.data['active_checks_enabled']) self.passivecheck_btn.update_btn_state( self.host_item.data['passive_checks_enabled']) self.customs_btn.setEnabled(bool(self.host_item.data['customs'])) # Update host history self.host_history = data_manager.get_item('history', self.host_item.item_id) if self.host_history: self.history_btn.setEnabled(True) self.history_btn.setToolTip(_('History is available')) else: self.history_btn.setToolTip( _('History is not available, please wait...')) self.history_btn.setEnabled(False) if app_backend.connected: thread_manager.add_high_priority_thread( 'history', { 'hostname': self.host_item.name, 'host_id': self.host_item.item_id }) else: thread_manager.stop_threads()