0

I have an Ubuntu 10.04 desktop at home that runs 24/7. Recently, I've noticed that the machine occasionally restarts on its own (approx once per month). I would like to set this server up so that I get an email every time the server starts. I would like to get the email at my gmail account and I'll set up another gmail account for the server to use to send emails (if necessary).

As an added bonus, it would be great if the email could contain some sort of diagnostic information pertaining to what caused the server to crash and restart.

Thanks

1

3 Answers 3

1

I see at least two possible options:

  1. Add a new cronjob (e.g. @reboot root mail -s "I just rebooted" [email protected]
  2. Install logcheck; it will send you the log messages containing the reason for the reboot (if there are any log entries about them)
0

It's hard to track why your computer crashed and restarted, but you can e-mail your logs with the notification e-mail. For advanced monitoring, I suggest trying out Zabbix, NagiOS or Cacti. However, here's a simple Python 2.7 script for sending an e-mail with a command "./script.py username_for_gmail path_to_letter":

#!/usr/bin/python2.7
import smtplib
import getopt
import sys

args = getopt.getopt(sys.argv[1:], ":")
laiskas = args[1][1]
SENDER = "no-reply@" + args[1][0]
mssg = open(laiskas, 'r').read()
RECIPIENTS = ['[email protected]']

server = smtplib.SMTP('smtp.gmail.com',587)
#server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(args[1][0] + "@gmail.com","R7olM6Qw")
server.sendmail(SENDER,RECIPIENTS,mssg)
server.quit()

You have to change R7olM6Qw to your own GMail password. Also, please change RECIPIENTS variable for your addresses. The script needs smtplib and python2.7, which may have to be installed on your server from apt.

Then you can create a simple text file next to the script:

To: [email protected]
From: [email protected]
Subject: Computer powered on!
Random text here...

In the end, just add a new line to the /etc/rc.local file:

/path/to/the/python/script gmail_username /path/to/the/letter

However, this is not a safe method as you will be storing your password insecurely, so I suggest creating a mailbox just for that. Also, please, mind it and chmod all scripts in order to prevent other users doing anything with them.

Instead, I think that it is rather more reasonable to have a separate server with PHP mailer script available only for servers' IPs. In that case, the separate server would be doing the mailing service securely, while leaving your other servers only with curl or wget for pushing the notifications.

0
#!/bin/sh
EMAIL="[email protected]"
SUBJECT="[$HOSTNAME] - System $1"

if [ "$1" = startup ]
then
    ACTION="started successfully"
else
    ACTION="is shutting down"
fi

# a printf format string to simplify a long body
BODY="This is an automated message to notify you that %s %s.\nDate and Time: %s\n"

printf "$BODY" "$HOSTNAME" "$ACTION" "$(date)" | mail -s "${SUBJECT}" "${EMAIL}"

Save this as, say, /usr/local/bin/bootmail.sh, make it executable, etc.

Then, to create a systemd service, create a file in /etc/systemd/system with extension .service (for example, /etc/systemd/system/bootmail.service) containing:

[Unit]
Description=Run Scripts at Start and Stop

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/bootmail.sh startup
ExecStop=/usr/local/bin/bootmail.sh shutdown

[Install]
WantedBy=multi-user.target
Now, do:

systemctl daemon-reload
systemctl enable bootmail.service

Now, you should get mails on startup and shutdown (assuming mailing is configured correctly, etc.).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .