wiki:MuninConfigurationMasterCGI

Dynamic generation of munin graphs and html

Attention: Content of this page should be moved to the Munin-Guide --> Visit the Guide now.
Attention: There are updated configuration examples available on the Munin Guide

Munin 2.0 can generate HTML and graphs dynamically. This enables munin to scale better for a master with many nodes.

Munin supports CGI and FastCGI.

Munin configuration

You need the following in /etc/munin/munin.conf to generate graphs and HTML dynamically:

html_strategy cgi
graph_strategy cgi

Debugging

The FastCGI processes will log to "/var/log/munin/munin-cgi-graph.log" and "/var/log/munin/munin-cgi-html.log".

Note that these run with the web server's permissions, unless specified otherwise. If the web server does not have write access to the specific log files (or access to the directory they are in), the cgi processes will hang, and not produce any output.

Upgrading from before 2.0.6

in "munin.conf" there needs to be:

cgiurl_graph /munin-cgi/munin-cgi-graph

and the apache configuration needs to have

       ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph
       <Location /munin-cgi/munin-cgi-graph>
               […]
       </Location>

or similar. And the same for munin-cgi-html, see following example:

Web server configuration

Apache 2

This example will make graphs and HTML with FastCGI if the FastCGI module is installed, or fall back to CGI if not. (Depending on your apache configuration you might also find the [PT] flag useful on rewrites if Alias or ScriptAlias? directives need to be applied post rewrite)

virtualhost configuration

Place the following in your Apache HTTPD virtual host configuration directory.

<VirtualHost *:80>
    ServerName munin.example.org
    ServerAlias munin
    Alias /static /var/cache/munin/www/static
    Alias /munin /var/cache/munin/www

      #apache debian
     <Directory "/var/cache/munin/www/static/">
        Options None
        Order allow,deny
        Allow from all
     </Directory>


    <IfModule !mod_rewrite.c>
        # required because we serve out of the cgi directory and URLs are relative
        Alias /munin-cgi/munin-cgi-html/static /var/cache/munin/www/static
        RedirectMatch ^/$ /munin-cgi/munin-cgi-html/
    </IfModule>

    <IfModule mod_rewrite.c>
        # Rewrite rules to serve traffic from the root instead of /munin-cgi
        RewriteEngine On

        # Static files
        RewriteRule ^/favicon.ico /var/cache/munin/www/static/favicon.ico [L]
        RewriteRule ^/static/(.*) /var/cache/munin/www/static/$1          [L]

        # HTML
        RewriteRule ^(/.*\.html)?$           /munin-cgi/munin-cgi-html/$1 [PT]

        # Images
        RewriteRule ^/munin-cgi/munin-cgi-graph/(.*) /$1
        RewriteCond %{REQUEST_URI}                 !^/static
        RewriteRule ^/(.*.png)$  /munin-cgi/munin-cgi-graph/$1 [L,PT]
    </IfModule>

    # Ensure we can run (fast)cgi scripts
    ScriptAlias /munin-cgi/munin-cgi-graph /usr/lib/munin/cgi/munin-cgi-graph
    <Location /munin-cgi/munin-cgi-graph>
        Options +ExecCGI
        <IfModule mod_fcgid.c>
            SetHandler fcgid-script
        </IfModule>
        <IfModule mod_fastcgi.c>
            SetHandler fastcgi-script
        </IfModule>
        <IfModule !mod_fastcgi.c>
            <IfModule !mod_fcgid.c>
                SetHandler cgi-script
            </IfModule>
        </IfModule>
        Allow from all
       # Replace above with "Require all granted" if running apache 2.4
    </Location>

    ScriptAlias /munin-cgi/munin-cgi-html /usr/lib/munin/cgi/munin-cgi-html
    <Location /munin-cgi/munin-cgi-html>
        Options +ExecCGI
        <IfModule mod_fcgid.c>
            SetHandler fcgid-script
        </IfModule>
        <IfModule mod_fastcgi.c>
            SetHandler fastcgi-script
        </IfModule>
        <IfModule !mod_fastcgi.c>
            <IfModule !mod_fcgid.c>
                SetHandler cgi-script
            </IfModule>
        </IfModule>
        Allow from all
       # Replace above with "Require all granted" if running apache 2.4
    </Location>

    <IfModule !mod_rewrite.c>
        <Location /munin-cgi/munin-cgi-html/static>
                # this needs to be at the end to override the above sethandler directives
                Options -ExecCGI
                SetHandler None
        </Location>
    </IfModule>
</VirtualHost>

See also the sample config in the Munin manual.

install needed software

On Debian, Ubuntu and related distributions. Install and enable needed modules:

apt-get install libapache2-mod-fcgid
a2enmod rewrite
a2enmod fcgid

On RedHat?, CentOS, Fedora and related distributions:

yum install mod_fcgid

(Note: I _think_ mod_fcgid and mod_rewrite are enabled by default. If not, enable them)

(There is also a mod_fastcgi module, which works, but this is not free software. Change "mod_fcgid" to "mod_fastcgi" if you use this instead.)

Additional configuration

Centos 5.3

The "FastCgiIpcDir?" directive should not be needed, but my CentOS 5.3 server has some problems with the IPC directory. I also have to give these commands as root:

mkdir /tmp/dynamic
chown apache:apache /tmp/dynamic
chmod 1777 /tmp/dynamic

which also is not completely right, but is still needed for some reason I can't figure out.

CentOS 6.x

If you are using fastcgi as described above then the following applies.

  • /usr/lib/munin/cgi/munin-cgi-graph needs to be /var/www/html/munin/cgi/munin-fastcgi-graph
  • /var/log/munin/munin-cgi-graph.log needs to be /var/log/munin/munin-fastcgi-graph.log with write permissions granted to group apache
FreeBSD

change above virtualhost configuration:

replace /var/cache/munin/www/ with /usr/local/etc/munin/ (Line 7,16,17)

replace /usr/lib/munin/cgi/ with /usr/local/www/cgi-bin/ (Line 29,46:)

add line to newsyslog.conf:

/var/log/munin/munin-cgi*.log www:www 755 7 * @T00 GNWZ

nginx

nginx does not spawn your FCGI-process automatically, so you need to spawn in manually, e.g. with spawn-fcgi.

You can get spawn-fcgi with your packet manager. On Debian / Ubuntu based systems run:

apt-get install spawn-fcgi

CGI graph

Spawn your FCGI-process with the following options:

spawn-fcgi -s /var/run/munin/fcgi-graph.sock -U www-data -u www-data -g www-data munin-fastcgi-graph

On Debian / Ubuntu the files are located slightly different:

spawn-fcgi -s /var/run/munin/fcgi-graph.sock -U www-data -u www-data -g www-data /usr/lib/munin/cgi/munin-cgi-graph

Note, munin-fastcgi-graph should really run as user www-data (or your distrib standard httpd user), otherwise you'll open write access to the CGI process. The user www-data should be able to read the state files, the rrd and write to the cgi-tmp dir.

Previous version of this page wrongly advocated using the munin user. It's a very tempting idea for simplicity, but don't do that. Also, don't forget to write an init.d script to spawn munin-fast-cgi-graph on system startup. An example can be found here : http://files.julienschmidt.com/public/cfg/munin/spawn-fcgi-munin-graph .

Configure your Nginx-vHost:

    location ^~ /cgi-bin/munin-cgi-graph/ {
	access_log off;
	fastcgi_split_path_info ^(/cgi-bin/munin-cgi-graph)(.*);
	fastcgi_param PATH_INFO $fastcgi_path_info;
	fastcgi_pass unix:/var/run/munin/fcgi-graph.sock;
	include fastcgi_params;
    }

CGI html

Spawn your FCGI-process with the following options:

spawn-fcgi -s /var/run/munin/fcgi-html.sock -U www-data -u www-data -g munin munin-fastcgi-html

On Debian / Ubuntu the files are located slightly different:

spawn-fcgi -s /var/run/munin/fcgi-html.sock -U www-data -u www-data -g munin  /usr/lib/munin/cgi/munin-cgi-html

Note, munin-fastcgi-html should really run as user www-data (or your distrib standard httpd user), otherwise you'll open write access to the CGI process. The user www-data should be able to read the state files, the rrd and write to the cgi-tmp dir.

Previous version of this page wrongly advocated using the munin user. It's a very tempting idea for simplicity, but don't do that. Also, don't forget to write an init.d script to spawn munin-fast-cgi-html on system startup. An example can be found here : http://files.julienschmidt.com/public/cfg/munin/spawn-fcgi-munin-html.

Configure your Nginx-vHost:

    location /munin/static/ {
	alias /etc/munin/static/;
    }

    location /munin/ {
	fastcgi_split_path_info ^(/munin)(.*);
	fastcgi_param PATH_INFO $fastcgi_path_info;
	fastcgi_pass unix:/var/run/munin/fcgi-html.sock;
	include fastcgi_params;
    }

Debugging

Most probably there is a issue with user rights, if the FCGI-process doesn't spawn correctly. Often it helps to run spawn-fcgi manually without forking, since forking hides possible error output: (notice the added -n parameter)

spawn-fcgi -s /var/run/munin/fcgi-html.sock -U www-data -u www-data -g munin  /usr/lib/munin/cgi/munin-cgi-html -n

lighttpd

This example will do CGI graphs, but lacks CGI html. Submissions welcome. :)

    fastcgi.server    = ( "/munin-cgi/munin-cgi-graph" =>
        (( 
            "bin-path" => " /usr/lib/munin/cgi//munin-cgi-graph",
            "socket" => "/tmp/munin-cgi-graph.sock",
            "check-local" => "disable",
        ))
    )
    alias.url += (
        "/munin/" => "/var/cache/munin/www/"
    )

This is a full and working example of a lighttpd configuration that uses CGI for both graph and html (requires enabling the mod_rewrite module in the main lighttpd.conf configuration file):

alias.url += ( "/munin/static" => "/etc/munin/static" )
alias.url += ( "/munin" => "/var/cache/munin/www/" )

fastcgi.server += ("/munin-cgi/munin-cgi-graph" =>
                   (( "socket" => "/var/run/lighttpd/munin-cgi-graph.sock",
                      "bin-path" => "/usr/lib/munin/cgi/munin-cgi-graph",
                      "check-local" => "disable",
                   )),
                  "/munin-cgi/munin-cgi-html" =>
                   (( "socket" => "/var/run/lighttpd/munin-cgi-html.sock",
                      "bin-path" => "/usr/lib/munin/cgi/munin-cgi-html",
                      "check-local" => "disable",
                   ))
                 )

url.rewrite-repeat += (
                   "^/munin/((?!static/).*\.png$)" => "/munin-cgi/munin-cgi-graph/$1",
                   "^/munin/((?!static/).*\.html$)" => "/munin-cgi/munin-cgi-html/$1"
                   )
Last modified at 2015-07-20T15:23:51+02:00 Last modified on 2015-07-20T15:23:51+02:00