Automating Log Monitoring, Email Reporting, and DingTalk Alerts with Python
This article presents a Python‑based solution that queries LogEasy data, calculates key metrics such as total requests, 5xx errors, average response time, and unique visitors, formats the results into Excel and HTML reports, and automatically sends them via email and DingTalk alerts for operational monitoring.
The company uses LogEasy for application‑level monitoring and large‑screen alerts, but lacks direct access to raw logs, email summaries, and DingTalk notifications.
To overcome these limitations, a Python script is built that calls LogEasy’s API using the requests module, executes predefined SPL queries (total count, 5xx errors, average response time, unique visitors, max/min response times, 4xx errors), and parses the JSON responses.
Configuration files ( config.py ) store the LogEasy domain ID, token, credentials, and email server settings, as well as a dictionary mapping business system names to their LogEasy identifiers.
The main program iterates over all systems, collects the metrics via the Get_Api class, computes error‑rate percentages, and aggregates the data into Python lists.
Using pandas and openpyxl , the Process_Msg class converts the metric dictionaries into an Excel workbook and an HTML table, applies conditional formatting (red for values ≥ 3 seconds or ≥ 10 % error rate), and writes the files to the report directory.
The send_msg method composes an HTML email that includes the report table, a summary header, and links to the monitored modules, then attaches the Excel file and sends it via SMTP over SSL.
For real‑time alerts, the RiZhiYi_Api class runs a 10‑minute window query, extracts any 5xx occurrences, formats the raw log messages, and posts a DingTalk text message when a system exceeds three 5xx errors.
An additional Curl_Api class demonstrates how to use curl to probe a list of service endpoints, collect HTTP status codes, and send a DingTalk alert if any response code is ≥ 500.
Both the email and DingTalk workflows are scheduled to run automatically (e.g., every weekday at 09:30 or every 10 minutes), providing continuous operational visibility.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.