caca-utilsで勉強会のデモにネタを仕込む
勉強会とかでターミナル作業のデモをやるときに仕込むと効果的です。
導入方法
sudo yum -y install caca-utils |
使い方
使い方としては、cacaviewかimg2txtコマンドを使って
img2txt -W `tput cols` path/to/image.jpg cacaview path/to/image.jpg img2txt -W 100 path/to/image.png |
てな感じ。一番上はtput colsで画面サイズを引数に渡してる感じです
~/.ssh/rc に
img2txt -W 60cols path/to/image.jpg" |
とか書いておくと、sshアタッチしたときに画像が出ます。ちょっとした小ネタにどうぞ。
logmonとMaatkitでMySQLレプリケーションエラーから自動復旧
昨日からMySQLのレプリケーションで特定のエラーが発生して止まってしまう現象が多発したので、logmonとMaatkitのmk-slave-restartで自動復旧するようにして対応。
エラー原因の根本解決じゃないですが、エラー検知して対応するまでレプリケーションが止まったままとか、深夜にレプリケーションが止まって叩き起こされるよりかは遙かにマシ。
logmonの導入
logmonについては以前ブログに書いたので、そちらを参照してください。
とりあえず下記のような感じで導入可能です。
mkdir ~/tmp cd ~/tmp wget 'http://www-06.ibm.com/jp/domino01/mkt/cnpages7.nsf/ec7481a5abd4ed3149256f9400478d7d/4925722f004efe92492575e200057580/$FILE/logmon_20100411.tgz' tar zxvf logmon_20100411.tgz cd logmon_20100411 ./setup.sh |
Maatkitの導入
Maatkitの導入は適当にググってください。私はRPM作成して導入してますが、ソースから入れる場合は下記のような感じです
mkdir ~/tmp cd ~/tmp wget http://maatkit.googlecode.com/files/maatkit-7284.tar.gz tar zxvf maatkit-7284.tar.gz cd maatkit-7284 perl Makefile.PL make make install clean |
/etc/logmon/logmon.conf の設定
監視用の設定を用意
:/var/lib/mysql/err.log (Error_code: 1100) mk-slave-restart -uroot --error-numbers 1100 --verbose -pxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx >> /var/lib/mysql/skip_query.log echo "<%%%%>" | mail -s "[db001.hogehoge.com] mk-slave-restart report" "alert@hogehoge.com" -- -f root@hogehoge.com |
パスワードは/root/.my.cnfか/root/.maatkit.confに書いておけばいいようですが、念のため設定。
mk-slave-restartが発動した際にはメールも発砲しておきます。
やっていることは
- mysqlのエラーログ /var/lib/mysql/err.log を監視
- [Error_code: 1100]という文字列を条件に指定
- 条件にマッチした場合は1100のエラーだけmk-slave-restartでskipしてalert@hogehoge.com宛てにメール発砲
- スキップしたクエリログは/var/lib/mysql/skip_query.logに記録
という感じです。実運用ではメールだけでなく社内IRCにも通知するようにもして、リアルタイムで検知出来るようにしてます。
構文のチェック
/etc/rc.d/init.d/logmon check |
logmonの起動
/etc/rc.d/init.d/logmon start |
これでレプリケーションが止まっても自動復旧するようになりました。実際はlogmonをSupervisordで管理してるんですが、長くなるんで別の機会にまとめます。
エラー番号ごとで実行する処理をかえておけば、他のエラーでも自動復旧可能ですが、あんまりskipしてるとDB壊れるんでご注意を。
LVM領域の拡張方法のメモ
LVMが個人的にあまり好きじゃいせいか、LVM領域の拡張方法をいつも忘れてググっているのでメモ
ディスク構成と容量の確認
パーティションは下記のような構成
[root@server002 ~]# fdisk -l Disk /dev/sda: 598.8 GB, 598879502336 bytes 255 heads, 63 sectors/track, 72809 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 131 1052226 83 Linux /dev/sda2 132 4047 31455270 83 Linux /dev/sda3 4048 7963 31455270 83 Linux /dev/sda4 7964 72809 520875495 5 Extended /dev/sda5 7964 8094 1052226 82 Linux swap /dev/sda6 8095 72809 519823206 8e Linux LVM |
下の例だと/dev/mapper/vg_home-lv_homeってのがLVMを組んでいる領域です。
[root@server002 ~]# df -ah Filesystem Size Used Avail Use% Mounted on /dev/sda2 30G 826M 28G 3% / none 0 0 0 - /proc none 0 0 0 - /sys none 0 0 0 - /dev/pts usbfs 0 0 0 - /proc/bus/usb /dev/sda1 1012M 41M 920M 5% /boot none 3.9G 0 3.9G 0% /dev/shm /dev/mapper/vg_home-lv_home 168G 92M 159G 1% /home /dev/sda3 30G 88M 28G 1% /var none 0 0 0 - /proc/sys/fs/binfmt_misc sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs |
未割り当て領域サイズの確認
[root@server002 ~]# vgdisplay -v Finding all volume groups Finding volume group "vg_home" --- Volume group --- VG Name vg_home System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 495.72 GB PE Size 32.00 MB Total PE 15863 Alloc PE / Size 5435 / 169.84 GB Free PE / Size 10428 / 325.88 GB VG UUID jraZSp-lOCp-NDK3-qevx-96Tc-eMvh-1lZdjd --- Logical volume --- LV Name /dev/vg_home/lv_home VG Name vg_home LV UUID nZbwzV-acnK-axmN-FDB2-06o7-voXn-e5nHkc LV Write Access read/write LV Status available # open 1 LV Size 169.84 GB Current LE 5435 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Physical volumes --- PV Name /dev/sda6 PV UUID GDG5eD-J1jo-oKmb-Eb7r-TCy6-9cIn-wh5o7Z PV Status allocatable Total PE / Free PE 15863 / 10428 |
最下行のTotal PEの15863が最大で割り当てたときのサイズで、Free PEの10428が未割当のサイズ
LVM領域の拡張
割当可能な領域全部を割り当てる
[root@server000 ~]# lvextend -l 15863 /dev/vg_home/lv_home Extending logical volume lv_home to 495.72 GB Logical volume lv_home successfully resized |
指定容量だけ増加させるには下記のような感じ
lvextend -L +20G /dev/vg_home/lv_home |
割り当てた領域の確認
[root@server000 ~]# vgdisplay -v Finding all volume groups Finding volume group "vg_home" --- Volume group --- VG Name vg_home System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 495.72 GB PE Size 32.00 MB Total PE 15863 Alloc PE / Size 15863 / 495.72 GB Free PE / Size 0 / 0 VG UUID qFAi4b-krCN-N9rN-u34V-euc8-eU1N-gKsD1N --- Logical volume --- LV Name /dev/vg_home/lv_home VG Name vg_home LV UUID 0QfiuO-MfB4-nyoL-9dQm-L0ps-BkOE-FzaFmZ LV Write Access read/write LV Status available # open 1 LV Size 495.72 GB Current LE 15863 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Physical volumes --- PV Name /dev/sda6 PV UUID 5t2Vka-tweo-2AcM-MTGj-pYAq-RfTz-SLJHeY PV Status allocatable Total PE / Free PE 15863 / 0 |
LV Sizeが495.72GBに増えているのでOK
LVM領域をumount
fsckとLVMのリサイズをするためにumount
umount /dev/vg_home/lv_home |
fsckをかけてファイルシステムのチェックと修復
[root@server002 ~]# fsck -f /dev/vg_home/lv_home fsck 1.35 (28-Feb-2004) e2fsck 1.35 (28-Feb-2004) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vg_home/lv_home: 11/22265856 files (9.1% non-contiguous), 722110/44523520 blocks |
LVM領域のサイズ変更
[root@server002 ~]# resize2fs /dev/vg_home/lv_home resize2fs 1.35 (28-Feb-2004) Resizing the filesystem on /dev/vg_home/lv_home to 129949696 (4k) blocks. The filesystem on /dev/vg_home/lv_home is now 129949696 blocks long. |
LVM領域の再mount
mount /dev/vg_home/lv_home /home |
容量の確認
[root@server002 ~]# df -ah Filesystem Size Used Avail Use% Mounted on /dev/sda2 30G 826M 28G 3% / none 0 0 0 - /proc none 0 0 0 - /sys none 0 0 0 - /dev/pts usbfs 0 0 0 - /proc/bus/usb /dev/sda1 1012M 41M 920M 5% /boot none 3.9G 0 3.9G 0% /dev/shm /dev/sda3 30G 88M 28G 1% /var none 0 0 0 - /proc/sys/fs/binfmt_misc sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs /dev/mapper/vg_home-lv_home 488G 102M 464G 1% /home |
/dev/mapper/vg_home-lv_homeが増えているので完了
スーパーサーバーSupervisorの導入手順メモ
daemontoolsの代替として最近使い出したスーパーサーバーSupervisorの導入方法をいつも忘れるのでメモ。ほとんど@hibomaが社内Wikiにまとめてた内容のパクリですが、自宅サーバ構築とかで外から見ようと思っても見えないので。いつもありがとう、ひろやん!!
導入環境はScientific Linux 6.1です。(Upstart経由での起動の部分を、init経由での起動にすればCentOS 5系でも動作するかと思います)
Supervisorの導入
easy_installでも良いんですが、pipで導入
sudo yum -y install python-setuptools sudo easy_install pip sudo pip install supervisor |
log用ディレクトリと、conf用ディレクトリを用意
デフォルトだと/tmp配下にログが吐かれるので、log用ディレクトリの作成
sudo mkdir /var/log/supervisor/ |
監視するデーモンごとに設定を分けるので、confディレクトリを用意
sudo mkdir /etc/supervisord.d |
Supervisorの設定を用意
conf生成用のコマンドで作成
sudo echo_supervisord_conf > /etc/supervisord.conf |
zshだとエラー(zsh: permission denied: /etc/supervisord.conf)が出てので、下記コマンドで生成。
sudo su - root -c "echo_supervisord_conf > /etc/supervisord.conf" |
下記のように修正をしてます。
--- /etc/supervisord.conf.dist 2011-11-24 15:19:15.940609281 +0900+++ /etc/supervisord.conf 2011-11-24 15:23:12.655620601 +0900@@ -13,11 +13,11 @@ ;password=123 ; (default is no password (open server))
[supervisord]-logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)+logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace)-pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)+pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200)@@ -125,5 +125,5 @@ ; interpreted as relative to this file. Included files *cannot* ; include files themselves.
-;[include]-;files = relative/directory/*.ini+[include]+files = /etc/supervisord.d/*.ini監視するデーモンの設定は
/etc/supervisord.d/hogehoge.ini |
みたいな感じで用意します。
Hesoを監視するようにした時の設定の/etc/supervisord.d/heso.iniは下記のような感じです。
[program:heso]command=/var/www/heso/bin/serverstdout_logfile_maxbytes=1MBstderr_logfile_maxbytes=1MBstdout_logfile=/var/log/%(program_name)s.logstderr_logfile=/var/log/%(program_name)s.logautorestart=trueUpstart経由でSupervisorを起動
Supervisor自体が落ちたら意味がないので、Upstartで経由で起動。Supervisorが落ちたらUpstartが自動で起動。
/etc/init/supervisord.conf を用意
description "supervisord"
start on runlevel [2345]stop on runlevel [!2345]
respawnexec /usr/bin/supervisord -n下記コマンドで起動
sudo initctl start supervisord |
psなどでプロセスを確認して起動していればOK。supervisordをkillして自動で起動してくるかも確認。
CentOS 5とかならinittabに下記のような設定をして、init経由で起動させれば良いかと。
svd:2345:respawn:/usr/bin/supervisord -nSupervisorの各種コマンド
supervisorctl
add -- Activates any updates in config for process/group
avail -- Display all configured processes
clear -- Clear process/multiple-process/all-process log files
fg -- Connect to a process in foreground mode
help -- Show help
maintail -- tail of supervisor main log file
open -- Connect to a remote supervisord process. (for UNIX domain socket, use unix:///socket/path)
pid -- Get the PID of supervisord.
quit exit -- Exit the supervisor shell.
reload -- Restart the remote supervisord.
remove -- Removes process/group from active config
reread -- Reload the daemon's configuration files
restart -- Restart process or group.
shutdown -- Shut the remote supervisord down.
start -- Start process or groups.
status -- Get process status info.
stop -- Stop process or group.
tail -- tail of process stdout
update -- Reload config and add/remove as necessary
version -- Show the version of the remote supervisord process |
たとえばHeso用の設定を変更した場合は下記みたいな感じで設定を再読込
sudo supervisorctl reread heso |
supervisorctlをzshで補完
supervisorctlコマンドをzshで補完出来るように。
functionsの下に置くと、自動で読み込まれるようになります。
sudo yum -y install hg mkdir ~/tmp cd ~/tmp hg clone https://bitbucket.org/hhatto/zshcompfunc4supervisor cd zshcompfunc4supervisor sudo cp _supervisor* /usr/share/zsh/4.3.10/functions/ |


