11

I am trying to use monit, and set up email server using gmail. The configuration file is like this:

set mailserver smtp.gmail.com port 587
username "someuser@gmail.com" password "password"
using tlsv1
with timeout 30 seconds

And I set an alert to test:

check file alerttest with path /.nonexistent
alert address@gmail.com with reminder on 500 cycles

But when I use monit validate, the error message I got is this:

Sendmail: error receiving data from the mailserver 'smtp.gmail.com' -- Resource temporarily unavailable
Alert handler failed, retry scheduled for next cycle
'alerttest' file doesn't exist
Sendmail: error receiving data from the mailserver 'smtp.gmail.com' -- Resource temporarily unavailable
'alerttest' trying to restart

Anyone has any ideas? Thanks a lot

Jiechao Li
  • 253
  • 1
  • 4
  • 13

4 Answers4

9

You can not configure some other company's email server to hand off emails unless you have an actual account there. Even if you do have an account monit isn't the best program to deal with submitting email. What I would suggest is to just install a local MTA to listen on 127.0.0.1 and then configure monit like this:

set mailserver 127.0.0.1

In that case monit will hand off the email delivery to an actual MTA who can then take care of sending it out, the MTA is perfectly capable of dealing with the remote server being unavailable, unlike monit (since it's not made for that).

How you set up and configure the MTA is out of the scope of this question, however if you want it to send mail directly it would be best to have a static IP, rDNS and mx records. Or you can use a remote smarthost/gateway.

Edit: brief explanation how to install postfix

  • run:

    apt-get install postfix

  • choose:

    internet site

  • system mail name:

    whatever hostname your system has

You will now have configured postfix to send and receive email to and from the internet. Now when you configure monit as described above you will be able to send out email.

Important, in order to improve deliverability you want to make sure that your IP address has a reverse DNS record that resolves back to your domain.

For example, if your domain is example.org and your server is monit.example.org then it should resolve something like this:

host monit.example.org
monit.example.org has address 192.0.43.10

host 192.0.43.10
10.43.0.192.in-addr.arpa domain name pointer monit.example.org

Although it could resolve to a different hostname, just as long as the domain is the same. This is because many email servers will check whether you have a valid rDNS. You can request your ISP (amazon in this case) to change the rDNS for you.

aseq
  • 4,550
  • 1
  • 22
  • 46
  • 1
    I used gmail, that should work according to this:http://blog.markfeeney.com/2010/09/monit-notifications-using-google-gmail.html – Jiechao Li May 11 '13 at 00:35
  • The remote server responds with "Resource temporarily unavailable". Since monit is not an MTA it doesn't deal well with temporary or permanent smtp failures. Hence my suggestion to use your own MTA. By the way ISPs do not block outgoing port 25 so your MTA should have no problem submitting email to a remote MTA. – aseq May 11 '13 at 01:45
  • So can I use postfix to do this? I don't know how to set this up, could you guide me to a tutorial? Thanks – Jiechao Li May 11 '13 at 03:07
  • Sure but I need to know which linux distro you use, if you have a static IP, and/or if you know a gateway to hand off email and whatever else may be helpful. – aseq May 12 '13 at 07:52
  • I am using Ubuntu 12.04, it's on my Amazon aws. I have a static IP, but I don't know any gateway or so. Thanks – Jiechao Li May 12 '13 at 16:55
  • Please see my updated answer. – aseq May 15 '13 at 00:18
  • Thank you for your patient explanation. I still don't understand completely. Can't I redirect postfix to my gmail account? Or do I have to have my own domain name? Then I need to use other DNS to redirect the domain name to my server's IP. Do I understand that correctly? – Jiechao Li May 15 '13 at 04:58
  • Postfix will take care of sending your email out to the right destination, no need to worry about that. You should have your own domain name since you got a static IP. If not, and it's a generic name from amazon, why not register one? Though even a generic one ought to work. I would say just install postfix and use it, then check the logs to see whether it can successfully send out email. – aseq May 15 '13 at 17:44
  • You can also use "sendmail" (which is easier to use then postfix IMO) of course , i.e. any MTA. – dr0i May 20 '21 at 14:31
5

You can use a remote mail server. Here are my sample configs which worked. It sends alerts to my gmail through a remote smtp server. I think you still use Gmail as an smtp relay as well.

#configure remote smtp server in monitrc
/etc/monit/monitrc

set mailserver mail.yourmailserver.com port 587    
    username "me@yourmailserver.com" password "mypassword"    
    using tlsv1    
    with timeout 30 seconds

#set the from email which should be same as the one above
set mail-format { from: me@yourmailserver.com }

//
/etc/monit/conf.d/monit.services

monitor apache2

check process apache with pidfile /var/run/apache2/apache2.pid
       alert username@gmail.com only on { timeout,nonexist,resource,pid,connection }
       start program = "/etc/init.d/apache2 start" with timeout 60 seconds
       stop program  = "/etc/init.d/apache2 stop"
David Okwii
  • 294
  • 5
  • 13
  • 1
    that "from email"-part or your answer reminded me, that my mailprovider (zoho.com) wanted a correct from-address in the past (when using mutt for example). so I changed it an instantly received mails successfully. – MacMartin Feb 28 '17 at 11:04
2

On Ubuntu 20.04 LTS with Monit 5.26.0, I was trying to get Monit to send mail with Mailgun and the reason it wasn't working was tlsv1 should have been tls. According to this wiki page, monit 5.17 or later should use tls with Gmail or similar email providers.

Tyler
  • 91
  • 3
0

Google has annoying "less secure apps" security that blocks just connecting to it. Annoying because they like to switch it back on when you've already turned it off.

See here https://support.google.com/accounts/answer/6010255?hl=en