事件编程:自定义响应与接口抖动处理
发布时间: 2025-08-11 16:54:43 阅读量: 1 订阅数: 4 

# 事件编程:自定义响应与接口抖动处理
## 1. 输出格式调整
在事件编程中,我们可以使用 `output-format` 语句将捕获输出的格式设置为原生 CLI ASCII 文本,而非 XML。以下是一个示例配置:
```plaintext
adamc@router> show configuration event-options policy SHOW-INT
events snmp_trap_link_down;
then {
execute-commands {
commands {
"show interfaces";
}
output-filename show-int;
destination NMS;
output-format text;
}
}
```
## 2. 事件的自定义响应
### 2.1 准备工作
要完成此操作,你需要访问 JUNOS 设备和运行 OpenSSH(或等效)服务器的类 UNIX 网络管理站。确保网络管理主机已配置合适的账户配置文件,以便 JUNOS 设备可以访问它。
### 2.2 操作步骤
1. **验证网络管理主机配置**:
```bash
user$ touch test.txt
user$ scp test.txt [email protected]:/home/junos/test.txt
```
2. **配置 JUNOS 设备访问信息**:
```plaintext
adamc@router# set event-options destinations NMS archive-sites "scp://[email protected]//home/junos" password MyPasswordHere
[edit event-options]
adamc@router# show
destinations {
NMS {
archive-sites {
"scp://[email protected]//home/junos" password "$9$fQ390BEevLBI-w"; ## SECRET-DATA
}
}
}
```
3. **创建 SLAX 操作脚本**:
```plaintext
version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
import "../import/junos.xsl";
param $interface="lo0";
var $arguments = {
<argument> {
<name> "interface";
<description> "Interface to show";
}
}
var $rpc = {
<get-interface-information> {
<interface-name> $interface;
}
}
var $result = jcs:invoke($rpc);
match / {
<op-script-result> {
copy-of $result;
}
}
```
4. **复制并注册操作脚本**:
```bash
$ scp show-interfaces.slax [email protected]:/var/db/scripts/op
adamc@router# set system scripts op file show-interfaces.slax
```
5. **测试操作脚本**:
```plaintext
adamc@router> op show-interfaces interface lo0.0
Logical interface lo0.0 (Index 72) (SNMP ifIndex 16)
Description: Loopback
Flags: SNMP-Traps Encapsulation: Unspecified
Input packets : 14
Output packets: 14
Protocol inet, MTU: Unlimited
Flags: Sendbcast-pkt-to-re
Addresses, Flags: Primary Preferred Is-Default Is-Primary
Local: 2.0.0.56
Addresses
Local: 2.0.0.57
```
6. **创建事件策略**:
```plaintext
adamc@router> show configuration event-options
policy SHOW-INT {
events snmp_trap_link_down;
then {
event-script show-interfaces.slax {
arguments {
interface "{$$.interface-name}";
}
output-filename show-int;
destination NMS;
}
}
}
```
7. **提交配置并测试**:提交配置后,通过禁用以太网接口或拔出电缆来测试事件检测。在网络管理站上,你应该会看到相应文件的创建。
### 2.3 工作原理
- **步骤 1**:通过本地 SSH 会话验证能否成功登录网络管理站并在可写目录中创建文件。
- **步骤 2**:在 JUNOS 事件管理框架中定义网络管理站及其相关凭证作为目标。
- **步骤 3**:创建一个简单的 SLAX 脚本,调用 `<get-interface-information>` RPC 并输出结果。
- **步骤 4**:将操作脚本复制到 JUNOS 设备并注册。
- **步骤 5**:配置 JUNOS 事件管理框架监听 `SNMP_TRAP_LINK_DOWN` 事件,并在事件发生时采取行动。
- **步骤 6**:当配置提交后,JUNOS 事件管理守护进程 `eventd` 加载处理事件的信息。当模拟链路断开时,事件触发并运行操作脚本。
### 2.4 事件脚本与操作脚本对比
| 特性/能力 | 事件脚本 | 操作脚本 | 命令 |
| --- | --- | --- | --- |
| 输入数据 | 可使用 XPath 对输入事件进行丰富分析 | 事件策略必须将属性映射到参数中 | 无法进行参数化输入 |
| 输出格式 | 仅 XML | 使用 `output-format` 指
0
0
相关推荐










