]> git.uio.no Git - uio-zabbix.git/blame - zabbix_get_webapps_info.py
Use env python header + Upgrade code to support python3 [GID-1248]
[uio-zabbix.git] / zabbix_get_webapps_info.py
CommitLineData
753b94ce 1#!/usr/bin/env python
3e790c61
RM
2#
3# Authors:
5af16a29 4# rafael@e-mc2.net / https://e-mc2.net/
3e790c61 5#
5af16a29 6# Copyright (c) 2016-2017 USIT-University of Oslo
3e790c61
RM
7#
8# zabbix_get_webapps_info.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# zabbix_get_webapps_info.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
22import os
23import sys
24import glob
25import json
26
27# ############################################
25a84224 28# Method generate_webapps_info_file()
3e790c61 29# ############################################
25a84224
RM
30
31def generate_webapps_info_file(src_directory,output_directory):
32 """
5af16a29 33 Generate webapps info files
25a84224
RM
34 """
35
36 try:
5af16a29 37 src_files = glob.glob(src_directory + '/*-health.json')
3e790c61
RM
38
39 #
40 # Open the output file that will have the webapp information
41 # that will be needed to generate the zabbix autodiscovery
5af16a29 42 # regel
3e790c61
RM
43 #
44 with open (output_directory + '/web-applications.txt','w') as output_file:
45
46 for src_file in src_files:
47
48 #
5af16a29 49 # Open every webapp health file that exist in this
3e790c61
RM
50 # server, and read the json data
51 #
58bf0a9e 52 try:
5af16a29 53
58bf0a9e
RM
54 with open(src_file,'r') as info_file:
55 data = json.load(info_file)
56
5af16a29 57 monitor_keys = []
58bf0a9e 58
5af16a29
RM
59 #
60 # Find all components attributtes in the file if
61 # the metadata attributte exists (version:2)
62 #
63
64 if 'metadata' in data:
58bf0a9e 65
5af16a29
RM
66 for key in data.keys():
67 if key not in ['metadata']:
68 monitor_keys.append(key)
58bf0a9e
RM
69
70 #
5af16a29
RM
71 # Find all components attributtes in the file if
72 # the metadata attributte does not exist (version:<2)
58bf0a9e 73 #
5af16a29
RM
74
75 else:
76
77 for key in data.keys():
78 if key not in ['zabbix-name','instance','host-group','url','updated']:
79 monitor_keys.append(key)
80
58bf0a9e
RM
81
82 #
83 # Save webapp information in the output file
84 #
85 monitor_keys_output = '::'.join(monitor_keys)
5af16a29
RM
86
87 if 'metadata' in data:
88 output_file.write(data['metadata']['zabbix-name'] + '::' + data['metadata']['instance'] + '::' + data['metadata']['host-group'] + '::' + data['metadata']['url'] + '::' + monitor_keys_output + '\n')
89
90 else:
91 output_file.write(data['zabbix-name'] + '::' + data['instance'] + '::' + data['host-group'] + '::' + data['url'] + '::' + monitor_keys_output + '\n')
92
753b94ce 93 except Exception as e:
5af16a29
RM
94
95 # If we have problems reading some of the health
96 # files or if the format used in the file is not
97 # right, jump to the next health file in the list.
98
99 continue
100
3e790c61 101
753b94ce
RMG
102 except Exception as e:
103 print ("1")
3e790c61
RM
104 sys.exit(1)
105
106
107# ############################################
108# Main
109# ############################################
110
111if __name__ == '__main__':
112
113 try:
114
115 if len(sys.argv) == 3:
116 src_directory = sys.argv[1]
117 output_directory = sys.argv[2]
118
119 generate_webapps_info_file(src_directory,output_directory)
120
5af16a29
RM
121 #
122 # The script output will be 0 if no errors have been
123 # generated when it is executed.
124 #
125 # If an error is generated the output will be 1 and the
126 # error information.
127 #
753b94ce 128 print ("0")
5af16a29 129
3e790c61 130 else:
753b94ce
RMG
131 print ("1")
132 print ("Error: Wrong number of parameters")
133 print ('Format: ' + sys.argv[0] + ' <source_directory> <output_directory>')
3e790c61
RM
134 sys.exit(1)
135
753b94ce
RMG
136 except Exception as e:
137 print ("1")
3e790c61 138 sys.exit(1)