# monit設定

##### 修改monitrc

<span style="color: rgb(224, 62, 45);">修改路徑在/etc/monit/monitrc</span>

檢查時間間隔

```
set daemon  60
```

log放置位置

```
set log /data/monit/logs
```

web監控路徑

```
set httpd port 2812 and
    use address 202.107.204.55
# only accept connection from localhost (drop if you use M/Monit)
    allow 0.0.0.0/0.0.0.0
# allow localhost to connect to the server and
    allow admin:monit
# require user 'admin' with password 'monit'
```

設定如果沒有mount成功

```
# mount.conf
check filesystem data with path /dev/sdb
       if does not exist then exec "/bin/mount /dev/sdb /data"
```

如果没有名为 `python server.py` 的进程，那就启动。

```
# recommender.conf
check process recommender with MATCHING "python server.py"
    if does not exist then exec "/usr/bin/nohup python /data/Recommender/src_tornado/server/server.py &"
    if changed pid then alert
```

##### monit常用指令

```
monit -t # 配置文件检测
monit # 启动monit daemon
monit -c /var/monit/monitrc # 启动monit daemon时指定配置文件
monit reload # 当更新了配置文件需要重载
monit status # 查看所有服务状态
monit status nginx # 查看nginx服务状态
monit stop all # 停止所有服务
monit stop nginx # 停止nginx服务
monit start all # 启动所有服务
monit start nginx # 启动nginx服务
monit -V # 查看版本
```

##### 郵件通知配置

```
set mailserver  smtp.monit.ro port 587
set mail-format { from: monit@monit.ro subject: $SERVICE $EVENT at $DATE on $HOST
 message: Monit $ACTION $SERVICE $EVENT at $DATE on $HOST : $DESCRIPTION.
       Yours sincerely,
          Monit
  }

set alert guletz@monit.ro
```

```
set mailserver smtp.gmail.com port 587
 username "username" password "password"
 using tls
```

```
set mail-format {
 from: Monit Alert <monit@$HOST>
 subject: 通知 專題伺服器 -- $EVENT $SERVICE
 message: $SERVICE => $EVENT
 日期： $DATE
 動作： $ACTION
 主機： $HOST
 說明： $DESCRIPTION
```

##### 檢視系統

```
check system localhost
    if loadavg (1min) > 10 then alert
    if loadavg (5min) > 6 then alert
    if memory usage > 75% then alert
    if cpu usage (user) > 70% then alert
    if cpu usage (system) > 60% then alert
    if cpu usage (wait) > 75% then alert
```

```
check system $HOST
 if loadavg (1min) per core > 2 for 5 cycles then alert
 if loadavg (5min) per core > 1.5 for 10 cycles then alert
 if cpu usage > 75% for 5 cycles then alert
 if memory usage > 80% for 1 cycles then alert
```

##### 單一檔案hash值

```
check file passwd with path /etc/passwd
 if changed checksum then alert
```

##### 系統效能監控

```
check file bashrc with path /etc/bash.bashrc
 if changed checksum then alert
```

##### 目錄修改時間監控

```
check filesystem root_directory PATH /
 if write rate > 50 MB/s for 10 cycles then alert
 if read rate > 50 MB/s for 10 cycles then alert
```

##### 網路介面卡上下行速度監控

```
check network enp3s0 with interface enp3s0
 if download > 20 MB/s for 10 cycles then alert
 if upload > 20 MB/s for 10 cycles then alert
```