<?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>へびにっき &#187; サーバ管理</title>
	<atom:link href="http://wp.serpere.info/archives/tag/%e3%82%b5%e3%83%bc%e3%83%90%e7%ae%a1%e7%90%86/feed" rel="self" type="application/rss+xml" />
	<link>http://wp.serpere.info</link>
	<description>樹上で暮らすヘビのように生きたい</description>
	<lastBuildDate>Thu, 09 Feb 2012 11:35:51 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>muninの集計負荷をtmpfsで低減する（集計時間の短縮も）</title>
		<link>http://wp.serpere.info/archives/2272</link>
		<comments>http://wp.serpere.info/archives/2272#comments</comments>
		<pubDate>Tue, 19 Jul 2011 12:45:21 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[munin]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=2272</guid>
		<description><![CDATA[リソース監視ツールのmuninは手軽に使えて素晴らしいのだが、監視対象のサーバが増えてくると集計サーバの負荷（主にディスクI/O）が馬鹿にならなくなってくる。そこでこちらのサイトを参考にして、データファイルとHTMLファイルをtmpfs上に移動してみた。 環境 CentOS 6 munin-1.4.5 (EPELリポジトリからインストール） メモリ: 8GB データファイルとHTMLファイルの場所は /etc/munin/munin.conf の dbdir と htmldir で変更できる…はずなのだが、プラグインの中には /var/lib/munin のパスを決め打ちにしているものがあるらしく、dbdir を変更するとうまく動かなかった。仕方がないのでHTMLファイルを /var/lib/munin 以下に移動して、tmpfs を /var/lib/munin にマウントすることにした。 まずHTMLファイルを移動する。以下の操作はroot権限で、muninの集計を停止してから行う。 muninの集計を止めるには、munin-cronの実行権限を外すか、/etc/cron.d/munin を編集してcronによる定期実行を止める。 # munin-cronの停止 chmod ugo-x /usr/bin/munin-cron &#160; # 念のためmuninプロセスが動いていないか確認しておく ps aux &#124; grep munin 次にHTMLファイルをディレクトリごとコピー、そのパスにあわせてmuninとApacheの設定を変更する。 cp -a /var/www/html/munin/ /var/lib/munin/html &#160; vim /etc/munin/munin.conf # htmldir /var/lib/munin/html &#160; vim /etc/httpd/conf.d/munin.conf # 私の環境ではcgiディレクトリは [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F2272%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22munin%E3%81%AE%E9%9B%86%E8%A8%88%E8%B2%A0%E8%8D%B7%E3%82%92tmpfs%E3%81%A7%E4%BD%8E%E6%B8%9B%E3%81%99%E3%82%8B%EF%BC%88%E9%9B%86%E8%A8%88%E6%99%82%E9%96%93%E3%81%AE%E7%9F%AD%E7%B8%AE%E3%82%82%EF%BC%89%22%20%7D);"></div>
<p>
リソース監視ツールのmuninは手軽に使えて素晴らしいのだが、監視対象のサーバが増えてくると集計サーバの負荷（主にディスクI/O）が馬鹿にならなくなってくる。そこで<a href="http://www.glidenote.com/archives/1054">こちらのサイト</a>を参考にして、データファイルとHTMLファイルをtmpfs上に移動してみた。
</p>
<h3>環境</h3>
<ul>
<li>CentOS 6</li>
<li>munin-1.4.5 (EPELリポジトリからインストール）</li>
<li>メモリ: 8GB</li>
</ul>
<p>
データファイルとHTMLファイルの場所は /etc/munin/munin.conf の dbdir と htmldir で変更できる…はずなのだが、プラグインの中には /var/lib/munin のパスを決め打ちにしているものがあるらしく、dbdir を変更するとうまく動かなかった。仕方がないのでHTMLファイルを /var/lib/munin 以下に移動して、tmpfs を /var/lib/munin にマウントすることにした。
</p>
<p>
まずHTMLファイルを移動する。以下の操作はroot権限で、muninの集計を停止してから行う。
</p>
<p>
muninの集計を止めるには、munin-cronの実行権限を外すか、/etc/cron.d/munin を編集してcronによる定期実行を止める。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># munin-cronの停止</span>
<span style="color: #c20cb9; font-weight: bold;">chmod</span> ugo-x <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>munin-cron
&nbsp;
<span style="color: #666666; font-style: italic;"># 念のためmuninプロセスが動いていないか確認しておく</span>
<span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> munin</pre></div></div>



<p>
次にHTMLファイルをディレクトリごとコピー、そのパスにあわせてmuninとApacheの設定を変更する。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-a</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>html<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>html
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>munin.conf
<span style="color: #666666; font-style: italic;"># htmldir /var/lib/munin/html</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>httpd<span style="color: #000000; font-weight: bold;">/</span>conf.d<span style="color: #000000; font-weight: bold;">/</span>munin.conf
<span style="color: #666666; font-style: italic;"># 私の環境ではcgiディレクトリは /var/www/munin/cgi に移してあるので</span>
<span style="color: #666666; font-style: italic;"># ScriptAlias /munin/cgi/ /var/www/munin/cgi/</span>
<span style="color: #666666; font-style: italic;"># Alias /munin/ /var/lib/munin/html/</span></pre></div></div>



<p>
ここまで終えたら一旦muninの集計を再開し、正しく動くことを確認しておく。確認できたら再び集計を停止して、tmpfsの設定に移る。tmpfsに割り当てる容量だが、とりあえず参考サイトに従って1GBとした。二十数サーバを数年間監視した結果のデータ+HTMLファイルが300MB弱だったので、1GBあれば当分は大丈夫だろう。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vim</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>fstab
<span style="color: #666666; font-style: italic;">#tmpfs  /var/lib/munin  tmpfs  rw,size=1024M   0 0</span></pre></div></div>



<p>
一旦データを退避してから /var/lib/munin にtmpfsをマウントし、退避しておいたデータを戻す。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin.backup
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin
<span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin
<span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-a</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin.backup<span style="color: #000000; font-weight: bold;">/*</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>



<p>
これでmuninが更新するファイルは全てメモリ上に置かれることになった。が、このままだとマシンが再起動されるとデータが消滅してしまうので、バックアップ策を講じる必要がある。参考サイトではcronを使って1時間ごとにバックアップを行っているが、それだと更新中の不完全なデータがバックアップされる危険性がある。そもそもmuninの集計はcronで5分ごとに実行されるのだから、次のような方針でバックアップ/リストアをすることにした。
</p>
<ul>
<li>30分に一回、集計の「後に」データをバックアップする</li>
<li>毎回、集計の「前に」データの有無をチェックして、なければデータをリストアする。このチェックは単純にファイルの有無を調べるだけなので、5分ごとに実行しても負担にはならない</li>
</ul>
<p>
このためにmunin-cronをラップする次のようなシェルスクリプトを書いた。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #007800;">rsync</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>rsync
<span style="color: #007800;">munin_cron</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>munin-cron
<span style="color: #007800;">munin_dir</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>munin
<span style="color: #007800;">backup_dir</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>backup
<span style="color: #007800;">lock_file</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>munin-cron-backup.lock
&nbsp;
die<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$1</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
lock<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$lock_file</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> die <span style="color: #ff0000;">&quot;Backup/retore process is already runnning&quot;</span> 
        <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$lock_file</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
unlock<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$lock_file</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
backup<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        lock <span style="color: #000000; font-weight: bold;">||</span> die <span style="color: #ff0000;">&quot;Failed to lock the backup process&quot;</span>
        <span style="color: #007800;">$rsync</span> <span style="color: #660033;">-au</span> <span style="color: #007800;">$munin_dir</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #007800;">$backup_dir</span><span style="color: #000000; font-weight: bold;">/</span>
        unlock
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
restore<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
        lock <span style="color: #000000; font-weight: bold;">||</span> die <span style="color: #ff0000;">&quot;Failed to lock the restore process&quot;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #ff0000;">&quot;(&quot;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$munin_dir</span><span style="color: #000000; font-weight: bold;">/</span>datafile <span style="color: #660033;">-a</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$munin_dir</span><span style="color: #000000; font-weight: bold;">/</span>html <span style="color: #ff0000;">&quot;)&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #007800;">$rsync</span> <span style="color: #660033;">-au</span> <span style="color: #007800;">$backup_dir</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #007800;">$munin_dir</span><span style="color: #000000; font-weight: bold;">/</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
        unlock
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
restore
&nbsp;
<span style="color: #7a0874; font-weight: bold;">test</span> <span style="color: #660033;">-x</span> <span style="color: #007800;">$munin_cron</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #007800;">$munin_cron</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> = <span style="color: #ff0000;">&quot;backup&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        backup
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>



<p>
このスクリプトでは datafile というファイルと html ディレクトリの存在をもって、「データが存在する」と判断する。一応、同時実行を避けるための簡易的なロックも付けてみた。5分に一回しか実行されないと分かっているのだから、この程度でも多少は意味があるだろう。各種コマンドやバックアップ先などのパスは好みで変えて欲しい。私はこのシェルスクリプトやバックアップを /home/munin 以下にまとめることにした。
</p>
<p>
あとはmunin-cronの代わりに、/etc/cron.d/munin の中でこのスクリプトを実行するよう設定する。30分に一回は引数に backup を与えて実行することで、バックアップが作成される。
</p>
<pre>
MAILTO=root

0,30 * * * * munin /home/munin/bin/munin-cron.sh backup
5,10,15,20,25,35,40,45,50,55 * * * * munin /home/munin/bin/munin-cron.sh
</pre>
<p>
リストアは集計前に勝手に行われるので、再起動しても何もしなくていい。
</p>
<p>
最後に実施前後のCPU使用率のグラフを載せておく。確かにI/O待ちはガクンと減った（ちょっとだけ出ているのはバックアップ）。
</p>
<div id="attachment_2287" class="wp-caption alignnone" style="width: 310px"><a href="http://wp.serpere.info/wp-content/uploads/2011/07/cpu-week.png"><img src="http://wp.serpere.info/wp-content/uploads/2011/07/cpu-week-300x219.png" alt="" title="munin-cpu-week" width="300" height="219" class="size-medium wp-image-2287" /></a><p class="wp-caption-text">CPU使用率の変化</p></div>
<p>
それから集計に要する時間も多少は短縮されたようだ。もっともネットワーク経由でクライアントに接続する部分がボトルネックになっているようなので、あまり結果は安定していない。
</p>
<div id="attachment_2290" class="wp-caption alignnone" style="width: 310px"><a href="http://wp.serpere.info/wp-content/uploads/2011/07/munin_stats-week.png"><img src="http://wp.serpere.info/wp-content/uploads/2011/07/munin_stats-week-300x190.png" alt="" title="munin_stats-week" width="300" height="190" class="size-medium wp-image-2290" /></a><p class="wp-caption-text">munin集計時間の変化</p></div>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/2272/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginxでメンテナンス中画面を表示する</title>
		<link>http://wp.serpere.info/archives/2145</link>
		<comments>http://wp.serpere.info/archives/2145#comments</comments>
		<pubDate>Thu, 07 Apr 2011 14:48:26 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=2145</guid>
		<description><![CDATA[どのURLにアクセスされてもステータスコード503を返して /home/www/maintenance.html を表示する設定。 server { server_name your.hostname; error_page 503 /maintenance.html; location / { return 503; } location = /maintenance.html { root /home/www; } } 同等の設定をApache(>=2.2)で書くと次のようになる。 &#60;VirtualHost *:80&#62; ServerName your.hostname DocumentRoot /home/www &#160; RewriteEngine On RewriteCond %{REQUEST_URI} !=/maintenance.html RewriteRule .* - [R=503] &#160; ErrorDocument 503 /maintenance.html &#60;/VirtualHost&#62;]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F2145%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22nginx%E3%81%A7%E3%83%A1%E3%83%B3%E3%83%86%E3%83%8A%E3%83%B3%E3%82%B9%E4%B8%AD%E7%94%BB%E9%9D%A2%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>
どのURLにアクセスされてもステータスコード503を返して /home/www/maintenance.html を表示する設定。
</p>
<pre>
server {
    server_name  your.hostname; 
    error_page 503 /maintenance.html; 
    location / { 
        return 503;
    }
    location = /maintenance.html {
        root /home/www;
    }
}
</pre>
<p>
同等の設定をApache(>=2.2)で書くと次のようになる。
</p>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;">&lt;<span style="color: #000000; font-weight:bold;">VirtualHost</span> *:<span style="color: #ff0000;">80</span>&gt;
    <span style="color: #00007f;">ServerName</span> your.hostname
    <span style="color: #00007f;">DocumentRoot</span> /home/www
&nbsp;
    <span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
    <span style="color: #00007f;">RewriteCond</span> %{REQUEST_URI} !=/maintenance.html
    <span style="color: #00007f;">RewriteRule</span> .* - [R=<span style="color: #ff0000;">503</span>]
&nbsp;
    <span style="color: #00007f;">ErrorDocument</span> <span style="color: #ff0000;">503</span> /maintenance.html
&lt;/<span style="color: #000000; font-weight:bold;">VirtualHost</span>&gt;</pre></div></div>



]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/2145/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>InnoDBの意外な制約: Got error 139 from storage engine</title>
		<link>http://wp.serpere.info/archives/1979</link>
		<comments>http://wp.serpere.info/archives/1979#comments</comments>
		<pubDate>Mon, 13 Dec 2010 13:18:41 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1979</guid>
		<description><![CDATA[環境: MySQL 5.0 （追記） 某CMSにて、1つのテーブルにTEXT型のフィールドをたくさん（10前後）作ったところ、次のようなエラーが出てデータを保存できなくなった。 Got error 139 from storage engine このエラーメッセージで検索すればいろいろと情報が出てくるが、こういうことらしい： InnoDBの行サイズの上限はページサイズの約半分で、デフォルトでは約8000バイト 可変長カラム(VARBINARY, VARCHAR, BLOB, TEXT)のデータは行の外部に保存されるが、先頭の768バイトだけは行の内部に保存される よって例えば一つのテーブルに11個のTEXT型フィールドを作り、それぞれに768バイト以上のデータを入れようとすると、768*11=8448 > 8000 なので保存できない ページサイズは8〜64KBまで設定できるが、変更するためにはMySQL本体をコンパイルし直した上でテーブルスペースとログファイルを再作成する必要があるらしく、運用途中での変更は難しい。よってどうしてもInnoDBでなければならない場合は、テーブルを分割するなどして対処するしかない。 件のCMSの場合は特にInnoDBでなくても良かったので、テーブルをMyISAMに変換することで回避できた。 ALTER TABLE TABLE_NAME ENGINE=MyISAM; MyISAMの場合、行サイズの上限は64KBで、かつTEXTやBLOBの保存に要するのは9〜12バイトなので、同じ問題はまず起こらない。最近は特に何も考えずにInnoDBを使っていたので、ヒヤリとさせられた。 参考にしたサイト： 13.2.11.2. File Space Management &#8211; MySQL 5.0 Reference Manual 10.5. Data Type Storage Requirements &#8211; MySQL 5.0 Reference Manual 限界までMySQLを使い尽くす!! &#8211; 漢(オトコ)のコンピュータ道 Nix::WebLab : Got error [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1979%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FhH0A5Y%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22InnoDB%E3%81%AE%E6%84%8F%E5%A4%96%E3%81%AA%E5%88%B6%E7%B4%84%3A%20Got%20error%20139%20from%20storage%20engine%22%20%7D);"></div>
<p>
環境: MySQL 5.0 （<strong><a href="#innodb-plugin-fix-the-problem">追記</a></strong>）
</p>
<p>
某CMSにて、1つのテーブルにTEXT型のフィールドをたくさん（10前後）作ったところ、次のようなエラーが出てデータを保存できなくなった。
</p>
<pre>Got error 139 from storage engine</pre>
<p>
このエラーメッセージで検索すればいろいろと情報が出てくるが、こういうことらしい：
</p>
<ul>
<li>InnoDBの行サイズの上限はページサイズの約半分で、デフォルトでは約8000バイト</li>
<li>可変長カラム(VARBINARY, VARCHAR, BLOB, TEXT)のデータは行の外部に保存されるが、先頭の768バイトだけは行の内部に保存される</li>
<li>よって例えば一つのテーブルに11個のTEXT型フィールドを作り、それぞれに768バイト以上のデータを入れようとすると、768*11=8448 > 8000 なので保存できない</li>
</ul>
<p>
ページサイズは8〜64KBまで設定できるが、変更するためにはMySQL本体をコンパイルし直した上でテーブルスペースとログファイルを再作成する必要があるらしく、運用途中での変更は難しい。よってどうしてもInnoDBでなければならない場合は、テーブルを分割するなどして対処するしかない。
</p>
<p>
件のCMSの場合は特にInnoDBでなくても良かったので、テーブルをMyISAMに変換することで回避できた。
</p>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM;</pre></div></div>



<p>
MyISAMの場合、行サイズの上限は64KBで、かつTEXTやBLOBの保存に要するのは9〜12バイトなので、同じ問題はまず起こらない。最近は特に何も考えずにInnoDBを使っていたので、ヒヤリとさせられた。
</p>
<p>
参考にしたサイト：
</p>
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/innodb-file-space.html">13.2.11.2. File Space Management</a> &#8211; MySQL 5.0 Reference Manual</li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html">10.5. Data Type Storage Requirements</a> &#8211; MySQL 5.0 Reference Manual</li>
<li><a href="http://nippondanji.blogspot.com/2009/05/mysql.html">限界までMySQLを使い尽くす!!</a> &#8211; 漢(オトコ)のコンピュータ道</li>
<li><a href="http://blog.livedoor.jp/nix_kobayashi/archives/51775930.html">Nix::WebLab : Got error 139 from storage engine</a>
</li>
</ul>
<h3 id="innodb-plugin-fix-the-problem">追記</h3>
<p>
SH2さんからのコメントで、MySQL 5.1＋InnoDB Plugin/MySQL 5.5以降で新たなファイルフォーマット（Barracuda）を使用すれば状況は改善される、と教えていただきました。どうやら<a href="http://dev.mysql.com/doc/innodb/1.1/en/innodb-compression-internals-storage-blobs.html">TEXT/BLOB一つあたり20バイトだけ</a>になるようです。詳細は以下のリンクなどを参照してください（私もまだ読んでいる途中ですが）。
</p>
<ul>
<li><a href="http://dev.mysql.com/doc/innodb/1.1/en/innodb-compression.html">MySQL :: InnoDB 1.1 for MySQL 5.5 User’s Guide :: 3 InnoDB Data Compression</a></li>
<li><a href="http://nippondanji.blogspot.com/2010/03/innodb-plugin.html">漢(オトコ)のコンピュータ道: InnoDB Pluginことはじめ。快適ストレージエンジン生活はじまる！ </a></li>
<li><a href="http://d.hatena.ne.jp/sh2/20090628">MySQL InnoDB Pluginのデータ圧縮機能 &#8211; SH2の日記</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1979/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CentOS 5にNginxをインストールする</title>
		<link>http://wp.serpere.info/archives/1799</link>
		<comments>http://wp.serpere.info/archives/1799#comments</comments>
		<pubDate>Mon, 25 Oct 2010 13:36:33 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[CentOS]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1799</guid>
		<description><![CDATA[CentOS 5.5にNginxをセットアップした記録。 EPELリポジトリにrpmパッケージもあるようだが、かなりバージョンが古かったので（0.6.39）ソースからインストールすることにした。現時点での最新版（stable）は0.8.53。 まず下準備。nginxユーザの追加とビルドに必要なパッケージのインストール。 sudo useradd -s /sbin/nologin -d /usr/local/nginx -M nginx sudo yum install gcc openssl-devel pcre-devel zlib-devel 続いてソースをダウンロードしてコンパイルする。Nginxはコンパイル時にしか拡張モジュールの組み込みができないので、ビルドの過程はシェルスクリプトの形で残しておいた方が良い。コンパイルオプションについてはWikiを参照のこと。一番下のExample 5（RedHat向け）を参考に調整を行った。 #!/bin/sh &#160; NGINX=nginx-0.8.53 &#160; # インストール先は /usr/local/nginx になる cd $NGINX \ &#38;&#38; make clean \ &#38;&#38; ./configure \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_stub_status_module \ --with-http_ssl_module \ [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1799%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F9sg9nn%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22CentOS%205%E3%81%ABNginx%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>
CentOS 5.5にNginxをセットアップした記録。
</p>
<p>
EPELリポジトリにrpmパッケージもあるようだが、かなりバージョンが古かったので（0.6.39）ソースからインストールすることにした。現時点での最新版（stable）は0.8.53。
</p>
<p>
まず下準備。nginxユーザの追加とビルドに必要なパッケージのインストール。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> useradd <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nologin <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #660033;">-M</span> nginx
<span style="color: #c20cb9; font-weight: bold;">sudo</span> yum <span style="color: #c20cb9; font-weight: bold;">install</span> <span style="color: #c20cb9; font-weight: bold;">gcc</span> openssl-devel pcre-devel zlib-devel</pre></div></div>



<p>
続いて<a href="http://wiki.nginx.org/Install">ソースをダウンロード</a>してコンパイルする。Nginxはコンパイル時にしか拡張モジュールの組み込みができないので、ビルドの過程はシェルスクリプトの形で残しておいた方が良い。コンパイルオプションについては<a href="http://wiki.nginx.org/NginxInstallOptions">Wiki</a>を参照のこと。一番下のExample 5（RedHat向け）を参考に調整を行った。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #007800;">NGINX</span>=nginx-0.8.53
&nbsp;
<span style="color: #666666; font-style: italic;"># インストール先は /usr/local/nginx になる</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #007800;">$NGINX</span> \
<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> clean \
<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> .<span style="color: #000000; font-weight: bold;">/</span>configure \
  <span style="color: #660033;">--conf-path</span>=<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>nginx.conf \
  <span style="color: #660033;">--error-log-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>error.log \
  <span style="color: #660033;">--pid-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>run<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>nginx.pid  \
  <span style="color: #660033;">--lock-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>lock<span style="color: #000000; font-weight: bold;">/</span>nginx.lock \
  <span style="color: #660033;">--user</span>=nginx \
  <span style="color: #660033;">--group</span>=nginx \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_gzip_static_module \
  --with-http_realip_module \
  <span style="color: #660033;">--http-log-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>access.log \
  <span style="color: #660033;">--http-client-body-temp-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>client<span style="color: #000000; font-weight: bold;">/</span> \
  <span style="color: #660033;">--http-proxy-temp-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>proxy<span style="color: #000000; font-weight: bold;">/</span> \
  <span style="color: #660033;">--http-fastcgi-temp-path</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>fcgi<span style="color: #000000; font-weight: bold;">/</span> \
<span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>



<p>
正常にビルドが完了したらインストール。足りないディレクトリも作っておく。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>proxy,client,fcgi<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>



<p>
この段階で正常に起動するかどうかを確認しておく。rootで <code>${prefix}/sbin/nginx</code> を実行後、<code>http://hostname/</code> にアクセスして、「Welcome to nginx!」と表示されたらOK。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx
&nbsp;
<span style="color: #666666; font-style: italic;"># 確認できたら終了しておく</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>nginx<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>nginx <span style="color: #660033;">-s</span> quit</pre></div></div>



<p>
この後、設定ファイル（上のコンパイルオプションの場合は <code>/etc/nginx/nginx.conf</code>）を編集してサーバの設定を行うことになるが、そこは省略。
</p>
<p>
サービス起動ファイルを作る。CentOS(RedHat系Linux)ならひな形が<a href="http://wiki.nginx.org/RedHatNginxInitScript">Wiki</a>に用意されているので、必要な部分を書き換えた後 <code>/etc/init.d/nginx</code> に保存して、 chkconfig コマンドで設定する。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 実行ファイルのパスなど変更しておく</span>
<span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>nginx
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">755</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>nginx
chkconfig <span style="color: #660033;">--add</span> nginx
chkconfig nginx on
&nbsp;
<span style="color: #666666; font-style: italic;"># 今すぐ起動するなら</span>
service nginx start</pre></div></div>



<p>
続いてログのローテーションの設定。<code>/etc/logrotate.d/nginx</code> を以下の内容で作成する。パスやパラメータは各自で調整のこと。
</p>
<pre>
/var/log/nginx/*log {
    missingok
    notifempty
    delaycompress
    sharedscripts
    postrotate
        /usr/local/nginx/sbin/nginx -s reopen
    endscript
}
</pre>
<p>
設定ファイルのチェックも行っておく。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> logrotate <span style="color: #660033;">-d</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>logrotate.conf</pre></div></div>



<p>
設定ファイルの内容について後で書くかも。
</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1799/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL MEMORYテーブルのサイズをmuninで監視する</title>
		<link>http://wp.serpere.info/archives/1109</link>
		<comments>http://wp.serpere.info/archives/1109#comments</comments>
		<pubDate>Mon, 25 Jan 2010 14:49:47 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[munin]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1109</guid>
		<description><![CDATA[DB内のMEMORYテーブルのサイズを、テーブルごとにグラフ化するための munin プラグインを作ってみました。 mysql_memory_tables_ インストール munin-node 本体のプラグインディレクトリ（/usr/share/munin/plugins など）にコピーしてください。 設定 ワイルドカードプラグインなので、mysql_memory_tables_{チェック対象となるDB名} という名前でシンボリックリンクを張ってください。 ln -s /usr/share/munin/plugins/mysql_memory_tables_ /etc/munin/plugins/mysql_memory_tables_db1 データを取得するには対象DBの INFORMATION_SCHEMA にアクセスする権限が必要です。プラグイン設定ファイル（/etc/munin/plugin-conf.d/munin-node など）で mysql コマンドに渡すユーザ名・パスワードを指定してください。 [mysql_memory_tables_*] env.mysqlopts -u cicindela --password=hogehoge 動作確認 munin-run コマンドでテストができます。 # 値の表示 munin-run mysql_memory_tables_db1 &#160; # グラフ設定情報の表示 munin-run mysql_memory_tables_db1 config 動作が確認できたら munin-node を再起動してください。]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1109%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F4qM1sC%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22MySQL%20MEMORY%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E3%81%AE%E3%82%B5%E3%82%A4%E3%82%BA%E3%82%92munin%E3%81%A7%E7%9B%A3%E8%A6%96%E3%81%99%E3%82%8B%22%20%7D);"></div>
<p>
DB内のMEMORYテーブルのサイズを、テーブルごとにグラフ化するための munin プラグインを作ってみました。
</p>
<p><a href="http://gist.github.com/285906">mysql_memory_tables_</a></p>
<h3>インストール</h3>
<p>
munin-node 本体のプラグインディレクトリ（/usr/share/munin/plugins など）にコピーしてください。
</p>
<h3>設定</h3>
<p>
ワイルドカードプラグインなので、<code>mysql_memory_tables_{チェック対象となるDB名}</code> という名前でシンボリックリンクを張ってください。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>mysql_memory_tables_ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>mysql_memory_tables_db1</pre></div></div>



<p>
データを取得するには対象DBの INFORMATION_SCHEMA にアクセスする権限が必要です。プラグイン設定ファイル（/etc/munin/plugin-conf.d/munin-node など）で mysql コマンドに渡すユーザ名・パスワードを指定してください。
</p>
<pre>
[mysql_memory_tables_*]
env.mysqlopts -u cicindela --password=hogehoge
</pre>
<h3>動作確認</h3>
<p>
munin-run コマンドでテストができます。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 値の表示</span>
munin-run mysql_memory_tables_db1
&nbsp;
<span style="color: #666666; font-style: italic;"># グラフ設定情報の表示</span>
munin-run mysql_memory_tables_db1 config</pre></div></div>



<p>
動作が確認できたら munin-node を再起動してください。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1109/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WARNING: mismatch_cnt is not 0 on /dev/md0</title>
		<link>http://wp.serpere.info/archives/1099</link>
		<comments>http://wp.serpere.info/archives/1099#comments</comments>
		<pubDate>Mon, 21 Dec 2009 09:31:18 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[CentOS]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1099</guid>
		<description><![CDATA[Software RAID1を組んでいるサーバで、CentOS 5.3 -> 5.4にアップグレードしたところ、週末ごとに次のような警告メールが届くようになった。 /etc/cron.weekly/99-raid-check: WARNING: mismatch_cnt is not 0 on /dev/md0 /proc/mdstat 等には特に異常などは見当たらないし、構成の異なる複数のサーバで同じ現象が起こったので、ハード的な問題ではないだろうと判断し、忙しかったこともあってそのままにしていた。しかしやはり気持ち悪いので、きちんと調べてみた。 メール中にある通り、この警告は /etc/cron.weekly/99-raid-check というスクリプトから送信されている。このスクリプトを提供しているのは mdadm パッケージで、5.4で更新されている。更新内容はレッドハットのサイトで RHBA-2009:1382-1 として提供されており、99-raid-check について説明しているのは、次の2箇所である。 * LinuxのソフトウェアRAIDスタックでは、データスクラビングがサポートされています（RAIDアレイのディスクを読み取って不良セクタを検索し、不良セクタが見つかると、他のディスクやパリティの情報を使用して、不良セクタを正常なデータで書き直します）。しかし、mdadmパッケージはこの機能を利用していませんでした。今回のパッケージは、cronジョブが/etc/cron.weeklyに追加されており、ディスクをチェックして不良セクタがないかどうかを確認し、見つかった場合は修復します。（BZ#233972） * 今回のアップデートでは、mdadmに冗長アレイでの「データスクラビング」を可能にする新しい機能が追加されています。データスクラビングでは、冗長アレイ内のドライブ上で不良セクタが検索され、見つかった場合は他のドライブのデータを使用して修正されます。それによって、読み取りエラーを返すセクタは再構築されます。注：この新しいパッケージでは、データスクラビングはデフォルトでオンになっています。この機能は週1回実行され、実行中にある程度のパフォーマンス低下を引き起こす場合があります。パフォーマンス上の理由でこの機能を無効にしたい場合、アレイに対して実行されるチェックのタイプを管理したい場合、あるいは、どのアレイをチェックするかを制御したい場合は、/etc/sysconfig/raid-checkファイルを編集します。オプションを設定する方法の詳細は、そのファイルにコメントとして含まれています。（BZ#513200） 「データスクライピング（Data Scrubbing）」とは何か、なぜ重要なのかについては、ここで示されているBugzilla #233972およびその先のリンクに説明がある（一箇所リンクが切れているのはRAID/Software &#8211; GEntoo Linux Wikiだと思われる）。 データスクライピングを実行するには、root権限で次のコマンドを実行する。これによって不良セクタが検出され、自動的に修復される。99-raid-check が実行するのも、まさにこのコマンドである（少なくともデフォルトの設定においては）。 echo check &#62; /sys/block/mdX/md/sync_action ではこのコマンドと、問題の mismatch_cnt とはどう関係するのか。/etc/sysconfig/raid-check のコメントを読むと次のように書いてある。 If it finds good sectors that contain bad data (meaning that [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1099%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22WARNING%3A%20mismatch_cnt%20is%20not%200%20on%20%2Fdev%2Fmd0%22%20%7D);"></div>
<p>
Software RAID1を組んでいるサーバで、CentOS 5.3 -> 5.4にアップグレードしたところ、週末ごとに次のような警告メールが届くようになった。
</p>
<pre>
/etc/cron.weekly/99-raid-check:

WARNING: mismatch_cnt is not 0 on /dev/md0
</pre>
<p>
/proc/mdstat 等には特に異常などは見当たらないし、構成の異なる複数のサーバで同じ現象が起こったので、ハード的な問題ではないだろうと判断し、忙しかったこともあってそのままにしていた。しかしやはり気持ち悪いので、きちんと調べてみた。
</p>
<p>
メール中にある通り、この警告は /etc/cron.weekly/99-raid-check というスクリプトから送信されている。このスクリプトを提供しているのは mdadm パッケージで、5.4で更新されている。更新内容はレッドハットのサイトで <a href="http://www.jp.redhat.com/support/errata/RHBA/RHBA-2009-1382J.html">RHBA-2009:1382-1</a> として提供されており、99-raid-check について説明しているのは、次の2箇所である。
</p>
<blockquote><p>
* LinuxのソフトウェアRAIDスタックでは、データスクラビングがサポートされています（RAIDアレイのディスクを読み取って不良セクタを検索し、不良セクタが見つかると、他のディスクやパリティの情報を使用して、不良セクタを正常なデータで書き直します）。しかし、mdadmパッケージはこの機能を利用していませんでした。今回のパッケージは、cronジョブが/etc/cron.weeklyに追加されており、ディスクをチェックして不良セクタがないかどうかを確認し、見つかった場合は修復します。（BZ#233972）
</p></blockquote>
<blockquote><p>
* 今回のアップデートでは、mdadmに冗長アレイでの「データスクラビング」を可能にする新しい機能が追加されています。データスクラビングでは、冗長アレイ内のドライブ上で不良セクタが検索され、見つかった場合は他のドライブのデータを使用して修正されます。それによって、読み取りエラーを返すセクタは再構築されます。注：この新しいパッケージでは、データスクラビングはデフォルトでオンになっています。この機能は週1回実行され、実行中にある程度のパフォーマンス低下を引き起こす場合があります。パフォーマンス上の理由でこの機能を無効にしたい場合、アレイに対して実行されるチェックのタイプを管理したい場合、あるいは、どのアレイをチェックするかを制御したい場合は、/etc/sysconfig/raid-checkファイルを編集します。オプションを設定する方法の詳細は、そのファイルにコメントとして含まれています。（BZ#513200）
</p></blockquote>
<p>
「データスクライピング（Data Scrubbing）」とは何か、なぜ重要なのかについては、ここで示されている<a href="https://bugzilla.redhat.com/show_bug.cgi?id=233972">Bugzilla #233972</a>およびその先のリンクに説明がある（一箇所リンクが切れているのは<a href="http://en.gentoo-wiki.com/wiki/RAID/Software#Data_Scrubbing">RAID/Software &#8211; GEntoo Linux Wiki</a>だと思われる）。
</p>
<p>
データスクライピングを実行するには、root権限で次のコマンドを実行する。これによって不良セクタが検出され、自動的に修復される。99-raid-check が実行するのも、まさにこのコマンドである（少なくともデフォルトの設定においては）。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> check <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>block<span style="color: #000000; font-weight: bold;">/</span>mdX<span style="color: #000000; font-weight: bold;">/</span>md<span style="color: #000000; font-weight: bold;">/</span>sync_action</pre></div></div>



<p>
ではこのコマンドと、問題の mismatch_cnt とはどう関係するのか。/etc/sysconfig/raid-check のコメントを読むと次のように書いてある。
</p>
<blockquote><p>If it finds good sectors that
 contain bad data (meaning that the data in a sector does
 not agree with what the data from another disk indicates
 the data should be, for example the parity block + the other
 data blocks would cause us to think that this data block
 is incorrect), then it does nothing but increments the
 counter in the file /sys/block/$dev/md/mismatch_count.
</p></blockquote>
<p>
つまりセクタ自体は正常だが、中に入ってるデータがディスク間で異なる場合に mismatch_cnt が増加するということらしい。
</p>
<p>
ならば mismatch_cnt が 0 でないということは、ただちにRAIDが正常に機能していないことを意味するのだろうか？いろいろなMLの投稿などを読む限り、少なくとも swap パーティションを含むRAIDデバイスにおいては、全く正常であっても各ディスク間の内容が異なり得る…すなわち mismatch_cnt が0でないことがあり得るようだ。では swap を含まないパーティションの場合は？私の（また他のCentOS利用者の）ように、/boot のみを含む md0 の場合は？……これがよく分からない。<a href="http://marc.info/?l=linux-raid&#038;m=117555829807542&#038;w=2">この投稿</a>を読む限り、ある種の状況においてはそうしたことも起こり得る、とのことである。
</p>
<p>
もしそうした状況に該当するなら。mismatch_cnt が 0 でなくとも、異なっているのは実際にはファイルシステムからアクセス不能な部分であって、システムの運用には影響しないのであれば。修復する方法はある。check の代わりに repair を使うことである。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#このコマンドを実行する前に以下の説明をよく読んでください！！</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> repair <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>block<span style="color: #000000; font-weight: bold;">/</span>mdX<span style="color: #000000; font-weight: bold;">/</span>md<span style="color: #000000; font-weight: bold;">/</span>sync_action</pre></div></div>



<p>
ただし /etc/sysconfig/raid-check には、次のような恐ろしいコメントが書いてある（強調は私による）。
</p>
<blockquote><p>
The repair option does the same thing, but
 when it encounters a mismatch in the data, it automatically
 updates the data to be consistent.  However, since <strong>we really
 don&#8217;t know whether it&#8217;s the parity or the data block that&#8217;s
 correct (or which data block in the case of raid1), it&#8217;s
 luck of the draw whether or not the user gets the right
 data instead of the bad data.</strong>
</p></blockquote>
<p>
というわけで、以下は「<strong>それでも自己責任でこの方法に賭けるぜ！</strong>」という方のみ読んでください。
</p>
<p>
まず各デバイスの mismatch_cnt の値をチェックしておく。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>block<span style="color: #000000; font-weight: bold;">/</span>md0<span style="color: #000000; font-weight: bold;">/</span>md<span style="color: #000000; font-weight: bold;">/</span>mismatch_cnt</pre></div></div>



<p>そして repair を実行する。</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'echo repair &gt; /sys/block/md0/md/sync_action'</span></pre></div></div>



<p>進行状況は /proc/mdstat で確認できる。</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>mdstat</pre></div></div>



<p>repair を実行しただけでは mismatch_cnt は更新されないので、もう一度 check を実行する。進行状況はやはり /proc/mdstat で確認できる。</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'echo check &gt; /sys/block/md0/md/sync_action'</span></pre></div></div>



<p>終了したら mismatch_cnt をチェックして、0 になっているか確認する。</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>block<span style="color: #000000; font-weight: bold;">/</span>md0<span style="color: #000000; font-weight: bold;">/</span>md<span style="color: #000000; font-weight: bold;">/</span>mismatch_cnt</pre></div></div>



<p>……</p>
<p>
参考にしたサイトは<a href="http://b.hatena.ne.jp/serpere/mismatch_cnt/">はてブの mismatch_cnt タグ</a>にまとめてあるので、この記事に書いてあることを鵜呑みにせず、自分で調べ自分で判断するようお願いします。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1099/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>XREA+からエックスサーバーに移転</title>
		<link>http://wp.serpere.info/archives/836</link>
		<comments>http://wp.serpere.info/archives/836#comments</comments>
		<pubDate>Fri, 23 Oct 2009 13:24:08 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[xrea]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=836</guid>
		<description><![CDATA[XREAが重過ぎて使い物にならなくなったので、エックスサーバーに移転した。ここを選んだ理由は PHP5, MySQL5が使える 比較的値段が安い（X10プランで月額1050〜1260円） ネット上の口コミでは速度に対する評価が高い &#8220;ハズレ&#8221;を引いたときの救済措置簡単サーバ移動機能がある SSHが使えないので、派手なことをするヤツは少ないだろうという希望的観測 今のところ速度には満足。サーバの管理画面も使いやすい（若干パワーユーザ向けだが）。WordPressも問題なく移転できた。 WordPressの移転にはいろいろな方法があるようなので、私の方法も紹介しておく。最も原始的かつ確実な方法で行った。 エックスサーバーでの下準備 管理画面「MySQL設定」で新しいMySQLデータベースとMySQLユーザを作る 「ドメイン設定」で独自ドメインを登録する できればここで /etc/hosts ファイルを書き換えてきちんとアクセスできることを確認しておく。WebサーバのIPアドレスは「DNSレコード設定」で確認できる 旧データのバックアップと修正 XREAの管理画面「データベース」で「保存」ボタンを押してバックアップを作成する 作成されたmysql.dumpおよびサイトの全ファイルをローカルにコピーする wp-config.php を開き、データベース設定をエックスサーバーのものに書き換える エックスサーバーにアップロード 管理画面からphpMyAdminにログインして、mysql.dumpを実行 ドキュメントルートに全ファイルをコピー 例によって /etc/hosts を書き換え、WordPressが正常に動くことを確認 あとはDNSを変更すればOK。]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F836%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22XREA%2B%E3%81%8B%E3%82%89%E3%82%A8%E3%83%83%E3%82%AF%E3%82%B9%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%81%AB%E7%A7%BB%E8%BB%A2%22%20%7D);"></div>
<p>XREAが重過ぎて使い物にならなくなったので、<a href="http://www.xserver.ne.jp/">エックスサーバー</a>に移転した。ここを選んだ理由は</p>
<ul>
<li>PHP5, MySQL5が使える</li>
<li>比較的値段が安い（X10プランで月額1050〜1260円）</li>
<li>ネット上の口コミでは速度に対する評価が高い</li>
<li>&#8220;ハズレ&#8221;を引いたときの救済措置<a href="http://www.xserver.ne.jp/feature.php#move">簡単サーバ移動機能</a>がある</li>
<li>SSHが<strong>使えない</strong>ので、派手なことをするヤツは少ないだろうという希望的観測</li>
</ul>
<p>今のところ速度には満足。サーバの管理画面も使いやすい（若干パワーユーザ向けだが）。WordPressも問題なく移転できた。</p>
<p>WordPressの移転にはいろいろな方法があるようなので、私の方法も紹介しておく。最も原始的かつ確実な方法で行った。</p>
<h3>エックスサーバーでの下準備</h3>
<ol>
<li>管理画面「MySQL設定」で新しいMySQLデータベースとMySQLユーザを作る</li>
<li>「ドメイン設定」で独自ドメインを登録する</li>
<li>できればここで /etc/hosts ファイルを書き換えてきちんとアクセスできることを確認しておく。WebサーバのIPアドレスは「DNSレコード設定」で確認できる</li>
</ol>
<h3>旧データのバックアップと修正</h3>
<ol>
<li>XREAの管理画面「データベース」で「保存」ボタンを押してバックアップを作成する</li>
<li>作成されたmysql.dumpおよびサイトの全ファイルをローカルにコピーする</li>
<li>wp-config.php を開き、データベース設定をエックスサーバーのものに書き換える</li>
</ol>
<h3>エックスサーバーにアップロード</h3>
<ol>
<li>管理画面からphpMyAdminにログインして、mysql.dumpを実行</li>
<li>ドキュメントルートに全ファイルをコピー</li>
<li>例によって /etc/hosts を書き換え、WordPressが正常に動くことを確認</li>
</ol>
<p>あとはDNSを変更すればOK。</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/836/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mymemcheckをCentOSで動かす</title>
		<link>http://wp.serpere.info/archives/536</link>
		<comments>http://wp.serpere.info/archives/536#comments</comments>
		<pubDate>Tue, 23 Jun 2009 10:23:19 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=536</guid>
		<description><![CDATA[『サーバ/インフラを支える技術』に載っていた mymemcheck を試してみようと思って CentOS 5 上で実行してみたところ、いくつかの Perl モジュールが足りなくて動かなかった。例によって rpmforge で検索してみたら、やはり見つかった。 sudo yum --enablerepo=rpmforge install perl-Readonly perl-UNIVERSAL-require 相変わらず便利過ぎるぜ rpmforge。]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F536%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FaBTrUW%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22mymemcheck%E3%82%92CentOS%E3%81%A7%E5%8B%95%E3%81%8B%E3%81%99%22%20%7D);"></div>
<p>『<a name="evtst|a|4774135666" href="http://www.amazon.co.jp/24%E6%99%82%E9%96%93365%E6%97%A5-%E3%82%A4%E3%83%B3%E3%83%95%E3%83%A9%E3%82%92%E6%94%AF%E3%81%88%E3%82%8B%E6%8A%80%E8%A1%93-%E2%80%BE%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%A9%E3%83%93%E3%83%AA%E3%83%86%E3%82%A3%E3%80%81%E3%83%8F%E3%82%A4%E3%83%91%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%B3%E3%82%B9%E3%80%81%E7%9C%81%E5%8A%9B%E9%81%8B%E7%94%A8-PRESS-plus%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA/dp/4774135666%3FSubscriptionId%3D02E5W5871AJF7PMMMS82%26tag%3Dhebinikki09-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774135666">サーバ/インフラを支える技術</a>』に載っていた <a href="http://dsas.blog.klab.org/archives/50860867.html">mymemcheck</a> を試してみようと思って CentOS 5 上で実行してみたところ、いくつかの Perl モジュールが足りなくて動かなかった。例によって rpmforge で検索してみたら、やはり見つかった。</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> yum <span style="color: #660033;">--enablerepo</span>=rpmforge <span style="color: #c20cb9; font-weight: bold;">install</span> perl-Readonly perl-UNIVERSAL-require</pre></div></div>



<p>相変わらず便利過ぎるぜ rpmforge。</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/536/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mod_dosdetector の使い方</title>
		<link>http://wp.serpere.info/archives/465</link>
		<comments>http://wp.serpere.info/archives/465#comments</comments>
		<pubDate>Thu, 07 May 2009 12:12:38 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[mod_dosdetector]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=465</guid>
		<description><![CDATA[『サーバ/インフラを支える技術』で知ったDoS攻撃判定モジュール mod_dosdetector の使い方。mod_dosdetecotr のバージョンは 0.2、環境は Apache 2.2.3 @ CentOS 5。 ※改造版も公開しています。 インストール wget http://ncu.dl.sourceforge.net/sourceforge/moddosdetector/mod_dosdetector-0.2.tar.gz tar xvzf mod_dosdetector-0.2.tar.gz cd mod_dosdetector-0.2 make sudo make install httpd.conf 設定例 # 実際にはこの一行は make install で追加される LoadModule dosdetector_module modules/mod_dosdetector.so &#160; DoSDetection on DoSPeriod 5 DoSThreshold 10 DoSHardThreshold 25 DoSBanPeriod 30 DoSTableSize 100 DoSIgnoreContentType image&#124;javascript&#124;css DoS判定の肝となる数値は DoSPeriod, DoSThreshold, DoSHardThreshold, DoSBanPeriod の4つで、上の例ではだいたい次のような意味になる： 同一IPアドレスから5秒間に10回以上のアクセスがあった場合「DoS攻撃の疑いあり」と見なし、その後30秒間のアクセスに対しては環境変数 [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F465%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22mod_dosdetector%20%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9%22%20%7D);"></div>
<p>『<a name="evtst|a|4774135666" href="http://www.amazon.co.jp/24%E6%99%82%E9%96%93365%E6%97%A5-%E3%82%A4%E3%83%B3%E3%83%95%E3%83%A9%E3%82%92%E6%94%AF%E3%81%88%E3%82%8B%E6%8A%80%E8%A1%93-%E2%80%BE%E3%82%B9%E3%82%B1%E3%83%BC%E3%83%A9%E3%83%93%E3%83%AA%E3%83%86%E3%82%A3%E3%80%81%E3%83%8F%E3%82%A4%E3%83%91%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%B3%E3%82%B9%E3%80%81%E7%9C%81%E5%8A%9B%E9%81%8B%E7%94%A8-PRESS-plus%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA/dp/4774135666%3FSubscriptionId%3D02E5W5871AJF7PMMMS82%26tag%3Dhebinikki09-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774135666">サーバ/インフラを支える技術</a>』で知ったDoS攻撃判定モジュール <a href="http://sourceforge.net/projects/moddosdetector">mod_dosdetector</a> の使い方。mod_dosdetecotr のバージョンは <a href="http://d.hatena.ne.jp/stanaka/20070801/1185921764">0.2</a>、環境は Apache 2.2.3 @ CentOS 5。</p>
<p><strong>※<a href="http://wp.serpere.info/archives/715">改造版</a>も公開しています。</strong></p>
<h3>インストール</h3>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>ncu.dl.sourceforge.net<span style="color: #000000; font-weight: bold;">/</span>sourceforge<span style="color: #000000; font-weight: bold;">/</span>moddosdetector<span style="color: #000000; font-weight: bold;">/</span>mod_dosdetector-<span style="color: #000000;">0.2</span>.tar.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> xvzf mod_dosdetector-<span style="color: #000000;">0.2</span>.tar.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> mod_dosdetector-<span style="color: #000000;">0.2</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>



<h3>httpd.conf 設定例</h3>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #adadad; font-style: italic;"># 実際にはこの一行は make install で追加される</span>
<span style="color: #00007f;">LoadModule</span> dosdetector_module modules/mod_dosdetector.so
&nbsp;
DoSDetection     <span style="color: #0000ff;">on</span>
DoSPeriod        <span style="color: #ff0000;">5</span>
DoSThreshold     <span style="color: #ff0000;">10</span>
DoSHardThreshold <span style="color: #ff0000;">25</span>
DoSBanPeriod     <span style="color: #ff0000;">30</span>
DoSTableSize     <span style="color: #ff0000;">100</span>
DoSIgnoreContentType  image|javascript|css</pre></div></div>



<p>DoS判定の肝となる数値は DoSPeriod, DoSThreshold, DoSHardThreshold, DoSBanPeriod の4つで、上の例ではだいたい次のような意味になる：</p>
<ol>
<li>同一IPアドレスから5秒間に10回以上のアクセスがあった場合「DoS攻撃の疑いあり」と見なし、その後30秒間のアクセスに対しては環境変数 SuspectDoS をセットする（値は&#8221;1&#8243;）</li>
<li>さらに5秒間のアクセス回数が25回に達した場合、「激しいDoS攻撃の疑い」と見なし、環境変数 SuspectHardDoS もセットする（値は&#8221;1&#8243;）</li>
<li>初めに「DoS攻撃の疑いあり」と判定してから30秒が経過したら、次のアクセスでもう一度判定をやり直す。直近5秒のアクセスが10回を下回っていれば、疑いは晴れる</li>
</ol>
<p>アクセス数の計測はファイルの種類・有無を問わずに行われるため、デフォルトではページ内の画像やCSSなども計測対象となってしまう。それらを除外したい場合は、DoSIgnoreContentType ディレクティブで除外したいファイルの MIME type を正規表現で指定しておく（ファイル名（URL）ではないので注意）。各種画像ファイル・JavaScript・CSSファイルを除外したい場合は上例のように設定しておけば良い。</p>
<p>ただし多くの環境では拡張子 ico に対する MIME type の指定は行われていないはずなので、そのままだと favicon.ico が text/plain 等と見なされて除外されない（.ico に限らず、未設定の場合は httpd.conf の DefaultType になる）。次のように AddType を追加しておけば image と判定される。</p>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">AddType</span>  image/vnd.microsoft.icon  .ico</pre></div></div>



<p>ちなみにこれがアイコンファイルの<a href="http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon">正式な MIME type</a> である。</p>
<h3>DoS攻撃に対する防御設定</h3>
<p>mod_dosdetector が行うのはあくまでも攻撃の”検出”だけなので、防御する方法はまた別に設定する必要がある。多くの場合は mod_rewrite と併用することになるだろう。例えば「激しいDoS攻撃の疑い」なアクセスに対してステータスコード503 + 静的なHTMLを返す場合は次のように設定する（Apache 2.2 only）。</p>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteCond</span> %{ENV:SuspectHardDoS} =<span style="color: #ff0000;">1</span>
<span style="color: #00007f;">RewriteRule</span> .*  - [R=<span style="color: #ff0000;">503</span>,L]
&nbsp;
<span style="color: #00007f;">ErrorDocument</span> <span style="color: #ff0000;">503</span> /server_is_busy.html</pre></div></div>



<p>しかし現実には、攻撃の判定とその後の防御は、匙加減が難しい。どの程度のアクセスを攻撃と見なすかはサイトによって千差万別だろうし、あまりに頻繁・過剰な反応をしてしまうとユーザが離れてしまう。</p>
<p>そこで個人的には、まず厳し目の設定を行って、ログを取ってみることをおすすめする。「DoS攻撃の疑いあり」なアクセスを普通のログと分けて記録するには次のように設定する。</p>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">LogFormat</span> <span style="color: #7f007f;">&quot;%{SuspectHardDoS}e %h %l %u %t <span style="color: #000099; font-weight: bold;">\&quot;</span>%r<span style="color: #000099; font-weight: bold;">\&quot;</span> %&gt;s %b <span style="color: #000099; font-weight: bold;">\&quot;</span>%{Referer}i<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>%{User-Agent}i<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> dos_suspect
<span style="color: #00007f;">CustomLog</span> logs/dos_suspect_log dos_suspect env=SuspectDoS</pre></div></div>



<p>ログの行頭に環境変数 SuspectHardDoS が記録されるため、どのような経過をたどって「疑いあり」から「激しい攻撃の疑い」へと移行したかも分かる。</p>
<p>後はこの記録を見つつ、通常のアクセスが引っかからないような適当な設定を探ることになる。もしサーバリソースの都合上、どうしても通常のアクセスが引っかてしまう可能性があるのなら、その時は防御方法をより穏当なものに設定することもできる（このあたりの柔軟性は mod_dosdetector の設計の妙だと思う）。</p>
<h3>その他</h3>
<p>上で触れなかったディレクティブについて少々：</p>
<dl>
<dt>DoSDetection</dt>
<dd>DoS判定機能の有効・無効を設定。on で有効</dd>
<dt>DoSTableSize</dt>
<dd>クライアントの追跡記録の最大保存数。この数値を大きくすればより多くのクライアントを同時に追跡できるが、メモリ使用量と負荷が上昇する</dd>
<dt>DoSShmemName</dt>
<dd>クライアントの追跡記録を保存しておくための共有メモリの名前。普通は設定する必要は無いはず</dd>
</dl>
<h3>最後に</h3>
<p>素晴らしいソフトウェアをオープンソースとして公開してくださった田中慎司さん（そして株式会社はてな）に感謝！</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/465/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apache 2.2ではmod_rewriteのRフラグで任意のステータスコードを返せる</title>
		<link>http://wp.serpere.info/archives/459</link>
		<comments>http://wp.serpere.info/archives/459#comments</comments>
		<pubDate>Wed, 06 May 2009 13:52:35 +0000</pubDate>
		<dc:creator>tkykmw</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=459</guid>
		<description><![CDATA[昔Apache 2.0系で試したときは無理だったのに、2.2系ではできるようになっていたのか。 mod_rewriteで503 &#8211; Do You PHP はてな 上エントリーで引用されているドキュメントは古くて、現在は次のような記述に置き換わっている。 While this is typically used for redirects, any valid status code can be given here. If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used. mod_rewrite &#8211; Apache HTTP [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F459%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Apache%202.2%E3%81%A7%E3%81%AFmod_rewrite%E3%81%AER%E3%83%95%E3%83%A9%E3%82%B0%E3%81%A7%E4%BB%BB%E6%84%8F%E3%81%AE%E3%82%B9%E3%83%86%E3%83%BC%E3%82%BF%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E8%BF%94%E3%81%9B%E3%82%8B%22%20%7D);"></div>
<p>昔Apache 2.0系で試したときは無理だったのに、2.2系ではできるようになっていたのか。</p>
<p><a href="http://d.hatena.ne.jp/shimooka/20070709/1183963568">mod_rewriteで503 &#8211; Do You PHP はてな</a></p>
<p>上エントリーで引用されているドキュメントは古くて、現在は次のような記述に置き換わっている。</p>
<blockquote class="quote"><div class="quote">While this is typically used for redirects, any valid status code can be given here. If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used.</div>
<p class="quote"><a href="http://httpd.apache.org/docs/2.2/ja/mod/mod_rewrite.html#rewriterule">mod_rewrite &#8211; Apache HTTP Server</a></p>
</blockquote>
<p>『このフラグは通常リダイレクトのために用いられるが、任意の妥当なステータスコードを指定することができる。もしリダイレクト以外のステータスコード（すなわち300〜399以外）が指定された場合、置換文字列は無視され、さらにLフラグが指定されたときと同じようにURLの書き換えが停止される。』</p>
<p>つまりリダイレクトもURLの書き換えも行われないので、デフォルトのエラー画面以外を表示するには ErrorDocument ディレクティブで指定しておく必要がある。</p>


<div class="wp_syntax"><div class="code"><pre class="apache" style="font-family:monospace;"><span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteCond</span> %{some_condition} =<span style="color: #ff0000;">1</span>
<span style="color: #00007f;">RewriteRule</span> .*  - [R=<span style="color: #ff0000;">503</span>]
&nbsp;
<span style="color: #00007f;">ErrorDocument</span> <span style="color: #ff0000;">503</span> /server_is_busy.html</pre></div></div>



<p>ソースに付属している文書（CHANGES）によれば、この仕様に変わったのはバージョン2.1.1。Bugzillaで以下のような議論も見つけた。</p>
<blockquote class="quote"><div class="quote">In the <300, >=400 cases, one can think of this as [R{esponse}=nnn} as opposed to [R{edirect}], and it really harms nothing to overload it.</div>
<p class="quote"><a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=45478">Bug 45478 – R flag in RewriteRule does not honor 400 and 500 range error code</a></p>
</blockquote>
<p>Redirect の &#8216;R&#8217; ではなく Response の &#8216;R&#8217; と見なしてくれ、と。なるほど…。</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/459/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

