linux监控脚本+自动触发邮件发送
linux脚本
需求:
CPU 负载:使用
uptime
命令,我们可以清楚地了解系统的 CPU 负载情况。这个命令会显示系统在过去 1 分钟、5 分钟和 15 分钟的平均负载。高负载可能意味着系统正在处理大量的任务,可能会导致性能下降或服务响应延迟。内存使用:通过
free -m
命令,我们可以以 MB 为单位查看系统的内存使用情况,包括已使用内存、空闲内存、缓存和交换空间。这有助于我们判断系统是否需要更多的内存资源,或者是否存在内存泄漏等问题。磁盘使用:
df -h
命令为我们提供了磁盘空间使用情况的信息,让我们清楚地知道各个文件系统的使用比例。当磁盘空间接近饱和时,会严重影响系统的正常运行,甚至导致服务无法写入数据。网络状态:
ifconfig
命令可以让我们查看网络接口的状态,包括 IP 地址、MAC 地址、接收和发送的数据包数量等,帮助我们确保网络连接的正常。服务状态检查:你可以添加检查服务是否正在运行的功能,比如检查
Apache
服务器是否正在运行,可以使用systemctl status apache2
或service apache2 status
命令,并将结果添加到报告中。
开发脚本内容:
#!/bin/bash # 定义报告文件和错误日志文件 REPORT_FILE="/root/linux_inspection_report.txt" HTML_REPORT="/root/linux_inspection_report.html" ERROR_LOG="/root/linux_inspection_error.log" DATE=`date "+%Y-%m-%d %H:%M:%S"` > $REPORT_FILE > $ERROR_LOG cat<<EOF > $HTML_REPORT <!DOCTYPE html> <html> <head> <title>Linux System Inspection Report</title> </head> <body> <h1>Linux System Inspection Report</h1> <p>Date:$DATE</p> EOF #uptime echo "<h2>System load</h2>" >> $HTML_REPORT if LOAD=`uptime 2>> $ERROR_LOG` then echo "<p>$LOAD</p>" >> $HTML_REPORT else echo "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT fi echo "<h2>Disk Usage</h2>" >> $HTML_REPORT if DISK_USAGE=`df -h 2>> $ERROR_LOG` then echo "<p>$DISK_USAGE</p>" >> $HTML_REPORT else echo "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT fi echo "<h2>Memory Usage</h2>" >> $HTML_REPORT if MEMORY_USAFE=`free -m 2>> $ERROR_LOG` then echo "<p>$MEMORY_USAFE</p>" >> $HTML_REPORT else echo "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT fi echo "<h2>Network Status</h2>" >> $HTML_REPORT if NETWORK_STATUS=`ifconfig 2>> $ERROR_LOG` then echo "<p>$NETWORK_STATUS</p>" >> $HTML_REPORT else echo "<p>Error occrred. Check $ERROR_LOG</p>" >> $HTML_REPORT fi echo "<h2>Nginx Service Status</h2>" >> $HTML_REPORT if NGINX_STATUS=`systemctl status nginx 2>> $ERROR_LOG` then echo "<p>$NGINX_STATUS</p>" >> $HTML_REPORT else echo "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT fi # echo "</body></html>
脚本文件需要设置可执行权限
# chmod +x jiankong.sh
执行结果如下:
[root@hcss-ecs- ~]# ll
total 32
-rwxr-xr-x 1 root root 1725 Jan 27 14:12 jiankong.sh
-rw-r--r-- 1 root root 0 Jan 27 14:12 linux_inspection_error.log
-rw-r--r-- 1 root root 2960 Jan 27 14:12 linux_inspection_report.html
-rw-r--r-- 1 root root 0 Jan 27 14:12 linux_inspection_report.txt
-rw------- 1 root root 16817 Jan 27 14:12 sent
执行记录的文件内容如下:
[root@hcss-ecs-6f98 ~]# cat linux_inspection_report.html
<!DOCTYPE html>
<html>
<head>
<title>Linux System Inspection Report</title>
</head>
<body>
<h1>Linux System Inspection Report</h1>
<p>Date:2025-01-27 14:12:24</p>
<h2>System load</h2>
<p> 14:12:24 up 4:49, 3 users, load average: 0.00, 0.00, 0.00</p>
<h2>Disk Usage</h2>
<p>Filesystem Size Used Avail Use% Mounted on
devtmpfs 389M 0 389M 0% /dev
tmpfs 405M 0 405M 0% /dev/shm
tmpfs 405M 5.6M 399M 2% /run
tmpfs 405M 0 405M 0% /sys/fs/cgroup
/dev/vda1 40G 2.9G 35G 8% /
tmpfs 81M 0 81M 0% /run/user/0</p>
<h2>Memory Usage</h2>
<p> total used free shared buff/cache available
Mem: 808 187 391 7 229 490
Swap: 0 0 0</p>
<h2>Network Status</h2>
<p>eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.5.15 netmask 255.255.240.0 broadcast 192.168.15.255
inet6 fe80::f816:3eff:fe68:c5cf prefixlen 64 scopeid 0x20<link>
ether fa:16:3e:68:c5:cf txqueuelen 1000 (Ethernet)
RX packets 79035 bytes 68357573 (65.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43600 bytes 7583848 (7.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 1454 bytes 113048 (110.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1454 bytes 113048 (110.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0</p>
<h2>Nginx Service Status</h2>
<p>● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2025-01-27 09:42:01 CST; 4h 30min ago
Process: 13209 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 13206 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 13205 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 13210 (nginx)
Tasks: 3 (limit: 4976)
Memory: 5.9M
CGroup: /system.slice/nginx.service
├─13210 nginx: master process /usr/sbin/nginx
├─13211 nginx: worker process
└─13212 nginx: worker process
Jan 27 09:42:01 hcss-ecs-6f98 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 27 09:42:01 hcss-ecs-6f98 nginx[13206]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 27 09:42:01 hcss-ecs-6f98 nginx[13206]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 27 09:42:01 hcss-ecs-6f98 systemd[1]: Started The nginx HTTP and reverse proxy server.</p>
</body></html>
发送到的邮箱邮件内容如下: