Beispiel #1
0
 def get_graphite_metrics_find():
     response.content_type = 'application/json'
     key = request.GET.get('query', '*')
     cprint("LIST GET TS FOR KEY", key)
     
     recursive = False
     if key.endswith('.*'):
         recursive = True
         key = key[:-2]
         cprint("LIST RECURSVIE FOR", key)
     
     # Maybe ask all, if so recursive is On
     if key == '*':
         key = ''
         recursive = True
     
     r = []
     keys = tsmgr.list_keys(key)
     l = len(key)
     added = {}
     for k in keys:
         cprint("LIST KEY", k)
         title = k[l:]
         # maybe we got a key that do not belong to us
         # like srv-linux10 when we ask for linux1
         # so if we don't got a . here, it's an invalid
         # dir
         cprint("LIST TITLE", title)
         if key and not title.startswith('.'):
             cprint("LIST SKIPPING KEY", key)
             continue
         
         # Ok here got sons, but maybe we are not asking for recursive ones, if so exit with
         # just the key as valid tree
         if not recursive:
             cprint("NO RECURSIVE AND EARLY EXIT for KEY", key)
             return jsoner.dumps(
                 [{"leaf": 0, "context": {}, 'text': key, 'id': key, "expandable": 1, "allowChildren": 1}])
         
         if title.startswith('.'):
             title = title[1:]
         cprint("LIST TITLE CLEAN", title)
         # if there is a . in it, it's a dir we need to have
         dname = title.split('.', 1)[0]
         # If the dnmae was not added, do it
         if dname not in added and title.count('.') != 0:
             added[dname] = True
             r.append({"leaf"         : 0, "context": {}, 'text': dname, 'id': k[:l] + dname, 'expandable': 1,
                       'allowChildren': 1})
             cprint("LIST ADD DIR", dname, k[:l] + dname)
         
         cprint("LIST DNAME KEY", dname, key, title.count('.'))
         if title.count('.') == 0:
             # not a directory, add it directly but only if the
             # key asked was our directory
             r.append({"leaf": 1, "context": {}, 'text': title, 'id': k, "expandable": 0, "allowChildren": 0})
             cprint("LIST ADD FILE", title)
     cprint("LIST FINALLY RETURN", r)
     return jsoner.dumps(r)
Beispiel #2
0
    def post_message(self,
                     channel,
                     text=None,
                     username=None,
                     as_user=None,
                     parse=None,
                     link_names=None,
                     attachments=None,
                     unfurl_links=None,
                     unfurl_media=None,
                     icon_url=None,
                     icon_emoji=None,
                     thread_ts=None):

        # Ensure attachments are json encoded
        if attachments:
            if isinstance(attachments, list):
                attachments = jsoner.dumps(attachments)

        return self.post('chat.postMessage',
                         data={
                             'channel': channel,
                             'text': text,
                             'username': username,
                             'as_user': as_user,
                             'parse': parse,
                             'link_names': link_names,
                             'attachments': attachments,
                             'unfurl_links': unfurl_links,
                             'unfurl_media': unfurl_media,
                             'icon_url': icon_url,
                             'icon_emoji': icon_emoji,
                             'thread_ts': thread_ts
                         })
Beispiel #3
0
 def update(self,
            channel,
            ts,
            text,
            attachments=None,
            parse=None,
            link_names=False,
            as_user=None):
     # Ensure attachments are json encoded
     if attachments is not None and isinstance(attachments, list):
         attachments = jsoner.dumps(attachments)
     return self.post('chat.update',
                      data={
                          'channel': channel,
                          'ts': ts,
                          'text': text,
                          'attachments': attachments,
                          'parse': parse,
                          'link_names': int(link_names),
                          'as_user': as_user,
                      })
Beispiel #4
0
    def stopping_agent(self):
        enabled = self.get_parameter('enabled')
        if not enabled:
            return
        groups = gossiper.groups  # no need to copy, the group pointer is in read only
        self.logger.info(
            'Pushing back ours groups and discovery informations to Shinken Enterprise'
        )

        collectors_data = {}
        for (ccls, e) in collectormgr.collectors.items():
            cname, c = collectormgr.get_collector_json_extract(e)
            collectors_data[cname] = c

        # In groups=> templates, we do not want : and . in the names
        _mapping = {':': '--', '.': '--'}
        use_value = ','.join(groups)
        for (k, v) in _mapping.items():
            use_value = use_value.replace(k, v)

        payload = {
            '_AGENT_UUID': gossiper.uuid,
            'use': use_value,
        }

        # System info
        system_results = collectors_data.get('system', {}).get('results', {})

        hostname = system_results.get('hostname', '')
        payload['host_name'] = hostname

        fqdn = system_results.get('fqdn', '')
        if fqdn:
            payload['_FQDN'] = fqdn

        publicip = system_results.get('publicip', '')
        if publicip:
            payload['_PUBLIC_IP'] = publicip

        # which address to use in fact?
        # how to choose:   fqdn > public_ip > hostname
        if fqdn:
            payload['address'] = fqdn
        elif publicip:
            payload['address'] = publicip
        else:
            payload['address'] = hostname

        # Timezone
        timezone = collectors_data.get('timezone',
                                       {}).get('results',
                                               {}).get('timezone', '')
        if timezone:
            payload['_TIMEZONE'] = bytes_to_unicode(timezone)

        cpucount = system_results.get('cpucount', '')
        if cpucount:
            payload['_CPU_COUNT'] = str(cpucount)  # data must be string

        linux_distribution = system_results.get('os', {}).get('linux', {}).get(
            'distribution', '')
        if linux_distribution:
            payload['_LINUX_DISTRIBUTION'] = linux_distribution

        # Memory
        physical_memory = collectors_data.get('timezone', {}).get(
            'results', {}).get('phys_total', '')
        if physical_memory:
            payload['_PHYSICAL_MEMORY'] = physical_memory

        # Network
        try:
            network_interfaces = ','.join(
                collectors_data.get('interfaces', {}).get('results',
                                                          {}).keys())
        except AttributeError:  # was without interfaces
            network_interfaces = ''
        if network_interfaces:
            payload['_NETWORK_INTERFACES'] = network_interfaces

        # Geoloc (lat and long)
        try:
            geoloc = collectors_data.get('geoloc', {}).get('results',
                                                           {}).get('loc', '')
        except AttributeError:  # was without interfaces
            geoloc = ''
        if geoloc and geoloc.count(',') == 1:
            lat, long = geoloc.split(',', 1)
            payload['_LAT'] = lat
            payload['_LONG'] = long

        # disks
        try:
            volumes = ','.join(
                collectors_data.get('diskusage', {}).get('results', {}).keys())
        except AttributeError:
            volumes = ''
        if volumes:
            payload['_VOLUMES'] = volumes

        file_result = self.get_parameter('file_result')
        self.logger.info('Writing file result to : %s' % file_result)
        if file_result:
            f = open(file_result, 'w')
            f.write(jsoner.dumps(payload, indent=4))
            f.close()
Beispiel #5
0
 def do_render(targets, _from):
     response.content_type = 'application/json'
     
     if not targets:
         return abort(400, 'Invalid target')
     # Default past values, round at an hour
     now = int(time.time())
     pastraw = int(time.time()) - 86400
     past = divmod(pastraw, 3600)[0] * 3600
     
     found = False
     # Try -Xd
     m = re.match(r'-(\d*)d', _from, re.M | re.I)
     if m:
         found = True
         nbdays = int(m.group(1))
         pastraw = int(time.time()) - (nbdays * 86400)
         past = divmod(pastraw, 86400)[0] * 86400
     # Try -Xh
     m = re.match(r'-(\d*)h', _from, re.M | re.I)
     if m:
         found = True
         nbhours = int(m.group(1))
         pastraw = int(time.time()) - (nbhours * 3600)
         past = divmod(pastraw, 3600)[0] * 3600
     # Try -Xhours
     if not found:
         m = re.match(r'-(\d*)hours', _from, re.M | re.I)
         if m:
             found = True
             nbhours = int(m.group(1))
             pastraw = int(time.time()) - (nbhours * 3600)
             past = divmod(pastraw, 3600)[0] * 3600
     # Try -Xmin
     if not found:
         m = re.match(r'-(\d*)min', _from, re.M | re.I)
         if m:
             found = True
             nbminutes = int(m.group(1))
             pastraw = int(time.time()) - (nbminutes * 60)
             past = divmod(pastraw, 60)[0] * 60
     # absolute value maybe?
     if not found:
         m = re.match(r'(\d*)', _from, re.M | re.I)
         if m:
             found = True
             past = divmod(int(m.group(1)), 3600)[0] * 3600
     
     if not found:
         return abort(400, 'Invalid range')
     
     # Ok now got the good values
     res = []
     for target in targets:
         
         nuuid = gossiper.find_group_node('ts', target)
         n = None
         if nuuid:
             n = gossiper.get(nuuid)
         nname = ''
         if n:
             nname = n['name']
         self.logger.debug('HTTP ts: target %s is managed by %s(%s)' % (target, nname, nuuid))
         # that's me or the other is no more there?
         if nuuid == gossiper.uuid or n is None:
             self.logger.debug('HTTP ts: /render, my job to manage %s' % target)
             
             # Maybe I am also the TS manager of these data? if so, get the TS backend data for this
             min_e = hour_e = day_e = None
             
             self.logger.debug('HTTP RENDER founded TS %s' % tsmgr.tsb.data)
             min_e = tsmgr.tsb.data.get('min::%s' % target, None)
             hour_e = tsmgr.tsb.data.get('hour::%s' % target, None)
             day_e = tsmgr.tsb.data.get('day::%s' % target, None)
             self.logger.debug('HTTP TS RENDER, FOUNDED TS data %s %s %s' % (min_e, hour_e, day_e))
             
             # Get from the past, but start at the good hours offset
             t = past
             r = []
             
             while t < now:
                 # Maybe the time match a hour we got in memory, if so take there
                 if hour_e and hour_e['hour'] == t:
                     self.logger.debug('HTTP TS RENDER match memory HOUR, take this value instead')
                     raw_values = hour_e['values'][:]  # copy instead of cherrypick, because it can move/append
                     for i in range(60):
                         # Get teh value and the time
                         e = raw_values[i]
                         tt = t + 60 * i
                         r.append((e, tt))
                         if e:
                             self.logger.debug('GOT NOT NULL VALUE from RENDER MEMORY cache %s:%s' % (e, tt))
                 else:  # no memory match, got look in the KS part
                     ukey = '%s::h%d' % (target, t)
                     raw64 = kvmgr.get_key(ukey)
                     if raw64 is None:
                         for i in range(60):
                             # Get the value and the time
                             tt = t + 60 * i
                             r.append((None, tt))
                     else:
                         raw = base64.b64decode(raw64)
                         v = pickle.loads(raw)
                         raw_values = v['values']
                         for i in range(60):
                             # Get teh value and the time
                             e = raw_values[i]
                             tt = t + 60 * i
                             r.append((e, tt))
                 # Ok now the new hour :)
                 t += 3600
             # Now build the final thing
             res.append({"target": target, "datapoints": r})
         else:  # someone else job, rely the question
             uri = 'http://%s:%s/render/?target=%s&from=%s' % (n['addr'], n['port'], target, _from)
             try:
                 self.logger.debug('TS: (get /render) relaying to %s: %s' % (n['name'], uri))
                 r = httper.get(uri)
                 self.logger.debug('TS: get /render founded (%d)' % len(r))
                 v = jsoner.loads(r)
                 self.logger.debug("TS /render relay GOT RETURN", v, "AND RES", res)
                 res.extend(v)
                 self.logger.debug("TS /render res is now", res)
             except get_http_exceptions() as exp:
                 self.logger.debug('TS: /render relay error asking to %s: %s' % (n['name'], str(exp)))
                 continue
     
     self.logger.debug('TS RENDER FINALLY RETURN', res)
     return jsoner.dumps(res)
Beispiel #6
0
 def __str__(self):
     return jsoner.dumps(self.body)