Cent OS 6 / Red Hat 6 で誤ってCent OS 7 / Red Hat 7向けのmackerel-agentを入れてしまった場合の復旧法

技術書典の執筆追い込み(いわゆる修羅場)で報告が遅れていましたが、このたびはてな様よりMackerelのアンバサダーとして認定をいただきました。

mackerel.io

以下はアンバサダーの認定にあたり、進呈いただいたノベルティーになります。ありがとうございます。

f:id:setoazusa:20190427215903j:plain

拙著「Mackerelではじめるお手軽Webサービス監視」もPODとしてはロングテールな売れ行きだそうですので、共々よろしくお願いします。

さて、Mackerelでホストを監視するためのエージェントであるmackerel-agentはRed Hat Enterprise Linux7 / Cent OS7(以下RHEL7)向けと Red Hat Enterprise Linux 6 / Cent OS6(以下RHEL6)向けでパッケージが分かれているのですが、インストーラーの中でバージョンのチェックを行っているわけでないので、バージョン違いのエージェントでもインストールの実行は出来てしまいます。

さて、RHEL6(確認したのはCentOS release 6.10です)の環境にRHEL7向けのエージェントをインストールすると、以下のような出力が出てインストールに失敗します。

$ curl -fsSL https://mackerel.io/file/script/setup-all-yum-v2.sh | MACKEREL_APIKEY='****' sh
This script requires superuser authority to setup Mackerel agent:
++ mktemp
+ gpgkey_path=/tmp/tmp.CoP2XOMeFA
+ curl -fsS -o /tmp/tmp.CoP2XOMeFA https://mackerel.io/file/cert/GPG-KEY-mackerel-v2
+ rpm --import /tmp/tmp.CoP2XOMeFA
+ rm /tmp/tmp.CoP2XOMeFA
+ cat
+ yum install -y mackerel-agent
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
(略)
Resolving Dependencies
--> Running transaction check
---> Package mackerel-agent.x86_64 0:0.59.2-1.el7.centos will be installed
--> Processing Dependency: systemd for package: mackerel-agent-0.59.2-1.el7.centos.x86_64
--> Processing Dependency: systemd for package: mackerel-agent-0.59.2-1.el7.centos.x86_64
--> Finished Dependency Resolution
Error: Package: mackerel-agent-0.59.2-1.el7.centos.x86_64 (mackerel)
           Requires: systemd
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
+ mackerel-agent init -apikey=****
sh: line 21: mackerel-agent: command not found
+ systemctl start mackerel-agent
sh: line 24: systemctl: command not found

このことは当然なのですが、この状態で正しいバージョン(RHEL6向け)のバージョンをインストールしようとしても、以下の様な出力が出てインストールに失敗してしまいます。なんてこったい。

$ curl -fsSL https://mackerel.io/file/script/setup-all-yum.sh | MACKEREL_APIKEY='****' sh
This script requires superuser authority to setup Mackerel agent:
+ command -v curl
/usr/bin/curl
+ http_get='curl -LfsS'
+ '[' 'curl -LfsS' = '' ']'
++ mktemp
+ gpgkey_path=/tmp/tmp.DowLPBCoHC
+ curl -LfsS https://mackerel.io/file/cert/GPG-KEY-mackerel
+ rpm --import /tmp/tmp.DowLPBCoHC
+ rm /tmp/tmp.DowLPBCoHC
+ cat
+ yum install -y mackerel-agent
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
mackerel                                                                                                                                                                                                    | 2.5 (略)

Not using downloaded repomd.xml because it is older than what we have:
  Current   : Wed Mar 27 08:50:16 2019
  Downloaded: Wed Mar 27 08:50:04 2019
Resolving Dependencies
--> Running transaction check
---> Package mackerel-agent.x86_64 0:0.59.2-1.el7.centos will be installed
--> Processing Dependency: systemd for package: mackerel-agent-0.59.2-1.el7.centos.x86_64
--> Processing Dependency: systemd for package: mackerel-agent-0.59.2-1.el7.centos.x86_64
--> Finished Dependency Resolution
Error: Package: mackerel-agent-0.59.2-1.el7.centos.x86_64 (mackerel)
           Requires: systemd
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
+ mackerel-agent init -apikey=****
sh: line 31: mackerel-agent: command not found
+ /etc/init.d/mackerel-agent start
sh: line 34: /etc/init.d/mackerel-agent: No such file or directory

これは、インストールの初期の時点で、mackerel-agentのRHEL7向けのyumレポジトリーが登録されてしまっているためです。

これを復旧するには、以下の様にして、RHEL7向けのyumレポジトリーの情報を削除します。

$ sudo rm /etc/yum.repos.d/mackerel.repo
$ sudo rm -rf /var/cache/yum/x86_64/6/mackerel

($はプロンプトです)

再度正しいバージョンのインストーラーを実行して、以下の様に出力されれば、正しいバージョンのエージェントがインストールできています。

$ curl -fsSL https://mackerel.io/file/script/setup-all-yum.sh | MACKEREL_APIKEY='****' sh
This script requires superuser authority to setup Mackerel agent:
+ command -v curl
/usr/bin/curl
+ http_get='curl -LfsS'
+ '[' 'curl -LfsS' = '' ']'
++ mktemp
+ gpgkey_path=/tmp/tmp.Xg7Em4SxAp
+ curl -LfsS https://mackerel.io/file/cert/GPG-KEY-mackerel
+ rpm --import /tmp/tmp.Xg7Em4SxAp
+ rm /tmp/tmp.Xg7Em4SxAp
+ cat
+ yum install -y mackerel-agent
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
 * base: ftp-srv2.kddilabs.jp
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
base                                                                                                                                                                                                        | 3.7 kB     00:00

(略)

Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mackerel-agent-0.59.2-1.noarch                                                                                                                                                                                  1/1

  Verifying  : mackerel-agent-0.59.2-1.noarch                                                                                                                                                                                  1/1


Installed:
  mackerel-agent.noarch 0:0.59.2-1


Complete!
+ mackerel-agent init -apikey=****
+ /etc/init.d/mackerel-agent start
Starting mackerel-agent:[  OK  ]
*************************************

     Done! Welcome to Mackerel!

*************************************

mackerel-agent.logを確認すると、以下の様に出力されているはずです。

$ tail /var/log/mackerel-agent.log
2019/04/27 12:28:11 INFO <main> Starting mackerel-agent version:0.59.2, rev:b15b1c0, apibase:https://api.mackerelio.com
2019/04/27 12:28:18 INFO <command> Start: apibase = https://api.mackerelio.com, hostName = localhost.localdomain, hostID = ****

以上、Cent OS 6 / Red Hat 6 で誤ってCent OS 7 / Red Hat 7向けのmackerel-agentを入れてしまった場合の復旧法についてでした。