有没有办法使用可以从 Linux 服务器执行的 shell 脚本在 Dynatrace 中创建问题指标?
这里,问题指标意味着,
假设我们正在使用 shell 脚本来检查 Linux 服务器上已部署服务的状态。
那么,
“问题指标”是什么意思?
您可以通过 Metric API 创建指标,并通过 Events API
创建问题您可以从 Linux 上的 shell 脚本调用任一端点。如果系统上有 OneAgent,您还可以使用 扩展程序。
import os
import requests
import csv
from datetime import datetime
from datetime import timedelta
# Function to generate date ranges
def generate_date_ranges(days=30):
date_ranges = []
for i in range(days):
from_date = f"-{i+2}d"
to_date = f"-{i+1}d" if i > 0 else "now"
date_ranges.append((from_date, to_date))
return date_ranges
# Generate the date ranges
date_ranges = generate_date_ranges()
# Define the file path for the CSV
file_path = '/Users//Desktop/DailyReport_30days.csv'
# Open the CSV file in append mode
with open(file_path, 'a', newline='') as csvfile:
csv_writer = csv.writer(csvfile, delimiter=',')
# Check if the file is empty
file_empty = os.stat(file_path).st_size == 0
# Write header only if the CSV is empty
if file_empty:
csv_writer.writerow(['Time', 'Seal', 'App', 'Module', 'SLI', 'Service', 'Endpoint', 'Location', 'Exception', 'DataValue'])
for from_date, to_date in date_ranges:
# Dynatrace - ServiceServiceAvailPrd
url_service_avail = f"http://127.0.0.1:5002/api1?from={from_date}&to={to_date}"
pass_dt_prod = os.environ.get("DT_password_prod")
headers = {
'accept': 'application/json',
'Authorization': f'Api-Token {pass_dt_prod}',
'Cookie': 'apmroute=xyz'
}
try:
response = requests.get(url_service_avail, headers=headers, verify=False)
output = response.json()
result = output.get('value', 0)
ServiceServiceAvailPrd = round(result, 2)
except Exception as e:
ServiceServiceAvailPrd = "_"
print(f'Error fetching ServiceServiceAvailPrd: {e}')
# Dynatrace - ServiceSyntheticAvailPrd
url_synthetic_avail = f"http://127.0.0.1:5002/api2?from={from_date}&to={to_date}"
try:
response = requests.get(url_synthetic_avail, headers=headers, verify=False)
output = response.json()
result = output.get('value', 0)
ServiceSyntheticAvailPrd = round(result, 2)
except Exception as e:
ServiceSyntheticAvailPrd = "_"
print(f'Error fetching ServiceSyntheticAvailPrd: {e}')
# Data to be written to the CSV
data = [
{"Key": "Service Availability", "Value": ServiceServiceAvailPrd},
{"Key": "Synthetic Healthcheck Availability", "Value": ServiceSyntheticAvailPrd},
]
timestamp = datetime.utcnow().strftime('%m/%d/%Y %H:%M UTC')
# Write data to CSV
for entry in data:
key = entry['Key']
data_value = entry['Value']
parts = str(data_value).split('$IS$') # Adjusted this line to handle data values
for part in parts:
# Resetting variables
service = ''
endpoint = ''
exception = ''
value_data = str(data_value) # Adjusted this line to handle data values
# Extract service name
start = part.find('$ServiceName$') + len('$ServiceName$')
end = part.find('$ServiceName$', start)
service = part[start:end] if end != -1 else ''
# Extract endpoint
start = part.find('$Endpoint$') + len('$Endpoint$')
end = part.find('$Endpoint$', start)
endpoint = part[start:end] if end != -1 else ''
# Extract exception
start = part.find('$Exception$')
if start != -1:
end = part.find('$Exception$', start + len('$Exception$'))
exception = part[start + len('$Exception$'):end] if end != -1 else ''
csv_writer.writerow([timestamp, 'Seal', 'App', 'Module', 'SLI', service, endpoint, 'Location', exception, value_data])