mytopインストール

MySQL版topコマンドみたいな感じ。

mytop - a top clone for MySQL

インストール

ダウンロードして解凍

# tar zxvf mytop-1.6.tar.gz

Make

# cd mytop-1.6
# perl Makefile.PL
# make
# make test
# make install

perl Makefile.PLでMakeMakerが無いと言われたらインストールする。

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .) at Makefile.PL line 1.
BEGIN failed--compilation aborted at Makefile.PL line 1.
# yum -y install perl-ExtUtils-MakeMaker
本来はCPANからインストールする様だけどyumでも出来た。

ReadKeyが無いとWarningが出たらインストールする。

Checking if your kit is complete...
Looks good
Warning: prerequisite Term::ReadKey 2.1 not found.
Writing Makefile for mytop
# yum -y install perl-TermReadKey

スクリプト微修正

そのままだとなんか動かない

Error in option spec: "long|!"
ので修正する。

# chmod u+w /usr/local/bin/mytop
# vi /usr/local/bin/mytop
...
"long|!" => \$config{long_nums}, ←これの"long|!"を"long|long_nums|l!"に。!の前の小文字Lを忘れずに。
...

実行

# /usr/local/bin/mytop -s1 -hホスト名 -uユーザ名 -pパスワード

MySQLのlogrotate設定

cronで動かした時だけログがフラッシュされなかったりとハマったわ…。

まずlogrotateの設定
コメントされてはいるものの、もともと内容が書いてあるが、
スロークエリログなどもローテーションしたいので少し書き換えて以下の様にした。

# vi /etc/logrotate.d/mysql
# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/var/log/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password =
# user= root
#
# where "" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !

# Then, un-comment the following lines to enable rotation of mysql's log file:

/var/log/mysqld.log /var/lib/mysql/slow.log {
#create 640 mysql mysql
notifempty
daily
rotate 7
missingok
dateext
compress
delaycompress
sharedscripts
postrotate
# just if mysqld is really running
if test -x /usr/bin/mysqladmin && \
/usr/bin/mysqladmin ping &>/dev/null
then
/usr/bin/mysqladmin flush-logs
fi
endscript
}

コメントに書いてある通り、/root/.my.cnfを作成してmysqladminがパスなしでも動作する様にする。

# vi /root/.my.cnf
[mysqladmin]
user=root
password=パスワード
# chmod 600 /root/.my.cnf

やらなくてもいいけど、うまく動作しない理由を知りたかったので
logrotateの出力を見れる様にした。

# vi /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate -v /etc/logrotate.conf >/var/log/logrotate 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

  • vオプションつけて、標準出力の内容を/var/log/logrotateに書き込む様にした。

ハマってた時にこのログを見ると、mysqladminで「error: 'Access denied for user 'root'@'localhost' (using password: NO)'」
が出ていて、
手動でlogrotate -f /etc/logrotate.confするとflush-logs実行されるのに何でcronだとダメなのかまったくわからなかったが、
それは↓のが原因だった。

cron.d/dailyjobsのHOMEを修正する。

# vi /etc/cron.d/dailyjobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/root/ ←コレ!!!!コレが/になってたら/root/に直す!!
/etc/crontabを変えても反映されないので注意。

これで毎日ログがフラッシュされる様になった。

logrotateで-fなしでもう一度ローテーション処理を実行させる

前日分のファイルがあればとりあえず消すなりリネームするなりする。

# mv hoge.log-20121013 hoge.log-20121013.bak

/var/lib/logrotate.statusを編集する

# vi /var/lib/logrotate.status
...
"hoge.log" 2012-10-13 ←2012-10-12とかに書き換える
...

logrotate実行

# logrotate -v /etc/logrotate.conf

access.confの謎

hogeユーザは特定IP以外禁止で、hoge2ユーザはどこからでもOKという設定をする時、
以下の設定のようにhoge2を先に持ってこないとhoge2が接続出来なかった。

# vi /etc/security/access.conf
...
# hoge2が先ならOK
+ : hoge2 : ALL
- : hoge : ALL EXCEPT xxx.xxx.xxx.xxx

逆だとNG。

# hogeが先だとNG。「hoge」の設定が適用されてるっぽい?
- : hoge : ALL EXCEPT xxx.xxx.xxx.xxx
+ : hoge2 : ALL

先にALL接続許可とかしてもダメだった。

# いけそうなもんだけどNG。
+ : ALL : ALL
- : hoge : ALL EXCEPT xxx.xxx.xxx.xxx

上の設定から順に前方一致で検索してるのかも?
勉強しないとダメだな。

ユーザ毎のSSH制限

ユーザ毎に認証方式を変える

/etc/ssh/sshd_config に Match で指定する。
Match は OpenSSH 4.4以降で使用出来る(らしい、4.3は無理だった)。

# vi /etc/ssh/sshd_config
...
Match User erio_nk
PasswordAuthentication no
erio_nkユーザのみパスワード認証をOFFにする。

ユーザ毎にアクセス元IPを制限する

PAMを利用して制限する。

PAMのsshアクセス制限を有効にする。

# vi /etc/pam.d/sshd
account required pam_access.so
...
一番「上」の行に追加する事に注意。

/etc/security/access.confを編集してアクセス制限を設定する。

# vi /etc/security/access.conf
...
+ : ALL : cron crond
- : erio_nk : ALL EXCEPT XXX.XXX.XXX.XXX.
erio_nkユーザは XXX.XXX.XXX.XXX からのみアクセス可能にする。

cronの行が無いとcronの動作に支障が出る様で、以下の様なログが出て正常に処理が実行されない模様(/usr/lib/sa/sa1辺りで出てるっぽい?)。
/var/log/cron

拒否されたパーミッション
CRON (root) ERROR: failed to open PAM security session: 成功です
CRON (root) ERROR: cannot set security context
/var/log/secure
pam_access(crond:account): access denied for user `root' from `cron'

CentOS5.5にOpenSSH5.8p2を入れる

手前のCentOS5.5で入っているOpenSSHが4.3p2-41.el5_5.1で、
yumで最新のを入れても4.3p2-82.el5だった為、以下のURLで紹介されている
/etc/ssh/sshd_configの「Match」が使用出来なかった。

ssh の認証方法をユーザごとに設定する - isseium's blog
http://jfut.integ.jp/2007/03/27/

ので、色々調べてみたところ、CentALTというリポジトリに5.8p2があるという
情報を見つけたので、入れてみた。

# rpm -ivh http://centos.alt.ru/repository/centos/5/x86_64/centalt-release-5-3.noarch.rpm

バージョンのところは読み替える事。
なくなってたらx86_64まで戻って最新を探す。

/etc/yum.repos.d/centalt.repoが作成され、enabledが1になっていたので、
面倒な競合を避けるために一旦OFFる。

# vi /etc/yum.repos.d/centalt.repo
...
enabled=0
...

確認。

# yum list openssh
...
Installed Packages
openssh.x86_64 4.3p2-41.el5_5.1 installed
Available Packages
openssh.x86_64 5.8p2-16.el5.1 CentALT

アップグレード

# yum --enablerepo=CentALT upgrade openssh
...
Dependencies Resolved

================================================================================================
Package Arch Version Repository Size
================================================================================================
Updating:
openssh x86_64 5.8p2-16.el5.1 CentALT 310 k
Updating for dependencies:
openssh-clients x86_64 5.8p2-16.el5.1 CentALT 645 k
openssh-server x86_64 5.8p2-16.el5.1 CentALT 353 k

Transaction Summary
================================================================================================
Install 0 Package(s)
Upgrade 3 Package(s)

Total download size: 1.3 M
Is this ok [y/N]:

依存パッケージもopenssh-clients,serverぐらいであまり影響は無さそう。

Is this ok [y/N]: y
...

Updated:
openssh.x86_64 0:5.8p2-16.el5.1

Dependency Updated:
openssh-clients.x86_64 0:5.8p2-16.el5.1 openssh-server.x86_64 0:5.8p2-16.el5.1

Complete!

Match使えるようになった!

指定ディレクトリ以下にあるファイルのみパーミッションを変更する

$ find . -name "*.sh" -type f -exec chmod a+w {} \;

セミコロンをエスケープしておかないと-execに引数が見つかりませんとか出るので注意。
ディレクトリのみに適用する場合は-type d。

パイプでつないでxargsを使ってchmodする方法もあるが、
コマンドラインで指定する等でパイプが使えないパターンではこちらが有効かと。
細かいけど-nameのクォーテーションはシングルクォートでも大丈夫。

参考
ファイルまたはディレクトリのみへの chmod コマンド - Qiita
UNIX findとxargsコマンドで-print0オプションを使う理由
2007-05-16