Skip to content

Commit

Permalink
# wording (and other) changes postfix_reporting.md (#1398)
Browse files Browse the repository at this point in the history
* wording changes, sentence simplification, removal of passive voice
* consistent use of `postfix`
* Sentence style capitalization on the headings
  • Loading branch information
sspencerwire authored Aug 8, 2023
1 parent a442c7e commit 771464e
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions docs/guides/email/postfix_reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ tags:
- tools
---

# Using Postfix For Server Process Reporting
# Using `postfix` for server process reporting

## Prerequisites

* Complete comfort operating from the command line on a Rocky Linux server
* Familiarity with an editor of your choice (this document uses the _vi_ editor, but you can substitute in your favorite editor)
* Familiarity with an editor of your choice (this document uses the `vi` editor, but you can substitute in your favorite editor)
* An understanding of DNS (the Domain Name System) and host names
* The ability to assign variables in a bash script
* Knowledge of what the _tail_, _more_, _grep_, and _date_ commands do
* Knowledge of what the `tail`, `more`, `grep`, and `date` commands do

## Introduction

Many Rocky Linux server administrators write scripts to perform specific tasks, like backups or file synchronization, and many of these scripts generate logs that have useful and sometimes very important information. Just having the logs, though, is not enough. If a process fails and logs that failure, but the busy administrator does not review the log, then a catastrophe could be in the making.

This document shows you how to use the _postfix_ MTA (mail transfer agent) to grab log details from a particular process, and send them to you via email. It also touches on date formats in logs, and helps you identify which format you need to use in the reporting procedure.
This document shows you how to use the `postfix` MTA (mail transfer agent) to grab log details from a particular process, and send them to you via email. It also touches on date formats in logs, and helps you identify which format you need to use in the reporting procedure.

Keep in mind, though, that this is just the tip of the iceberg as far as what can be done with reporting via postfix. Please note, too, that it is always a good security move to limit running processes to only those that you will need all the time.
Remember, this is just the tip of the iceberg regarding what you can do with reporting via `postfix`. Note that it is always a good security move to limit running processes to only those you will need all the time.

This document shows you how to enable postfix only for the reporting you need it to do, and then shut it down again.

## Postfix Defined
## `postfix` defined

postfix is a server daemon used for sending email. It is more secure and simpler than sendmail, another MTA that was the default go-to MTA for years. It can be used as part of a full-featured mail server.
`postfix` is a server daemon used for sending email. It is more secure and simpler than sendmail, another MTA that was the default go-to MTA for years. You can use it as part of a full-featured mail server.

## Installing postfix
## Installing `postfix`

Aside from postfix, we will need _mailx_ for testing our ability to send emails. To install both, and any dependencies required, enter the following on the Rocky Linux server command line:
Aside from `postfix`, you will need `mailx` to test your ability to send emails. To install these and any dependencies required, enter the following on the Rocky Linux server command line:

`dnf install postfix mailx`

Expand All @@ -47,37 +47,37 @@ Aside from postfix, we will need _mailx_ for testing our ability to send emails.
dnf install postfix s-nail
```

## Testing And Configuring Postfix
## Testing and configuring `postfix`

### Testing Mail First
### Testing mail first

Before we configure postfix, we need to find out how mail will look when it leaves the server, because we will probably want to change this. To do this, start postfix:
Before you configure `postfix`, you need to find out how mail will look when it leaves the server, as you will probably want to change this. To do this, start `postfix`:

`systemctl start postfix`

Then test it using mail command that is installed with mailx:
Test with the `mail` provided by `mailx` (or `s-nail`):

`mail -s "Testing from server" myname@mydomain.com`

This will bring up a blank line. Simply type your testing message in here:
This will display a blank line. Type your testing message in here:

`testing from the server`

Now hit enter, and enter a single period:
Hit enter, and enter a single period:

`.`

The system will respond with:
The system will respond with the following:

`EOT`

Our purpose for doing this is to check to see how our mail looks to the outside world, which we can get a feel for from the maillog that goes active with the starting of postfix.
The purpose for doing this is to see how our mail looks to the outside world. You can get a feel for this from the _maillog_ that goes active with the starting of `postfix`.

Use this command to see the output of the log file:

`tail /var/log/maillog`

You should see something like this, although the log file may have different domains for the email address, etc:
You will see something like this, although the log file will have different domains for the email address, and other items:

```
Mar 4 16:51:40 hedgehogct postfix/postfix-script[735]: starting the Postfix mail system
Expand All @@ -90,103 +90,103 @@ Mar 4 16:52:06 hedgehogct postfix/smtp[745]: C9D42EC0ADD: to=<myname@mydomain.c
:25, delay=1.4, delays=0.02/0.02/0.99/0.32, dsn=2.0.0, status=sent (250 2.0.0 OK 1614876726 z8si17418573ilq.142 - gsmtp)
Mar 4 16:52:06 hedgehogct postfix/qmgr[739]: C9D42EC0ADD: removed
```
The "somehost.localdomain" shows us that we need to make some changes, so stop the postfix daemon first:
The "somehost.localdomain" shows that you need to make some changes. Stop the `postfix` daemon first:

`systemctl stop postfix`

## Configuring Postfix
## Configuring `postfix`

Since we aren't setting up a complete, fully functional mail server, the configuration options that we will be using are not as extensive. The first thing we need to do is to modify the _main.cf_ file (literally the main configuration file for postfix), so let's make a backup first:
Since you are not setting up a complete, fully functional mail server, the configuration options that you will use are not as extensive. The first thing to do is to modify the _main.cf_ file (literally the main configuration file for `postfix`). Make a backup first:

`cp /etc/postfix/main.cf /etc/postfix/main.cf.bak`

Then edit it:
Edit it:

`vi /etc/postfix/main.cf`

In our example, our server name is going to be "bruno" and our domain name is going to be "ourdomain.com". Find the line in the file:
In our example, our server name is "bruno" and our domain name is "ourdomain.com". Find the line in the file:

`#myhostname = host.domain.tld`

You can either remove the remark (#) or you can add a new line under this line. Based on our example, the line would read:
You can remove the remark (#) or add a line under this line. Based on our example, the line will read:

`myhostname = bruno.ourdomain.com`

Next, find the line for the domain name:

`#mydomain = domain.tld`

Either remove the remark and change it, or add a new line:
Again, remove the remark and change it, or add a line under it:

`mydomain = ourdomain.com`

Finally, go to the bottom of the file and add this line:

`smtp_generic_maps = hash:/etc/postfix/generic`

Save your changes (in vi, that will be `Shift : wq!`) and exit the file.
Save your changes (in `vi` it is `Shift : wq!`) and exit the file.

Before we continue editing the generic file, we need to see how email will look. Specifically, we want to create the "generic" file that we referenced in the _main.cf_ file above:
Before you continue editing the generic file, you need to see how the email will look. Specifically, you want to create the "generic" file that you referenced in the _main.cf_ file above:

`vi /etc/postfix/generic`

This file tells postfix how any email coming from this server should look. Remember our test email and the log file? This is where we fix all of that:
This file tells `postfix` how any email coming from this server will look. Remember our test email and the log file? This is where we fix all of that:

```
root@somehost.localdomain root@bruno.ourdomain.com
@somehost.localdomain root@bruno.ourdomain.com
```
Now we need to tell postfix to use all of our changes. This is done with the postmap command:
Next, you need to tell `postfix` to use all of our changes. Do this with the postmap command:

`postmap /etc/postfix/generic`

Now start postfix and test your email again using the same procedure as above. You should now see that all of the "localdomain" instances have been changed to your actual domain.
Start `postfix` and test your email again with the same procedure used above. You will now see that all "localdomain" instances are now the actual domain.

### The date Command and a Variable Called today
### The `date` command and a variable called today

Not every application will use the same logging format for the date. This means that you may have to get creative with any script you write for reporting by date.
Not every application will use the same logging format for the date. You might have to get creative with any script you write for reporting by date.

Let's say that you want to look at your system log as an example and pull everything that has to do with dbus-daemon for today's date, and email it to yourself. (It's probably not the greatest example, but it will give you an idea of how we would do this.)
Say that you want to see your system log as an example and pull everything that has to do with dbus-daemon for today's date, and email it to yourself. (It is probably not the greatest example, but it will give you an idea of how you might do this.)

We need to use a a variable in our script that we will call "today" and we want it to relate to output from the "date" command and format it in a specific way, so that we can get the data we need from our system log (in _/var/log/messages_). To start with, let's do some investigative work.
You need to use a variable in your script. Call it "today". You want it to relate to output from the "date" command and format it in a specific way, so that you can get the data you need from our system log (in _/var/log/messages_). To start with, do some investigative work.

First, enter the date command in the command line:

`date`

This should give you the default system date output, which could be something like this:
This will give you the default system date output, which might be something like this:

`Thu Mar 4 18:52:28 UTC 2021`

Now let's check our system log and see how it records information. To do this, we will use the "more" and "grep" commands:
Check our system log and see how it records information. To do this, use the `more` and `grep` commands:

`more /var/log/messages | grep dbus-daemon`

Which should give you something like this:
Which will give you something like this:

```
Mar 4 18:23:53 hedgehogct dbus-daemon[60]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Mar 4 18:50:41 hedgehogct dbus-daemon[60]: [system] Activating via systemd: service name='org.freedesktop.nm_dispatcher' unit='dbus-org.freedesktop.nm-dispatcher.service' requested by ':1.1' (uid=0 pid=61 comm="/usr/sbin/NetworkManager --no-daemon " label="unconfined")
Mar 4 18:50:41 hedgehogct dbus-daemon[60]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher
```

The date and log outputs need to be exactly the same in our script, so let's look at how to format the date using a variable called "today".
The date and log outputs need to be exactly the same in our script. Let us see how to format the date with a variable called "today".

First, let's look at what we need to do with the date to get the same output as the system log. You can reference the [Linux man page](https://man7.org/linux/man-pages/man1/date.1.html) or type `man date` on the command line to pull up the date manual page to get the information you need.
Examine what you need to do with the date to get the same output as the system log. You can reference the [Linux man page](https://man7.org/linux/man-pages/man1/date.1.html) or type `man date` on the command line to pull up the date manual page to get the information you need.

What you will find is that in order to format the date the same way that _/var/log/messages_ has it, we need to use the %b and %e format strings, with %b being the 3 character month and %e being the space-padded day.
What you will find is to format the date the same way as _/var/log/messages_ , you need to use the %b and %e format strings, with %b being the 3 character month and %e being the space-padded day.

### The Script
### The script

For our bash script, we can see that we are going to use the date command and a variable called "today". (Keep in mind that "today" is arbitrary. You could call this variable "late_for_dinner" if you wanted!). We will call our script in this example, test.sh and place it in _/usr/local/sbin_:
For our bash script, you can see that you are going to use the `date` command and a variable called "today". (Remember that "today" is arbitrary. You can call this variable anything you like). You will call your script in this example, `test.sh` and place it in _/usr/local/sbin_:

`vi /usr/local/sbin/test.sh`

Let's start with, well, the beginning of our script. Note that even though the comment in our file says we are sending these messages to email, for now, we are just sending them to a standard log output so that we can verify that they are correct.
At the beginning, notice that even though the comment in our file says you are sending these messages to email, for now, you are just sending them to a standard log output to verify that they are correct.

Also, in our first attempt, we are grabbing all of the messages for the current date, not just the dbus-daemon messages. We will deal with that shortly.
Also, in our first run of the script, you are grabbing all of the messages for the current date not just the dbus-daemon messages. You will deal with that shortly.

Another thing to be aware of is that the grep command will return the filename in the output, which we don't want in this case, so we have added the "-h" option to grep to remove the prefix of the filename. In addition, once the variable "today" is set, we need to look for the entire variable as a string, so we need it all in quotes:
Be aware that the `grep` command will return the filename in the output, which you will not want in this case. To remove this, add the "-h" option to grep. In addition, when you set the variable "today", you need to look for the entire variable as a string, which needs the string within quotes:

```
#!/bin/bash
Expand All @@ -198,15 +198,15 @@ today=`date +"%b %e"`
grep -h "$today" /var/log/messages
```

That's it for now, so save your changes and then make the script executable:
That is it for now. Save your changes and make the script executable:

`chmod +x /usr/local/sbin/test.sh`

And then let's test it:
Test it:

`/usr/local/sbin/test.sh`

If all works correctly, you should get a long list of all of the messages in /var/log/messages from today, including but not limited to the dbus-daemon messages. If so, then the next step is to limit the messages to the dbus-daemon messages. So let's modify our script again:
If all works correctly, you will get a long list of all of the messages in _/var/log/messages_ from today, including but not limited to the dbus-daemon messages. The next step is to limit the messages to the dbus-daemon messages. Change your script again:

`vi /usr/local/sbin/test.sh`

Expand All @@ -220,13 +220,13 @@ today=`date +"%b %e"`
grep -h "$today" /var/log/messages | grep dbus-daemon
```

Running the script again, should get you only the dbus-daemon messages and only the ones that occurred today (whenever you're following this guide).
Running the script again, will get you only the dbus-daemon messages and only the ones that occurred today.

There's one final step, however. Remember, we need to get this emailed to the administrator for review. Also, because we are only using _postfix_ on this server for reporting, we don't want to leave the service running, so we will start it at the beginning of the script and then stop it at the end. We'll introduce the _sleep_ command here to pause for 20 seconds to make sure that the email has been sent before shutting _postfix_ down again. This final edit, adds the stop, start, and sleep issues just discussed, and also pipes the content to the administrator's email.
One final step remains. Remember, you need to get this emailed to the administrator for review. Because you are only using `postfix` on this server for reporting, you do not want to leave the service running. Start it at the beginning of the script and stop it at the end. Here, the `sleep` command pauses for 20 seconds, ensuring sending the email before shutting `postfix` down again. This final edit, adds the stop, start, and sleep issues just discussed, and also pipes the content to the administrator's email.

`vi /usr/local/sbin/test.sh`

And modify the script:
Change the script:

```
#!/bin/bash
Expand All @@ -247,10 +247,10 @@ sleep 20
/usr/bin/systemctl stop postfix
```

Run the script again, and you should now have an email from the server with the dbus-daemon message.
Run the script again, and you will now have an email from the server with the dbus-daemon message.

You can now use [a crontab](../automation/cron_jobs_howto.md) to schedule this to run at a specific time.

## Conclusion

Using postfix can help you keep track of process logs that you want to monitor. You can use it along with bash scripting to gain a firm grasp of your system processes and be informed if there is trouble.
Using `postfix` can help you keep track of process logs that you want to monitor. You can use it along with bash scripting to gain a firm grasp of your system processes and be informed if there is trouble.

0 comments on commit 771464e

Please sign in to comment.