10月 23

pgpoolAdmin インストール

By ststyle Database, PostgreSQL, pgpool-II, pgpoolAdmin コメントは受け付けていません。

pgpoolAdminはpgpoolの管理をするためのツールです。

GUIベースで、pgpoolの状態監視や起動・停止、pgpool.confの設定を行うことができます.

pgpoolAdminの取得

pgpoolAdminを入手する

apacheのドキュメントディレクトリにて展開&リネーム

tar zxvf pgpoolAdmin-2.1-beta1.tar.gz
mv pgpoolAdmin-2.1-beta1 pgpoolAdmin

chown nobody /usr/local/apache/htdocs_ssl/pgpoolAdmin/conf/pgmgt.conf.php
chmod 644 /usr/local/apache/htdocs_ssl/pgpoolAdmin/conf/pgmgt.conf.php

pcp.confにログインアカウントを登録する

ここではtestとしていますが実際はセキュアなパスワードを設定する事

md5パスワードを取得


/usr/local/pgpool/bin/pg_md5 test

098f6bcd4621d373cade4e832627b4f6

pcp.confに追記


vi /usr/local/pgpool/etc/pcp.conf

# USERID:MD5PASSWD
test:098f6bcd4621d373cade4e832627b4f6

ブラウザで確認

http://<サーバ>/pgpoolAdmin/login.php

pgpoolAdmin

TOP画面

pgpoolAdmin-02

Tagged with:
10月 23

pgpool-II インストール

By ststyle Database, PostgreSQL, pgpool-II コメントは受け付けていません。

pgpool-IIはPostgreSQL専用のミドルウェアで,

PostgreSQLのデータベースクライアントとPostgreSQLサーバの間に割り込む形で動作し,

PostgrSQLに以下のような機能を追加します.

・コネクションプーリング::接続を保存し,再利用することで接続オーバヘッドを低減しスループット向上を図る

・負荷分散::多数のリクエストをそれぞれのサーバで分担して負荷を軽減させ,システム全体の性能を向上させることができます

・レプリケーション::物理的に2台以上のディスクにリアルタイムで書き込み処理を行い、万が一どれかのディスクに障害が発生しても運用を継続することができます.

・パラレルクエリ::それぞれのサーバに問い合わせを行い、問い合わせの処理時間を短縮

ここでの環境のポート番号は以下のようにしております

※PORT5439にて接続した場合はPGPOOL経由となりレプリケーションなどが働きます。

PGPOOLはメインDBにインストールしています

—————————————–

アプリ

pgpool(5439)

↓                    ↓

メインDB(5432)       バックエンドDB(5432)

—————————————–

PGPOOLインストール

事前準備


su -
mkdir /usr/local/pgpool
chown postgres:postgres /usr/local/pgpool

展開&インストール


su postgres
cd /usr/local/src/
% tar zxf pgpool-II-2.1.tar.gz
% cd pgpool-II-2.1
% PATH=/usr/local/pgsql/bin:$PATH
% ./configure --prefix=/usr/local/pgpool
% make
% make install
% ls /usr/local/pgpool/bin/pgpool
pgpool

パスの追加

~/.bashrcにパス追加


vi ~/.bashrc
PATH="$PATH":/usr/local/pgsql/bin:/usr/local/pgpool/bin

source ~/.bashrc

インストールが完了すると設定ファイルの雛形が
作成されるので、これをコピーする。


% cp /usr/local/pgpool/etc/pgpool.conf{.sample,}
% cp /usr/local/pgpool/etc/pcp.conf{.sample,}
% cp /usr/local/pgpool/etc/pool_hba.conf{.sample,}

以上の設定で、/usr/local/pgpool/etc/pgpool.confがpgpool-IIの設定ファイルになる。

PGPOOLの起動

起動するには、

※pgpoolⅡ2.5では/va/run/pgpoolというディレクトリが必要

permission:postgres:postgres

postgres postgres 4096 11月 21 16:05 pgpool


% pgpool -n &amp;c

とコマンドを入力する。

PGPOOLの停止


% pgpool -n stop

PGPOOLの自動起動

pgpoolの起動ですが、
redhat系の場合は自動起動用のスクリプトが
pgpoolのソールディレクトリの
redhat/pgpool.initにあります。
(/usr/local/src/pgpool-II-2.1/redhat/pgpool.init)

スクリプトの最初の方の変数を適宜環境に合せて書き換えて、


vi /usr/local/src/pgpool-II-2.1/redhat/pgpool.init

PGPOOLENGINE=/usr/local/pgpool/bin
PGPOOLDAEMON=$PGPOOLENGINE/pgpool
PGPOOLCONF=/usr/local/pgpool/etc/pgpool.conf
PGPOOLPID=/tmp/pgpool.pid
PGPOOLLOG=/var/log/pgpool.log

/etc/init.d/pgpoolにコピーします。


su -
cp /usr/local/src/pgpool-II-2.1/redhat/pgpool.init /etc/init.d/pgpool

これで


/etc/init.d/pgpool start

で起動できます。


su -
cp /usr/local/src/pgpool-II-2.1/redhat/pgpool.init /etc/init.d/pgpool
vi /etc/init.d/pgpool
chkconfig --add pgpool
chkconfig  pgpool on

chkconfig --list pgpool

又は

ntsysv でチェックする

PGPOOLの設定

それでは、実際にレプリケーションを行ってみる。
レプリケーションのために、pgpool.confに以下の項目を追加する。
なお、ここでの例ではPostgreSQLサーバが
localhostの5432ポートで動いているものとする。


vi /usr/local/pgpool/etc/pgpool.conf

# Host name or IP address to listen on: '*' for all, '' for no TCP/IP
# connections
listen_addresses = '*'

# Port number for pgpool
port = 5439

# Port number for pgpool communication manager
pcp_port = 9898

# レプリケーションの設定

# Replication mode
replication_mode = true

# ロードバランスの設定

# Load balancing mode, i.e., all SELECTs are load balanced.
# This is ignored if replication_mode is false.
load_balance_mode = true

#set pgpool2 hostname
pgpool2_hostname = ''

# メインDB

# system DB info
system_db_hostname = 'localhost'
system_db_port = 5432
system_db_dbname = 'pgpool'
system_db_schema = 'pgpool_catalog'
system_db_user = 'pgpool'
system_db_password = ''

# backend_hostname, backend_port, backend_weight
# here are examples

#バックエンド1

backend_hostname0 = 'バックエンドDB1'
backend_port0 = 5432
backend_weight0 = 1
#backend_data_directory0 = '/data'

#バックエンド2

backend_hostname1 = 'バックエンドDB2'
backend_port1 = 5432
backend_weight1 = 1
#backend_data_directory1 = '/data1'

※メインDBのpostgres.confのPortは5432とする

設定ファイルの編集が完了したところでpgpoolを起動し、
レプリケーションされているかどうかを確認する。

まずはpgpool-IIに接続し、データベース、テーブル、データ5件を作成する。


% createdb -p 5439 replication (5439 番ポートへ接続)
% psql -p 5439 replication
replication=# CREATE TABLE rep (a INT);
replication=# INSERT INTO rep SELECT generate_series(1, 10);

次に直接メインDBとバックエンドDBに5432ポートに接続し、実際にデータが5件作成されているかどうかを確認する。


メインDB

% for p in 5432 ; do
for&gt; echo port: $p
for&gt; psql -p $p -c 'select count(*) from rep' replication
for&gt; done
port: 5432
count
-------
5
(1 row)

バックエンドDB

port: 5432
count
-------
5
(1 row)

PGPOOLのステータス確認


psql -p 5439 replication

show pool_status;
Tagged with:
10月 23

PostgreSQL インストール

By ststyle Database, PostgreSQL コメントは受け付けていません。

postgreSQL インストール (postgreSQL8.3.4)

事前準備

readlineをインストール

# cd /usr/local/src
# wget ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
# tar zxvf readline-5.1.tar.gz
# cd readline-5.1
# ./configure
# make
# make install

readline-develをインストール


yum install readline-devel

zlibをインストール


tar zxvf zlib-1.1.4.tar.gz
cd zlib-1.1.4
make
make install

zlib-develをインストール


yum install zlib-devel

postgreSQLユーザの準備&展開

$ su -
# useradd postgres
# mkdir /usr/local/pgsql
# chown postgres:postgres /usr/local/pgsql/

# cd /usr/local/src/

# mkdir postgresql-8.3.4/
# chown postgres:postgres postgresql-8.3.4

# su postgres
# tar zxvf postgresql-8.3.4.tar.gz
<h3>PostgreSQlのコンパイル&amp;インストール</h3>




cd postgresql-8.3.4

--8系--

./configure \
--prefix=/usr/local/pgsql \
--with-readline \
--with-zlib

$ make all
$ make install
PostgreSQL installation complete.

インストール後、環境変数の設定

usr/local下にpgsqlディレクトリが存在する事を確認する。

vi /home/postgres/.bashrcに太字部分 (PATH~5行)下記を追加する。
vi /home/XXXX/.bashrcに太字部分 (PATH~5行)下記を追加する。

postgresユーザーにスイッチ


su postgres

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export EMACS_IM="canna"

if [ "$LANG" = "ja" -o "$LANG" = "ja_JP.eucJP" ]; then
export JLESSCHARSET=japanese
fi

PATH="$PATH":/usr/local/pgsql/bin
PG=/usr/local/pgsql
export PGLIB=$PG/lib
export LD_LIBRARY_PATH=$PGLIB
export PGDATA=$PG/data

環境変数の反映


source ~/.bashrc

データベースの初期化


initdb --encoding=EUC_JP --no-locale

postgresの自動起動スクリプトの準備


su root
cp /usr/local/src/postgresql-8.3.4/contrib/start-scripts/linux /etc/rc.d/init.d/pgsql
chmod 700 /etc/rc.d/init.d/pgsql
cd /etc/rc.d/rc3.d
ln -sf ../init.d.pgsql S83pgsql

/etc/rc.d/init.d/pgsql start

chkconfig --add pgsql
ntsysvでチェック

DB実体場所の変更

( ここではvarをデータ領域としている為)
☆DB実体場所の変更 var/pgsql下に実体を持たせ
/usr/local/pgsql/data/へシンボリックリンクする


su
mkdir /var/pgsql
chown postgres:root /var/pgsql/
su postgres
pg_ctl stop
mv /usr/local/pgsql/data /var/pgsql/
ln -s /var/pgsql/data /usr/local/pgsql/data

cd /usr/local/pgsql/
ls -F にて@になればOK

アクセス権限


vi /usr/local/pgsql/data/pg_hba.conf

host all all XXX.XXX.XXX.0/24 trust

postgresql.confの基本設定


vi /var/pgsql/data/postgresql.conf

#IPアクセスの許可
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
↓
#listen_addresses = '*'         # what IP address(es) to listen on;

#PORT番号
#port = 5432                            # (change requires restart)
↓
port = 5432                            # (change requires restart)

※PGPOOL入れる場合は5431にする(PGPOOLを5432とする)

(まぁ、PORT番号はその環境に合わせて..)
#バージョン差異?(7系からの移行時にこの設定でうまくいったっけ)
#escape_string_warning = on
↓
escape_string_warning = off

postgresql.logの作成及びローテーション


vi /var/pgsql/data/postgresql.conf

#------------------------------------------------------------------------------
# ERROR REPORTING AND LOGGING
#------------------------------------------------------------------------------

# - Where to Log -

#エラーをコンソールからlogにする

○#log_destination = 'stderr'             # Valid values are combinations of
○log_destination = 'syslog'              # Valid values are combinations of
# stderr, csvlog, syslog and eventlog,
# depending on platform.  csvlog
# requires logging_collector to be on.

# This is used when logging to stderr:
#logging_collector = off                # Enable capturing of stderr and csvlog
# into log files. Required to be on for
# csvlogs.
# (change requires restart)

# These are only used if logging_collector is on:
#log_directory = 'pg_log'               # directory where log files are written,
# can be absolute or relative to PGDATA
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'        # log file name pattern,
# can include strftime() escapes
#log_truncate_on_rotation = off         # If on, an existing log file of the
# same name as the new log file will be
# truncated rather than appended to.
# But such truncation only occurs on
# time-driven rotation, not on restarts
# or size-driven rotation.  Default is
# off, meaning append to existing files
# in all cases.
#log_rotation_age = 1d                  # Automatic rotation of logfiles will
# happen after that time.  0 to disable.
#log_rotation_size = 10MB               # Automatic rotation of logfiles will
# happen after that much log output.
# 0 to disable.

# These are relevant when logging to syslog:
○syslog_facility = 'LOCAL0'
○syslog_ident = 'postgres'

/etc/logrotate.d/syslogの編集

ログは、放っておくと膨大な量になります。
そこでログをローテーションして、古いログを消去します。

/etc/logrotate.d/syslogに追記をします。


su
vi /etc/logrotate.d/syslog

以下を追記

/var/log/postgres.log {
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2&gt; /dev/null` 2&gt; /dev/null || true
endscript
}

syslogの編集

vi /etc/syslog.conf

以下を追記

#postgres
local0.*                                                /var/log/postgres.log

syslogdとpgsqlの再起動


service syslog restart
service pgsql start
Tagged with:
10月 23

AWstats インストール

By ststyle AWstats, Watch コメントは受け付けていません。

AWstatsとはWebログを解析しグラフィカルに表示することの出来る

アクセス解析のフリーソフトです。

その他に似たような解析ものとしては

analog
Webalizer

などもあります。

事前準備

まず、apache のログ形式を調査します。

ログのフォーマットをcombinedにします


# vi /usr/local/apache/conf/httpd.conf

で apache の設定ファイルを開きます。
(VirtualHost を使用していない場合です。)

commonになっている場合は、以下のようにフォーマット形式を変更してください。


#CustomLog logs/access_log common
CustomLog /usr/local/apache/logs/access_log combined

AWstatsのソース取得

1. ソースディレクトリへ移動


# cd /usr/local/src

2. ソースをダウンロード


# wget http://nchc.dl.sourceforge.net/sourceforge/awstats/awstats-6.9.tar.gz

3. 取得したファイルを解凍


# tar zxvf awstats-6.9.tar.gz

4. 解凍したフォルダへ移動


# cd awstats-6.9/wwwroot

5. ディレクトリをapache の cgi が動作するフォルダへコピー。


# mkdir /usr/local/apache/cgi-bin/awstats
# cp -r icon /usr/local/apache/cgi-bin/awstats/
# cd cgi-bin
# cp -r * /usr/local/apache/cgi-bin/awstats/

6. コピーしたディレクトリへ移動


# cd /usr/local/apache/cgi-bin/awstats/

7. 設定ファイルの準備


# cp awstats.model.conf awstats.conf
# chmod 644 awstats.conf

8. 設定ファイルの編集


vi awstats.conf

#ログファイルの指定
apacheのログの場所を定義します。

LogFile="/var/log/httpd/mylog.log"
↓変更
LogFile="/usr/local/apache/logs/access_log"  ←利用環境に置き換えてください。

#ログの種類を指定 W はweb のログになります。
LogType=W

#ログフォーマット(apache で combined の場合は 1 を設定)
LogFormat = 1

#運用しているドメインを指定します。
SiteDomain="www.example.com"

#DNSの逆引きをする場合は1を設定
DNSLookup=1

#解析後のデータ格納ディレクトリ
DirData="."

#CGI設置場所の指定(cgi を実行するディレクトリ)
http://SiteDomain/以下で閲覧するパスの指定です。
DirCgi="/cgi-bin/awstats"

#アイコンの場所を指定
DirIcons="/cgi-bin/awstats/icon"

#年間表示する場合は3を設定
AllowFullYearView=3

#日本語で表示する場合
Lang="jp"

#解析対象外にするホストを指定します。
SkipHosts="192.168.0.1 10.0.0.1"

#解析対象外にするユーザエージェントを指定
SkipUserAgents="IE6″

#解析対象外にするファイルを指定
SkipFiles="/badpage.php /page.php?param=x"

手動で解析テスト


# /usr/local/apache/cgi-bin/awstats/awstats.pl -config=awstats.conf -update

Create/Update database for config "/usr/local/apache/cgi-bin/awstats/awstats.conf" by AWStats version 6.9 (build 1.925)
From data in log file "/usr/local/apache/logs/access_log"...
Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line 116)
Jumped lines in file: 116
Found 116 already parsed records.
Parsed lines in file: 51
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 51 new qualified records.

[/bash]

解析結果をHTML出力


/usr/local/apache/cgi-bin/awstats/awstats.pl -config=awstats.conf -output -staticlink &gt; /usr/local/apache/htdocs/awstats.html

WEBブラウザで確認(HTML出力版)

http://<WEBサーバ>/awstats.html

こんな感じで解析されたHTMLが表示されます。

awstats

WEBブラウザで確認(CGI版)

http://<WEBサーバ>/cgi-bin/awstats/awstats.pl

シェルを作成


#!/bin/bash
#-------------------------
# AWstats WEBログ解析
#
# @author Seiji Tsukioka
#-------------------------
PATH=/usr/local/apache/cgi-bin/awstats
OUTPUT=/usr/local/apache/htdocs
CONF_NAME=awstats.conf

$PATH/awstats.pl -config=$CONF_NAME -update
$PATH/awstats.pl -config=$CONF_NAME -output -staticlink &gt; $OUTPUT/awstats.html

Cronで定期実行する(ここでは30分おきに実施するようスケジュール)


# crontab -e
#AWstats::WEBログ解析
0,30 * * * * /root/bin/awstats.sh &gt; /root/bin/logs/awstats.log
Tagged with:
10月 22

net-snmp インストール

By ststyle Watch, net-snmp コメントは受け付けていません。

SNMPのインストール

「SNMP」とは「Simple Network Management Protocol」の略でルータやコンピュータなどの
様々な機器を監視、制御するためのプロトコルです。

SourceForgetから
net-snmp-5.1.2.tar.gzをDLします

http://sourceforge.net/project/showfiles.php?group_id=12694


$ tar xvzf net-snmp-5.4.1.2.tar.gz
$ cd net-snmp-5.4.1.2
$ ./configure
$ make
$ su
# make install

./configureの途中でSNMPのバージョンを

一番最初に問われるので ” v2 “ と入力し、(Enterでも良いです)
あとは全てEnterですっ飛ばします。

SNMP設定

デーモンの設定を行います

デフォルトの設定をコピーして、編集します。
編集すべき部分を矢印で示しますので、変更してください。


# cp EXAMPLE.conf /usr/local/share/snmp/snmpd.conf
# vi /usr/local/share/snmp/snmpd.conf

####
# First, map the community name (COMMUNITY) into a security name
# (local and mynetwork, depending on where the request is coming
# from):

# sec.name source community
→  com2sec local localhost private
→  #com2sec mynetwork XXX.XXX.XXX.0/24 public

####
# Second, map the security names into group names:

# sec.model sec.name
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
group MyROGroup v1 local
group MyROGroup v2c local
group MyROGroup usm local

####
# Third, create a view for us to let the groups have rights to:

# incl/excl subtree mask
view all included .1 80

####
# Finally, grant the 2 groups access to the 1 view with different
# write permissions:

# context sec.model sec.level match read write notif
access MyROGroup "" any noauth exact all none none
access MyRWGroup "" any noauth exact all all none

###################################
# System contact information
#

# It is also possible to set the sysContact and sysLocation system
# variables through the snmpd.conf file. **PLEASE NOTE** that setting
# the value of these objects here makes these objects READ-ONLY
# (regardless of any access control settings). Any attempt to set the
# value of an object whose value is given here will fail with an error
# status of notWritable.

syslocation Right here, right now.
→  syscontact admin &lt;admin@server.jp&gt;

一番下に書いたsyscontactは各自、管理用のユーザ・アドレスに置き換えてください。

起動スクリプトの作成

「/etc/init.d/snmpd」として保存してください。


vi /etc/init.d/snmpd

#!/bin/bash
# ucd-snmp init file for snmpd
#
# chkconfig: - 50 50
# description: Simple Network Management Protocol (SNMP) Daemon
#
# processname: /usr/local/sbin/snmpd
# config: /usr/local/share/snmp/snmpd.conf
# pidfile: /var/run/snmpd

# source function library
. /etc/init.d/functions

OPTIONS="-Lsd -Lf /dev/null -p /var/run/snmpd -a"
RETVAL=0
prog="snmpd"

start() {
echo -n $"Starting $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
daemon /usr/local/sbin/snmpd $OPTIONS
RETVAL=$?
[ $RETVAL -eq 0 ] &amp;&amp; touch /var/lock/subsys/snmpd
fi;
echo
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
if [ $UID -ne 0 ]; then
RETVAL=1
failure
else
killproc /usr/local/sbin/snmpd
RETVAL=$?
[ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/snmpd
fi;
echo
return $RETVAL
}

reload(){
echo -n $"Reloading $prog: "
killproc /usr/local/sbin/snmpd -HUP
RETVAL=$?
echo
return $RETVAL
}

restart(){
stop
start
}

condrestart(){
[ -e /var/lock/subsys/snmpd ] &amp;&amp; restart
return 0
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
condrestart)
condrestart
;;
status)
status snmpd
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
RETVAL=1
esac

exit $RETVAL

(アンパサンド書き換えて三箇所)

権限付与と自動起動の設定

# cd /etc/init.d
# chmod 755 snmpd
# chkconfig --add snmpd
# chkconfig --level 2345 snmpd on

SNMPの実行


/etc/init.d/snmpd start
Tagged with:
preload preload preload