需要构建一个 Python/Django “仪表板”替换管理员的系统电子邮件

发布于 2024-10-04 18:11:45 字数 549 浏览 7 评论 0原文

对于我的 python/django 站点,我需要构建一个“仪表板”,它将更新我正在发生的数十个错误/心跳/意外事件的状态。

我目前正在通过 Django 站点向管理员帐户发送电子邮件来跟踪几种类型的“事件”:
1)正常情况下应该发生的事情出了问题。我们每隔几个小时将文件同步到不同的服务和其他机器,当出现问题时我会发送错误电子邮件。
2)当应该发生的事情确实发生时。有时,第 1 项中的事件会失败得如此严重,以至于它们甚至不发送电子邮件(尝试:例外:事件周围应该始终有效,但某些内容可能会从 crontab 中删除,系统配置可能会在某些情况下无法正常运行)运行等,我什至不会收到错误电子邮件,并且缺少成功/心跳电子邮件会让我知道应该发生的事情没有发生。)
3)当任何意外发生时。我们对后端操作将如何运行做出了很多假设,如果违反任何这些假设(例如,我们发现两个具有相同电子邮件地址的用户),我们想了解这一点。这些事件不一定是错误,更像是需要调查的警告。


因此,我想构建一个仪表板,我可以轻松地从 python/django 更新它,以鸟瞰所有这些类型的活动,这样我就可以停止每周发送数百封电子邮件(这已经难以管理)。

For my python/django site I need to build a "dashboard" that will update me on the status of dozens of error/heartbeat/unexpected events going on.

There are a few types of "events" that I'm currently tracking by having the Django site send emails to the admin accounts:

1) Something that normally should happen goes wrong. We synch files to different services and other machines every few hours and I send error emails when this goes wrong.

2) When something that should happen actually happens. Sometimes events in item #1 fail so horribly that they don't even send emails (try: except: around an event should always work, but things can get deleted from the crontab, the system configuration can get knocked askew where things won't run, etc. where I won't even get an error email and the lack of a success/heartbeat email will let me know something that should have happened didn't happen.)

3) When anything unexpected happens. We've made a lot of assumptions on how backend operations will run and if any of these assumptions are violated (e.g. we find two users who have the same email address) we want to know about it. These events aren't necessarily errors, more like warnings to investigate.

So I want to build a dashboard that I can easily update from python/django to give me a bird's eye view of all of these types of activity so I can stop sending hundreds of emails out per week (which is already unmanageble.)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听起来您想创建一个输出到网页的基本日志系统。

因此,您可以编写一个名为 systemevents 的简单应用程序,每当网站上发生某些事情时,它都会创建一个 Event 记录。您可以添加一个信号挂钩,以便在站点的其余部分中的任何位置都可以编写类似以下内容的代码:

from systemevents.signals import record_event

...

try:
    # code goes here
except Exception, inst:
    record_event("Error occurred while taunting %s: %s" % (obj, inst,), type="Error")
else:
    record_event("Successfully taunted %s" % (obj, ), type="Success")

然后您可以非常轻松地创建一个列出这些事件的视图。

然而,请记住,这会增加一层复杂性,从而带来很大的问题。如果错误出在您的数据库中怎么办?然后,每次您尝试记录错误事件时,都会发生另一个错误!

更好的方法是使用内置日志系统之类的东西来创建基于文本的日志文件,然后创建可以导入该文本文件并以更易读的方式布局的东西。

还有一点提示:为了改变 Django 处理异常的方式,你必须编写一个 500 个错误的自定义视图。如果使用systemevents,您将编写如下内容:

from django.views.defaults import server_error
def custom_error_view(request)
    try:
        import sys
        type, value, tb = sys.exc_info()    
        error_message = "" # create an error message from the values above
        record_event("Error occurred: %s" % (error_message,), type="Error")
    except Exception:
        pass
    return server_error(request)

请注意,此代码均未经过正确性测试。它只是作为指南。

Sounds like you want to create a basic logging system that outputs to a web page.

So you could write something simple app called, say, systemevents that creates an Event record each time something happens on the site. You'd add a signal hook so that anywhere in the rest of the site you could code something like:

from systemevents.signals import record_event

...

try:
    # code goes here
except Exception, inst:
    record_event("Error occurred while taunting %s: %s" % (obj, inst,), type="Error")
else:
    record_event("Successfully taunted %s" % (obj, ), type="Success")

Then you can pretty easily create a view that lists these events.

However, keep in mind that this is adding a layer of complexity that is highly problematic. What if the error lies in your database? Then each time you try to record an error event, another error occurs!

Far better to use something like a built-in logging system to create a text-based log file, then whip up something that can import that text file and lay it out in a somewhat more readable fashion.

One more tip: in order to change how Django handles exceptions, you have to write a custom view for 500 errors. If using systemevents, you'd write something like:

from django.views.defaults import server_error
def custom_error_view(request)
    try:
        import sys
        type, value, tb = sys.exc_info()    
        error_message = "" # create an error message from the values above
        record_event("Error occurred: %s" % (error_message,), type="Error")
    except Exception:
        pass
    return server_error(request)

Note that none of this code has been tested for correctness. It's just meant as a guide.

一身骄傲 2024-10-11 18:11:45

您尝试过查看 django-sentry 吗?
http://dcramer.github.com/django-sentry/

Have you tried looking at django-sentry?
http://dcramer.github.com/django-sentry/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文