systemdでサービスの状態、設定、ログを見る方法を調べてみたのでメモ

[Vagrant上のCoreOSでetcdを動かして別の3台でクラスタリングしてみた - Qiita](http://qiita.com/hnakamur/items/bccac980bef68642a814)でハマったのでサービスの状態を見てみようと思ったら、systemdについて何も知らない事に気づいたので少しだけ調べました。

# 状態の確認

systemctlというコマンドでサービスの確認などを行うらしいです。[systemd (日本語) - ArchWiki](https://wiki.archlinux.org/index.php/Systemd_%28%e6%97%a5%e6%9c%ac%e8%aa%9e%29)が詳しくて良さそうです。

systemctl status サービス名 -lで状態を確認します。-lなしだと下半分ぐらいのログの行が...で省略されて、フルで見るには-lを付けるように言われました。

例

```shell-session
core@core-00 ~ $ systemctl status etcd -l
etcd.service - etcd
   Loaded: loaded (/usr/lib64/systemd/system/etcd.service; disabled)
  Drop-In: /run/systemd/system/etcd.service.d
           └─20-cloudinit.conf
   Active: active (running) since Sun 2014-04-13 05:55:20 UTC; 29min ago
 Main PID: 3051 (etcd)
   CGroup: /system.slice/etcd.service
           └─3051 /usr/bin/etcd

Apr 13 05:55:20 core-00 systemd[1]: Starting etcd...
Apr 13 05:55:20 core-00 systemd[1]: Started etcd.
Apr 13 05:55:20 core-00 etcd[3051]: [etcd] Apr 13 05:55:20.821 INFO      | e91e07624f254be3835aa6b221f4e176: state changed from 'stopped' to 'follower'.
Apr 13 05:55:20 core-00 etcd[3051]: [etcd] Apr 13 05:55:20.821 INFO      | e91e07624f254be3835aa6b221f4e176: state changed from 'follower' to 'leader'.
Apr 13 05:55:20 core-00 etcd[3051]: [etcd] Apr 13 05:55:20.822 INFO      | e91e07624f254be3835aa6b221f4e176: leader changed from '' to 'e91e07624f254be3835aa6b221f4e176'.
Apr 13 05:55:20 core-00 etcd[3051]: [etcd] Apr 13 05:55:20.823 INFO      | etcd server [name e91e07624f254be3835aa6b221f4e176, listen on [::]:4001, advertised url http://172.12.8.100:4001]
Apr 13 05:55:20 core-00 etcd[3051]: [etcd] Apr 13 05:55:20.824 INFO      | peer server [name e91e07624f254be3835aa6b221f4e176, listen on [::]:7001, advertised url http://172.12.8.100:7001]
```

# 設定の確認

systemctl statusの出力結果で設定ファイルがわかるので、それを見ます。

例

```shell-session
core@core-01 ~ $ cat /run/systemd/system/etcd.service.d/20-cloudinit.conf
[Service]
Environment="ETCD_DISCOVERY=http://172.17.8.100:4001/v2/keys/machines"
Environment="ETCD_ADDR=172.17.8.101:4001"
Environment="ETCD_PEER_ADDR=172.17.8.101:7001"
Environment="ETCD_NAME=3e15b02dcddc4a079c04d7fa9c91b621"
```

# ログの確認

[φ(.. )ﾒﾓｼﾃｵｺｳ　systemdのjournalによるログの確認方法 - φ(・・*)ゞ ｳｰﾝ　カーネルとか弄ったりのメモ](http://kernhack.hatenablog.com/entry/2013/10/10/170630)によるとsystemdでは/var/log/messagesは作られず、ログの確認にはjournalctlコマンドを使うそうです。-bをつければブート以後のログのみが見られるとのこと。

実行例

```
journalctl -b | less
```

出力抜粋

```
Apr 13 05:56:43 core-01 etcd[3055]: [etcd] Apr 13 05:56:43.514 INFO      | Discovery via http://172.17.8.100:4001 using prefix machines.
Apr 13 05:56:44 core-01 tlsdated[2891]: wait for child attempt 1
Apr 13 05:56:44 core-01 tlsdated[2891]: child exited with 0
Apr 13 05:56:44 core-01 tlsdated[2891]: tlsdate succeeded
Apr 13 05:56:44 core-01 tlsdated[2891]: synced rtc to sysclock
Apr 13 05:56:44 core-01 tlsdated[2891]: open failed: No such file or directory
Apr 13 05:56:44 core-01 tlsdated[2891]: wait for child attempt 1
Apr 13 05:56:44 core-01 tlsdated[2891]: child exited with 0
Apr 13 05:56:44 core-01 tlsdated[2891]: tlsdate succeeded
Apr 13 05:56:44 core-01 tlsdated[2891]: synced rtc to sysclock
Apr 13 05:56:44 core-01 tlsdated[2891]: open failed: No such file or directory
Apr 13 05:56:44 core-01 tlsdated[2891]: Failed to obtain UID for `tlsdate-dbus'
Apr 13 05:56:45 core-01 etcd[3055]: [etcd] Apr 13 05:56:45.924 CRITICAL  | Discovery failed and a backup peer list wasn't provided: 501: All the given peers are not reachable (Tried to connect to each peer twice and failed) [0]
Apr 13 05:56:45 core-01 systemd[1]: etcd.service: main process exited, code=exited, status=1/FAILURE
Apr 13 05:56:45 core-01 systemd[1]: Unit etcd.service entered failed state.
```