#!/usr/bin/env python # # Authors: # rafael@postgresql.org.es / http://www.postgresql.org.es/ # # Copyright (c) 2016 USIT-University of Oslo # # get_webapps_get_status.py is free software: you can redistribute # it and/or modify it under the terms of the GNU General Public # License as published by the Free Software Foundation, either version # 3 of the License, or (at your option) any later version. # # get_webapps_get_status.py is distributed in the hope that it will # be useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with sms_send. If not, see . # import os import sys import glob import json import tempfile # Zabbix proxies zabbix_proxy = ['zabbix-proxy-prod03.uio.no','zabbix-proxy-prod04.uio.no'] # Path to zabbix_sender zabbix_sender = '/usr/bin/zabbix_sender' # ############################################ # generate_webapps_health_output() # ############################################ def generate_webapps_health_output(src_file): try: result = '' # # Open every webapp status file that exist in this server, and # read the json data # # Exit the function with a empty result if the file can not be # open or has not json code. # try: with open(src_file,'r') as info_file: data = json.load(info_file) except Exception as e: return result # # Json data must have all metadata attributes defined to be # considered. # # Exit the function with a empty result if the file can not be # open or has not json code. # if len(set(['zabbix-name','instance','host-group','url','updated']) - set(data.keys())) > 0: return result # # Generate the webapp.component.health* items information # needed by zabbix_sender # metadata_attributes = ['zabbix-name','instance','host-group','url'] for component in data.keys(): if data["zabbix-name"] != '' and component not in metadata_attributes: # # Convert True to 1 and False to 0. # if data[component] == True: data[component] = 1 elif data[component] == False: data[component] = 0 result += data["zabbix-name"] + ' webapp.component.health[' + data["instance"] + ',' + component + '] ' + str(data[component]) + '\n' # # Add the value of the webapp.type item. If the application # name does not include one of these terms [-prod, -test, # -utv, -demo] we will assume that the application is a # production installation. # if '-prod' in data["zabbix-name"].lower(): result += data["zabbix-name"] + ' webapp.type prod\n' elif '-test' in data["zabbix-name"].lower(): result += data["zabbix-name"] + ' webapp.type test\n' elif '-utv' in data["zabbix-name"].lower(): result += data["zabbix-name"] + ' webapp.type utv\n' elif '-demo' in data["zabbix-name"].lower(): result += data["zabbix-name"] + ' webapp.type demo\n' else: result += data["zabbix-name"] + ' webapp.type prod\n' return result except Exception as e: raise Exception(str(e)) # ############################################ # Main # ############################################ if __name__ == '__main__': try: if len(sys.argv) == 2: src_directory = sys.argv[1] # # Get list with all webapps json status files # src_files = glob.glob(src_directory + '/*-health.json') for src_file in src_files: result = generate_webapps_health_output(src_file) # # Continue the execution with the next json file if # one of the files can not be open or has not json # code. if result == '': continue # Temp file with full json output tmp_stat_file = tempfile.NamedTemporaryFile(delete=False,dir='/tmp') # # We create a temp 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 # Delete temp file with zabbix_sender data os.remove(tmp_stat_file.name) else: print ("1") sys.exit(1) except Exception as e: print ("1") sys.exit(1) # Return value 0 = execution OK print ("0")