Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.22 KB | None | 0 0
  1. (py27)dbao@dev37-devc 08:57:18 ~
  2.    $ cat sfx_cstar_cluster.py
  3. # -*- coding: utf-8 -*-
  4.  
  5. import json
  6. import signalfx
  7.  
  8.  
  9. # SFX_TOKEN = '<censored>'
  10. SFX_TOKEN = '2My124gvcc4DA4TggGwSzA'
  11.  
  12.  
  13. def pretty_json(data):
  14.     """Generate pretty-printed JSON string from object.
  15.  
  16.   :param data: Object to convert to a JSON string
  17.   :return: Pretty-printed JSON.
  18.   :rtype: str
  19.   """
  20.     return json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
  21.  
  22.  
  23. def get_cassandra_goodness(product='cassandra_cluster', additional_filter=None):
  24.     """Query for dimensions matching a query while defaulting to looking for Cassandra cluster names.
  25.  
  26.   :param product: (optional) Make sure this field exists in the results and generate a list of values for this field.
  27.                   (default: 'cassandra_cluster')
  28.   :type product: str`
  29.   :param additional_filter: (optional) Additional query string to append to the original query. (default: None)
  30.   :return: List of values.
  31.   :rtype: list
  32.   """
  33.     query = '_exists_:ecosystem AND _exists_:{}'.format(product)
  34.     if additional_filter:
  35.         query += ' AND {}'.format(additional_filter)
  36.  
  37.         print query
  38.     product_results = []
  39.  
  40.     with signalfx.SignalFx().rest(token=SFX_TOKEN) as sfx:
  41.         sfx_results = sfx.search_dimensions(query=query)
  42.         if not sfx_results:
  43.             return None
  44.  
  45.         results = sfx_results.get('results')
  46.         if not results:
  47.             return None
  48.  
  49. #       print pretty_json(results)
  50.         for result in results:
  51.             custom_properties = result.get('customProperties')
  52.             if custom_properties:
  53.                 cp_product = custom_properties.get(product)
  54.                 if cp_product:
  55.                     if cp_product in product_results:
  56.                         continue
  57.  
  58.                     product_results.append(cp_product)
  59.                     continue
  60.  
  61.             print('Found nothing interesting for: {}'.format(result))
  62.     return sorted(product_results)
  63.  
  64.  
  65. def main():
  66.     main_results = get_cassandra_goodness(
  67.         product='cassandra_cluster',
  68.         additional_filter='ecosystem:prod'
  69.     )
  70.  
  71.     print(pretty_json(main_results))
  72.  
  73.  
  74. if __name__ == '__main__':
  75.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement