-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlatest_code
More file actions
214 lines (176 loc) · 7.73 KB
/
Copy pathlatest_code
File metadata and controls
214 lines (176 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import subprocess
import yaml
# Default values
default_http_port = 8000
config_file_path = '/opt/apps/gxadm/xtest/tuxedo_monitor_config.yaml'
# Define the request handler for the HTTP server
class TuxedoMetricsHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/metrics':
collect_tuxedo_metrics()
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
for key, value in metrics.items():
label = labels.get(key, '')
self.wfile.write('{}{{{}}} {}\n'.format(key, label, value))
# Initialize metrics and labels
metrics = {}
labels = {}
# Parse the YAML configuration
def parse_yaml_config():
global default_http_port
if os.path.isfile(config_file_path):
with open(config_file_path, 'r') as file:
config_data = file.read()
config = yaml.load(config_data, Loader=yaml.Loader)
if 'http_port' in config:
default_http_port = config['http_port']
return config # Return the loaded configuration
# Function to collect Tuxedo metrics from script 2
def collect_tuxedo_metrics():
global metrics, labels
# User-Defined Variables from script 2
yaml_file_path = "/opt/apps/gxadm/xtest/tuxedo_monitor_config.yaml"
frequency = 60
# Metrics Data Structure from script 2
tuxedo_exporter_error = 0
metrics["tuxedo_exporter_error"] = tuxedo_exporter_error
# Read YAML File from script 2
try:
with open(yaml_file_path, 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
applications = config["applications"]
except IOError as e:
print("Error: YAML file not found.")
tuxedo_exporter_error = 1
metrics["tuxedo_exporter_error"] = tuxedo_exporter_error
except Exception as e:
print("Error reading YAML file: {}".format(str(e))
tuxedo_exporter_error = 1
metrics["tuxedo_exporter_error"] = tuxedo_exporter_error
for app in applications:
app_name = app["name"]
command = app["command"]
try:
output = subprocess.check_output(command, shell=True, universal_newlines=True)
if "ulimit: core file size: cannot modify limit: Operation not permitted" in output:
continue # Modified this line
parse_command_output(output)
except Exception as e:
if "ulimit: core file size: cannot modify limit: Operation not permitted" in str(e):
continue
print("Error executing command for {}: {}".format(app_name, str(e)))
tuxedo_exporter_error = 1
metrics["tuxedo_exporter_error"] = tuxedo_exporter_error
# Function to extract and update metrics from script 2
def parse_command_output(output):
global metrics, labels
# Metric extraction logic from script 2
for line in output.splitlines():
if "processes" in line:
# Scenario 1: Metrics Extraction
if "Normal" in line:
tuxedo_process_status = 2 # Normal
tuxedo_total_processes = 0
tuxedo_missing_processes = 0
tuxedo_process_status_message = ""
parts = line.split()
for i, part in enumerate(parts):
if part == "comp=":
tux_comp = parts[i + 1]
break
# Extract the message
message = " ".join(parts[3:])
# Extract the total and missing processes
total_processes = missing_processes = None
for part in parts:
if part.isdigit():
if total_processes is None:
total_processes = int(part)
else:
missing_processes = int(part)
if total_processes is not None:
tuxedo_total_processes = total_processes
if missing_processes is not None:
tuxedo_missing_processes = missing_processes
tuxedo_process_status_message = message
elif "Critical" in line:
tuxedo_process_status = 1 # Critical
tuxedo_total_processes = 0
tuxedo_missing_processes = 0
tuxedo_process_status_message = ""
parts = line.split()
for i, part in enumerate(parts):
if part == "comp=":
tux_comp = parts[i + 1]
break
# Extract the message
message = " ".join(parts[3:])
# Extract the total and missing processes
total_processes = missing_processes = None
for part in parts:
if part.isdigit():
if total_processes is not None:
total_processes = int(part)
else:
missing_processes = int(part)
if total_processes is not None:
tuxedo_total_processes = total_processes
if missing_processes is not None:
tuxedo_missing_processes = missing_processes
tuxedo_process_status_message = message
else:
continue
labels["tux_comp"] = app_name
labels["tux_process_status_message"] = tuxedo_process_status_message
metrics["tuxedo_process_status"] = tuxedo_process_status
metrics["tuxedo_total_processes"] = tuxedo_total_processes
metrics["tuxedo_missing_processes"] = tuxedo_missing_processes
elif "proxies" in line:
# Scenario 2: Metrics Extraction
if "Normal" in line:
tuxedo_proxy_status = 2 # Normal
tuxedo_missing_proxies = 0
tuxedo_proxy_status_message = " ".join(line.split()[3:])
elif "Critical" in line:
tuxedo_proxy_status = 1 # Critical
tuxedo_missing_proxies = 0
tuxedo_proxy_status_message = " ".join(line.split()[3:])
parts = line.split()
for i, part in enumerate(parts):
if part == "comp=":
tux_comp = parts[i + 1]
break
# Extract the message
message = " ".join parts[3:]
# Extract the total and missing proxies
total_proxies = missing_proxies = None
for part in parts:
if part.isdigit():
if total_proxies is not None:
total_proxies = int(part)
else:
missing_proxies = int(part)
if total_proxies is not None:
tuxedo_total_proxies = total_proxies
if missing_proxies is not None:
tuxedo_missing_proxies = missing_proxies
tuxedo_proxy_status_message = message
else:
continue
labels["tux_comp"] = app_name
labels["tux_proxy_status_message"] = tuxedo_proxy_status_message
metrics["tuxedo_proxy_status"] = tuxedo_proxy_status
metrics["tuxedo_missing_proxies"] = tuxedo_missing_proxies
# Main function
def main():
parse_yaml_config()
# Start an HTTP server to expose metrics
server = HTTPServer(('127.0.0.1', default_http_port), TuxedoMetricsHandler)
print("Started HTTP server on port {0}".format(default_http_port))
while True:
server.handle_request()
if __name__ == '__main__':
main()