当前位置: 首页 > news >正文

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_LOGcat<<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_REPORTif LOAD=`uptime 2>> $ERROR_LOG`
thenecho "<p>$LOAD</p>" >> $HTML_REPORT
elseecho "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT
fiecho "<h2>Disk Usage</h2>" >> $HTML_REPORT
if DISK_USAGE=`df -h 2>> $ERROR_LOG`
thenecho "<p>$DISK_USAGE</p>" >> $HTML_REPORT
elseecho "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT
fiecho "<h2>Memory Usage</h2>" >> $HTML_REPORT
if MEMORY_USAFE=`free -m 2>> $ERROR_LOG`
thenecho "<p>$MEMORY_USAFE</p>" >> $HTML_REPORT
elseecho "<p>Error occurred. Check $ERROR_LOG</p>" >> $HTML_REPORT
fiecho "<h2>Network Status</h2>" >> $HTML_REPORT
if NETWORK_STATUS=`ifconfig 2>> $ERROR_LOG`
thenecho "<p>$NETWORK_STATUS</p>" >> $HTML_REPORT
elseecho "<p>Error occrred. Check $ERROR_LOG</p>" >> $HTML_REPORT
fiecho "<h2>Nginx Service Status</h2>" >> $HTML_REPORT
if NGINX_STATUS=`systemctl status nginx 2>> $ERROR_LOG`
thenecho "<p>$NGINX_STATUS</p>" >> $HTML_REPORT
elseecho "<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 1500inet 192.168.5.15  netmask 255.255.240.0  broadcast 192.168.15.255inet6 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 0TX packets 43600  bytes 7583848 (7.2 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::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 0TX 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 serverLoaded: 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 agoProcess: 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.9MCGroup: /system.slice/nginx.service├─13210 nginx: master process /usr/sbin/nginx├─13211 nginx: worker process└─13212 nginx: worker processJan 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>

发送到的邮箱邮件内容如下:

http://www.lryc.cn/news/528354.html

相关文章:

  • 【漫话机器学习系列】066.贪心算法(Greedy Algorithms)
  • 代码随想录算法训练营第三十八天-动态规划-完全背包-279.完全平方数
  • ts 基础核心
  • 在RHEL 8.10上安装开源工业物联网解决方案Thingsboard 3.9
  • linux通过deb包安装(命令模式)
  • 「Unity3D」在Unity中使用C#控制显示Android的状态栏
  • LLM评估优化与新技术创新综述
  • 【Git】使用笔记总结
  • ZZNUOJ(C/C++)基础练习1000——1010(详解版)
  • 搜狐Android开发(安卓)面试题及参考答案
  • WPS数据分析000007
  • SpringCloud系列教程:微服务的未来(十八)雪崩问题、服务保护方案、Sentinel快速入门
  • 把markdown转换为pdf的方法
  • Controller 层优化四步曲
  • Python数据分析-Python语法基础,IPython和Jupyter-Notebooks(二)
  • Nginx 开发总结
  • centos7安装SVN
  • LTV预估 | 多视角对比学习框架CMLTV
  • llama.cpp LLM_ARCH_DEEPSEEK and LLM_ARCH_DEEPSEEK2
  • C语言自定义数据类型详解(二)——结构体类型(下)
  • DeepSeek学术写作测评第二弹:数据分析、图表解读,效果怎么样?
  • 深入理解 Python 中的 `__all__`:控制模块的公共接口
  • 虚幻基础07:蓝图接口
  • 数据结构---哈希表
  • DataWhale组队学习 leetCode task4
  • 【ESP32】ESP-IDF开发 | WiFi开发 | UDP用户数据报协议 + UDP客户端和服务器例程
  • 【PyQt5】数据库连接失败: Driver not loaded Driver not loaded
  • Unity游戏(Assault空对地打击)开发(1) 创建项目和选择插件
  • Rust:如何动态调用字符串定义的 Rhai 函数?
  • A星算法两元障碍物矩阵转化为rrt算法四元障碍物矩阵