I have a nagios server running on a centos-7 box.There is an option in a nagios server GUI to send availability report through email. I am planning to email that report to another centos machine (I will call it Nagios-client for understanding) and parse the file for my another script.

I have a postfix running on a Nagios-client and I see the report being received under "/var/spool/mail/nagios" (Thats the account where nagios-server sends the email). However I am not sure how to download the report and to further parsing/scripting with that.

Could someone guide me how to download the attachment from the received email in linux terminal?

  • Take a look at this question. Utilizing the Nagios API might be a better way to get availability reports. – Henrik Pingel May 14 '18 at 18:31

Attachments are stored inside of an email as MIME types; you'll need a tool to decode these properly. If your email isn't actually attaching anything and it's just plain text, then you should be able to just read the contents with cat.

If you actually need to parse the email properly, then you'll need to use a mail client that you can script, or use something that has a library you can reference through a scripting language like Ruby, Python, Perl, etc.

It also depends on what type of storage system you're using for email. There are two primary platforms for this, maildir (or maildir++) and mbox format. Each has different ways to read the files and both are configurable in Postfix.

If you can provide additional information, we might be able to provide more guidance, but your question lacks specificity to really let us dive in on this topic.

  • Thanks for your response. Apparently i need to parse the email witht he attachement. My plan is once Nagios-server send an email with attachment, i need to someone download/copy it to another folder and use that file for my script. PLease dont mind asking this, how should I check the email storage system I use? – Pradeep s May 14 '18 at 18:25
  • I would suggest either looking at the postfix configuration, or just looking at the file/folder layout. Something like this page may help in identifying which you have: linuxmail.info/mbox-maildir-mail-storage-formats. In short, maildir is a folder hierarchy and mbox is a single file. – Andrew May 14 '18 at 18:29
  • It is really easy to parse mime attachments off of emails. They are usually base64 encoded. You simply have to grab the lines between the mime boundaries, concatenate them, then decode and write contents to a file. I do it all the time with one liner shell commands. – varlogtim May 15 '18 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.