monitを使った環境でsshd.pidが消える原因を調べる

先日 Monit を使った環境で、sshdがMonitに再起動されるとpidファイルが消える事象が発生しました。
その際に調べたときのメモです。

環境


環境は以下の通りです。

  • CentOS6.4(64bit)
  • openssh-server-5.3p1-84.1.el6.x86_64
  • monit-5.5-1.el6.rf.x86_64


「/etc/init/monit.conf」は以下の通りです。

description     "monit" 

start on runlevel [2345]
stop on runlevel [!2345] 

respawn
exec /usr/bin/monit -Ic /etc/monitrc


「/etc/monitrc」は以下の通りです。

# cat /etc/monitrc 
set daemon 120 with start delay 30
set logfile /var/log/monit/monit.log
set idfile /var/monit/id
set statefile /var/monit/state
set eventqueue
    basedir /var/monit  
    slots 100           
set httpd port 2812 and
    allow localhost 
    allow admin:monit      
include /etc/monit.d/*.monit


「/etc/monit.d/sshd.monit」は以下の通りです。

check process sshd
      with pidfile "/var/run/sshd.pid"
      start program = "/etc/init.d/sshd start"
      stop program = "/etc/init.d/sshd stop"
      if 3 restarts within 3 cycles then alert
      if failed port 22 protocol ssh then restart

事象


sshdがMonitに起動された場合、プロセスは存在するのですが、pidファイル(/var/run/sshd.pid)ファイルが
存在しないケースがありました。


具体的には以下のようなケースです。

# ps -fe | grep sshd | grep -v grep
root      2461     1  0 07:42 ?        00:00:00 /usr/sbin/sshd
# ls -la /var/run/sshd.pid
ls: cannot access /var/run/sshd.pid: No such file or directory


Monitのログ(/var/log/monit/monit.log)、「/var/log/secure」には以下のように出力されていました。

  • /var/log/monit/monit.log
[EDT Jun 15 14:05:26] error    : 'sshd' failed protocol test [SSH] at INET[localhost:22] via TCP -- SSH: error receiving identification string -- Resource temporarily unavailable
[EDT Jun 15 14:05:26] info     : 'sshd' trying to restart
[EDT Jun 15 14:05:26] info     : 'sshd' stop: /etc/init.d/sshd
[EDT Jun 15 14:05:27] info     : 'sshd' start: /etc/init.d/sshd
[EDT Jun 15 14:07:27] error    : 'sshd' process is not running
[EDT Jun 15 14:07:27] info     : 'sshd' trying to restart
[EDT Jun 15 14:07:27] info     : 'sshd' start: /etc/init.d/sshd
[EDT Jun 15 14:07:57] error    : 'sshd' failed to start
  • /var/log/secure
Jun 15 14:07:27 localhost sshd[7858]: error: Bind to port 22 on :: failed: Address already in use.
Jun 15 14:07:27 localhost sshd[7858]: fatal: Cannot bind any address.

原因


原因は「/etc/hosts.allow」で、localhost(127.0.0.1)が許可されていないことが原因でした。

[EDT Jun 15 14:05:26] error    : 'sshd' failed protocol test [SSH] at INET[localhost:22] via TCP -- SSH: error receiving 


に、出力されていますが。


ということで、「/etc/hosts.allow」に以下のエントリを追加し、解決しました。

sshd: 127.0.0.1


今日はこんなところで。