ローカルに置いてある証明書の有効期限をチェックするスクリプト



WEBページで使ってるわけではない、ローカルに置いてある
SSLの証明書の期限を確認する必要があったのでつくったメモ。

certwatchが入ってるcrypto-utilsをyumでインストール

$ yum install crypto-utils

$ /usr/bin/certwatch --help
/usr/bin/certwatch: unrecognized option '--help'


$ info certwatch
SYNOPSIS
certwatch [OPTION...] filename

DESCRIPTION
The certwatch program is used to issue warning mail when an SSL
certificate is about to expire.

...

The program has two modes of operation: normal mode and quiet mode. In
normal mode, the certificate given by the filename argument is
examined, and a warning email is issued to standard output if the
certificate is outside its validity period, or approaching expiry. If
the certificate cannot be found, or any errors occur whilst parsing the
certificate, the certificate is ignored and no output is produced. In
quiet mode, no output is given, but the exit status can still be used.

The certificate can be specified by its nickname or by a path to the
containing file.

OPTIONS
--quiet, -q
Enable quiet mode; no output is produced whether the certificate is
expired or not

--period days, -p days
Specify the number of days within which an expiry warning will be
produced; default is 30. Expiry warnings are always produced if, on
the day of invocation, the certificate is not yet valid, has
already expired, or is due to expire either that day or the
following day.

--address address, -a address
Specify the address used in the To field of the warning e-mail
issued if quiet mode is not enabled. The default is root.

--directory cert-directory, -d cert-directory
Specify the database directory containing the certificate and key
database files. The default is yet to be determined.



という感じ。


で、/etc/cron.daily/certwatchというファイルが
インストールしたらできるんですが
夜中の04:22分に起こされて

「キャッホー!有効期限がもうすぐ切れるんだね!キャハハ!オッケー!」

なんてテンションにはなれないので
cron.dailyのヤツは無効化します。

$ mv -i /etc/cron.daily/certwatch /etc/cron.daily/.certwatch

ちなみにRedHatの6系になってからcronがいい感じにヤバいですね。
/etc/crontabがなくなり、
/etc/anacrontabを使っていこうぜと。

詳細はこちら
Vixie cron と Cronie


いい感じに柔軟ですね。


anacron使えばデイリーで設定しても03:00-22:00の間で実行してくれると。
問題がなければ03:00に実行してくれる。


何故anacrontabなのかは


anacronふたたび。(「RHEL6で悩ましい諸々」シリーズ)


Nakaiさんのまとめがとても素晴らしいです。


従来通りに使うなら /etc/crontab に記述しても読み込んでくれるのでそっちします。

話がそれましたが、有効期限のチェックスクリプトを自分で置きます。

/etc/cron.d/certwatch

0 12 * * * root /root/script/certwatch_check.sh


/root/script/certwatch_check.sh

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
export PATH

LIST="foo@example.org foo@example.org baz@example.org"

CERT_FILE="/path/example.cer"
CHECK_DAY="90"

for MAIL_TO in $LIST
do
/usr/bin/certwatch $CERT_FILE -p $CHECK_DAY -a $MAIL_TO | /usr/sbin/sendmail -oem -t $MAIL_TO
done

if [ $? -ne 0 ]
then
/usr/bin/curl -d 'message=im.kayac.com Cert File Check Errrrorr' http://im.kayac.com/api/post/example
exit 1
fi

みたいなスクリプトを置いて確認するようにしました。
そもそも90日前になったら通知するように
cronかGoogleカレンダーにでも登録しておけばいいじゃんという話もありますが。。


そんな感じです。