<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Ssmtp on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/ssmtp/</link>
    <description>Recent content in Ssmtp on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Sun, 08 Dec 2013 12:25:41 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/ssmtp/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How to make logrotate use SSMTP</title>
      <link>https://mpcabd.xyz/make-logrotate-use-ssmtp/</link>
      <pubDate>Sun, 08 Dec 2013 12:25:41 +0000</pubDate>
      <guid>https://mpcabd.xyz/make-logrotate-use-ssmtp/</guid>
      <description>&lt;p&gt;A few days ago I was looking at my server logs, my website logs are more than a year old and growing, so I was reading some of them and wanted to download the old logs, truncate the logs, so that next time I read the logs I won’t have to look at very old data. So basically I wanted to rotate the logs and archive them, luckily &lt;a title=&#34;logrotate man page&#34; href=&#34;http://linuxcommand.org/man_pages/logrotate8.html&#34; target=&#34;_blank&#34;&gt;logrotate&lt;/a&gt; is there to let me manage and administer log files in my system. It has a long list of features with a very easy configuration, so it was the tool I’m looking for.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>A few days ago I was looking at my server logs, my website logs are more than a year old and growing, so I was reading some of them and wanted to download the old logs, truncate the logs, so that next time I read the logs I won’t have to look at very old data. So basically I wanted to rotate the logs and archive them, luckily <a title="logrotate man page" href="http://linuxcommand.org/man_pages/logrotate8.html" target="_blank">logrotate</a> is there to let me manage and administer log files in my system. It has a long list of features with a very easy configuration, so it was the tool I’m looking for.</p>
<p>logrotate uses the system’s mail <code>/bin/mail</code> to send the email when a log file is to be archived and removed from the system. But my system doesn’t use /bin/mail, it uses <a title="SSMTP man page" href="http://linux.die.net/man/8/ssmtp" target="_blank">SSMTP</a> with some custom configurations. I wanted to use SSMTP with logrotate but apparently it’s not a very simple task, since the man page for logrotate says:</p>
<blockquote>
<p>Option -m, –mail <command></p>
</blockquote>
<blockquote>
<p>Tells logrotate which command to use when mailing logs. This command should accept two arguments: <strong>1) the subject of the message, and 2) the recipient. The command must then read a message on standard input and mail it to the recipient.</strong> The default mail command is /bin/mail -s.</p>
</blockquote>
<p>Which is not actually how SSMT works, SSMTP as it’s described in the man page accepts the recipients emails as command line arguments, and reads the message (along with it’s subject header) from the standard input.</p>
<h2 id="the-shell-script">The shell script</h2>
<p>The solution to this problem would be by creating a shell script that behaves like a middleware between logrotate and SSMTP.</p>
<p>The shell script would simply read the arguments passed from logrotate and then build the right command to call SSMTP with by prepending the appropriate SMTP headers (To, Subject, From) to the message.</p>
<p>You can download the shell script from this link <a title="logrotate_smtp.sh Gist on GitHub" href="https://gist.github.com/mpcabd/03ce7d302eb4e1f5bfb7" target="_blank">logrotate_ssmtp.sh</a>.</p>
<p>As you can easily see that the shell script only accepts two arguments, so it sends the email to only one person, if you want to have more than one recipient and you’ve configured logrotate to send them the email, you’ll have to modify the script to accept more arguments and pass them to SSMTP.</p>
<p>Also you’ll have to replace <code>server@example.com</code> with the email address you’re sending from.</p>
<h2 id="configuring-logrotate">Configuring logrotate</h2>
<p>Now all we have to do is to configure logrotate to use the shell script as a mail command instead of /bin/mail. To do so you’ll have to edit all the calls that you make to logrotate to be like:</p>
<p><code>/usr/sbin/logrotate -m '~/logrotate_ssmtp.sh' /etc/logrotate.conf</code></p>
<p>The main call that you should change is located in <code>cron.daily</code> directory, I’m using Ubuntu so I have to edit this file <code>/etc/cron.daily/logrotate</code> and change the command at the end of the file to the command above.</p>
<p>Hope this helps you with your logs 🙂<figure style="width: 648px" class="wp-caption aligncenter"></p>
<p><img class="" alt="Logs Vancouver - photo by ahisgett on Flickr" src="http://images.cdn.fotopedia.com/flickr-220279395-original.jpg" width="648" height="972" /><figcaption class="wp-caption-text">photo by <a href="http://www.flickr.com/photos/37804979@N00" target="_blank">ahisgett</a> on Flickr</figcaption></figure></p>]]></content:encoded>
    </item>
  </channel>
</rss>
