You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Bash基于日期设置告警?检测超45天未更新的工作站

检测设备更新超时告警脚本方案

核心逻辑

  • 获取每台设备的最后更新日期
  • 计算当前日期与更新日期的间隔天数
  • 筛选出间隔超过45天的设备并触发告警

Python 实现示例

import datetime

def is_update_stale(update_date_str, threshold=45):
    # 按实际日期格式调整解析规则,这里用YYYY-MM-DD
    update_date = datetime.datetime.strptime(update_date_str, '%Y-%m-%d').date()
    days_since_update = (datetime.date.today() - update_date).days
    return days_since_update > threshold

# 替换为你的真实设备数据(比如从CSV/API/数据库读取)
device_list = [
    {"hostname": "Workstation-001", "last_update": "2024-05-01"},
    {"hostname": "Workstation-002", "last_update": "2024-04-01"},
    {"hostname": "Workstation-003", "last_update": "2024-04-01"},
    # ... 其余97台设备
]

# 筛选超时设备
stale_devices = [dev for dev in device_list if is_update_stale(dev["last_update"])]

if stale_devices:
    print("⚠️ 超时未更新设备列表:")
    for dev in stale_devices:
        print(f"- {dev['hostname']} | 最后更新:{dev['last_update']}")
else:
    print("✅ 所有设备均在45天内完成更新")

关键调整点

  • 日期格式适配:如果你的更新日期是MM/DD/YYYY或其他格式,修改strptime的格式参数(比如%m/%d/%Y)
  • 数据源对接:把模拟的device_list替换成实际数据读取逻辑(比如用pandas读CSV、调用设备管理接口)
  • 告警扩展:可以把打印输出改成发送邮件、企业微信消息,或者写入监控告警系统

Shell 脚本实现(适用于Linux批量检查)

#!/bin/bash
THRESHOLD_DAYS=45

# 假设设备数据存于devices.txt,每行格式:设备名 最后更新日期
while read -r host update_date; do
    # 转换为时间戳计算天数差
    update_timestamp=$(date -d "$update_date" +%s)
    current_timestamp=$(date +%s)
    days_diff=$(( (current_timestamp - update_timestamp) / 86400 ))

    if [ $days_diff -gt $THRESHOLD_DAYS ]; then
        echo "⚠️ $host 已超过${THRESHOLD_DAYS}天未更新 | 最后更新:$update_date"
    fi
done < devices.txt

内容的提问来源于stack exchange,提问作者Mrdsanta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:12:41