Split email message with procmail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split email message with procmail
# 1  
Old 06-14-2013
Split email message with procmail

Hi,

I'm using procmail to ingest incoming messages. The goal is to split the message body into chunks and forward those to a different adddress. Here's a sample of the message body:


Code:
Group: testgroup
---------------------------------------
Host: test1.local:30200  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT
---------------------------------------
Host: test1.local:30000  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT
---------------------------------------
Host: test1.local:30500  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT
---------------------------------------
---------------------------------------
Configure alerts: Go to this URL

The trick is to extract anything between the 2 delimiters (/^Host:/ && /^Created:/) and mail them. The sample above should result in 3 distinct messages:

Message1:
Code:
Host: test1.local:30200  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT

Message2:
Code:
Host: test1.local:30000  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT

Message3:
Code:
Host: test1.local:30500  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT

Thanks for the help!

Waltari

Last edited by Scott; 06-14-2013 at 10:11 AM.. Reason: Please use code tags
# 2  
Old 06-14-2013
Is the destination address and subject in there? A simple sed script can turn it into a shell script that mails with here files.
Code:
mailx -s something-from-inside something-else@some_mail_server <<!
Host: test1.local:30500  
Type: conf
Alert: Host Down
Status: Open
Last Updated: 2013/06/13 14:11 GMT
Created: 2013/06/13 14:11 GMT
!

# 3  
Old 06-14-2013
Actually the Subject header is dynamic and will contain the concatenation of the
values of the Alert, Host & Created lines (e.g. Host Down on test1.local:30000 at 2013/06/13 14:11 GMT).

I've managed to use awk as follows:

cat msg.file | awk '/^Host:/,/^Created:/ { print $0 }'

But I need to process each extract between my two delimiters.

Thanks!

W
# 4  
Old 06-14-2013
Well, awk and sed can do that: awk can collect data in variables for later printing, and sed can get the whole block in the buffer using N. then pick out values with an s to build the shell wrapper with mail command.

We need requirements that define the subject and email address.
# 5  
Old 06-14-2013
Quote:
Originally Posted by DGPickett
Well, awk and sed can do that: awk can collect data in variables for later printing, and sed can get the whole block in the buffer using N. then pick out values with an s to build the shell wrapper with mail command.

We need requirements that define the subject and email address.
Thanks DGPickett for you input!

The format of the Subject line is described above. The Sender (From) address will be the hostname portion of the value of the "Host:" line (e.g. admin@test1.local) and the Recipient is static (postmaster@mydomain.com).

W
# 6  
Old 06-14-2013
You want to forge a remote sender? I just wanted a recipient and subject.

In my experience, a time stamp somewhere in the subject is nice, for selecting email by subject, perhaps for sorting, too, "<General_topic>-Hostname-YYYY-MM-DD_HH:MM".

So, in awkish, you need to collect all lines of the block in a body variable and collect bits for the subject and recipient in two or more variables, and the after collecting the Created line, spit out the mail command and input data and maybe clean your variables. Or you can clear your variables on the first line of the block. If your recipient variable ends up empty, you might want to send to a default address with a 'DefectiveBlock:' prefixed subject.

Last edited by DGPickett; 06-14-2013 at 03:24 PM..
# 7  
Old 06-14-2013
The outgoing messages resulting from the split will be consumed downstream by our monitoring system, which will convert the email message (headers + body) into an event.

Since the incoming message could contain alerts on multiple hosts, I need to break it down into individual messages, one for each 'host'.

I'm already including a time stamp in the subject line (derived from the Created line).

Does this make sense?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Message Body while sending an email

I am making use of the following code to display the results of my txt file in an email: mail -s 'Count Validation Test Comparison Results' Ronit@XYZ.com < Count_Validation_Results_`date +%m%d%Y`.txt Email Output: ----------Query 1 Count Validation Results-------- Source count is 4 Target... (7 Replies)
Discussion started by: ronitreddy
7 Replies

2. UNIX and Linux Applications

Ssmtp -t < /path/to/the/message.txt (How to format message.txt for html email)

ssmtp has been running well under Kubuntu 12.04.1 for plain text messages. I would like to send html messages with ssmtp -t < /path/to/the/message.txt, but I cannot seem to get the message.txt file properly formatted. I have tried various charsets, Content-Transfer-Encoding, rearranging the... (0 Replies)
Discussion started by: Ronald B
0 Replies

3. UNIX for Dummies Questions & Answers

How to get failure notice message when email is not sent.

I am using mailx command to send emails from the Unix command prompt. Whenever email is not sent it is not giving me any message "Email not sent" or failure delivery notice for the wrong email addresses. When I give correct email address I am able to receive them correctly. Can anyone please... (0 Replies)
Discussion started by: szc0025
0 Replies

4. Shell Programming and Scripting

Email alert doesnt contain any message

I have created a script which will monitor disk space in unix, it will send an email alert that will notify the specified receipients. I used echo in the mailx command but the email doesnt contain any message. I have used printf to store the message ($message2) and when tried to display on the... (2 Replies)
Discussion started by: lscanaleta
2 Replies

5. Shell Programming and Scripting

how to send contents of a file to email as a message

i have a file new1.txt i want to send the contents of that file as a message to email ali@sms.com i m using ksh script......... plz help me (5 Replies)
Discussion started by: ali560045
5 Replies

6. Shell Programming and Scripting

error-when trying to send the message thru email.

Hi All, I want to send a message through email. I have written below code. But it is not worling. Anybody has idea, why it is not working?. export $file1=$home1/pip1.$$ mailx -s "This Message from unix" abc@yahoo.com< $file1 thanks,Mary. (5 Replies)
Discussion started by: MARY76
5 Replies

7. UNIX for Dummies Questions & Answers

Procmail 3.22 install error message

I'm trying to install : Procmail 3.22 onto a : E10K domain running : Solaris 2.6 Generic_105181-35 I untar the file to /stage/procmail-3.22. When I run the command: /usr/ccs/bin/make install I get the following message, then the install aborts: License Error : Cannot find license... (2 Replies)
Discussion started by: antalexi
2 Replies

8. UNIX for Dummies Questions & Answers

Split-Email-removing files

Friends, I have a datafile that have unknown a number of CHARACTERS. if the datafile have more than 1 character and less than 20 email me the file rm datafile if the datafile have more than 20 characters split the datafile into 20 characters in each file email datafileaa email... (4 Replies)
Discussion started by: bobo
4 Replies

9. UNIX for Advanced & Expert Users

email attachment, with a message using mailx

Hi, I am trying to include a message along with an attachment with an email using mailx on AIX. uuencode Test.dat Test.dat| mailx -s 'Testing' mymail@yahoo.com < MESGFILE This only gives me the contents of MESGFILE as my message. If I remove the < FILE I recieve the attachment. What... (4 Replies)
Discussion started by: edog
4 Replies
Login or Register to Ask a Question