promethes 起動をサービス登録テンプレートを使ってサービス登録する

習慣化166日目

prometheus と node_exporterは実行時に&を付けてバックグラウンドで実行するようにしている。 現状だと実行時オプションを覚えておかないといけないし、OS再起動などで自動実行されない。 下記サイトを参考にサービスに登録し、自動起動するようにする。

参考

スクリプト をサービス登録するテンプレート[init.d編] - Qiita
https://qiita.com/KEINOS/items/abb5985a7c15081ad22e

(CentOS6まで)自作のサービスをchkconfigで登録する
https://kazmax.zpp.jp/linux_beginner/self-made_service_chkconfig.html

設定作業

テンプレートを持ってくる

[root@centos6 ~]# cd /etc/init.d/

[root@centos6 init.d]# vi init-script-template.sh

https://github.com/fhd/init-script-template/blob/master/template
のスクリプトを転記

3つの項目の値を設定します。
dir
プロセスの作業ディレクトリを指定します
cmd
プロセスを実行するコマンド(いつも入力しているコマンド)
user
コマンドを実行するユーザー(オプション)。実行ユーザーが指定されていない場合は sudo ... を通して root として実行されます。

テンプレートを複製して起動スクリプトとする

[root@centos6 init.d]# cp -ip init-script-template.sh prometheus_service.sh
[root@centos6 init.d]# vi prometheus_service.sh

(snip)
dir="/usr/local/src/prometheus-2.22.1.linux-amd64/"
cmd="/usr/local/src/prometheus-2.22.1.linux-amd64/prometheus --config.file=prometheus.yml --storage.tsdb.retention 1095d"
user="root"
(snip)

[root@centos6 init.d]# ls -lh prometheus_service.sh
-rw-r--r--. 1 root root 2.2K 11月 16 09:34 2020 prometheus_service.sh
[root@centos6 init.d]# chmod +x prometheus_service.sh
[root@centos6 init.d]# ls -lh prometheus_service.sh
-rwxr-xr-x. 1 root root 2.2K 11月 16 09:34 2020 prometheus_service.sh
[root@centos6 init.d]#

Prometheusのプロセスを上記スクリプトを使って起動する

▽既存のプロセスをkill
[root@centos6 init.d]# ps aux |grep prometheus
[root@centos6 init.d]# kill 383
[root@centos6 init.d]# ps aux |grep prometheus

なんか二重で起動している?

[root@centos6 init.d]# /etc/init.d/prometheus_service.sh status
Stopped
[root@centos6 init.d]# /etc/init.d/prometheus_service.sh start
Starting prometheus_service.sh
[root@centos6 init.d]# /etc/init.d/prometheus_service.sh status
Running

[root@centos6 init.d]# ps aux |grep prometheus-
root     20676  0.0  0.0 175204  2712 pts/1    S    09:37   0:00 sudo -u root /usr/local/src/prometheus-2.22.1.linux-amd64/prometheus --config.file=prometheus.yml --storage.tsdb.retention 1095d
root     20680  0.4  1.3 788300 55364 pts/1    Sl   09:37   0:00 /usr/local/src/prometheus-2.22.1.linux-amd64/prometheus --config.file=prometheus.yml --storage.tsdb.retention 1095d
root     20707  0.0  0.0 103336   920 pts/1    S+   09:38   0:00 grep prometheus-
[root@centos6 init.d]#

→2重で起動していた訳では無く、sudoは子プロセスを持つため、複数行出る、みたいな感じっぽい。

https://linuxjm.osdn.jp/html/sudo/man8/sudo.8.html

sudo は、コマンドを実行するとき、まず fork(2) を呼び、 実行環境を上記のように設定してから、子プロセスで execve システムコールを呼び出す。 メインの sudo プロセスは、コマンドが完了するまで wait し、完了したら、 コマンドの終了ステータスをセキュリティポリシーの close 関数に渡してから、 終了する。

ただ、sudoはほぼ使用しないので(よくないのかも知れないが)削除しちゃう。

一旦停止して

[root@centos6 init.d]# /etc/init.d/prometheus_service.sh stop
Stopping prometheus_service.sh...
Stopped
[root@centos6 init.d]#

下記を書き換えた

[root@centos6 init.d]# diff -u init-script-template.sh prometheus_service.sh
--- init-script-template.sh     2020-11-16 09:27:46.044663995 +0900
+++ prometheus_service.sh       2020-11-16 09:45:19.223533051 +0900
@@ -1,4 +1,5 @@
-N INIT INFO
+#!/bin/sh
+### BEGIN INIT INFO
 # Provides:
 # Required-Start:    $remote_fs $syslog
 # Required-Stop:     $remote_fs $syslog
@@ -8,8 +9,8 @@
 # Description:       Enable service provided by daemon.
 ### END INIT INFO

-dir=""
-cmd=""
+dir="/usr/local/src/prometheus-2.22.1.linux-amd64/"
+cmd="/usr/local/src/prometheus-2.22.1.linux-amd64/prometheus --config.file=prometheus.yml --storage.tsdb.retention 1095d"
 user=""

 name=`basename $0`
@@ -33,7 +34,8 @@
         echo "Starting $name"
         cd "$dir"
         if [ -z "$user" ]; then
-            sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
+            #sudo $cmd >> "$stdout_log" 2>> "$stderr_log" &
+            $cmd >> "$stdout_log" 2>> "$stderr_log" &
         else
             sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
         fi
[root@centos6 init.d]#

sudo はほぼ使用しないので、テンプレートの方も書き換えておいた。

起動してプロセスが1つになっていることを確認した

[root@centos6 init.d]# /etc/init.d/prometheus_service.sh start
Starting prometheus_service.sh
[root@centos6 init.d]#
[root@centos6 init.d]# ps aux |grep prometheus-
root     20932  4.4  1.3 788300 55340 pts/1    Sl   09:45   0:00 /usr/local/src/prometheus-2.22.1.linux-amd64/prometheus --config.file=prometheus.yml --storage.tsdb.retention 1095d
root     20943  0.0  0.0 103336   924 pts/1    S+   09:45   0:00 grep prometheus-
[root@centos6 init.d]#

自動起動を設定しておく

▽設定がされていないことを確認
[root@centos6 init.d]# chkconfig --list |grep pro

▽追加
[root@centos6 init.d]# chkconfig --add prometheus_service.sh

▽追加されたことを確認
[root@centos6 init.d]# chkconfig --list |grep pro
prometheus_service.sh   0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@centos6 init.d]#

node_exporterも同じように対応した。

yumイントールしたときに入った設定を外しておく

[root@centos6 init.d]# chkconfig --list |grep node
node_exporter   0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@centos6 init.d]# chkconfig --del node_exporter
[root@centos6 init.d]# chkconfig --list |grep node
[root@centos6 init.d]#

テンプレートを複製し起動スクリプトを編集

[root@centos6 init.d]# cp -ip init-script-template.sh node_exporter_service.sh
[root@centos6 init.d]# vi node_exporter_service.sh
dir="/usr/bin/"
cmd="/usr/bin/node_exporter  --no-collector.arp --no-collector.bcache --no-collector.bonding --no-collector.conntrack --no-collector.cpu --no-collector.cpufreq --no-collector.diskstats --no-collector.edac --no-collector.entropy --no-collector.filefd --no-collector.filesystem --no-collector.hwmon --no-collector.infiniband --no-collector.ipvs --no-collector.loadavg --no-collector.mdadm --no-collector.meminfo --no-collector.netclass --no-collector.netdev --no-collector.netstat --no-collector.nfs --no-collector.nfsd --no-collector.pressure --no-collector.rapl --no-collector.schedstat --no-collector.sockstat --no-collector.softnet --no-collector.stat --no-collector.thermal_zone --no-collector.time --no-collector.timex --no-collector.udp_queues --no-collector.uname --no-collector.vmstat --no-collector.xfs --no-collector.zfs --no-collector.btrfs --no-collector.powersupplyclass --web.disable-exporter-metrics --collector.textfile.directory /root/prometheus/textfilecollector"
user=""

[root@centos6 init.d]# chmod +x node_exporter_service.sh

既存のプロセスをkillして

[root@centos6 init.d]# ps aux |grep node
root     21270  0.0  0.4 717320 17572 pts/1    Sl   10:00   0:00 node_exporter --no-collector.arp --no-collector.bcache --no-collector.bonding --no-collector.conntrack --no-collector.cpu --no-collector.cpufreq --no-collector.diskstats --no-collector.edac --no-collector.entropy --no-collector.filefd --no-collector.filesystem --no-collector.hwmon --no-collector.infiniband --no-collector.ipvs --no-collector.loadavg --no-collector.mdadm --no-collector.meminfo --no-collector.netclass --no-collector.netdev --no-collector.netstat --no-collector.nfs --no-collector.nfsd --no-collector.pressure --no-collector.rapl --no-collector.schedstat --no-collector.sockstat --no-collector.softnet --no-collector.stat --no-collector.thermal_zone --no-collector.time --no-collector.timex --no-collector.udp_queues --no-collector.uname --no-collector.vmstat --no-collector.xfs --no-collector.zfs --no-collector.btrfs --no-collector.powersupplyclass --web.disable-exporter-metrics --collector.textfile.directory /root/prometheus/textfilecollector
root     21385  0.0  0.0 103336   920 pts/1    S+   10:08   0:00 grep node

[root@centos6 init.d]# kill 21270
[root@centos6 init.d]# ps aux |grep node
root     21389  0.0  0.0 103336   920 pts/1    S+   10:08   0:00 grep node

サービス起動

[root@centos6 init.d]# service node_exporter_service.sh status
Stopped
[root@centos6 init.d]# service node_exporter_service.sh start
Starting node_exporter_service.sh
[root@centos6 init.d]# service node_exporter_service.sh status
Running
[root@centos6 init.d]# ps aux |grep node
root     21480  0.0  0.2 715528 10716 pts/1    Sl   10:10   0:00 /usr/bin/node_exporter --no-collector.arp --no-collector.bcache --no-collector.bonding --no-collector.conntrack --no-collector.cpu --no-collector.cpufreq --no-collector.diskstats --no-collector.edac --no-collector.entropy --no-collector.filefd --no-collector.filesystem --no-collector.hwmon --no-collector.infiniband --no-collector.ipvs --no-collector.loadavg --no-collector.mdadm --no-collector.meminfo --no-collector.netclass --no-collector.netdev --no-collector.netstat --no-collector.nfs --no-collector.nfsd --no-collector.pressure --no-collector.rapl --no-collector.schedstat --no-collector.sockstat --no-collector.softnet --no-collector.stat --no-collector.thermal_zone --no-collector.time --no-collector.timex --no-collector.udp_queues --no-collector.uname --no-collector.vmstat --no-collector.xfs --no-collector.zfs --no-collector.btrfs --no-collector.powersupplyclass --web.disable-exporter-metrics --collector.textfile.directory /root/prometheus/textfilecollector
root     21501  0.0  0.0 103336   920 pts/1    S+   10:10   0:00 grep node
[root@centos6 init.d]#

InfluxDBについても同様に起動スクリプトを使用した

20201127追記

[root@centos6 ~]# cd /etc/init.d/
[root@centos6 init.d]# cp -ip init-script-template.sh influxdb_service.sh
[root@centos6 init.d]# vi influxdb_service.sh

[root@centos6 init.d]# diff -u init-script-template.sh influxdb_service.sh
--- init-script-template.sh     2020-11-16 10:10:13.752979866 +0900
+++ influxdb_service.sh 2020-11-27 08:17:45.011266258 +0900
@@ -9,9 +9,9 @@
 # Description:       Enable service provided by daemon.
 ### END INIT INFO

-dir=""
-cmd=""
-user=""
+dir="/usr/bin/"
+cmd="/usr/bin/influxd"
+user="root"

 name=`basename $0`
 pid_file="/var/run/$name.pid"
[root@centos6 init.d]#

[root@centos6 init.d]# ll influxdb_service.sh
-rw-r--r--. 1 root root 2054 11月 27 08:17 2020 influxdb_service.sh
[root@centos6 init.d]# chmod +x influxdb_service.sh
[root@centos6 init.d]# ll influxdb_service.sh
-rwxr-xr-x. 1 root root 2054 11月 27 08:17 2020 influxdb_service.sh
[root@centos6 init.d]#

[root@centos6 init.d]# ps aux | grep influ[x]
[root@centos6 init.d]# /etc/init.d/influxdb_service.sh status
Stopped
[root@centos6 init.d]# /etc/init.d/influxdb_service.sh start
Starting influxdb_service.sh
[root@centos6 init.d]# /etc/init.d/influxdb_service.sh status
Running
[root@centos6 init.d]# ps aux | grep influ[x]
root     30222  0.1  0.0 175204  2708 pts/1    S    08:20   0:00 sudo -u root /usr/bin/influxd
root     30226  2.3  0.7 340688 31928 pts/1    Sl   08:20   0:00 /usr/bin/influxd
[root@centos6 init.d]#

[root@centos6 init.d]# chkconfig --list |grep inf
[root@centos6 init.d]# chkconfig --add influxdb_service.sh
[root@centos6 init.d]# chkconfig --list |grep inf
influxdb_service.sh     0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@centos6 init.d]#