4 # rafael@e-mc2.net / https://e-mc2.net/
6 # Copyright (c) 2016-2017 USIT-University of Oslo
8 # get_webapps_get_status.py is free software: you can redistribute
9 # it and/or modify it under the terms of the GNU General Public
10 # License as published by the Free Software Foundation, either version
11 # 3 of the License, or (at your option) any later version.
13 # get_webapps_get_status.py is distributed in the hope that it will
14 # be useful, but WITHOUT ANY WARRANTY; without even the implied
15 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 # See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with sms_send. If not, see <http://www.gnu.org/licenses/>.
29 zabbix_proxy = ['zabbix-proxy-prod03.uio.no','zabbix-proxy-prod04.uio.no']
31 # Path to zabbix_sender
32 zabbix_sender = '/usr/bin/zabbix_sender'
34 # ############################################
35 # generate_webapps_health_output()
36 # ############################################
38 def generate_webapps_health_output(src_file):
45 # Open every webapp status file that exist in this server, and
48 # Exit the function with a empty result if the file can not be
49 # open or has not json code.
52 with open(src_file,'r') as info_file:
53 data = json.load(info_file)
59 # Json data must have all metadata attributes defined to be
62 # Exit the function with a empty result if the file can not be
63 # open or has not json code.
66 if 'metadata' in data:
68 if len(set(['zabbix-name','instance','host-group','url','updated']) - set(data['metadata'].keys())) > 0:
74 if len(set(['zabbix-name','instance','host-group','url','updated']) - set(data.keys())) > 0:
80 # Add the value of the webapp.type item. If the application
81 # name does not include one of these terms [-prod, -test,
82 # -utv, -demo] we will assume that the application is a
83 # production installation.
86 if 'metadata' in data:
87 zabbix_name = data["metadata"]["zabbix-name"]
88 result += zabbix_name + ' webapp.health.file.version ' + str(data["metadata"]["health-file-version"]) + '\n'
90 zabbix_name = data["zabbix-name"]
91 result += zabbix_name + ' webapp.health.file.version 0\n'
93 if '-prod' in zabbix_name:
94 result += zabbix_name + ' webapp.type prod\n'
96 elif '-test' in zabbix_name:
97 result += zabbix_name + ' webapp.type test\n'
99 elif '-utv' in zabbix_name:
100 result += zabbix_name + ' webapp.type utv\n'
102 elif '-demo' in zabbix_name:
103 result += zabbix_name + ' webapp.type demo\n'
106 result += zabbix_name + ' webapp.type prod\n'
110 # Generate the webapp.component.health* items information
111 # needed by zabbix_sender
114 metadata_attributes = ['zabbix-name','instance','host-group','url',"updated"]
116 if 'metadata' in data:
118 for component in data.keys():
120 if zabbix_name != '' and component != 'metadata':
123 # Convert True to 1 and False to 0.
126 if data[component]["status"] == True:
127 data[component]["status"] = 1
128 elif data[component]["status"] == False:
129 data[component]["status"] = 0
131 result += zabbix_name + ' webapp.component.health[' + data["metadata"]["instance"] + ',' + component + '] ' + str(data[component]["status"]) + '\n'
132 result += zabbix_name + ' webapp.component.alarm.severity[' + data["metadata"]["instance"] + ',' + component + '] ' + str(data[component]["severity"]) + '\n'
134 result += zabbix_name + ' webapp.lastupdate[' + data["metadata"]["instance"] + '] ' + str(data["metadata"]["updated"]) + '\n'
138 if '-test' in zabbix_name or '-utv' in zabbix_name or '-demo' in zabbix_name:
139 severity = 'information'
141 elif '-prod' in zabbix_name:
147 for component in data.keys():
149 if zabbix_name != '' and component not in metadata_attributes:
152 # Convert True to 1 and False to 0.
155 if data[component] == True:
157 elif data[component] == False:
160 result += zabbix_name + ' webapp.component.health[' + data["instance"] + ',' + component + '] ' + str(data[component]) + '\n'
161 result += zabbix_name + ' webapp.component.alarm.severity[' + data["instance"] + ',' + component + '] ' + severity + '\n'
163 result += zabbix_name + ' webapp.component.health[' + data["instance"] + ',updated] ' + str(data["updated"]) + '\n'
165 result += zabbix_name + ' webapp.lastupdate[' + data["instance"] + '] ' + str(data["updated"]) + '\n'
170 raise Exception(str(e))
173 # ############################################
175 # ############################################
177 if __name__ == '__main__':
181 if len(sys.argv) == 2:
183 src_directory = sys.argv[1]
186 # Get list with all webapps json status files
188 src_files = glob.glob(src_directory + '/*-health.json')
190 for src_file in src_files:
191 result = generate_webapps_health_output(src_file)
194 # Continue the execution with the next json file if
195 # one of the files can not be open or has not json
202 # Temp file with full json output
203 tmp_stat_file = tempfile.NamedTemporaryFile(delete=False,dir='/tmp')
206 # We create a temp file with the data that
207 # zabbix_sender will send in a bulk execution.
210 with open(tmp_stat_file.name,'w') as f:
214 # The monitoring of this host can be done by any of the
215 # zabbix proxyer defined in zabbix_proxy[]. We try all of
216 # them until one of them accepts our data
219 for proxy in zabbix_proxy:
220 returncode = os.system(zabbix_sender + ' -z ' + proxy + ' -i ' + tmp_stat_file.name + ' > /dev/null 2>&1')
225 # Delete temp file with zabbix_sender data
226 os.remove(tmp_stat_file.name)
236 # Return value 0 = execution OK