Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have some xml data they are passing last_time_down and last_time_up but how to calculate uptime and downtime with percentage.
Please see my xml data.

I need calculation also. Note : I want get data filter by today or this month or yesterday or last 24 hours.



XML
<hoststatus id="49495">
   <host_id>3132</host_id>
   <name>zacnvstsapex01</name>
   <display_name>zacnvstsapex01</display_name>
   <address>172.18.95.2</address>
   <alias>ex Test Server</alias>
   <status_update_time>2015-09-19 07:58:26</status_update_time>
   <status_text>OK - 172.18.95.2: rta 5.647ms, lost 0%</status_text>
   <status_text_long/>
   <current_state>0</current_state>
   <performance_data>
     rta=5.647ms;3000.000;5000.000;0; pl=0%;80;100;; rtmax=14.706ms;;;; rtmin=0.863ms;;;;
   </performance_data>
   <should_be_scheduled>1</should_be_scheduled>
   <check_type>0</check_type>
   <last_state_change>1970-01-01 02:00:00</last_state_change>
   <last_hard_state_change>1970-01-01 02:00:00</last_hard_state_change>
   <last_hard_state>0</last_hard_state>
   <last_time_up>2015-09-19 07:58:26</last_time_up>
   <last_time_down>1970-01-01 02:00:00</last_time_down>
   <last_time_unreachable>1970-01-01 02:00:00</last_time_unreachable>
   <last_notification>1970-01-01 02:00:00</last_notification>
   <next_notification>1970-01-01 02:00:00</next_notification>
   <no_more_notifications>0</no_more_notifications>
   <acknowledgement_type>0</acknowledgement_type>
   <current_notification_number>0</current_notification_number>
   <event_handler_enabled>1</event_handler_enabled>
   <process_performance_data>1</process_performance_data>
   <obsess_over_host>1</obsess_over_host>
   <modified_host_attributes>0</modified_host_attributes>
   <event_handler/>
   <check_command>check_xi_host_ping!3000.0!80%!5000.0!100%</check_command>
   <normal_check_interval>5</normal_check_interval>
   <retry_check_interval>1</retry_check_interval>
   <check_timeperiod_id>125</check_timeperiod_id>
   <has_been_checked>1</has_been_checked>
   <current_check_attempt>1</current_check_attempt>
   <max_check_attempts>5</max_check_attempts>
   <last_check>2015-09-19 07:58:26</last_check>
   <next_check>2015-09-19 08:03:26</next_check>
 </hoststatus>



I am using this type of calculation but am not getting exact data.

C#
var src = Convert.ToDateTime(result.GetString(result.GetOrdinal("status_update_time")));
                           var hm = new DateTime(src.Year, src.Month, src.Day, src.Hour, src.Minute, 0);
                           var dow = Convert.ToDateTime(result.GetString(result.GetOrdinal("last_time_down")));
                           var dm = new DateTime(dow.Year, dow.Month, dow.Day, dow.Hour, dow.Minute, 0);
                           var uptime = hm;
                           var downtime = hm - dm;
                           double up = uptime.Ticks / 6000;
                           double dt = downtime.Ticks / 6000;
                           double percentage = (double)(up / (up + dt) * 100);
                           string slastatus = percentage > Convert.ToDouble(_inputpercentage) ? "PASSED" : "FAILED";
                           hostlist.Add(new HostData
                           {
                               host_id = result.GetString(result.GetOrdinal("host_id")),
                               status_update_time = result.GetString(result.GetOrdinal("status_update_time")),
                               last_time_down = result.GetString(result.GetOrdinal("last_time_down")),
                               alias = result.GetString(result.GetOrdinal("alias")),
                               display_name = result.GetString(result.GetOrdinal("display_name")),
                               uptime = ((int)Math.Ceiling(percentage)).ToString("0.00") + "%",
                               status = slastatus,
                               totaluptime = (float)percentage
                           });
Posted
Comments
Maciej Los 5-Oct-15 7:57am    
What is expected output?
Mohamed Hasan Shali 6-Oct-15 13:12pm    
we need uptime percentage.

var uptime = hm;
var downtime = hm - dm;
double up = uptime.Ticks / 6000;
double dt = downtime.Ticks / 6000;
double percentage = (double)(up / (up + dt) * 100);

this is my calculation but I didn't get exat data.

Don't use ticks. Don't mix up time (a point on the time line), with duration (time span between to points in time). Use the type System.TimeSpan in addition to System.DateTime:
C#
System.DateTime first = //...
System.DateTime second = System.DateTime.Now;
System.TimeSpan duration = second - first; // operator defined
var allYouWantedToKnowAboutDurationButWasAfraidToAsk = duration.// ...anything, see MSDN: :-)

https://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Maciej Los 5-Oct-15 12:43pm    
I wouldn't be able to say it better!
Sergey Alexandrovich Kryukov 5-Oct-15 15:28pm    
Thank you, Maciej.
—SA
Mohamed Hasan Shali 6-Oct-15 2:07am    
Maciej Los @ how to calculate uptime percentage by duration?
Sergey Alexandrovich Kryukov 6-Oct-15 2:53am    
Please define percentage.
—SA
In addition to solution 1 by Sergey Alexandrovich Kryukov[^], i would like to provide sample (using LinqToXml[^]):

C#
var result = xdoc.Descendants("hoststatus")
       .Select(x=>new
           {
               hostid = (int)x.Element("host_id"),
               statusUpdTime = (DateTime)x.Element("status_update_time"),
               lastDownTime = (DateTime)x.Element("last_time_down"),
               alias = (string)x.Element("alias"),
               timeDiff = (TimeSpan)((DateTime)x.Element("status_update_time") - (DateTime)x.Element("last_time_down"))
           });


result:
hostid statusUpdTime       lastDownTime        alias          timeDiff
3132   2015-09-19 07:58:26 1970-01-01 02:00:00 ex Test Server 16697.05:58:26 
 
Share this answer
 
Comments
Mohamed Hasan Shali 6-Oct-15 2:07am    
How to calculate uptime percentage by timediff?
Maciej Los 6-Oct-15 10:54am    
:laugh:
Did you read Sergey's answer?
Mohamed Hasan Shali 6-Oct-15 12:52pm    
yes I cannot get anything

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900