enebular INFOMOTIONでサーバ監視（リソース監視）

## 概要

Node-REDのノードで[node-red-contrib-os](http://flows.nodered.org/node/node-red-contrib-os)というものがあります。これを使えばサーバの基本情報を取得したり、CPU/メモリ/ディスクといったリソースを監視したりすることができます。

## とりあえずenebular Flowで試してみる

Flowは以下です。

```json
[{"id":"67b92c01.7430ec","type":"milkcocoa","z":"fe18e088.694ba","appId":"＜あなたのMilkcocoa App ID＞"},{"id":"f7925704.7ab208","type":"CPUs","z":"fe18e088.694ba","name":"","x":98.5,"y":209,"wires":[["ba663891.29ea08"]]},{"id":"f7de6c38.ea8e5","type":"Loadavg","z":"fe18e088.694ba","name":"","x":104.5,"y":158,"wires":[["8e77d6b9.27c198"]]},{"id":"7e475bd9.4655dc","type":"Memory","z":"fe18e088.694ba","name":"","x":271.5,"y":57,"wires":[["c5b91d21.456ea"]]},{"id":"25c96c09.14060c","type":"inject","z":"fe18e088.694ba","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"x":100.5,"y":44.5,"wires":[["7e475bd9.4655dc","2137a046.a2371","f7de6c38.ea8e5","f7925704.7ab208"]]},{"id":"89f73b88.486e48","type":"milkcocoa out","z":"fe18e088.694ba","milkcocoa":"67b92c01.7430ec","dataStore":"server1/loadavg","operation":"push","targetId":"","name":"","x":446.5,"y":158,"wires":[["a9e9f392.0fa768"]]},{"id":"a9e9f392.0fa768","type":"debug","z":"fe18e088.694ba","name":"","active":true,"console":"false","complete":"false","x":544.5,"y":273,"wires":[]},{"id":"2137a046.a2371","type":"Drives","z":"fe18e088.694ba","name":"","x":113.5,"y":107,"wires":[["eef9a751.5d3b6"]]},{"id":"8e77d6b9.27c198","type":"change","z":"fe18e088.694ba","name":"","rules":[{"t":"set","p":"payload.loadavg1","pt":"msg","to":"payload.loadavg.0","tot":"msg"},{"t":"set","p":"payload.loadavg5","pt":"msg","to":"payload.loadavg.1","tot":"msg"},{"t":"set","p":"payload.loadavg15","pt":"msg","to":"payload.loadavg.2","tot":"msg"},{"t":"delete","p":"payload.loadavg","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":271.5,"y":157.5,"wires":[["89f73b88.486e48"]]},{"id":"ba663891.29ea08","type":"change","z":"fe18e088.694ba","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.cpus.0","tot":"msg"},{"t":"set","p":"payload.user","pt":"msg","to":"payload.times.user","tot":"msg"},{"t":"set","p":"payload.nice","pt":"msg","to":"payload.times.nice","tot":"msg"},{"t":"set","p":"payload.sys","pt":"msg","to":"payload.times.sys","tot":"msg"},{"t":"set","p":"payload.idle","pt":"msg","to":"payload.times.idle","tot":"msg"},{"t":"set","p":"payload.irq","pt":"msg","to":"payload.times.irq","tot":"msg"},{"t":"delete","p":"payload.times","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":273,"y":210,"wires":[["bb10b0e8.ecd9b8"]]},{"id":"bb10b0e8.ecd9b8","type":"milkcocoa out","z":"fe18e088.694ba","milkcocoa":"67b92c01.7430ec","dataStore":"server1/cpus","operation":"push","targetId":"","name":"","x":449,"y":208,"wires":[["a9e9f392.0fa768"]]},{"id":"5c1e076.78c99f8","type":"milkcocoa out","z":"fe18e088.694ba","milkcocoa":"67b92c01.7430ec","dataStore":"server1/drives","operation":"push","targetId":"","name":"","x":447,"y":107,"wires":[["a9e9f392.0fa768"]]},{"id":"c5b91d21.456ea","type":"milkcocoa out","z":"fe18e088.694ba","milkcocoa":"67b92c01.7430ec","dataStore":"server1/memory","operation":"push","targetId":"","name":"","x":449,"y":58,"wires":[["a9e9f392.0fa768"]]},{"id":"eef9a751.5d3b6","type":"change","z":"fe18e088.694ba","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.0","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":269,"y":105,"wires":[["5c1e076.78c99f8"]]}]
```

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/149f487e-a432-1d1a-12ef-d719eb4c72e7.png)

CPU、メモリ、ディスク、Load Averageといった情報を1分間隔で取得して多少データ加工をしてからMilkcocoaへ投入しています。とりあえず試すためにCPUとディスクは取得できる配列の最初の要素のデータだけ投入しています。

今回はMilkcocoaのデータストアを以下のように各メトリクス毎に用意しました。

| メトリクス | データストア |
|:-:|:-:|
| Load Average | server1/loadavg |
| CPU | server1/cpus |
| ディスク | server1/drives |
| メモリ | server1/memory |

Milkcocoaのデータストアの詳細については[ドキュメント](https://mlkcca.com/document/dashboard/datastore.html)を参照します。

## enebular INFOMOTIONで可視化

あとはenebular INFOMOTIONの設定をします。[データソースの設定](http://enebular.com/developers/infomotion-datasource)や[INFOMOTIONの作成](http://enebular.com/developers/infomotion-start)の詳細は各リンク先のドキュメントを参照します。

### Load Average

#### データソース設定

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/880b03d1-f59e-a983-97b4-4df872d59a15.png)

#### INFOMOTION設定

| 項目 | 設定する値 |
|:-:|:-:|
| TYPE | metricsgraphics |
| DATASOURCE | server1/loadavg |
| rows y_accessor | loadavg1 |
| rows y_accessor | loadavg5 |
| rows y_accessor | loadavg15 |
| legend name | 1分 |
| legend name | 5分 |
| legend name | 15分 |
| limit | 20 |

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/09b149f9-517f-a8f7-6aca-77873563034d.png)

### 完成

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/c5c1a7bc-c095-d4bc-b013-51b966362e5a.png)

### CPU

#### データソース設定

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/450b4f0c-33c6-42d7-668c-d416635ff134.png)

#### INFOMOTION設定

| 項目 | 設定する値 |
|:-:|:-:|
| TYPE | metricsgraphics |
| DATASOURCE | server1/cpus |
| rows y_accessor | user |
| rows y_accessor | nice |
| rows y_accessor | sys |
| rows y_accessor | irq |
| rows y_accessor | idle |
| legend name | user |
| legend name | nice |
| legend name | sys |
| legend name | irq |
| legend name | idle |
| limit | 20 |

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/5caeefd4-0bfc-68e1-2c11-1bb995637260.png)

### 完成

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/91864f07-b6f5-1b3e-0446-339ace14ed6a.png)

### ディスク

#### データソース設定

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/18339049-ca6d-ae8f-517e-84c6453af798.png)

#### INFOMOTION設定

| 項目 | 設定する値 |
|:-:|:-:|
| TYPE | metricsgraphics |
| DATASOURCE | server1/drives |
| rows y_accessor | size |
| rows y_accessor | used |
| rows y_accessor | available |
| legend name | size |
| legend name | used |
| legend name | available |
| limit | 20 |

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/8df25c5b-790e-5a84-dae9-05bf14ff7c61.png)

### 完成

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/446f625b-09b9-5da1-3209-199a6407f22e.png)

### メモリ

#### データソース設定

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/398b66e7-3bc8-abe2-93d7-bb2357118d93.png)

#### INFOMOTION設定

| 項目 | 設定する値 |
|:-:|:-:|
| TYPE | metricsgraphics |
| DATASOURCE | server1/memory |
| rows y_accessor | totalmem |
| rows y_accessor | freemem |
| rows y_accessor | memusage |
| legend name | totalmem |
| legend name | freemem |
| legend name | memusage |
| limit | 20 |

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/c5bd912a-9721-1a25-823b-220ca55523d9.png)

### 完成

![image](https://qiita-image-store.s3.amazonaws.com/0/21109/ef9c5515-a004-b021-5827-69de16520dec.png)

## おまけ

ここまでやったらディスクI/OとネットワークI/Oも取得したいですが、環境によってコマンドが異なるようなメトリクスを取得する場合は[node-red-node-daemon](https://www.npmjs.com/package/node-red-node-daemon)を使って定期的に取得する方法が良さそうです。

こちらはまたの機会に。