可以用Shell语言写一个Linux服务器巡检脚本么
时间: 2024-03-09 17:47:15 浏览: 29
是的,可以使用Shell语言编写一个Linux服务器巡检脚本。Shell是一种解释性语言,可以在Linux系统上运行,用于编写脚本和执行各种系统管理任务。以下是一个简单的Linux服务器巡检脚本示例:
```bash
#!/bin/bash
# display the system uptime
uptime
# display the disk usage
df -h
# display the memory usage
free -m
# display the network interfaces and their IP addresses
ifconfig -a
# display the current connections
netstat -anp | grep tcp
# display the running processes
ps aux
```
该脚本将显示系统的运行时间,磁盘使用情况,内存使用情况,网络接口和IP地址,当前连接和正在运行的进程。您可以将其保存为巡检脚本,并在需要时运行它以检查服务器的状态。
相关问题
用Shell语言写一个Linux服务器巡检脚本,巡检到异常,发送邮箱通知
好的,以下是一个Shell脚本示例,用于巡检Linux服务器并在发现异常时发送电子邮件通知。
```bash
#!/bin/bash
# set email recipient(s)
email_recipient="your.email@example.com"
# set subject for email notification
subject="Linux Server Status Alert"
# get system uptime
uptime=$(uptime)
# check disk usage and send email if usage exceeds 80%
disk_usage=$(df -h | awk '{print $5 " " $6}' | sed 's/%//g')
while read -r output; do
usage=$(echo $output | awk '{ print $1}' )
partition=$(echo $output | awk '{ print $2}' )
if [ $usage -ge 80 ]; then
echo "WARNING: Disk usage for \"$partition\" is at $usage%"
echo "WARNING: Disk usage for \"$partition\" is at $usage%" | mail -s "$subject" "$email_recipient"
fi
done <<< "$disk_usage"
# check CPU usage and send email if usage exceeds 80%
cpu_usage=$(top -bn1 | grep load | awk '{printf "%.2f\n", $(NF-2)}')
if (( $(echo "$cpu_usage > 0.80" | bc -l) )); then
echo "WARNING: CPU usage is at $cpu_usage%"
echo "WARNING: CPU usage is at $cpu_usage%" | mail -s "$subject" "$email_recipient"
fi
# check memory usage and send email if usage exceeds 80%
mem_usage=$(free -m | awk '/Mem:/ { printf("%.2f"), $3/$2*100 }')
if (( $(echo "$mem_usage > 80" | bc -l) )); then
echo "WARNING: Memory usage is at $mem_usage%"
echo "WARNING: Memory usage is at $mem_usage%" | mail -s "$subject" "$email_recipient"
fi
# check for failed login attempts and send email if attempts exceed 5
failed_attempts=$(grep "Failed password" /var/log/auth.log | wc -l)
if [ $failed_attempts -ge 5 ]; then
echo "WARNING: There have been $failed_attempts failed login attempts"
echo "WARNING: There have been $failed_attempts failed login attempts" | mail -s "$subject" "$email_recipient"
fi
```
该脚本将检查磁盘使用情况、CPU使用情况、内存使用情况和失败的登录尝试,并在任何一项超过预设阈值时发送电子邮件警报。您需要将脚本中的电子邮件收件人和主题更改为适合您自己的值,并确保在服务器上安装了邮件客户端(例如sendmail或postfix)以便发送电子邮件。
用shell写一个服务器巡检脚本
服务器巡检脚本可以通过 shell 脚本编写,以下是一个简单的服务器巡检脚本示例:
```bash
#!/bin/bash
# 服务器巡检脚本
# 检查系统负载
load=$(uptime | awk '{print $10}')
if [ $(echo "$load > 1" | bc) -eq 1 ]
then
echo "系统负载过高:$load"
fi
# 检查磁盘空间
disk=$(df -h | grep /dev/sda1 | awk '{print $5}' | cut -d'%' -f1)
if [ $disk -ge 90 ]
then
echo "磁盘空间不足:$disk%"
fi
# 检查内存使用率
mem=$(free | awk '/Mem/{printf("%.2f"), $3/$2*100}')
if [ $(echo "$mem > 80" | bc) -eq 1 ]
then
echo "内存使用率过高:$mem%"
fi
# 检查网络连接数
conn=$(netstat -an | grep :80 | wc -l)
if [ $conn -ge 100 ]
then
echo "网络连接数过多:$conn"
fi
# 检查 CPU 使用率
cpu=$(top -b -n 1 | grep "Cpu(s)" | awk '{print $2}')
if [ $(echo "$cpu > 80" | bc) -eq 1 ]
then
echo "CPU 使用率过高:$cpu%"
fi
# 检查日志文件大小
log=$(du -sh /var/log/* | sort -hr | head -n 1 | awk '{print $1}')
if [ $(echo "$log > 10" | bc) -eq 1 ]
then
echo "日志文件过大:$log"
fi
# 发送邮件告警
if [ $(echo "$load > 1" | bc) -eq 1 -o $disk -ge 90 -o $(echo "$mem > 80" | bc) -eq 1 -o $conn -ge 100 -o $(echo "$cpu > 80" | bc) -eq 1 -o $(echo "$log > 10" | bc) -eq 1 ]
then
echo "服务器存在异常,发送邮件告警"
# 发送邮件的命令,需要替换成实际使用的邮件服务和账号
echo "服务器存在异常,请及时处理。" | mail -s "服务器巡检告警" user@example.com
fi
```
该脚本会检查系统负载、磁盘空间、内存使用率、网络连接数、CPU 使用率和日志文件大小等指标,如果出现异常,就会发送邮件告警。需要注意的是,邮件发送命令需要替换成实际使用的邮件服务和账号。
该脚本可以通过 crontab 定时执行,例如每天凌晨 3 点执行一次:
```
0 3 * * * /path/to/check_server.sh
```
以上是一个简单的服务器巡检脚本示例,可以根据实际需要进行修改和扩展。