如何计算C#中的系统正常运行时间和停机时间 [英] How to calculate system uptime and downtime in C#

查看:107
本文介绍了如何计算C#中的系统正常运行时间和停机时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些xml数据,他们正在传递last_time_down和last_time_up但是如何计算正常运行时间和停机时间百分比。

请查看我的xml数据。



我还需要计算。注意:我希望在今天或本月或昨天或过去24小时内获取数据过滤器。







 <   hoststatus     id   =  49495 >  
< host_id > 3132 < / host_id >
< name > zacnvstsapex01 < / name >
< display_name > zacnvstsapex01 < / display_name >
< 地址 > 172.18.95.2 < / address >
< 别名 > ex测试服务器< /别名 >
< status_update_time > 2015-09-19 07:58:26 < / statu s_update_time >
< status_text > 确定 - 172.18.95.2:rta 5.647ms,丢失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:0 0 < / 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 < span class =code-keyword>> 1 < / process_performance_data >
< obsess_over_host < span class =code-keyword>> 1 < / obsess_over_host >
< modified_host_attributes < span class =code-keyword>> 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_inter val >
< check_timeperiod_id > 125 < / check_timeperiod_id >
< has_been_checked > 1 < / has_been_checked >
< current_check_attempt > 1 < / current_check_attempt >
< max_check_attempts <温泉n class =code-keyword>> 5 < / max_check_attempts >
< last_check < span class =code-keyword>> 2015-09-19 07:58:26 < / last_check >
< next_check > 2015-09-19 08:03:26 < / next_check >
< / hoststatus >







我正在使用这种类型的计算,但我没有得到确切的数据。



  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 停机时间= hm - dm;
double up = uptime.Ticks / 6000 ;
double dt = downtime.Ticks / 6000 ;
double percentage =( double )(上/(上+ 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 ( 别名)),
display_name = result.GetString(result.GetOrdinal(< span class =code-string>
display_name)) ,
uptime =(( int )Math.Ceiling(percentage))。ToString( 0.00)+
status = slastatus,
totaluptime =( float )百分比
});

解决方案

不要使用刻度线。不要混淆时间(时间线上的一个点),持续时间(到时间点之间的时间跨度)。除了 System.DateTime 之外,还要使用 System.TimeSpan 类型:

 System.DateTime first =  //   ...  
System.DateTime second = System.DateTime.Now;
System.TimeSpan duration = second - first; // 运营商定义
var allYouWantedToKnowAboutDurationButWasAfraidToAsk = duration。 // ...任何内容,请参阅MSDN :: - )



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



-SA


除了 Sergey Alexandrovich Kryukov [ ^ ],我想提供样本(使用 LinqToXml [ ^ ]):



  var  result = xdoc.Descendants(  hoststatus
。选择(x = > new
{
hostid =( int )x.Element( host_id),
statusUpdTime =(DateTime)x.Element( status_update_time),
lastDownTime =(DateTime)x.Element( last_time_down),
别名 =( string )x.Element ( a lias),
timeDiff =(TimeSpan)((DateTime)x.Element( status_update_time) - (DateTime)x.Element( last_time_down))
});





结果:

  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


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.



<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.

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
                           });

解决方案

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:

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


In addition to solution 1 by Sergey Alexandrovich Kryukov[^], i would like to provide sample (using LinqToXml[^]):

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 


这篇关于如何计算C#中的系统正常运行时间和停机时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆