Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Info

The basic primary function of this check is to log in to an IMAP or POP3 mailbox to monitor whether or not the login is possible. An extended feature is , that the check can fetch all (or just some) from the mailbox and forward them as events to the Event Console.

Status
colourGreen
titleLAST TESTED ON CHECKMK 2.3.0P1


Panel
borderColorblack
bgColor#f8f8f8
titleTable of Contents

Table of Contents

Step-by-step guide

  1. Setup → Services → HTTP, TCP, EMail, ... → Check Email → Add Rule

  • Service description

Here you can type in a service description. Please

...

ensure this is unique per host and does not collide with other services.

  • Mail Receiving

...

You can select if you want to check your IMAP or POP3 login.

...

It would be best to have your login credentials for your

...

Email Mailbox.

Screenshot of New rule named Check Email. Imap Server enabled and set to imap.provider.com. TCP port enabled and set to 993.Image Modified



  • Forward emails as events to Event Console

    • Forwarding Method

...


    • You can select if you want to send the events

...

    • to the

...

    • Event Console or

...

    • a remote syslog host. If you want to send the events to the local EC, we

...

    • recommend using the spooling mechanism.

...


    • The check will store all collected events in one file in ~/var/mkeventd/spool using the spooling mechanism.


      Code Block
      languagebash
      themeRDark
      OMD[mysite]:~$ ll ~/var/mkeventd/spool/

...

    • 
      total 3600

...

    • 
      -rw-rw---- 1 mysite mysite 79124 Dec 7 15:36 mysite_407343_1607351778

...

    • 
      -rw-rw---- 1 mysite mysite 79124 Dec 7 15:37 mysite_408329_1607351841
      

...



    • Only process mails with matching subject

Use this option to not process all messages found in the inbox

...

but only

...

those whose subject matches the given regular expression. The text entered here is handled as a regular expression pattern. The pattern is matched from the beginning. Add a tailing $ to change it to a whole text match. The match is performed case-sensitive. Read more about regular expression matching in Checkmk in our official user

...

guide.
.

You need to be aware that a subject field could contain line breaks. This is depending on your mail client and mail gateway.

...

You could check the raw email to find out how many line breaks you have

...

.

Screenshot of Email header informationImage Added

.

Now you can use regex platforms like regex101.com to create a regex.

...


Screenshot of Regex examplesImage Added

...

.

For advanced

...

debugging:
.

        1. Copy the plugin to the local path.

          Code Block
          languagebash
          themeRDark
          OMD[mysite]:~/lib/nagios/plugins$ cp check_mail ~/local/lib/nagios/plugins/

          .

        2. Add a print for the subject

          Code Block
  1. 104
        1. languagebash
          themeRDark
               for index, msg in sorted(mails.items()):
          
  2. 105
        1.          subject = msg.get('Subject', 'None')
          
  3. 106
        1.          print(subject)

          .

        2. Run the script manually

          Code Block
          languagebash
          themeRDark
          OMD[
  4. jph2
        1. mysite]:~/lib/nagios/plugins$ ./check_mail --pwstore=6@17@password_1 '--fetch-protocol=IMAP' '--fetch-server=imap.strato.de' '--fetch-tls' '--fetch-port=993' '--fetch-username=
  5. mail@anastasios-thomaidis
        1. user@provider.
  6. de
        1. com' '--fetch-password=*************' '--connect-timeout=10' '--forward-ec' '--match-subject=.*Incident.*\n.*assigned' 
          =?utf-8?Q?Incident_INC000000XXXX_has_bee?=
           =?utf-8?Q?n_assigned_to_your_group_'XXXX=
           =?utf-8?Q?XXXXX'_=XXXXX:_Comuni?=
           =?utf-8?Q?XXXXX_-_Priority:_Low?=
          =?utf-8?Q?Incident_INC000000XXXX_has_bee?=
           =?utf-8?Q?n_assigned_to_your_group_'XXXX=
           =?utf-8?Q?XXXXX'_=XXXXX:_Comuni?=
           =?utf-8?Q?XXXXX_-_Priority:_Low?=
          Forwarded 2 messages to event console | messages=2
          
          

          .

    • Events: Syslog facility

Use this syslog facility for all created events.

    • Events: Hostname

Use this hostname for all created events instead of the

...

mail server's name.

    • Limit the length of the mail body.

When forwarding

...

mail from the mailbox to the event console, the body of the mail is limited to the given number of characters.

    • Cleanup messages

The handled messages (see subject matching) can be cleaned up by

...

deleting them or moving them to a subfolder. By default, nothing is cleaned up.

...


Screenshot of Forward mails as events to Event console enabled. Forward method set to send events to local event console.Image Added



Debugging

...

Event Console Log

In the log of the EC Event Console, you can see the processing messages from the spool file:

Code Block
languagebash
themeRDark
title~/var/log/mkeventd.log
2020-12-07 12:51:18,078 [20] [cmk.mkeventd.EventServer] Processing message '<21>Dec 7 12:51:17 localhost Checkmk EC: Checkmk EC|Email to EC of Checkmk!'

2020-12-07 12:51:18,079 [20] [cmk.mkeventd.EventServer] Parsed message:

application: Checkmk EC

core_host:

facility: 2

host: localhost

host_in_downtime: False

ipaddress:

pid: 0

priority: 5

text: Checkmk EC|Email to EC of Checkmk!

time: 1607341877.0


Execute active Check manually.

...

For debugging reasons, it could be useful helpful to run the check manually. Therefore, you need to open the service site of the E-Mail Email service and search for 'Service check command'. Now you can use the command for executing the check manually.

...

Image Removed

Image Removed


Screenshot of Example of messages being set to the Event Console.Image Added


Screenshot of Example of a service command checkImage Added


Code Block
languagebash
themeRDark
title~/lib/nagios/plugins
OMD[
nagnis_master
mysite]:~/lib/nagios/plugins$ ./check_mail --pwstore=6@11@strato '--protocol=IMAP' '--server=imap.provider.de' '--ssl' '--port=993' '--username=
mail@provider
user@provider.
de
com' '--password=*************' '--forward-ec' '--forward-method=spool:' '--match-subject=Checkmk EC' '--forward-facility=2' '--forward-host=localhost' '--cleanup=delete'
OK - Forwarded 0 messages to event console | messages=0

.

For a detailed log, you can use --debug at the end of the command:

Code Block
languagebash
themeRDark
OMD[
nagnis_master
mysite]:~$ ~/lib/nagios/plugins/check_mail --pwstore=6@11@strato '--protocol=IMAP' '--server=imap.provider.de' '--ssl' '--port=993' '--username=
mail@provider
user@provider.
de
com' '--password=*************' '--forward-ec' '--forward-method=spool:' '--match-subject=Checkmk EC' '--forward-facility=2' '--forward-host=localhost' '--cleanup=delete' --debug

.
For more information, you can use --help

Code Block
languagebash
themeRDark
OMD[mysite]:~$ ~/lib/nagios/plugins/check_mail -
h
-help

.

One alternative:

Code Block
languagebash
themeRDark
OMD[
dashboard
mysite]:~$ lq "GET services\nColumns:host_name description check_command_expanded\nFilter: host_name = 
localhost
mysite\nFilter: description = Email"
localhost;Email;check_mk_active-mail!--pwstore=6@11@cmkmail '--protocol=IMAP' '--server=imap.strato.de' '--ssl' '--port=993' '--username=
cmk@anastasios-thomaidis
user@provider.
de
com' '--password=****************' '--forward-ec' '--forward-method=spool:' '--forward-facility=2' '--forward-host=localhost'

OMD[
dashboard
mysite]:~$ cd ~/lib/nagios/plugins/

OMD[
dashboard
mysite]:~/lib/nagios/plugins$ ./check_mail --pwstore=6@11@cmkmail '--protocol=IMAP' '--server=imap.strato.de' '--ssl' '--port=993' '--username=
cmk@anastasios-thomaidis
user@provider.
de
com' '--password=****************' '--forward-ec' '--forward-method=spool:' '--forward-facility=2' '--forward-host=localhost' --debug


The Event would look like this:

...


Screenshot of Example eventImage Added

...


Filter by label (Content by label)
showLabelsfalse
max5
spacesKB
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel in ( "email" , "check_mail_loop" , "emailloop" , "active_check" ) and type = "page" and space = "KB"
labelsemail emailloop check_mail_loop

...