]> git.uio.no Git - uio-zabbix.git/blob - zabbix_get_webapps_status.py
Add zabbix autodiscovery script for rabbitmq
[uio-zabbix.git] / zabbix_get_webapps_status.py
1 #!/usr/bin/env python
2 #
3 # Authors:
4 # rafael@e-mc2.net / https://e-mc2.net/
5 #
6 # Copyright (c) 2016-2017 USIT-University of Oslo
7 #
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.
12 #
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.
17 #
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/>.
20 #
21
22 import os
23 import sys
24 import glob
25 import json
26 import tempfile
27
28 # Zabbix proxies
29 zabbix_proxy = ['zabbix-proxy-prod03.uio.no','zabbix-proxy-prod04.uio.no']
30
31 # Path to zabbix_sender
32 zabbix_sender = '/usr/bin/zabbix_sender'
33
34 # ############################################
35 # generate_webapps_health_output()
36 # ############################################
37
38 def generate_webapps_health_output(src_file):
39
40     try:
41
42         result = ''
43
44         #
45         # Open every webapp status file that exist in this server, and
46         # read the json data
47         #
48         # Exit the function with a empty result if the file can not be
49         # open or has not json code.
50         #
51         try:
52             with open(src_file,'r') as info_file:    
53                 data = json.load(info_file)
54
55         except Exception,e:
56             return result
57
58         #
59         # Json data must have all metadata attributes defined to be
60         # considered.
61         #
62         # Exit the function with a empty result if the file can not be
63         # open or has not json code.
64         #
65
66         if 'metadata' in data:
67
68             if len(set(['zabbix-name','instance','host-group','url','updated']) - set(data['metadata'].keys())) > 0:
69
70                 result = ''
71                 return result
72         else:
73
74             if len(set(['zabbix-name','instance','host-group','url','updated']) - set(data.keys())) > 0:
75
76                 result = ''
77                 return result
78
79         #
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.
84         #
85
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' 
89         else:
90             zabbix_name = data["zabbix-name"]
91             result += zabbix_name + ' webapp.health.file.version 0\n'
92
93         if '-prod' in zabbix_name:
94             result += zabbix_name + ' webapp.type prod\n'
95             
96         elif '-test' in zabbix_name:
97             result += zabbix_name + ' webapp.type test\n'
98
99         elif '-utv' in zabbix_name:
100             result += zabbix_name + ' webapp.type utv\n'
101
102         elif '-demo' in zabbix_name:
103             result += zabbix_name + ' webapp.type demo\n'
104
105         else:
106             result += zabbix_name + ' webapp.type prod\n'
107
108             
109         #
110         # Generate the webapp.component.health* items information
111         # needed by zabbix_sender
112         # 
113             
114         metadata_attributes = ['zabbix-name','instance','host-group','url',"updated"]
115
116         if 'metadata' in data:
117         
118             for component in data.keys():
119
120                 if zabbix_name != '' and component != 'metadata':
121
122                     #
123                     # Convert True to 1 and False to 0.
124                     # 
125                     
126                     if data[component]["status"] == True:
127                         data[component]["status"] = 1
128                     elif data[component]["status"] == False: 
129                         data[component]["status"] = 0
130                     
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'
133
134             result += zabbix_name + ' webapp.lastupdate[' + data["metadata"]["instance"] + '] ' + str(data["metadata"]["updated"]) + '\n'
135
136         else:
137
138             if '-test' in zabbix_name or '-utv' in zabbix_name or '-demo' in zabbix_name:
139                 severity = 'information'
140
141             elif '-prod' in zabbix_name:
142                 severity = 'average'
143
144             else:
145                 severity = 'average'
146
147             for component in data.keys():
148
149                 if zabbix_name != '' and component not in metadata_attributes:
150
151                     #
152                     # Convert True to 1 and False to 0.
153                     # 
154                     
155                     if data[component] == True:
156                         data[component] = 1
157                     elif data[component] == False: 
158                         data[component] = 0
159                     
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'
162
163                     result += zabbix_name + ' webapp.component.health[' + data["instance"] + ',updated] ' + str(data["updated"]) + '\n'
164
165             result += zabbix_name + ' webapp.lastupdate[' + data["instance"] + '] ' + str(data["updated"]) + '\n'
166
167         return result
168
169     except Exception,e:
170         raise Exception(str(e)) 
171
172
173 # ############################################
174 # Main
175 # ############################################
176
177 if __name__ == '__main__':
178
179     try:
180
181         if len(sys.argv) == 2:
182
183             src_directory = sys.argv[1]
184
185             #
186             # Get list with all webapps json status files
187             #
188             src_files = glob.glob(src_directory + '/*-health.json')
189
190             for src_file in src_files:
191                 result = generate_webapps_health_output(src_file)
192
193                 #
194                 # Continue the execution with the next json file if
195                 # one of the files can not be open or has not json
196                 # code.
197
198                 if result == '':
199                     continue
200
201
202                 # Temp file with full json output
203                 tmp_stat_file = tempfile.NamedTemporaryFile(delete=False,dir='/tmp')
204
205                 #
206                 # We create a temp file with the data that
207                 # zabbix_sender will send in a bulk execution.
208                 #
209
210                 with open(tmp_stat_file.name,'w') as f: 
211                     f.write(result)
212                     
213                 #
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
217                 #
218             
219                 for proxy in zabbix_proxy:
220                     returncode = os.system(zabbix_sender + ' -z ' + proxy + ' -i ' + tmp_stat_file.name + ' > /dev/null 2>&1')
221                     
222                     if returncode == 0:
223                         break
224
225                 # Delete temp file with zabbix_sender data
226                 os.remove(tmp_stat_file.name)
227
228         else:
229             print "1"
230             sys.exit(1)
231
232     except Exception, e:
233         print "1"
234         sys.exit(1)
235
236     # Return value 0 = execution OK
237     print "0"