【问题标题】:MS SQL query to calculate link outage time and link up timeMS SQL 查询计算链路中断时间和链路正常运行时间
【发布时间】:2015-09-30 05:53:04
【问题描述】:

下表,设备运行状态的前几行,每次轮询:

samplevalue = 2 运行状态下降,samplevalue = 1 运行状态上升

source          target                                              sampletime      samplevalue
------------------------------------------------------------------------------------------------
128.42.196.11   Se2/0/2(Serial2/0/2)                                9/30/15 11:10   2
128.42.196.11   Fa3/0/0(FastEthernet3/0/0)                          9/30/15 11:10   1
128.42.196.11   Gi1/0/0.10(GigabitEthernet1/0/0.10)                 9/30/15 11:10   1
128.42.196.11   Se2/0/2.305(Serial2/0/2.305)                        9/30/15 11:10   2
128.42.196.11   Se2/0/2.309(Serial2/0/2.309)                        9/30/15 11:10   2
128.42.196.11   Gi1/0/0.20(GigabitEthernet1/0/0.20)                 9/30/15 11:10   1
128.42.196.11   Se2/0/2.300(Serial2/0/2.300)                        9/30/15 11:10   2
128.42.196.11   Gi0/0/0(GigabitEthernet0/0/0)                       9/30/15 11:10   1
128.42.196.11   Se2/0/2.306(Serial2/0/2.306)                        9/30/15 11:10   2
128.42.196.11   PO2/1/0(POS2/1/0--SONET/SDH Medium/Section/Line)    9/30/15 11:09   2
128.42.196.11   Tu10(Tunnel10-mpls layer)                           9/30/15 11:09   2
128.42.196.11   Tu4(Tunnel4)                                        9/30/15 11:09   2
128.42.196.11   Gi1/0/0.40(GigabitEthernet1/0/0.40)                 9/30/15 11:09   1
128.42.196.11   Se2/0/1(Serial2/0/1)                                9/30/15 11:09   1
128.42.196.11   Gi1/0/0.20(GigabitEthernet1/0/0.20)                 9/30/15 11:09   1
128.42.196.11   Tu10(Tunnel10)                                      9/30/15 11:08   2
128.42.196.11   Se2/0/0(Serial2/0/0)                                9/30/15 11:08   1
128.42.196.11   Se2/0/2.309(Serial2/0/2.309)                        9/30/15 11:08   2
128.42.196.11   Se2/0/2.306(Serial2/0/2.306)                        9/30/15 11:08   2

从上表中,我们必须计算设备的特定接口与sampletime = 1sampletime = 2 的时间,这也只是接口的最新状态。

我可以处理以下查询:

with temp as ( 
    select 
        p.source, 
        p.target, 
        p.samplevalue,
        p.sampletime,
        lag(p.samplevalue,1) over (order by p.source, p.target, p.sampletime desc) as newvalue,
        lag(p.sampletime,1) over (order by p.source, p.target, p.sampletime desc) as changedat 
    from INTERFACE_OPERSTATUS p 
)
select * 
from( 
    select
        ROW_NUMBER() over (partition by source, target order by source, target, changedat desc) as therow,
        source,
        target,
        samplevalue as oldvalue,
        newvalue,
        changedat,
        DATEDIFF(MINUTE,changedat,GETDATE()) as time
    FROM temp 
    where 
        newvalue <> samplevalue
        and newvalue = 2
) a 
where therow >= 1

上述查询的问题是:

  1. 即使链接已打开,它仍显示处于中断状态
  2. 无法考虑最新的链接条件
  3. 在链路断开和链路接通之间需要中断时间(无法考虑链路接通)

【问题讨论】:

  • 样本的预期输出是什么?
  • 什么是接口?例如是Se2Se2/0/2 还是Se2/0/2(Serial2/0/2)
  • 预期输出:已启动的链接 - 总运行时间和已关闭的链接 - 总停机时间。 @JulienVavasseur
  • 是的,它们是接口@DmitryPolyakov
  • @harikumarmunjala 哪一个? :) 如果target 中的整个字符串代表一个接口,那么您的示例中没有一个接口会更改状态。

标签: sql-server networking network-programming


【解决方案1】:

由于我没有准备好访问 MS-SQL,因此针对 Oracle 数据库进行了以下测试:

SELECT SOURCE, TARGET, SUM(UP_TIME) AS UP_TIME, SUM(DOWN_TIME) AS DOWN_TIME  FROM (
  SELECT LO.*, (SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60 AS  ACTUAL_TIME, 
     CASE SAMPLE_VALUE WHEN 1 THEN ((SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60) ELSE 0 END AS UP_TIME,
     CASE SAMPLE_VALUE WHEN 2 THEN ((SAMPLE_TIME - PREVIOUS_SAMPLE_TIME) * 24 * 60) ELSE 0 END AS DOWN_TIME
     FROM 
       (
         select SOURCE, TARGET, SAMPLE_VALUE, SAMPLE_TIME,  
         LAG(SAMPLE_TIME, 1, NULL) OVER (PARTITION BY SOURCE,TARGET ORDER BY SOURCE, TARGET, SAMPLE_TIME) AS PREVIOUS_SAMPLE_TIME 
         from INTERFACE_OPERSTATUS
       ) LO
) GROUP BY SOURCE, TARGET ORDER BY SOURCE, TARGET;

上面的查询应该也可以在 MS-SQL 上运行,只需要很少的语法更改,因为我没有使用 Oracle 独有的功能。

【讨论】:

    猜你喜欢
    • 2021-08-29
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多