Centos使用mailx发送邮件

12/16/2022 centos监控

想要写一个日志监控shell脚本出来,但是并没有深入了解过,所幸有ChatGPT。通过ChatGPT慢慢将所需的功能补全了。

#!/bin/bash
MONITOR_DIR="/root/.pm2/logs/"

# 获取目录下文件的名字和修改时间
# FILES=$(ls -l $MONITOR_DIR | awk '{print $9 " " $6 " " $7}')
if ! command -v mail > /dev/null 2>&1; then
  # 安装mail命令
  sudo yum install mailx -y
  if ! command -v vim > /dev/null 2>&1; then
  # 安装vi命令
  sudo yum install vim -y
  fi
fi
# 检查邮箱是否配置好
MAIL_FILE="/etc/mail.rc"

if ! grep -q "set from=" "$MAIL_FILE"; then
  cat >> "$MAIL_FILE" << EOF
set from=example@163.com
set smtp=smtp://smtp.163.com
set smtp-auth-user=example@163.com
set smtp-auth-password=授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs
EOF
  if ! command -v openssl > /dev/null 2>&1; then
    sudo yum install openssl -y
  fi
  sudo mkdir -p /root/.certs/
  echo -n | openssl s_client -connect smtp.163.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/163.crt
  certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
  certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/163.crt
  cd /root/.certs && certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu"  -d ./ -i ./163.crt && certutil -L -d /root/.certs
fi
# 将文件信息发送到指定邮箱
CHECK_PATH="/home/watchdog/check.log"
if [ -f "$CHECK_PATH" ]; then
  # 如果文件存在,则将其中的内容读入变量 old_file_times
  old_file_times="$(cat $CHECK_PATH)"
  rm -rf "$CHECK_PATH"
fi

# 清空 check.log 文件
touch ${CHECK_PATH}

# 创建空变量 only_file
only_file=""

# 获取 /root/pm2/logs/ 目录下所有文件的更新时间
# 并将它们的文件名和更新时间一起写入 check.log 文件
for file in /root/.pm2/logs/*; do
  # 获取文件的更新时间
  update_time="$(stat -c %y "$file")"
  if echo "$file" | grep -q "error"; then
    continue
  fi

  # 将文件名和更新时间写入 check.log 文件
  echo "$file $update_time" >> "$CHECK_PATH"

  # 检查该文件的更新时间是否存在于 old_file_times 中
  if [[ "$old_file_times" == *"$update_time"* ]]; then
    update_time=$(date -d "$update_time" +'%Y-%m-%d %H:%M:%S')
    only_file="$only_file $file $update_time\n"
  fi
done
# 判断是否有ifconfig命令
if ! command -v ifconfig > /dev/null 2>&1; then
  sudo yum install net-tools
fi
# 获取当前主机hostname
hn=$(echo `ifconfig | grep -A1 "ens192"|grep 'inet'| awk '{print $2}'`)
# 将路径名替换掉,只展示文件名
only_file=$(echo $only_file|sed 's/\/root\/.pm2\/logs\///g')
# 如果 only_file 变量不为空,则发送邮件到 example@qq.com
title=$hn '监控目录'
if [[ ! -z "$only_file" ]]; then
  echo -e "$only_file" | mailx -s "$title" example@gmail.com
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

利用Chat GPT解释一下这个脚本的意思吧

这个脚本主要用来监控某个目录下文件的变化,并在文件发生变化时发送邮件通知。

具体做法如下:

  1. 在脚本开头定义了一个变量 MONITOR_DIR,用于存储要监控的目录的路径。
  2. 判断本机是否安装了 mailxvim 命令,如果没有则通过 sudo yum install 命令安装这两个命令。
  3. 检查邮箱是否已经配置好,如果没有则在文件 /etc/mail.rc 中写入邮箱的配置信息。
  4. 清空文件 /home/watchdog/check.log
  5. 遍历目录 /root/.pm2/logs/ 下的所有文件,获取每个文件的更新时间,并将文件名和更新时间写入 check.log 文件。
  6. 读取文件 check.log 中的内容,并将其存储在变量 new_file_times 中。
  7. 判断文件 check.log 中的内容是否发生了变化,如果发生了变化,则使用 mailx 命令发送邮件通知。

整个脚本的流程就是这样,它可以用来监控文件的变化,并在文件发生变化时发送邮件通知。

然后再利用crontab创建定时任务即可。