<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Lazy Sys Admin&#187; PgFouine</title>
	<atom:link href="http://www.thelazysysadmin.net/tag/pgfouine/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thelazysysadmin.net</link>
	<description>Why do things the hard way?</description>
	<lastBuildDate>Mon, 07 Jun 2010 22:24:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PgFouine Automatic Report Setup with PostgreSQL &amp; Logrotate</title>
		<link>http://www.thelazysysadmin.net/2009/08/pgfouine-automatic-report-setup-with-postgresql-logrotate/</link>
		<comments>http://www.thelazysysadmin.net/2009/08/pgfouine-automatic-report-setup-with-postgresql-logrotate/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 04:49:06 +0000</pubDate>
		<dc:creator>Jon Smith</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Sys Admin]]></category>
		<category><![CDATA[Logrotate]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[PgFouine]]></category>
		<category><![CDATA[Syslog]]></category>

		<guid isPermaLink="false">http://www.thelazysysadmin.net/?p=605</guid>
		<description><![CDATA[Monitoring &#38; analysing your PostgreSQL server logs with PgFouine provides you with a way to see which queries are performing badly and need optimisation. It can also provide insight into busy periods and give some basic query stats such as the number of INSERT, DELETE, UPDATE, and SELECT operations. Requirements PgFouine php Logrotate PostgreSQL setup [...]]]></description>
			<content:encoded><![CDATA[<p>Monitoring &amp; analysing your PostgreSQL server logs with PgFouine provides you with a way to see which queries are performing badly and need optimisation. It can also provide insight into busy periods and give some basic query stats such as the number of INSERT, DELETE, UPDATE, and SELECT operations.</p>
<p><span id="more-605"></span><strong>Requirements</strong></p>
<ul>
<li><a title="PgFouine" href="http://pgfouine.projects.postgresql.org/" target="_blank">PgFouine</a></li>
<li>php</li>
<li>Logrotate</li>
<li>PostgreSQL setup to log to syslog (Tested with PostgreSQL 8.3 but should work with earlier versions)</li>
</ul>
<p><strong>PostgreSQL Setup</strong></p>
<p>First you need to setup PostgreSQL to log to syslog. Set the following in your postgresql.conf</p>
<pre class="brush: plain;">log_destination = 'syslog'

# Ensure that the following lines are REMMED out
#
# logging_collector
# log_directory
# log_filename
# log_truncate_on_rotation</pre>
<p>You may set the &#8220;syslog_facility&#8221; and &#8220;syslog_ident&#8221; options if you wish, but i handle these items in the syslog-ng.conf file.</p>
<p>You will also need to tell Postgres what to log</p>
<pre class="brush: plain;">client_min_messages = notice            # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   log
                                        #   notice
                                        #   warning
                                        #   error

log_min_messages = notice               # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic

log_error_verbosity = default           # terse, default, or verbose messages

log_min_error_statement = debug2        # values in order of decreasing detail:
                                        #   debug5
                                        #   debug4
                                        #   debug3
                                        #   debug2
                                        #   debug1
                                        #   info
                                        #   notice
                                        #   warning
                                        #   error
                                        #   log
                                        #   fatal
                                        #   panic (effectively off)

log_min_duration_statement = 200        # -1 is disabled, 0 logs all statements
                                        # and their durations, &amp;gt; 0 logs only
                                        # statements running at least this time.
log_line_prefix = '%t %d %u %h '</pre>
<p>I have a fairly busy DB server so I am only interested in queries that take longer then 200ms. You will need to set this value to suit your needs, set it to low and you will produce many log lines, set it to high and you wont produce anything.</p>
<p><strong>Syslog Setup</strong></p>
<p>Next you need to setup syslog to log PostgreSQL to a different file (the following instruction will work for Syslog-ng on an OpenSUSE box, your milage may vary).</p>
<p>Setup a filter:</p>
<pre class="brush: plain;">filter f_postgres   { match('^postgres'); };</pre>
<p>And a Destination:</p>
<pre class="brush: plain;"># PostgreSQL messages
destination postgres { file(&quot;/var/log/pgsql/postgresql.log&quot;); };
log { source(src); filter(f_postgres); destination(postgres); flags(final); };</pre>
<p><strong>Scripts</strong></p>
<p>Create the following as a script for bash. Ensure you setup the execute bit on the file ie &#8220;chmod +x pgfouine_process&#8221;.</p>
<pre class="brush: plain;">#!/bin/bash

PGFOUINE=&quot;/srv/www/htdocs/pgfouine/pgfouine.php&quot;
DATESTAMP=$(date +%G%m%d)
YEAR=$(date +%G)
MONTH=$(date +%m)
REPORTDIR=&quot;/srv/www/htdocs/reports&quot;
PGSQLLOG=&quot;/var/log/pgsql/postgresql.log&quot;

mkdir $REPORTDIR/$YEAR/$MONTH -p

php $PGFOUINE -file $PGSQLLOG &amp;gt; $REPORTDIR/$YEAR/$MONTH/$DATESTAMP.htm</pre>
<p>You may customise this script to suit your needs depending on where you put the PgFouine files and where you want to store/serve your reports from. You may also with to send the report via email once it is generated, the sky is the limit (ok, sorry for the cliche).</p>
<p><strong>Logrotate</strong></p>
<p>To execute this script each time the log file is rotated put the following file into your /etc/logrotate.d directory.</p>
<pre class="brush: plain;">/var/log/pgsql/postgresql.log {
    compress
    dateext
    rotate 99
    daily
    notifempty
    missingok
    create 640 postgres users
    copytruncate
    prerotate
        /var/lib/pgsql/bin/pgfouine_process
    endscript
}</pre>
<p>The important thing for this is the prerotate section. This will run the script first before rotating the log file.</p>
<p>If you don&#8217;t have Logrotate setup you could easily run the pgfouine script on a daily basis with Cron.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thelazysysadmin.net/2009/08/pgfouine-automatic-report-setup-with-postgresql-logrotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
