diff --git a/module/helper.py b/module/helper.py index 7583e43e..8355148c 100644 --- a/module/helper.py +++ b/module/helper.py @@ -45,6 +45,7 @@ from shinken.misc.sorter import hst_srv_sort from shinken.misc.perfdata import PerfDatas +from shinken.macroresolver import MacroResolver # pylint: disable=no-self-use @@ -905,4 +906,144 @@ def get_event_icon(self, event, disabled=False, label='', use_title=True): ''' % (color, icon_text, label) + def render_url(self, obj, items, popover=False, css=''): + """Returns formatted HTML for an element URL + + If popover is true, a bootstrap popover is built, else a standard link ... + """ + result = [] + for (icon, title, description, url) in items: + if not url and not description: + # Nothing to do here! + continue + + # Replace MACROS in url, title and description + if hasattr(obj, 'get_data_for_checks'): + if url: + url = MacroResolver().resolve_simple_macros_in_string(url, + obj.get_data_for_checks()) + if title: + title = MacroResolver().resolve_simple_macros_in_string(title, + obj.get_data_for_checks()) + if description: + description = MacroResolver().resolve_simple_macros_in_string(description, + obj.get_data_for_checks()) + + if not description: + description = url + + if not title: + popover = False + + if icon: + icon = '' % icon + else: + icon = '' + + content = ' %s' % description + if popover: + content = ' %s' % title + + if popover: + for code in [['&', '&'], ['<', '<'], ['>', '>'], ['"', '"']]: + description = description.replace(code[0], code[1]) + + popover = 'data-toggle="popover medium" data-html="true" data-content="%s" ' \ + 'data-trigger="hover focus" data-placement="bottom"' % description + else: + popover = '' + + if not url: + url = 'href="#" ' + else: + url = 'href="%s" target="_blank" ' % url + + result.append('%s%s' % (url, css, popover, icon, content)) + + return result + + def get_element_urls(self, obj, property, default_title=None, default_icon=None, popover=False, css=''): + """"Return list of element notes urls + + The notes field is a string in which individual notes are separated with a | character. + + Each note is composed of a left part and a right part: description::url where url is + never present in the Nagios legacy syntax :) + + notes_url contain a list of | separated URLs that would be rendered + as navigable links. If a left part exist left::url, the left part is composed as an + individual note + + Indeed notes, notes_url and actions_url are the same encoding: description::url where + description and url are optional! If url is present, it will be rendered as a navigable + link in the UI. If description is present, it may be title,,icon + + As a sump-up, a notes declaration with all kind of notes: + notes simple note... only text but may be formated\ + |Title::note with only title...\ + |Title,,file::note with a title and an icon...\ + |Title,,file::note with a title and an icon and an url...,,http://my-url.fr\ + |KB1023,,tag::Lorem ipsum dolor sit amet, consectetur adipiscing elit. \ + Proin et leo gravida, lobortis nunc nec, imperdiet odio. Vivamus quam velit, scelerisque \ + nec egestas et, semper ut massa. Vestibulum id tincidunt lacus. Ut in arcu at ex egestas \ + vestibulum eu non sapien. Nulla facilisi. \ + Aliquam non blandit tellus, non luctus tortor. \ + Mauris tortor libero, egestas quis rhoncus in, sollicitudin et tortor.\ + + notesèurl and actionsèurl may be declared with the same information! + + """ + if not obj or not hasattr(obj, property): + return [] + + # We build a list of: title, icon, description, url + notes = [] + + # Several notes are defined in the notes attribute with | character + for item in getattr(obj, property).split('|'): + # A note is: [[title][,,icon]::]description[,,url] + try: + (decoration, description) = item.split('::') + except: + decoration = "%s,,%s" % (default_title, default_icon) + description = item + + try: + (title, icon) = decoration.split(',,') + except: + icon = default_icon + title = decoration + + if popover and not title: + title = description[5:] + + try: + (description, url) = description.split(',,') + except: + # description = 'No description provided' + url = None + + notes.append((icon, title, description, url)) + + return self.render_url(obj, notes, popover=popover, css=css) + + def get_element_notes(self, obj, default_title=None, default_icon=None, popover=False, css=''): + """"See the comment of get_element_urls""" + return self.get_element_urls(obj, 'notes', + default_title=default_title, default_icon=default_icon, + popover=popover, css=css) + + def get_element_notes_url(self, obj, default_title=None, default_icon=None, popover=False, css=''): + """"See the comment of get_element_urls""" + return self.get_element_urls(obj, 'notes_url', + default_title=default_title, default_icon=default_icon, + popover=popover, css=css) + + def get_element_actions_url(self, obj, default_title=None, default_icon=None, popover=False, css=''): + """"See the comment of get_element_urls""" + return self.get_element_urls(obj, 'action_url', + default_title=default_title, default_icon=default_icon, + popover=popover, css=css) + + helper = Helper()