sql server - MS SQL query to calculate link outage time and link up time -
following table, top few lines operational status of device, every poll:
samplevalue = 2
operational status down, samplevalue = 1
operational status up
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
from above table have calculate how time particular interface of device sampletime = 1
, sampletime = 2
, latest condition of interface.
i able work on following query:
with temp ( select p.source, p.target, p.samplevalue, p.sampletime, lag(p.samplevalue,1) on (order p.source, p.target, p.sampletime desc) newvalue, lag(p.sampletime,1) on (order p.source, p.target, p.sampletime desc) changedat interface_operstatus p ) select * from( select row_number() on (partition source, target order source, target, changedat desc) therow, source, target, samplevalue oldvalue, newvalue, changedat, datediff(minute,changedat,getdate()) time temp newvalue <> samplevalue , newvalue = 2 ) therow >= 1
problem above query is:
- even though link up, shows still in outage
- not able consider latest link condition
- need outage time between link down , link (not able consider link up)
the following tested against oracle database since did not ready access ms-sql:
select source, target, sum(up_time) up_time, sum(down_time) down_time ( select lo.*, (sample_time - previous_sample_time) * 24 * 60 actual_time, case sample_value when 1 ((sample_time - previous_sample_time) * 24 * 60) else 0 end up_time, case sample_value when 2 ((sample_time - previous_sample_time) * 24 * 60) else 0 end down_time ( select source, target, sample_value, sample_time, lag(sample_time, 1, null) on (partition source,target order source, target, sample_time) previous_sample_time interface_operstatus ) lo ) group source, target order source, target;
the above query should work on ms-sql little syntax changes since did not make use of oracle features.
Comments
Post a Comment