How does one include the server hostname in a django error email?

Go To StackoverFlow.com

3

The title pretty much sums up the question. I have a django based site load balanced across multiple servers. As much as all the servers should be identical, $#%& happens... It would be really useful if the error emails django sends included the hostname of the server. How would this be achieved?

p.s. It's django 1.3 on python 2.7.2 if that makes any difference.

UPDATE:

I can get the hostname, that's not the issue. The problem is how to add it to the error emails.

2012-04-03 19:58
by Endophage


8

You can use the SERVER_EMAIL or EMAIL_SUBJECT_PREFIX setting.

In your settings.py:

 import socket
 SERVER_ADMIN = 'alerts+{0}@mydomain.com'.format(socket.gethostname())

or

 import socket
 EMAIL_SUBJECT_PREFIX = '[my_django_app - {0}] '.format(socket.gethostname())
2012-04-03 21:33
by Sam Dolan
Definitely an interesting idea but support for that format of email is not universal - Endophage 2012-04-03 21:47
Sorry, I prefer to change the email address to make setting up filters easier.. You can change the subject too; check out my updated answer - Sam Dolan 2012-04-03 21:52
Oooh, I like that subject version. It'll make it super easy to glance at the emails and see where they're coming from. Wasn't aware of that particular setting. +1'd and accepted (also edited, assume it was a typo, should be EMAILSUBJECTPREFIX - Endophage 2012-04-03 21:56


-1

If you want the httpd server name you can get it from the request.

site_name = request.META['SERVER_NAME']

This isn't necessarily the hostname of the machine, but it is the server name alias in the Apache/IIS directive for the site. Alternately, if you absolutely need the server's machine name:

import socket
host_name = socket.gethostname()

UPDATE:

Now that you have the hostname of the particular server, you can add that to the error emails in a couple of ways. You could write your own error notification system, or as @sdolan mentioned you can change the SERVER_ADMIN or EMAIL SUBJECT_PREFIX directives in settings.py. Or, you can change your SERVER_EMAIL directive. You can use the "{0}".format(socket.gethostname()) syntax, or you can use the string joining of a list of strings "".join(string_parts_list).

# settings.py
import socket
SERVER_EMAIL = "".join(['webmaster@', socket.gethostname(), '.com'])
2012-04-03 20:09
by Furbeenator
LOL got downvoted when I had the same answer as the accepted, but didn't show how to concatenate a string - Furbeenator 2013-11-21 19:28
he want to know how to send the hostname along with the error emails. i guess thats why the answer was downvote - Muhia NJoroge 2015-05-04 21:10


-2

Python's socket.gethostname() (socket docs) is one method.

from socket import gethostname; print gethostname()

You can also get some parameters from os.uname() (os docs)

import os; print os.uname()[1]

Would get the 'nodename'.

2012-04-03 20:02
by jvc26
Downvote with no explanation - jvc26 2013-12-19 10:34
people are downvoting coz the answer you gave does not address the question, he already said I can get the hostname, that's not the issue. The problem is how to add it to the error emails. plus he only wants to include the hostname to the admin error emails - Muhia NJoroge 2015-05-04 21:08