zuntan02のはてなブログ

備忘録的なものです。時々職場の技術者ブログにも転記してますが、メインはこちらで。

【さくらのクラウド】日次アーカイブで削除に失敗したときにメール通知させる

【前提】

SCHotBackup
https://github.com/hsur/SCHotBackup/blob/master/daily_archive.sh
を利用してアーカイブを取得しているとき、アーカイブタイムアウト→前々日の削除処理ができないでアーカイブがたまってしまって請求が!!!みたいなことがあったので、ログをチェックして処理の衝突ログ(Conflict)があったらメールするようにしています。

【チェック対象となるアーカイブのログについて】

上記アーカイブスクリプトをcronで実行する際、以下の様にしてログを出力しています
ex.crontabで以下の様な設定

05 01 * * * /bin/bash -l /home/hoge/bin/daily_archive.sh > /home/hoge/log/sakura_auto_archive_`date +\%Y\%m\%d`.log

失敗したときのログ

20xx-xx-xx 01:05:02 [18516]: ===== START =====
20xx-xx-xx 01:05:02 [18516]: get_disk_list()
20xx-xx-xx 01:05:04 [18516]: ----- START: xxxxxxxxxxxx (hogehoge.jp) -----
20xx-xx-xx 01:05:04 [18516]: archive:xxxx.archive.json
20xx-xx-xx 01:05:04 [18516]: create_archive()
20xx-xx-xx 01:05:06 [18516]: ARCHIVE_ID: xxxxxxxxxxxx
20xx-xx-xx 01:05:37 [18516]: waiting... (0/512000MB)
(中略)
20xx-xx-xx 05:05:30 [18516]: waiting... (326208/512000MB)
20xx-xx-xx 05:05:30 [18516]: [ERROR] Timed out!: xxxxxxxxxxxx
20xx-xx-xx 05:05:30 [18516]: [ERROR] Failed to create archive.
20xx-xx-xx 05:05:30 [18516]: delete_archive()
20xx-xx-xx 05:05:30 [18516]: {"is_fatal":true,"serial":"xxxxxxxxxx","status":"409 Conflict","error_code":"disk_is_copying","error_msg":"(省略)"}

【チェック&メール通知スクリプト

#!/bin/sh

# メール送信設定
_to="fuga@hogehoge.jp"
_from="sakurabackup@hogehoge.jp"

# メール件名設定
_hostname=`hostname`
_application="SAKURA Cloud Daily BackUP"
_subject="Fatal error!!"
_title="[${_application}][${_hostname}]${_subject}"

# エラー条件
_error_conditions="Conflict"

for file in `find /home/hoge/log/* -type f -mtime -1`; do
        err_line=`cat $file | grep -a ${_error_conditions}`
        if [[ ${err_line} = *${_error_conditions}* ]]; then
                /usr/sbin/sendmail -t <<- EOS
                MIME-Version: 1.0
                Content-Type: text/plain; charset=iso-2022-jp
                From: ${_from}
                To: ${_to}
                Subject: ${_title}
                SAKURA Cloud Backup Failed
                ----------
                log_file:
                ${file}

                error_message:
                ${err_line}
                ----------
                Please check with a administrator for details.
EOS
        fi
done

上記を

00 12 * * * /bin/bash -l /home/hoge/bin/sakurabkup-notice.sh  2>&1

のようにして実行してヒットがあれば見に行く。
連続するようならアーカイブスクリプトタイムアウト時間(MAX_SLEEP_SECS=)を延ばすなどの対応をしています。

【qmail+vpopmail】vchkpwでCannot allocate memory

qmail+vpopmailの環境でpop3接続しようとするとエラーになる。telnetしてみると以下のエラー

telnet localhost 110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
+OK <xxxx.xxxxxxxxxx@hogheoge.jp>
user fugafuga@hogheoge.jp
+OK
pass hogehoge
/home/vpopmail/bin/vchkpw: error while loading shared libraries: libdl.so.2: failed to map segment from shared object: Cannot allocate memory
-ERR authorization failed
Connection closed by foreign host.

→メモリが足りない模様
# 念の為lddしてモジュールがあることは確認しておく
ldd /home/vpopmail/bin/vchkpw

        linux-vdso.so.1 =>  (0x00007fff485dc000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f3f7bde2000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f3f7ba1e000)
        libfreebl3.so => /lib64/libfreebl3.so (0x00007f3f7b81c000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f3f7c019000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f3f7b618000)

→OK

【解決】

qmail-pop3dのsoftlimitを増やす
vi /var/qmail/supervise/qmail-pop3d/run

exec /usr/local/bin/softlimit -m 10000000 \

exec /usr/local/bin/softlimit -m 50000000 \

qmail再起動したらつながった

無知メモ:MXレコードが無い時はAレコードを見る

detail.chiebukuro.yahoo.co.jp

RFC5321より
http://www.ietf.org/rfc/rfc5321.txt

2.3.5. Domain Names
Only resolvable, fully-qualified domain names (FQDNs) are permitted
when domain names are used in SMTP. In other words, names that can
be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
in Section 5) are permitted, as are CNAME RRs whose targets can be
resolved, in turn, to MX or address RRs.

ということなので、MXじゃなくてもFQDNであれば、AでもAAAAでもCNAMEでもいい

AmazonLinux+NginxにLet's Encrypt

AWS EC2で運用しているWebサイト(Nginx)にLet's EncryptでSSL証明書設置したメモ。

【作業】

証明書発行用ドキュメントルート用意

Let's Encryptで証明書発行(ドメイン使用権認証)用のファイルを設置するドキュメントルートが必要です。
ここでは、既存のvhostについて、Let's encrypt が webroot 更新で使う部分だけを別のディレクトリに設定しています。

mkdir /srv/www/letsencrypt_webroot
chown nginx:nginx /srv/www/letsencrypt_webroot

# nginx conf
cd /etc/nginx/conf.d
vi hogehoge.conf
以下を追記する

location /.well-known/ {
    root  /srv/www/letsencrypt_webroot;
}
nginx書式テスト&反映

nginx -t
service nginx reload

Let's Encrypt インストール

1. AWSのセキュリティグループを設定

→サーバに適用されているセキュリティグループのインバウンドのHTTP(80)、HTTPS(443)を許可(0.0.0.0/0)にしておきます。

2. certbotのダウンロード&配置&権限設定
curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
chmod 700 /usr/bin/certbot-auto
3. 証明書の発行

cerbotのコマンドで証明書が自動生成されます。
※2018年1月までワイルドカード証明書は発行できないらしいので、今回はhogehoge.comとwww.hogehoge.comの、www有無それぞれで発行します。

certbot-auto certonly --webroot -w /srv/www/letsencrypt_webroot -d hogehoge -d www.hogehoge --email admin@hogehoge --debug

→ 不足している必須モジュールがインストールされる

※ --debugについて:
これつけないと、AmazonLinuxでは以下のメッセージが出るもよう。

FATAL: Amazon Linux support is very experimental at present...
if you would like to work on improving it, please ensure you have backups
and then run this script again with the --debug flag!
Alternatively, you can install OS dependencies yourself and run this script
again with --no-bootstrap.

※そのほかcertbot-autoのオプションについては下端にメモしておきました。

※その他のエラーが出た場合の対応メモ
エラー1

/usr/bin/certbot-auto: line 700: virtualenv: command not found

→ virtualenvは「/usr/bin/virtualenv-2.7」以下に存在したので、update-alternativesでリンクを張る

# update-alternatives設定
update-alternatives --install /usr/bin/pip pip /usr/bin/pip-2.7 27
update-alternatives --install /usr/bin/virtualenv virtualenv /usr/bin/virtualenv-2.7 27

# 確認
update-alternatives --display pip
update-alternatives --display virtualenv

エラー2

Error: couldn't get currently installed version for /opt/eff.org/certbot/venv/bin/letsencrypt:

→参考
Let's Encrypt 証明書更新時のエラー対処 Amazon Linux AMI - とうふの雑記帳



以下対話式で進める

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-staging.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y

(省略)

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/hogehoge/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/hogehoge/privkey.pem
   Your cert will expire on 2017-12-04. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
(省略)
4. 生成された証明書の確認

以下のディレクトリに証明書が生成されます。

/etc/letsencrypt/live/hogehoge/
5.nginx側に設定
server {
    listen 443 ssl;
    ssl on;
(省略)
    ssl_certificate     /etc/letsencrypt/live/hogehoge/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/hogehoge/privkey.pem;
(省略)
6.確認

https://hogehoge/
https://www.hogehoge/
SSL通信できていることを確認
https://www.ssllabs.comで確認してランクAであることを確認

7.証明書の自動更新

certbot-auto renew

# →crontabに設置する:例
crontab -e

50 3 * * 0 /usr/bin/certbot-auto renew --post-hook "/etc/init.d/nginx reload" > /path/to/log 2>&1

追記
設置環境により証明書更新時に以下の様なエラーを確認した

Failed to find executable service in expanded PATH: /usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Unable to find post-hook command service in the PATH.
(PATH is /usr/bin:/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin)

上記はpost-hookが実行するコマンドにpathが通っていないから。
(ex:service nginx reloadなどのばあい、/sbin/service/nginxなどとする必要がある)

【注意】
https://への総301転送を行っている様なサイトの場合、.well-knownだけはhttpに落とさせる必要がある模様
例)

     # for lets encrypt
         location /.well-known/ {
             root  /hoge/fuga/letsencrypt_webroot;
         }

     # 301 redirect from old URL to new URL
         location / {
             return 301 https://hogefuga.jp$request_uri;
         }

のようにしておくとよい

certbot-auto オプション説明

certonly

SSL/TLS サーバ証明書の取得のみを行います。

--webroot

→ウェブサーバの DocumentRoot ディレクトリ以下に認証用のファイルを設置することでドメイン使用権者の認証を行って、SSL/TLS サーバ証明書を取得します。
Certbot クライアントを実行するコンピュータでウェブサーバ(httpd)が動作中であり、ウェブサーバを停止することなく SSL/TLS 証明書を発行したい場合には、Webroot プラグインを利用する必要がある(https://letsencrypt.jp/docs/using.html***webroot

--webroot-path WEBROOT_PATH もしくは -w WEBROOT_PATH

→"public_html" や "webroot" のパスを指定します。

-d DOMAIN もしくは --domains DOMAIN もしくは --domain DOMAIN

SSL/TLS サーバ証明書の取得を申請するドメイン名を指定します。

--post-hook POST_HOOK

証明書を取得・更新する試みが終わった後にシェルで実行するコマンドを指定します。
これは、証明書を取得・更新する試みが行われた場合のみ実行されます。

OracleのJDKをLinuxのwgetで取得する

【参考サイト】
qiita.com

【現象】
OracleのサイトのDLリンクwgetそのままではうまくDLできない。
→以下の様なオプションをつけることでwgetできる。

--no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie"

例)

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u144-b01/ダウンロードリンク/jdk-8u144-linux-x64.rpm

【AWS】EIPの再関連付け

【作業概要】

インスタンス 関連付けられているEIP
インスタンスA 54.1.1.1
インスタンスB 54.2.2.2

があったと仮定して、インスタンスBのEIPをインスタンスAに再付与する。

インスタンスAのEIPを解除する

これをやっとかないとあとで他のEIPを関連付けようとすると既に関連付けがある旨のエラーになる。
[Elastic IP]-[アクション]-[アドレスの関連付けの解除]でインスタンスAのEIPを解除

インスタンスBのEIPをインスタンスAに関連付ける

インスタンスBに付与されていた 54.2.2.2 を
[アドレスの関連付け]でインスタンスAに関連付ける

  • 再関連付け:ON

で[関連付け]
インスタンスBからはEIPがはく奪され、インスタンスAに関連付けられる。

Windows10のプロダクトキー確認方法

【メモ】

以前のバージョンからWin10にアップグレードした環境においては、「プロダクトキー」は共通化されているようです。
【参照】
【2018年版】Windows 10 無償アップデート終了の行方 | Project DC-01 Sub Blog
によれば、以前のバージョンからUpgradeした人は以下のプロダクトキーに書き換えられるようです
Windows 10 Home Edition YTMG3-N6DKC-DKB77-7M9GH-8HVX7
Windows 10 Home Edition Single Language BT79Q-G7N6G-PGBYW-4YWX6-6F4BT
Windows 10 Pro Edition VK7JG-NPHTM-C97JM-9MPGT-3V66T
Windows 10 Home N Edition 4CPRK-NM3K3-X6XXQ-RXX86-WXCHW
Windows 10 Pro N Edition 2B87N-8KFHP-DKV6R-Y2C8J-PKCKT

※じゃあWIn10 home editionのPCで上記Proのプロダクトキー入れたらどうなるの?って思うけど当然アップグレードはできないのだった(キーが正しくないと言われる)

Windows10の[プロダクトキー]確認方法

コマンドプロンプトPowerShellでは、以前の様には確認出来なくなっているようです。

フリーのツールがあるのでそちらを試すのが良いかと。

Windows10の[プロダクトID]確認方法

※プロダクトIDとは、インストールすると自動生成される番号で、MSへの問い合わせの際などに利用します。
[設定]-[システム]-[バージョン情報]
で確認可能です。

ライセンス形態について

http://www.vwnet.jp/Windows/Other/license.htm