linux实现一台监测机定时监测多台服务器(主机是否在线、内存使用率、CPU使用率),并实现邮件报警。

1、信任关系建立

首先要实现监测机server与被监测机client之间的ssh信任关系。

步骤:

1、client机以root角色在root目录下生成 .ssh文件夹

  1. [root@db ~]$ ssh localhost
  2. The authenticity of host 'localhost(127.0.0.1)' can't be established.
  3. RSA key fingerprint isc5:08:8a:e3:b9:fb:44:f4:0b:e3:9f:c4:d7:e2:11:8a.
  4. Are you sure you want to continueconnecting (yes/no)? yes
  5. Warning: Permanently added 'localhost'(RSA) to the list of known hosts.
  6. root@db's password:
  7. [root@db ~]# ls -a
  8. . .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc id_rsa.pub .ssh .tcshrc

2、修改配置文件/etc/sshd_config
RSAAuthentication yes //开启RSA加密
PubkeyAuthentication yes //开启公钥
AuthorizedKeysFile .ssh/authorized_keys //密钥的路径
PermitRootLogin yes //允许root登陆
PasswordAuthentication no //关闭密码验证

3、在server机上生成公钥,并传到client机上。注意替换IP。
注意:存在多台被监控机时,公钥只需生成一遍。

  1. [root@localhost .ssh]# scp id_rsa.pub root@你的IP:/root/
  2. The authenticity of host '192.168.4.71 (192.168.4.71)' can't be established.
  3. ECDSA key fingerprint is SHA256:xyeUDbFoTbHGZxhpSUJNadqWZPoxvqHbKPtGWxHiQlw.
  4. ECDSA key fingerprint is MD5:0a:3d:80:d2:8e:12:eb:f1:9e:35:4b:26:72:83:c9:59.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added '192.168.4.71' (ECDSA) to the list of known hosts.
  7. root@192.168.4.71's password:
  8. id_rsa.pub 100% 408 220.8KB/s 00:00

4、在client机root目录下可以看到传过来的id_rsa.pub公钥,把公钥存放在/root/.ssh目录下命名为authorized_keys,并修改权限。

  1. [root@db ~]# mv id_rsa.pub /root/.ssh/authorized_keys
  2. [root@db ~]# chmod 644 /root/.ssh/authorized_keys

5、此时,再通过server机ssh连接client机,即可不用密码登录。

  1. [root@localhost .ssh]# ssh -l root ClientIP
  2. Last failed login: Fri Jul 10 11:08:45 CST 2020 from 192.168.4.73 on ssh:notty
  3. There was 1 failed login attempt since the last successful login.
  4. Last login: Fri Jul 10 11:08:27 2020 from 192.168.3.168
  5. [root@db ~]#

至此信任关系建立成功

2、监控服务器

准备
1、server机安装mutt邮件工具。
yum install mutt

2、server机准备监控列表iplist.txt。其中存放已建立信任关系的被监测机ip。可从附件下载,该文件必须为unix格式文件,否则读取ip列表时会读取到换行。
image.png

步骤
1、编写监控脚本monitor.sh,可从附件下载,该文件必须为unix格式文件,否则shell脚本会出错。
本文iplist.txt和shell脚本均放在/data/monitor下。

  1. #!/bin/bash
  2. print_str="monitor:" #最终邮件内容
  3. flag="false" #判断标志位,防止空邮件
  4. for ip in `cat /data/monitor/iplist.txt`;do #iplist地址需指定访问路径
  5. ping $ip -c1>/dev/null #先检查主机是否在线,如果在线则进行进一步监控
  6. if [ $? -eq 0 ]; then
  7. # rootused=`ssh $ip df -h|grep /$|awk '{print $4}'|cut -d% -f1` #取根目录使用情况
  8. #for hardid in `ssh $ip df -h|grep "^\/dev\/s[a-z][a-z]"|awk '{print $1}'`;do
  9. #hardused=`ssh -$ip df -h|grep $hardid|awk '{print $5}'|cut -d% -f1` #取其它本地硬盘的使用情况
  10. # done
  11. memtotal=`ssh $ip free -m|grep Mem|awk '{print $2}'`
  12. memused=`ssh $ip free -m|grep Mem|awk '{print $3}'`
  13. mem=`expr $memused \* 100 / $memtotal`
  14. idelcpu=`ssh $ip top -b -n1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' '{split($1, vs, ","); v=vs[length(vs)]; printf "%d", v; }'`
  15. cpuused=`expr 100 - $idelcpu`
  16. if [[ $mem -gt 80 ]] || [[ $cpuused -gt 90 ]]; then #如果内存使用高于80%或CPU高于90%则邮件通知
  17. flag="true"
  18. print_str="${print_str}\n warn:$ip memory is $mem%; the Cpu used is $cpuused%"
  19. fi
  20. else
  21. echo "host:$ip is not alive"|/usr/bin/mutt -s"$ip monitor report" xxxx@163.com #如果主机无法ping通则邮件通知
  22. fi
  23. done
  24. if [[ $flag == "true" ]]; then
  25. echo -e "$print_str"|/usr/bin/mutt -s"monitor report" xxxx@163.com #统一发送一封告警邮件 mutt命令需指明访问路径
  26. fi

2、crontab每五分钟执行一次脚本监听。

  1. [root@localhost ~]# crontab -e
  2. */5 * * * * bash /data/monitor/monitor.sh

问题查错
1、可通过/var/log下cron.log查看cron执行日志,检查定时任务是否成功执行。
2、可通过/var/spool/mail查看root.log日志,检查mutt邮件发送是否成功。

建议使用163邮箱,目前公司邮箱和QQ邮箱经测试由于收信规则等原因无法接收邮件。