]> git.uio.no Git - uio-zabbix.git/commitdiff
Update zabbix_get_logstash_stats.py script to get logstash stats via API
authorRafael Martinez <r.m.guerrero@usit.uio.no>
Fri, 9 Mar 2018 12:43:57 +0000 (13:43 +0100)
committerRafael Martinez <r.m.guerrero@usit.uio.no>
Fri, 9 Mar 2018 12:43:57 +0000 (13:43 +0100)
zabbix_get_logstash_stats.py

index a92819eb1bc5aea173bc606c8b616ac4cff5a713..6f306598af6b1f81f1d50328c782c17979ea1706 100644 (file)
@@ -23,7 +23,16 @@ import requests
 import json
 import sys
 import os
+import tempfile
 
+# Zabbix proxy
+zabbix_proxy = ['zabbix-proxy-prod03.uio.no','zabbix-proxy-prod04.uio.no']
+
+# Path to zabbix_sender
+zabbix_sender = '/usr/bin/zabbix_sender'
+
+# Temp file with full json output
+tmp_stat_file = tempfile.NamedTemporaryFile(delete=False,dir='/tmp')
 
 # ############################################
 # get_logstash_stats_data()
@@ -55,32 +64,82 @@ def get_logstash_stats_data():
 def generate_zabbix_sender_data(stats):
 
     try:
-
-        print "logstash-node events.duration_in_millis " + str(stats['events']['duration_in_millis'])
-        print "logstash-node events.in " + str(stats['events']['in'])
-        print "logstash-node events.out " + str(stats['events']['out'])
-        print "logstash-node events.filtered " + str(stats['events']['filtered'])
-        print "logstash-node events.queue_push_duration_in_millis " + str(stats['events']['queue_push_duration_in_millis'])
-        print 
-
+        result = ""
+            
+        result +=  stats['host'] + " events.duration_in_millis " + str(stats['events']['duration_in_millis']) + "\n"
+        result +=  stats['host'] + " events.in " + str(stats['events']['in']) + "\n"
+        result +=  stats['host'] + " events.out " + str(stats['events']['out']) + "\n"
+        result +=  stats['host'] + " events.filtered " + str(stats['events']['filtered']) + "\n"
+        result +=  stats['host'] + " events.queue_push_duration_in_millis " + str(stats['events']['queue_push_duration_in_millis']) + "\n"
+        
+        result +=  stats['host'] + " process.max_file_descriptors " + str(stats['process']['max_file_descriptors']) + "\n"
+        result +=  stats['host'] + " process.open_file_descriptors " + str(stats['process']['open_file_descriptors']) + "\n"
+                  
+        result +=  stats['host'] + " reloads.failures " + str(stats['reloads']['failures']) + "\n"
+        result +=  stats['host'] + " reloads.successes " + str(stats['reloads']['successes']) + "\n"
+        
+        result +=  stats['host'] + " logstash.version " + str(stats['version']) + "\n"
+        
+        result +=  stats['host'] + " jvm.gc.collectors.old.collection_count " + str(stats['jvm']['gc']['collectors']['old']['collection_count']) + "\n"
+        result +=  stats['host'] + " jvm.gc.collectors.old.collection_time_in_millis " + str(stats['jvm']['gc']['collectors']['old']['collection_time_in_millis']) + "\n"
+        result +=  stats['host'] + " jvm.gc.collectors.young.collection_count " + str(stats['jvm']['gc']['collectors']['young']['collection_count']) + "\n"
+        result +=  stats['host'] + " jvm.gc.collectors.young.collection_time_in_millis " + str(stats['jvm']['gc']['collectors']['young']['collection_time_in_millis']) + "\n"
+        
+        result +=  stats['host'] + " jvm.mem.heap_committed_in_bytes " + str(stats['jvm']['mem']['heap_committed_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.heap_max_in_bytes " + str(stats['jvm']['mem']['heap_max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.heap_used_in_bytes " + str(stats['jvm']['mem']['heap_used_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.heap_used_percent " + str(stats['jvm']['mem']['heap_used_percent']) + "\n"
+        result +=  stats['host'] + " jvm.mem.non_heap_committed_in_bytes " + str(stats['jvm']['mem']['non_heap_committed_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.non_heap_used_in_bytes " + str(stats['jvm']['mem']['non_heap_used_in_bytes']) + "\n"
+        
+        result +=  stats['host'] + " jvm.mem.pools.old.committed_in_bytes " + str(stats['jvm']['mem']['pools']['old']['committed_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.old.max_in_bytes " + str(stats['jvm']['mem']['pools']['old']['max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.old.peak_max_in_bytes " + str(stats['jvm']['mem']['pools']['old']['peak_max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.old.peak_used_in_bytes " + str(stats['jvm']['mem']['pools']['old']['peak_used_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.old.used_in_bytes " + str(stats['jvm']['mem']['pools']['old']['used_in_bytes']) + "\n"
+        
+        result +=  stats['host'] + " jvm.mem.pools.survivor.committed_in_bytes " + str(stats['jvm']['mem']['pools']['survivor']['committed_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.survivor.max_in_bytes " + str(stats['jvm']['mem']['pools']['survivor']['max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.survivor.peak_max_in_bytes " + str(stats['jvm']['mem']['pools']['survivor']['peak_max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.survivor.peak_used_in_bytes " + str(stats['jvm']['mem']['pools']['survivor']['peak_used_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.survivor.used_in_bytes " + str(stats['jvm']['mem']['pools']['survivor']['used_in_bytes']) + "\n"
+        
+        result +=  stats['host'] + " jvm.mem.pools.young.committed_in_bytes " + str(stats['jvm']['mem']['pools']['young']['committed_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.young.max_in_bytes " + str(stats['jvm']['mem']['pools']['young']['max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.young.peak_max_in_bytes " + str(stats['jvm']['mem']['pools']['young']['peak_max_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.young.peak_used_in_bytes " + str(stats['jvm']['mem']['pools']['young']['peak_used_in_bytes']) + "\n"
+        result +=  stats['host'] + " jvm.mem.pools.young.used_in_bytes " + str(stats['jvm']['mem']['pools']['young']['used_in_bytes']) + "\n"
+        
+        
         pipelines_list = []
         
         for pipeline in stats['pipelines']:
             
-            print "logstash-node events.duration_in_millis['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['events']['duration_in_millis'])
-            print "logstash-node events.in['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['events']['in'])
-            print "logstash-node events.out['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['events']['out'])
-            print "logstash-node events.filtered['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['events']['filtered'])
-            print "logstash-node events.queue_push_duration_in_millis['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['events']['queue_push_duration_in_millis'])
-            print
-            print "logstash-node queue.capacity.max_queue_size_in_bytes['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['capacity']['max_queue_size_in_bytes'])
-            print "logstash-node queue.capacity.max_unread_events['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['capacity']['max_unread_events'])
-            print "logstash-node queue.capacity.page_capacity_in_bytes['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['capacity']['page_capacity_in_bytes'])
-            print "logstash-node queue.capacity.queue_size_in_bytes['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['capacity']['queue_size_in_bytes'])
-            print "logstash-node queue.events['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['events'])
-            print "logstash-node queue.type['" + pipeline + "'] " + str(stats['pipelines'][pipeline]['queue']['type'])
-            print 
+            result +=  stats['host'] + " events.duration_in_millis[" + pipeline + "] " + str(stats['pipelines'][pipeline]['events']['duration_in_millis']) + "\n"
+            result +=  stats['host'] + " events.in[" + pipeline + "] " + str(stats['pipelines'][pipeline]['events']['in']) + "\n"
+            result +=  stats['host'] + " events.out[" + pipeline + "] " + str(stats['pipelines'][pipeline]['events']['out']) + "\n"
+            result +=  stats['host'] + " events.filtered[" + pipeline + "] " + str(stats['pipelines'][pipeline]['events']['filtered']) + "\n"
+            result +=  stats['host'] + " events.queue_push_duration_in_millis[" + pipeline + "] " + str(stats['pipelines'][pipeline]['events']['queue_push_duration_in_millis']) + "\n"
+            
+
+            if "capacity" in stats['pipelines'][pipeline]['queue']:
+
+                result +=  stats['host'] + " queue.capacity.max_queue_size_in_bytes[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['capacity']['max_queue_size_in_bytes']) + "\n"
+                result +=  stats['host'] + " queue.capacity.max_unread_events[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['capacity']['max_unread_events']) + "\n"
+                result +=  stats['host'] + " queue.capacity.page_capacity_in_bytes[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['capacity']['page_capacity_in_bytes']) + "\n"
+                result +=  stats['host'] + " queue.capacity.queue_size_in_bytes[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['capacity']['queue_size_in_bytes']) + "\n"
+                result +=  stats['host'] + " queue.events[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['events']) + "\n"
+                result +=  stats['host'] + " queue.type[" + pipeline + "] " + str(stats['pipelines'][pipeline]['queue']['type']) + "\n"
+                
 
+            result +=  stats['host'] + " reloads.failures[" + pipeline + "] " + str(stats['pipelines'][pipeline]['reloads']['failures']) + "\n"
+            result +=  stats['host'] + " reloads.last_error[" + pipeline + "] " + str(stats['pipelines'][pipeline]['reloads']['last_error']) + "\n"
+            result +=  stats['host'] + " reloads.last_failure_timestamp[" + pipeline + "] " + str(stats['pipelines'][pipeline]['reloads']['last_failure_timestamp']) + "\n"
+            result +=  stats['host'] + " reloads.last_success_timestamp[" + pipeline + "] " + str(stats['pipelines'][pipeline]['reloads']['last_success_timestamp']) + "\n"
+            result +=  stats['host'] + " reloads.successes[" + pipeline + "] " + str(stats['pipelines'][pipeline]['reloads']['successes']) + "\n"
+            
+        return result
+                           
     except Exception as e:
       raise Exception("[ERROR]: %s\n" % e)
 
@@ -90,8 +149,41 @@ if __name__ == '__main__':
     try:
         
         stats = get_logstash_stats_data()
-        generate_zabbix_sender_data(stats)
+        result = generate_zabbix_sender_data(stats)
+
+        #
+        # We create a file with the data that zabbix_sender will
+        # send in a bulk execution.
+        #
+
+        with open(tmp_stat_file.name,'w') as f: 
+            f.write(result)
+               
+        #
+        # The monitoring of this host can be done by any of the
+        # zabbix proxyer defined in zabbix_proxy[]. We try all of
+        # them until one of them accepts our data
+        #
+        
+        for proxy in zabbix_proxy:
+            returncode = os.system(zabbix_sender + ' -z ' + proxy + ' -i ' + tmp_stat_file.name + ' > /dev/null 2>&1')
+
+            if returncode == 0:
+                break
+
+        if returncode != 0:
+            # Delete temp file with zabbix_sender data
+            os.remove(tmp_stat_file.name)
+            
+            print "1"
+            sys.exit(1)
+        
+        # Delete temp file with zabbix_sender data
+        os.remove(tmp_stat_file.name)
+
+        # Return value 0 = execution OK
+        print("0")
 
     except Exception, e:
-        print e
+        print("1")
         sys.exit(1)