如何通过警报实时监控Python脚本

发布于 2025-01-11 16:14:34 字数 591 浏览 1 评论 0原文

我有很多 python 脚本在不同的机器上运行(PC 和服务器)。 这些 Python 脚本正在执行各种操作(抓取、处理 Excel 文件、调用 API 来存储数据等)。 我在所有这些脚本上实现了日志记录,效果很好,但由于这些脚本的数量不断增加我需要一种方法来监控它们的活动能够获取警报以防他们不跑。所有这些脚本都会在 Linux 计算机上作为 cronjob 或通过 Windows 任务计划程序自动启动。所有上述脚本不应全天候运行,但它们仅在需要时启动,然后在完成或失败时退出。

我想出了一个想法,使用常规数据库(例如 MariaDB)将所有运行时存储为来自所述脚本的时间戳。然后,我将在同一个数据库中拥有第二个“转向”表,并在该表中设置应该启动哪个脚本。

然后,我将使用 Grafana 中的 MariaDB 视图来查看脚本是否按时运行并成功完成,并且我还会设置警报,以防它们未成功启动/完成。

优点:易于实现

缺点:MariaDB 不是实时数据库,对于此类任务感觉有点繁重。 我需要添加一个 mariaDB 连接器并定义 SQL 插入到我的所有 Python 脚本中。

我觉得可能已经存在某种解决方案,甚至可能是免费的?

I have quite a few python scripts running on different machines (both PCs and servers).
These Python scripts are doing all kind of things (scraping, processing excel files, calling APIs to store data from etc).
I have logging implemented on all of these scripts which works nice, but due to the number of these scripts increasing I need a way to monitor their activity and be able to get alerts in case they did not run. All of these scripts are launched automatically either as cronjob on Linux machines or with Windows Task Scheduler. All said scripts are not supposed to run 24/7 but they are only launched when needed and then they exit when they are done or fail.

I came up with an idea of using a regular DB (e.g. MariaDB) to store all runtimes as timestamps from said scripts. Then I would have a second 'steering' table in the same DB and in this table I would set which script is supposed to be launched.

Then I would use a MariaDB view in Grafana to see if scripts ran on time and finished successfully and I would also set up alerts in case they some not launch / finish successfully.

PROS: easy to implement

CONS: MariaDB is not a real time DB, feels kind of heavy for this kind of task.
I would need to add a mariaDB connector and define SQL inserts to all my Python scripts.

I feel like there could already exist some kind of solution for this, maybe even for free?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文