帮我写一个linux巡检脚本
时间: 2023-10-24 22:09:04 浏览: 46
你好!下面是一个简单的 Linux 巡检脚本的例子:
```
#!/bin/bash
# 设置日志文件路径
log_file="/var/log/inspection.log"
# 创建日志文件
touch $log_file
# 写入日期到日志文件
echo "Inspection started on: $(date)" >> $log_file
# 检查 CPU 使用率
cpu_usage=$(top -n 1 -b | grep "Cpu(s)" | awk '{print $2 + $4}')
echo "CPU usage: ${cpu_usage}%" >> $log_file
# 检查内存使用率
memory_usage=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
echo "Memory usage: ${memory_usage}" >> $log_file
# 检查磁盘使用率
disk_usage=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
echo "Disk usage: ${disk_usage}" >> $log_file
# 检查进程数
process_count=$(ps aux | wc -l)
echo "Number of processes: ${process_count}" >> $log_file
# 写入结束时间到日志文件
echo "Inspection completed on: $(date)" >> $log_file
```
这个脚本执行以下操作:
1. 创建一个名为 `inspection.log` 的日志文件,并将日期写入日志文件。
2. 检查 CPU 使用率,并将结果写入日志文件。
3. 检查内存使用率,并将结果写入日志文件。
4. 检查磁盘使用率,并将结果写入日志文件。
5. 检查进程数,并将结果写入日志文件。
6. 将结束时间写入日志文件。
你可以根据需要修改这个脚本,