Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alignak-integ #24

Open
wants to merge 8 commits into
base: refactored
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion module/graphite_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import re
import logging
import urlparse
import copy


# encapsulate graph styles
Expand Down Expand Up @@ -135,6 +136,10 @@ def parse_graphite_args(part):

def parse_graphite_part(part):
logging.debug('Parsing %s', part)

if part[0:2] == '__' and part[-2:] == '__':
return part[2:-2]

ndx = part.find('(')
if ndx < 0:
logging.debug('No function call')
Expand Down Expand Up @@ -174,7 +179,7 @@ def __init__(self, server='', title='', style=GraphStyle(), start=0, end=0, min=
if targets is not None:
for t in targets:
self.add_target(t)
self.style = style
self.style = copy.copy(style)
for k in ('height', 'width', 'font_size', 'line_style'):
if k in kwargs:
setattr(self.style, k, kwargs[k])
Expand Down
23 changes: 20 additions & 3 deletions module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,32 @@
for mainly get graphs and links.
"""

import os
import re
import socket
import time

ALIGNAK = False
if os.environ.get('ALIGNAK_SHINKEN_UI', None):
if os.environ.get('ALIGNAK_SHINKEN_UI') not in ['0']:
ALIGNAK = True

from .graphite_utils import GraphStyle, GraphiteMetric
from .util import GraphFactory
from shinken.log import logger
from shinken.basemodule import BaseModule

if ALIGNAK:
from alignak.basemodule import BaseModule
else:
from shinken.basemodule import BaseModule

from shinken.misc.perfdata import PerfDatas


properties = {
'daemons': ['webui'],
'type': 'graphite_webui'
'type': 'graphite_webui',
'external': False
}


Expand All @@ -60,6 +72,10 @@ def __init__(self, modconf):
self._uri = ''
self.app = None

if ALIGNAK:
self.module_type = getattr(modconf, 'module_type', 'unset')
self.module_name = getattr(modconf, 'module_name', 'unset')

# service name to use for host check
self.hostcheck = getattr(modconf, 'hostcheck', '')

Expand Down Expand Up @@ -126,7 +142,8 @@ def _load_styles(self, modconf):

# Try to connect if we got true parameter
def init(self):
pass
if ALIGNAK:
return True

# To load the webui application
def load(self, app):
Expand Down
36 changes: 34 additions & 2 deletions module/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import os
from string import Template
import json
import re

from .graphite_utils import GraphiteURL, GraphiteMetric, graphite_time

Expand Down Expand Up @@ -135,6 +136,21 @@ def servicename(self):
else:
return GraphiteMetric.normalize_name(self.cfg.hostcheck)

@property
def tags(self):
if self.element_type == 'service':
string_tags = self.element.host.cpe_registration_tags or "dummy_tag:'''dummy_taglabel'''"
else:
string_tags = self.element.cpe_registration_tags or "dummy_tag:'''dummy_taglabel'''"

regex = re.compile(r"(?P<tag>[a-zA-Z0-9-_/\.\*]+):'''(?P<taglabel>[a-zA-Z0-9 \-_\"+:\.,\*\/#]*)'''($|\s)")
# print("string_tags", string_tags)
self.logger.info("tags...string_tags -> [[%s]]", string_tags)
# for match in regex.finditer(string_tags):
# print(" - tags... %s->%s", match.group('tag'), match.group('taglabel'))

return {match.group('tag'):match.group('taglabel') for match in regex.finditer(string_tags)}

# retrieve a style with graceful fallback
def get_style(self, name):
try:
Expand Down Expand Up @@ -204,7 +220,7 @@ def _generate_graph_uris(self):
link=graph.url('composer'),
img_src=graph.url('render')
)
self.logger.debug("[Graphite UI] uri: %s / %s", v['link'], v['img_src'])
self.logger.info("[Graphite UI] uri: %s / %s", v['link'], v['img_src'])
uris.append(v)

return uris
Expand Down Expand Up @@ -262,9 +278,25 @@ def _get_uris_from_file(self):


# Split, we may have several images.
for img in html.substitute(context).split('\n'):
logger.debug("[ui-graphite] tags elt={}...".format(self.hostname))
if '{tag}' in template_html: # Dirty hack for untagged templates
for tag in self.tags:
logger.debug("[ui-graphite] tag={}".format(tag))
context['tag'] = tag
context['taglabel'] = self.tags[tag]
uris += self._get_uris_from_string_template(html, context, graph_start, graph_end)
else:
uris += self._get_uris_from_string_template(html, context, graph_start, graph_end)
return uris

def _get_uris_from_string_template(self, template, context, graph_start, graph_end):
uris = []
for img in template.substitute(context).split('\n'):
if not img:
continue
# FIXME Temporal fix for no time interval in uri
# https://github.com/shinken-monitoring/mod-ui-graphite/issues/16
img = img + "&from=" + graph_start + "&until=" + graph_end
graph = GraphiteURL.parse(img, style=self.style)
uris.append(dict(link=graph.url('composer'), img_src=graph.url('render')))
return uris
Expand Down