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

Perl脚本需求:计数不匹配时发送mailx告警并执行外部脚本

Perl Script Optimization: Add Alerts & External Script Execution on Count Mismatch

Alright, let's tweak your Perl script to add those extra alerting and script-execution features when the IP counts don't match. Here's a complete, updated version of your script with the new functionality integrated:

#!/usr/bin/perl
use strict;
use warnings;

# Replace this with your actual data source (e.g., database query, file read)
my %ip_monitor_data = (
    '10.0.0.1' => { current => 12, expected => 12 },
    '10.0.0.2' => { current => 8, expected => 10 },
    '10.0.0.3' => { current => 5, expected => 5 },
);

# Update these config values to match your environment
my $alert_recipient = 'ops-team@yourcompany.com';
my $remediation_script = '/opt/scripts/ip_count_fix.pl';

foreach my $ip (keys %ip_monitor_data) {
    my $current = $ip_monitor_data{$ip}{current};
    my $expected = $ip_monitor_data{$ip}{expected};

    if ($current == $expected) {
        print "good\n";
    } else {
        # Keep original output logic
        print "bad - IP: $ip, current_count: $current\n";

        # 1. Send mailx alert with detailed info
        my $mail_subject = "URGENT: IP Count Mismatch for $ip";
        my $mail_content = <<"EMAIL_BODY";
IP Address: $ip
Current Count: $current
Expected Count: $expected
The counts do not match - please investigate.
EMAIL_BODY
        my $mail_cmd = qq{echo "$mail_content" | mailx -s "$mail_subject" $alert_recipient};
        my $mail_exit = system($mail_cmd);
        warn "Failed to send alert for $ip (exit code: $mail_exit)\n" if $mail_exit != 0;

        # 2. Run external remediation script (pass args for context)
        my $script_cmd = qq{$remediation_script "$ip" "$current" "$expected"};
        my $script_exit = system($script_cmd);
        warn "Failed to run remediation script for $ip (exit code: $script_exit)\n" if $script_exit != 0;
    }
}

Key Changes Breakdown:

  • Config Section: Added easy-to-update variables for your alert email and script path, so you don't have to hunt through logic code to adjust settings.
  • Mailx Alert: Uses system() to execute a formatted mailx command, sending a detailed alert with the mismatched IP and counts. We check the exit code to warn you if the email fails (critical for debugging delivery issues).
  • External Script Execution: Runs your secondary script with the IP and count values as arguments (adjust the arguments if your script doesn't need them). Error handling here ensures you know if the script fails to run.
  • Original Logic Preserved: The "good" and "bad" console outputs stay exactly as you had them before—no changes to your existing workflow.

Quick Tips:

  • Test the mailx command manually first to confirm it works in your environment (some systems use mail instead of mailx).
  • Ensure the user running the Perl script has execute permissions for both mailx and your external script.
  • For extra robustness, you could add logging to a file alongside the warn() messages, but that's optional based on your monitoring needs.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:04:03