- fixed uptime calculation, refactor later
authorAlex Kalimulin <alex@netxms.org>
Fri, 2 Sep 2011 11:16:14 +0000 (11:16 +0000)
committerAlex Kalimulin <alex@netxms.org>
Fri, 2 Sep 2011 11:16:14 +0000 (11:16 +0000)
src/server/core/bizservice.cpp
src/server/include/nms_objects.h

index 8949366..e060e87 100644 (file)
@@ -402,7 +402,7 @@ double BusinessService::getUptimeFromDBFor(Period period, LONG *downtime)
                        if (newStatus == STATUS_CRITICAL) // the service is still down, add period till now
                                *downtime += LONG(time(NULL) - prevChangeTimestamp);
                }
-               percentage = 100.0 - (double)(*downtime * 100) / (double)timediffTillNow;
+               percentage = 100.0 - (double)(*downtime * 100) / (double)getSecsInPeriod(period);
 
                DBFreeResult(hResult);
                DBFreeStatement(hStmt);
@@ -434,21 +434,21 @@ void BusinessService::updateUptimeStats()
        m_downtimeDay += downtimeBetweenPolls;
        if (timediffTillNow < m_prevDiffDay)
                m_downtimeDay = 0;
-       m_uptimeDay = 100.0 - (double)(m_downtimeDay * 100) / (double)timediffTillNow;
+       m_uptimeDay = 100.0 - (double)(m_downtimeDay * 100) / (double)BusinessService::getSecsInPeriod(DAY);
        m_prevDiffDay = timediffTillNow;
 
        timediffTillNow = BusinessService::getSecondsSinceBeginningOf(WEEK, NULL);
        m_downtimeWeek += downtimeBetweenPolls;
        if (timediffTillNow < m_prevDiffWeek)
                m_downtimeWeek = 0;
-       m_uptimeWeek = 100.0 - (double)(m_downtimeWeek * 100) / (double)timediffTillNow;
+       m_uptimeWeek = 100.0 - (double)(m_downtimeWeek * 100) / (double)BusinessService::getSecsInPeriod(WEEK);
        m_prevDiffWeek = timediffTillNow;
 
        timediffTillNow = BusinessService::getSecondsSinceBeginningOf(MONTH, NULL);
        m_downtimeMonth += downtimeBetweenPolls;
        if (timediffTillNow < m_prevDiffMonth)
                m_downtimeMonth = 0;
-       m_uptimeMonth = 100.0 - (double)(m_downtimeMonth * 100) / (double)timediffTillNow;
+       m_uptimeMonth = 100.0 - (double)(m_downtimeMonth * 100) / (double)BusinessService::getSecsInPeriod(MONTH);
        m_prevDiffMonth = timediffTillNow;
 
        if ((prevUptimeDay != m_uptimeDay) || (prevUptimeWeek != m_uptimeWeek) || (prevUptimeMonth != m_uptimeMonth))
@@ -495,3 +495,32 @@ LONG BusinessService::getSecondsSinceBeginningOf(Period period, time_t *beginTim
 
        return LONG(curTime - beginTimeL);
 }
+
+//
+// Calculate number of seconds in the current month
+//
+
+/* static */ LONG BusinessService::getSecsInMonth()
+{
+       time_t curTime = time(NULL);
+       struct tm *tms;
+       struct tm tmBuffer;
+
+#if HAVE_LOCALTIME_R
+       tms = localtime_r(&curTime, &tmBuffer);
+#else
+       tms = localtime(&curTime);
+#endif
+
+       int& month = tms->tm_mon;
+       int year = tms->tm_year + 1900;
+       int days = 31;
+       
+       if (month == 3 || month == 5 || month == 8 || month == 10)
+               days = 30;      
+       else if (month == 1) /* February */
+               days = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28;
+               
+       return LONG(days * 24 * 3600);
+}
+
index 7d1174b..bfdf446 100644 (file)
@@ -1662,6 +1662,10 @@ protected:
        LONG m_prevDiffMonth;
 
        static LONG logRecordId;
+       static const LONG secsInDay = 3600 * 24;
+       static const LONG secsInWeek = 3600 * 24 * 7;
+       static LONG getSecsInMonth();
+       static LONG getSecsInPeriod(Period period) { return period == MONTH ? getSecsInMonth() : (period == WEEK ? secsInWeek : secsInDay); }
        static LONG getSecondsSinceBeginningOf(Period period, time_t *beginTime = NULL);
 
        BOOL addHistoryRecord();