Advertisement
Typhoon

Zabbix_ES_Cluster.py

Oct 8th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. import sys
  2.  
  3. import requests
  4.  
  5. r = requests.get('https://dev.myserver.com:9200/_cluster/stats', auth=('admin', 'admin'))
  6. data = r.json()
  7.  
  8.  
  9. # data = str(data)
  10.  
  11. # print("#################")
  12. # print(type(data))
  13. # print(data)
  14. #
  15. # print("\nStatus :", data['status'])
  16. # print("Cluster Name :", data['cluster_name'])
  17. # print("Number of Indices :", data['indices']['count'])
  18. # print("Number of Shards Total:", data['indices']['shards']['total'])
  19. # print("Number of Shards Primaries:", data['indices']['shards']['primaries'])
  20. # print("Number of Shards Replication:", data['indices']['shards']['replication'])
  21. # print("Number of Docs Total:", data['indices']['docs']['count'])
  22. # print("Number of Docs Deleted:", data['indices']['docs']['deleted'])
  23. # print("Store Size:", int(data['indices']['store']['size_in_bytes']) / 1000000, "MB")
  24.  
  25. def get_Status():
  26.     try:
  27.         return data['status']
  28.     except:
  29.         return "ERROR : Get Cluster Status"
  30.  
  31.  
  32. def get_StatusAll():
  33.     try:
  34.         return data
  35.     except:
  36.         return "ERROR : Get Cluster Status"
  37.  
  38.  
  39. def get_ClusterName():
  40.     try:
  41.         return data['cluster_name']
  42.     except:
  43.         return "ERROR : Get Cluster Name"
  44.  
  45.  
  46. def get_IndicesCount():
  47.     try:
  48.         return data['indices']['count']
  49.     except:
  50.         return "ERROR : Get Indices Count"
  51.  
  52.  
  53. def get_ShardsTotal():
  54.     try:
  55.         return data['indices']['shards']['total']
  56.     except:
  57.         return "ERROR : Get shards Count"
  58.  
  59. def get_ShardsPrimary():
  60.     try:
  61.         return data['indices']['shards']['primaries']
  62.     except:
  63.         return "ERROR : Get shards Count"
  64.  
  65. def get_ShardsReplica():
  66.     try:
  67.         return data['indices']['shards']['replication']
  68.     except:
  69.         return "ERROR : Get shards Count"
  70.  
  71. def get_DocsCount():
  72.     try:
  73.         return data['indices']['docs']['count']
  74.     except:
  75.         return "ERROR : Get Docs Count"
  76.  
  77. def get_DocsDeleted():
  78.     try:
  79.         return data['indices']['docs']['deleted']
  80.     except:
  81.         return "ERROR : Get Docs Count"
  82.  
  83.  
  84. def get_StoreSize():
  85.     try:
  86.         return data['indices']['store']['size_in_bytes']
  87.     except:
  88.         return "ERROR : Get Store Size"
  89.  
  90.  
  91. def get_StoreThrottleTime():
  92.     try:
  93.         return data['indices']['store']['throttle_time_in_millis']
  94.     except:
  95.         return "ERROR : Get Store Throttle time in milis"
  96.  
  97.  
  98. def get_Plugins():
  99.     try:
  100.         plugin_names = data['nodes']['plugins']
  101.         plugin_list=[]
  102.         for plugin_name in plugin_names:
  103.             plugin_list.append(plugin_name['name'].upper())
  104.         plugin_list.sort()
  105.         return ' '.join(plugin_list)
  106.     except:
  107.         return "ERROR : Get Plugins installed"
  108.  
  109.  
  110. def no_Argument():
  111.     print("!ERROR! Use this Script with arguments like : SCRIPT.PY status")
  112.     print("Available arguments : all, status, name, indices, shards_total")
  113.  
  114.  
  115. def main(argv):
  116.     if not argv:
  117.         no_Argument()
  118.     for arg in argv:
  119.         if arg.lower() == 'status':
  120.             print(get_Status().upper())
  121.             exit(1)
  122.         elif arg.lower() == 'all':
  123.             print(get_StatusAll())
  124.             exit(1)
  125.         elif arg.lower() == 'name':
  126.             print(get_ClusterName())
  127.             exit(1)
  128.         elif arg.lower() == 'indices':
  129.             print(get_IndicesCount())
  130.             exit(1)
  131.         elif arg.lower() == 'shards_total':
  132.             print(get_ShardsTotal())
  133.             exit(1)
  134.         elif arg.lower() == 'shards_primary':
  135.             print(get_ShardsPrimary())
  136.             exit(1)
  137.         elif arg.lower() == 'shards_replica':
  138.             print(get_ShardsReplica())
  139.             exit(1)
  140.         elif arg.lower() == 'docs_count':
  141.             print(get_DocsCount())
  142.             exit(1)
  143.         elif arg.lower() == 'docs_deleted':
  144.             print(get_DocsDeleted())
  145.             exit(1)
  146.         elif arg.lower() == 'store_size':
  147.             print(get_StoreSize())
  148.             exit(1)
  149.         elif arg.lower() == 'store_thtime':
  150.             print(get_StoreThrottleTime())
  151.             exit(1)
  152.         elif arg.lower() == 'plugins':
  153.             print(get_Plugins())
  154.             exit(1)
  155.         else:
  156.             no_Argument()
  157.  
  158.  
  159. if __name__ == "__main__":
  160.     main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement