PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/features/reporting/availability/src/main/java/org/opennms/reporting/availability/AvailCalculations.java

https://bitbucket.org/peternixon/opennms-mirror
Java | 1287 lines | 795 code | 119 blank | 373 comment | 200 complexity | 1d58bfaeab7353a760fdc6371049a43b MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-2.0
  1. /*******************************************************************************
  2. * This file is part of OpenNMS(R).
  3. *
  4. * Copyright (C) 2010-2012 The OpenNMS Group, Inc.
  5. * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
  6. *
  7. * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
  8. *
  9. * OpenNMS(R) is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published
  11. * by the Free Software Foundation, either version 3 of the License,
  12. * or (at your option) any later version.
  13. *
  14. * OpenNMS(R) is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with OpenNMS(R). If not, see:
  21. * http://www.gnu.org/licenses/
  22. *
  23. * For more information contact:
  24. * OpenNMS(R) Licensing <license@opennms.org>
  25. * http://www.opennms.org/
  26. * http://www.opennms.com/
  27. *******************************************************************************/
  28. package org.opennms.reporting.availability;
  29. import java.text.SimpleDateFormat;
  30. import java.util.ArrayList;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.GregorianCalendar;
  34. import java.util.HashMap;
  35. import java.util.Iterator;
  36. import java.util.List;
  37. import java.util.ListIterator;
  38. import java.util.Map;
  39. import java.util.Set;
  40. import java.util.TreeMap;
  41. import org.opennms.core.utils.ThreadCategory;
  42. import org.opennms.reporting.datablock.IfService;
  43. import org.opennms.reporting.datablock.Interface;
  44. import org.opennms.reporting.datablock.Node;
  45. import org.opennms.reporting.datablock.OutageSince;
  46. import org.opennms.reporting.datablock.OutageSvcTimesList;
  47. import org.opennms.reporting.datablock.Service;
  48. /**
  49. * AvailCalculations does all computations for all reports for a category. The
  50. * types include Last 30 days daily availability Last 30 days total availability
  51. * Last 30 days daily service availability Last Months Top 20 offenders Last
  52. * Months Top 20 Service outages Last N Months Availability Last Months Daily
  53. * Availability Last Months Total Availability Last Months Daily Service
  54. * Availability Month To Date Daily Availability Month To Date Total
  55. * Availability
  56. *
  57. * @author <A HREF="mailto:jacinta@oculan.com">Jacinta Remedios </A>
  58. */
  59. public class AvailCalculations extends Object {
  60. /**
  61. * The log4j category used to log debug messsages and statements.
  62. */
  63. private static final String LOG4J_CATEGORY = "OpenNMS.Report";
  64. private final ThreadCategory log;
  65. /**
  66. * Castor object that holds all the information required for the generating
  67. * xml to be translated to the pdf.
  68. */
  69. private static Report m_report = null;
  70. /**
  71. * End time
  72. */
  73. private long m_endTime;
  74. /**
  75. * Services map
  76. */
  77. private Map<String, Map<IfService, OutageSvcTimesList>> m_services = null;
  78. /**
  79. * End time of the last month.
  80. */
  81. private long m_endLastMonthTime;
  82. /**
  83. * Number of days in the last month.
  84. */
  85. private int m_daysInLastMonth;
  86. /**
  87. * The time in milliseconds per day.
  88. */
  89. private static final long ROLLING_WINDOW = 86400000l;
  90. /**
  91. * Constant
  92. */
  93. private static final int THIRTY = 30;
  94. /**
  95. * Constant (Number of months)
  96. */
  97. private static final int NMONTHS = 12;
  98. /**
  99. * Nodes that match this category.
  100. */
  101. private List<Node> m_nodes;
  102. /**
  103. * Monitored Services for the category
  104. */
  105. private List<String> m_monitoredServices;
  106. /**
  107. * This is used for the PDF Report generation
  108. */
  109. private int m_sectionIndex;
  110. /**
  111. * Constructor
  112. *
  113. * @param nodes
  114. * List of nodes
  115. * @param endTime
  116. * End time ( end of yesterday in milliseconds)
  117. * @param lastMonthEndTime
  118. * Last months end time (end of the last day of last month in
  119. * milliseconds)
  120. * @param monitoredServices
  121. * Monitored services belonging to the category.
  122. * @param report
  123. * Castor Report class.
  124. * @param offenders
  125. * Map of all offenders -- percent/(list of node) pairs
  126. * @param format
  127. * Value can be "SVG / all"
  128. * @param warning a double.
  129. * @param normal a double.
  130. * @param comments a {@link java.lang.String} object.
  131. * @param name a {@link java.lang.String} object.
  132. * @param monthFormat a {@link java.lang.String} object.
  133. * @param catIndex a int.
  134. * @param sectionIndex a int.
  135. */
  136. public AvailCalculations(List<Node> nodes, long endTime, long lastMonthEndTime, List<String> monitoredServices, Report report, TreeMap<Double, List<String>> offenders, double warning, double normal, String comments, String name, String format, String monthFormat, int catIndex, int sectionIndex) {
  137. m_sectionIndex = sectionIndex;
  138. org.opennms.reporting.availability.Category category = new org.opennms.reporting.availability.Category();
  139. category.setWarning(warning);
  140. category.setNormal(normal);
  141. category.setCatComments(comments);
  142. category.setCatName(name);
  143. category.setCatIndex(catIndex);
  144. category.setNodeCount(nodes.size());
  145. int ipaddrCount = 0;
  146. int serviceCount = 0;
  147. for(Node tmpNode : nodes) {
  148. if (tmpNode != null) {
  149. ipaddrCount += tmpNode.getInterfaceCount();
  150. serviceCount += tmpNode.getServiceCount();
  151. }
  152. }
  153. category.setIpaddrCount(ipaddrCount);
  154. category.setServiceCount(serviceCount);
  155. org.opennms.reporting.availability.Categories categories = report.getCategories();
  156. String oldPrefix = ThreadCategory.getPrefix();
  157. ThreadCategory.setPrefix(LOG4J_CATEGORY);
  158. log = ThreadCategory.getInstance(this.getClass());
  159. if (log.isDebugEnabled())
  160. log.debug("Inside AvailCalculations using endTime " + endTime);
  161. ThreadCategory.setPrefix(oldPrefix);
  162. m_monitoredServices = monitoredServices;
  163. m_endLastMonthTime = lastMonthEndTime;
  164. m_daysInLastMonth = getDaysForMonth(m_endLastMonthTime);
  165. m_report = report;
  166. m_nodes = nodes;
  167. m_endTime = endTime;
  168. String label;
  169. String descr;
  170. // Please node the following 4 formats are displayed on the graphical
  171. // report.
  172. // (i) last12MoAvail
  173. // (ii) LastMonthsDailyAvailability
  174. // (iii) MonthToDateDailyAvailability
  175. // (iv) lastMoTop20offenders
  176. if (log.isDebugEnabled())
  177. log.debug("Now computing last 12 months daily availability ");
  178. //
  179. // N Months Availability
  180. //
  181. label = AvailabilityConstants.NMONTH_TOTAL_LABEL;
  182. descr = AvailabilityConstants.NMONTH_TOTAL_DESCR;
  183. if (label == null || label.length() == 0)
  184. label = "The last 12 Months Availability";
  185. if (descr == null || descr.length() == 0)
  186. descr = "The last 12 Months Availability";
  187. CatSections catSections = new CatSections();
  188. lastNMonthsAvailability(NMONTHS, m_endLastMonthTime, catSections, label, descr);
  189. if (log.isDebugEnabled())
  190. log.debug("Computed lastNMonthsAvailability");
  191. //
  192. // Last Months Daily Availability
  193. //
  194. if (log.isDebugEnabled())
  195. log.debug("Now computing last months daily availability ");
  196. label = AvailabilityConstants.LAST_MONTH_DAILY_LABEL;
  197. descr = AvailabilityConstants.LAST_MONTH_DAILY_DESCR;
  198. if (label == null || label.length() == 0)
  199. label = "The last Months Daily Availability";
  200. if (descr == null || descr.length() == 0)
  201. descr = "Daily Average of svcs monitored and availability of svcs divided by the total svc minutes (last month)";
  202. if (monthFormat.equalsIgnoreCase("calendar")){
  203. lastCalMoDailyAvailability(m_daysInLastMonth, m_endLastMonthTime, catSections, label, descr, "LastMonthsDailyAvailability");
  204. }else {
  205. lastMoDailyAvailability(m_daysInLastMonth, m_endLastMonthTime, catSections, label, descr, "LastMonthsDailyAvailability");
  206. }
  207. if (log.isDebugEnabled())
  208. log.debug("Computed lastNDaysDailyAvailability");
  209. //
  210. // Month To Date Daily Availability
  211. //
  212. if (log.isDebugEnabled())
  213. log.debug("Now computing month to date daily availability ");
  214. label = AvailabilityConstants.LAST_MTD_DAILY_LABEL;
  215. descr = AvailabilityConstants.LAST_MTD_DAILY_DESCR;
  216. if (label == null || label.length() == 0)
  217. label = "Month To Date Daily Availability";
  218. if (descr == null || descr.length() == 0)
  219. descr = "Daily Average of svc monitored and availability of svcs div by total svc minutes of month frm 1st till date";
  220. Calendar calendar = new GregorianCalendar();
  221. calendar.setTime(new Date(m_endTime));
  222. int numDaysInMonth = calendar.get(Calendar.DAY_OF_MONTH);
  223. if (monthFormat.equalsIgnoreCase("calendar")){
  224. lastCalMTDDailyAvailability(numDaysInMonth, m_endTime, catSections, label, descr, "MonthToDateDailyAvailability");
  225. }else {
  226. lastMTDDailyAvailability(numDaysInMonth, m_endTime, catSections, label, descr, "MonthToDateDailyAvailability");
  227. }
  228. if (log.isDebugEnabled())
  229. log.debug("Computed lastNDaysDailyAvailability");
  230. //
  231. // Last Months Top Offenders
  232. //
  233. if (log.isDebugEnabled())
  234. log.debug("Now computing Last Months Top Offenders ");
  235. label = AvailabilityConstants.NOFFENDERS_LABEL;
  236. descr = AvailabilityConstants.NOFFENDERS_DESCR;
  237. if (label == null || label.length() == 0)
  238. label = "Last Months Top Offenders";
  239. if (descr == null || descr.length() == 0)
  240. descr = "This is the list of the worst available devices in the category for the last month";
  241. lastMoTopNOffenders(offenders, catSections, label, descr);
  242. if (log.isDebugEnabled())
  243. log.debug("Computed lastMoTopNOffenders ");
  244. //
  245. // Last N days Daily Availability
  246. //
  247. if (!format.equals("SVG")) {
  248. if (log.isDebugEnabled())
  249. log.debug("Now computing LAST_30_DAYS_DAILY_LABEL ");
  250. label = AvailabilityConstants.LAST_30_DAYS_DAILY_LABEL;
  251. descr = AvailabilityConstants.LAST_30_DAYS_DAILY_DESCR;
  252. if (label == null || label.length() == 0)
  253. label = "The last 30 Days Daily Availability";
  254. if (descr == null || descr.length() == 0)
  255. descr = "Daily average of svcs and dvcs monitored and their availability divided by total mins for 30days";
  256. lastNDaysDailyAvailability(THIRTY, m_endTime, catSections, label, descr, "Last30DaysDailyAvailability");
  257. if (log.isDebugEnabled())
  258. log.debug("Computed lastNDaysDailyAvailability");
  259. }
  260. //
  261. // N days total availability
  262. //
  263. if (!format.equals("SVG")) {
  264. if (log.isDebugEnabled())
  265. log.debug("Now computing LAST_30_DAYS_TOTAL_LABEL ");
  266. label = AvailabilityConstants.LAST_30_DAYS_TOTAL_LABEL;
  267. descr = AvailabilityConstants.LAST_30_DAYS_TOTAL_DESCR;
  268. if (label == null || label.length() == 0)
  269. label = "The last 30 Days Total Availability";
  270. if (descr == null || descr.length() == 0)
  271. descr = "Average of svcs monitored and availability of svcs divided by total svc minutes of the last 30 days";
  272. lastNDaysTotalAvailability(THIRTY, m_endTime, catSections, label, descr);
  273. if (log.isDebugEnabled())
  274. log.debug("Computed lastNDaysTotalAvailability");
  275. }
  276. //
  277. // Last Months Total Availability
  278. //
  279. if (!format.equals("SVG")) {
  280. if (log.isDebugEnabled())
  281. log.debug("Now computing LAST_MONTH_TOTAL_LABEL ");
  282. label = AvailabilityConstants.LAST_MONTH_TOTAL_LABEL;
  283. descr = AvailabilityConstants.LAST_MONTH_TOTAL_DESCR;
  284. if (label == null || label.length() == 0)
  285. label = "The last Months Total Availability";
  286. if (descr == null || descr.length() == 0)
  287. descr = "Average of svcs monitored and availability of svcs divided by the total svc minutes of the month";
  288. lastMoTotalAvailability(m_daysInLastMonth, m_endLastMonthTime, catSections, label, descr);
  289. if (log.isDebugEnabled())
  290. log.debug("Computed lastNDaysDailyAvailability");
  291. }
  292. //
  293. // Month To Date Total Availability
  294. //
  295. if (!format.equals("SVG")) {
  296. if (log.isDebugEnabled())
  297. log.debug("Now computing LAST_MTD_TOTAL_LABEL ");
  298. label = AvailabilityConstants.LAST_MTD_TOTAL_LABEL;
  299. descr = AvailabilityConstants.LAST_MTD_TOTAL_DESCR;
  300. if (label == null || label.length() == 0)
  301. label = "Month To Date Total Availability";
  302. if (descr == null || descr.length() == 0)
  303. descr = "Average of svc monitored and availability of svcs dividedby total svc minutes of month frm 1st till date";
  304. lastMoTotalAvailability(numDaysInMonth, m_endTime, catSections, label, descr);
  305. if (log.isDebugEnabled())
  306. log.debug("Computed MTDTotalAvailability");
  307. }
  308. m_services = new HashMap<String, Map<IfService, OutageSvcTimesList>>();
  309. for(Node node : nodes) {
  310. if (node != null) {
  311. for(Interface intf : node.getInterfaces()) {
  312. if (intf != null) {
  313. for(Service svc : intf.getServices()) {
  314. if (svc != null) {
  315. OutageSvcTimesList outages = svc.getOutages();
  316. if (outages != null) {
  317. IfService ifservice = new IfService(node.getNodeID(), intf.getName(), -1, node.getName(), svc.getName());
  318. Map<IfService, OutageSvcTimesList> svcOutages = m_services.get(svc.getName());
  319. if (svcOutages == null)
  320. svcOutages = new HashMap<IfService, OutageSvcTimesList>();
  321. svcOutages.put(ifservice, outages);
  322. m_services.put(svc.getName(), svcOutages);
  323. }
  324. }
  325. }
  326. }
  327. }
  328. }
  329. }
  330. if (log.isDebugEnabled())
  331. log.debug("Services " + m_services);
  332. m_nodes = null;
  333. //
  334. // N Days Daily Service Availability
  335. //
  336. if (!format.equals("SVG")) {
  337. if (log.isDebugEnabled())
  338. log.debug("Now computing LAST_30_DAYS_SVC_AVAIL_LABEL ");
  339. label = AvailabilityConstants.LAST_30_DAYS_SVC_AVAIL_LABEL;
  340. descr = AvailabilityConstants.LAST_30_DAYS_SVC_AVAIL_DESCR;
  341. if (label == null || label.length() == 0)
  342. label = "The last 30 days Daily Service Availability";
  343. if (descr == null || descr.length() == 0)
  344. descr = "The last 30 days Daily Service Availability is the daily average of services";
  345. lastNDaysDailyServiceAvailability(THIRTY, m_endTime, catSections, label, descr);
  346. if (log.isDebugEnabled())
  347. log.debug("Computed lastNDaysDailyServiceAvailability");
  348. }
  349. //
  350. // Last Months Daily Service Availability
  351. //
  352. if (!format.equals("SVG")) {
  353. if (log.isDebugEnabled())
  354. log.debug("Now computing LAST_MONTH_SVC_AVAIL_LABE");
  355. label = AvailabilityConstants.LAST_MONTH_SVC_AVAIL_LABEL;
  356. descr = AvailabilityConstants.LAST_MONTH_SVC_AVAIL_DESCR;
  357. if (label == null || label.length() == 0)
  358. label = "The last Months Daily Service Availability";
  359. if (descr == null || descr.length() == 0)
  360. descr = "The last Months Daily Service Availability is the daily average of services and devices";
  361. lastNDaysDailyServiceAvailability(m_daysInLastMonth, m_endLastMonthTime, catSections, label, descr);
  362. if (log.isDebugEnabled())
  363. log.debug("Computed lastNDaysDailyServiceAvailability");
  364. }
  365. //
  366. // Top N Service Outages
  367. //
  368. if (!format.equals("SVG")) {
  369. if (log.isDebugEnabled())
  370. log.debug("Now computing TOP20_SVC_OUTAGES_LABEL");
  371. label = AvailabilityConstants.TOP20_SVC_OUTAGES_LABEL;
  372. descr = AvailabilityConstants.TOP20_SVC_OUTAGES_DESCR;
  373. if (label == null || label.length() == 0)
  374. label = "Last Month Top Service Outages for ";
  375. if (descr == null || descr.length() == 0)
  376. descr = "Last Month Top Service Outages for ";
  377. lastMonTopNServiceOutages(catSections, label, descr);
  378. if (log.isDebugEnabled())
  379. log.debug("Computed lastMonTopNServiceOutages");
  380. }
  381. m_services = null;
  382. category.addCatSections(catSections);
  383. categories.addCategory(category);
  384. m_report.setCategories(categories);
  385. report = m_report;
  386. if (log.isDebugEnabled())
  387. log.debug("Leaving AvailCalculations");
  388. }
  389. /**
  390. * <p>getSectionIndex</p>
  391. *
  392. * @return a int.
  393. */
  394. public int getSectionIndex() {
  395. return m_sectionIndex;
  396. }
  397. /**
  398. * Last Months Top N Service Outages.
  399. *
  400. * @param catSections
  401. * Castors sections
  402. * @param label
  403. * Section name in the xml
  404. * @param descr
  405. * Section descr.
  406. */
  407. private void lastMonTopNServiceOutages(CatSections catSections, String label, String descr) {
  408. // Result is a map of outage / lost time
  409. //
  410. // For each monitored service, get all individual outages.
  411. //
  412. TreeMap<Long, List<OutageSince>> treeMap = null;
  413. for(String service : m_services.keySet()) {
  414. treeMap = new TreeMap<Long, List<OutageSince>>();
  415. Map<IfService, OutageSvcTimesList> ifSvcOutageList = m_services.get(service);
  416. for(IfService ifservice : ifSvcOutageList.keySet()) {
  417. if (ifservice != null) {
  418. OutageSvcTimesList outageSvcList = (OutageSvcTimesList) ifSvcOutageList.get(ifservice);
  419. if (outageSvcList != null) {
  420. long rollingWindow = m_daysInLastMonth * ROLLING_WINDOW;
  421. List<OutageSince> svcOutages = outageSvcList.getServiceOutages(ifservice.getNodeName(), m_endLastMonthTime, rollingWindow);
  422. for(OutageSince outageSince : svcOutages) {
  423. if (outageSince != null) {
  424. long outage = outageSince.getOutage() / 1000;
  425. List<OutageSince> tmpList = treeMap.get(new Long(outage));
  426. if (tmpList == null)
  427. tmpList = new ArrayList<OutageSince>();
  428. tmpList.add(outageSince);
  429. treeMap.put(new Long(-1 * outage), tmpList);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. log.debug("Top 20 service outages from the list " + treeMap);
  436. int top20Count = 0;
  437. Rows rows = new Rows();
  438. loop : for(Long outage : treeMap.keySet()) {
  439. List<OutageSince> list = treeMap.get(outage);
  440. for(OutageSince outageSince : list) {
  441. top20Count++;
  442. String nodeName = outageSince.getNodeName();
  443. Value nodeValue = new Value();
  444. nodeValue.setContent(nodeName);
  445. nodeValue.setType("title");
  446. Value value = new Value();
  447. long outtime = outageSince.getOutage() / 1000;
  448. int hrs = (new Long(outtime / (60 * 60))).intValue();
  449. int remain = (new Long(outtime % (60 * 60))).intValue();
  450. int mins = remain / (60);
  451. remain = remain % (60);
  452. int secs = remain;
  453. log.debug("Outage : " + outtime + " in mins " + hrs + " hrs " + mins + " mins " + secs + " secs ");
  454. value.setContent(hrs + " hrs " + mins + " mins " + secs + " secs ");
  455. value.setType("data");
  456. Value datevalue = new Value();
  457. SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  458. datevalue.setContent(fmt.format(new Date(outageSince.getOutTime())));
  459. datevalue.setType("other");
  460. Row row = new Row();
  461. row.addValue(nodeValue);
  462. row.addValue(value);
  463. row.addValue(datevalue);
  464. rows.addRow(row);
  465. if (top20Count >= 20) {
  466. break loop;
  467. }
  468. }
  469. }
  470. Col col = new Col();
  471. col.addColTitle(0, "Node Name");
  472. col.addColTitle(1, "Duration Of Outage");
  473. col.addColTitle(2, "Service Lost Time");
  474. ClassicTable table = new ClassicTable();
  475. table.setCol(col);
  476. table.setRows(rows);
  477. Section section = new Section();
  478. section.setClassicTable(table);
  479. section.setSectionName(label + " " + service);
  480. section.setSectionTitle(label + " " + service);
  481. section.setSectionDescr(descr + " " + service);
  482. section.setSectionIndex(m_sectionIndex);
  483. m_sectionIndex++;
  484. catSections.addSection(section);
  485. }
  486. }
  487. /**
  488. * Last Month To Date Daily Availability
  489. *
  490. * @param days
  491. * Number of days for which the availability computations are
  492. * made.
  493. * @param endTime
  494. * End time
  495. * @param sections
  496. * Castors sections
  497. * @param label
  498. * Section name in the xml
  499. * @param descr
  500. * Section descr.
  501. * @param sectionName
  502. * Section name.
  503. */
  504. private void lastMTDDailyAvailability(int days, long endTime, CatSections sections, String label, String descr, String sectionName) {
  505. lastNDaysDailyAvailability(days, endTime, sections, label, descr, sectionName);
  506. }
  507. /**
  508. * Last Month To Date Daily Availability
  509. *
  510. * @param days
  511. * Number of days for which the availability computations are
  512. * made.
  513. * @param endTime
  514. * End time
  515. * @param sections
  516. * Castors sections
  517. * @param label
  518. * Section name in the xml
  519. * @param descr
  520. * Section descr.
  521. * @param sectionName
  522. * Section name.
  523. */
  524. private void lastCalMTDDailyAvailability(int days, long endTime, CatSections sections, String label, String descr, String sectionName) {
  525. lastNDaysCalDailyAvailability(days, endTime, sections, label, descr, sectionName);
  526. }
  527. /**
  528. * Last N Days Total Availability.
  529. *
  530. * @param days
  531. * Number of days for which the availability computations are
  532. * made.
  533. * @param endTime
  534. * End time
  535. * @param catSections
  536. * Castors sections
  537. * @param label
  538. * Section name in the xml
  539. * @param descr
  540. * Section descr.
  541. */
  542. private void lastMoTotalAvailability(int days, long endTime, CatSections catSections, String label, String descr) {
  543. lastNDaysTotalAvailability(days, endTime, catSections, label, descr);
  544. }
  545. /**
  546. *
  547. * Last Months Top N offenders.
  548. *
  549. * @param offenders
  550. * Top Offenders
  551. * @param catSections
  552. * Castors sections
  553. * @param label
  554. * Section name in the xml
  555. * @param descr
  556. * Section descr.
  557. *
  558. */
  559. private void lastMoTopNOffenders(TreeMap<Double, List<String>> offenders, CatSections catSections, String label, String descr) {
  560. // copy this method from the outage data code.
  561. //
  562. if (log.isDebugEnabled()) {
  563. log.debug("Offenders " + offenders);
  564. log.debug("Inside lastMoTopNOffenders");
  565. }
  566. Set<Double> percentValues = offenders.keySet();
  567. Iterator<Double> iter = percentValues.iterator();
  568. Rows rows = new Rows();
  569. int top20Count = 0;
  570. loop: while (iter.hasNext()) {
  571. Double percent = (Double) iter.next();
  572. if (percent.doubleValue() < 100.0) {
  573. List<String> nodeNames = offenders.get(percent);
  574. if (nodeNames != null) {
  575. ListIterator<String> lstIter = nodeNames.listIterator();
  576. while (lstIter.hasNext()) {
  577. top20Count++;
  578. String nodeName = lstIter.next();
  579. Value dateValue = new Value();
  580. dateValue.setContent(nodeName);
  581. dateValue.setType("title");
  582. Value value = new Value();
  583. value.setContent(formatNumber("" + percent.doubleValue()));
  584. value.setType("data");
  585. Row row = new Row();
  586. row.addValue(dateValue);
  587. row.addValue(value);
  588. rows.addRow(row);
  589. if (top20Count >= 20) {
  590. break loop;
  591. }
  592. }
  593. }
  594. }
  595. }
  596. Col col = new Col();
  597. col.addColTitle(0, "Node Name");
  598. col.addColTitle(1, "Percentage Availability");
  599. ClassicTable table = new ClassicTable();
  600. table.setCol(col);
  601. table.setRows(rows);
  602. Section section = new Section();
  603. section.setClassicTable(table);
  604. section.setSectionName("lastMoTop20offenders");
  605. section.setSectionTitle(label);
  606. section.setSectionDescr(descr);
  607. section.setSectionIndex(m_sectionIndex);
  608. m_sectionIndex++;
  609. catSections.addSection(section);
  610. if (log.isDebugEnabled())
  611. log.debug("Leaving lastMoTopNOffenders");
  612. }
  613. /**
  614. * Last Months Daily availability
  615. *
  616. * @param days
  617. * Number of days for which the availability computations are
  618. * made
  619. * @param endTime
  620. * End time
  621. * @param sections
  622. * Castors sections
  623. * @param label
  624. * Section name in the xml
  625. * @param descr
  626. * Section descr.
  627. * @param sectionName
  628. * Section name
  629. */
  630. private void lastMoDailyAvailability(int days, long endTime, CatSections sections, String label, String descr, String sectionName) {
  631. lastNDaysDailyAvailability(days, endTime, sections, label, descr, sectionName);
  632. }
  633. /**
  634. * Last Months Daily availability
  635. *
  636. * @param days
  637. * Number of days for which the availability computations are
  638. * made
  639. * @param endTime
  640. * End time
  641. * @param sections
  642. * Castors sections
  643. * @param label
  644. * Section name in the xml
  645. * @param descr
  646. * Section descr.
  647. * @param sectionName
  648. * Section name
  649. */
  650. private void lastCalMoDailyAvailability(int days, long endTime, CatSections sections, String label, String descr, String sectionName) {
  651. lastNDaysCalDailyAvailability(days, endTime, sections, label, descr, sectionName);
  652. }
  653. /**
  654. * Last N days daily availability.
  655. *
  656. * @param days
  657. * Number of days for which the availability computations are
  658. * made.
  659. * @param endTime
  660. * End time
  661. * @param catSections
  662. * Castors sections
  663. * @param label
  664. * Section name in the xml
  665. * @param descr
  666. * Section descr.
  667. * @param sectionName
  668. * Section name
  669. */
  670. private void lastNDaysCalDailyAvailability(int days, long endTime, CatSections catSections, String label, String descr, String sectionName) {
  671. if (log.isDebugEnabled())
  672. log.debug("Inside lastNDaysDailyAvailability");
  673. int numdays = 0;
  674. CalendarTableBuilder calBuilder = new CalendarTableBuilder(endTime);
  675. TreeMap<Date, Double> treeMap = new TreeMap<Date, Double>();
  676. SimpleDateFormat fmt = new SimpleDateFormat("dd MMM, yyyy");
  677. String periodEnd = fmt.format(new java.util.Date(endTime));
  678. String periodFrom = "";
  679. while (numdays++ < days) {
  680. if (log.isDebugEnabled())
  681. log.debug("Computing for " + new Date(endTime));
  682. int serviceCount = 0;
  683. long outage = 0;
  684. //
  685. // get the outage and service count.
  686. //
  687. for(Node node : m_nodes) {
  688. outage += node.getOutage(endTime, ROLLING_WINDOW);
  689. serviceCount += node.getServiceCount();
  690. }
  691. double percentAvail;
  692. if (serviceCount > 0){
  693. log.debug("LOOK: calculating percentAvail using outage " + outage + " service count " + serviceCount + " ROLLING_WINODW " + ROLLING_WINDOW + " endTime " + endTime);
  694. percentAvail = 100.0 * (1 - (outage * 1.0) / (1.0 * serviceCount * ROLLING_WINDOW));
  695. }
  696. else
  697. percentAvail = 100.0;
  698. //need a double object in here
  699. treeMap.put(new Date(endTime), new Double (percentAvail));
  700. Date nicedate = new Date(endTime);
  701. log.debug("Inserting " + percentAvail + " into " + nicedate);
  702. periodFrom = fmt.format(new java.util.Date(endTime));
  703. endTime -= ROLLING_WINDOW;
  704. }
  705. Set<Date> keyDates = treeMap.keySet();
  706. Iterator<Date> iter = keyDates.iterator();
  707. int dateSlot = 0;
  708. while (iter.hasNext()) {
  709. Date key = iter.next();
  710. Double percent = treeMap.get(key);
  711. log.debug("Inserting value " + percent.doubleValue() + " into date slot " + dateSlot);
  712. dateSlot++;
  713. log.debug("Inserting value " + percent.doubleValue() + " into date slot " + dateSlot);
  714. calBuilder.setPctValue(dateSlot, percent.doubleValue());
  715. }
  716. Section section = new Section();
  717. section.setCalendarTable(calBuilder.getTable());
  718. section.setSectionName(sectionName); // "LastMonthsDailyAvailability");
  719. section.setSectionTitle(label);
  720. section.setSectionDescr(descr);
  721. section.setPeriod(periodFrom + " to " + periodEnd);
  722. section.setSectionIndex(m_sectionIndex);
  723. m_sectionIndex++;
  724. catSections.addSection(section);
  725. log.debug("Leaving lastNDaysCalDailyAvailability");
  726. }
  727. /**
  728. * Last N days daily availability.
  729. *
  730. * @param days
  731. * Number of days for which the availability computations are
  732. * made.
  733. * @param endTime
  734. * End time
  735. * @param catSections
  736. * Castors sections
  737. * @param label
  738. * Section name in the xml
  739. * @param descr
  740. * Section descr.
  741. * @param sectionName
  742. * Section name
  743. */
  744. private void lastNDaysDailyAvailability(int days, long endTime, CatSections catSections, String label, String descr, String sectionName) {
  745. if (log.isDebugEnabled())
  746. log.debug("Inside lastNDaysDailyAvailability");
  747. int numdays = 0;
  748. Rows rows = new Rows();
  749. TreeMap<Date, String> treeMap = new TreeMap<Date, String>();
  750. SimpleDateFormat fmt = new SimpleDateFormat("dd MMM, yyyy");
  751. String periodEnd = fmt.format(new java.util.Date(endTime));
  752. String periodFrom = "";
  753. while (numdays++ < days) {
  754. if (log.isDebugEnabled())
  755. log.debug("Computing for " + new Date(endTime));
  756. int serviceCount = 0;
  757. long outage = 0;
  758. //
  759. // get the outage and service count.
  760. //
  761. for(Node node : m_nodes) {
  762. outage += node.getOutage(endTime, ROLLING_WINDOW);
  763. serviceCount += node.getServiceCount();
  764. }
  765. double percentAvail;
  766. if (serviceCount > 0)
  767. percentAvail = 100.0 * (1 - (outage * 1.0) / (1.0 * serviceCount * ROLLING_WINDOW));
  768. else
  769. percentAvail = 100.0;
  770. treeMap.put(new Date(endTime), formatNumber(percentAvail + ""));
  771. periodFrom = fmt.format(new java.util.Date(endTime));
  772. endTime -= ROLLING_WINDOW;
  773. }
  774. Set<Date> keyDates = treeMap.keySet();
  775. Iterator<Date> iter = keyDates.iterator();
  776. while (iter.hasNext()) {
  777. Date key = iter.next();
  778. Value dateValue = new Value();
  779. SimpleDateFormat fmtmp = new SimpleDateFormat("dd");
  780. dateValue.setContent(fmtmp.format(key));
  781. dateValue.setType("title");
  782. String percent = treeMap.get(key);
  783. Value value = new Value();
  784. value.setContent(percent);
  785. value.setType("data");
  786. Row row = new Row();
  787. row.addValue(dateValue);
  788. row.addValue(value);
  789. rows.addRow(row);
  790. }
  791. Col col = new Col();
  792. col.addColTitle(0, "Date");
  793. col.addColTitle(1, "Percentage Availability");
  794. ClassicTable table = new ClassicTable();
  795. table.setCol(col);
  796. table.setRows(rows);
  797. Section section = new Section();
  798. section.setClassicTable(table);
  799. section.setSectionName(sectionName); // "LastMonthsDailyAvailability");
  800. section.setSectionTitle(label);
  801. section.setSectionDescr(descr);
  802. section.setPeriod(periodFrom + " to " + periodEnd);
  803. section.setSectionIndex(m_sectionIndex);
  804. m_sectionIndex++;
  805. catSections.addSection(section);
  806. log.debug("Leaving lastNDaysDailyAvailability");
  807. }
  808. /**
  809. * Last N Days Total Availability.
  810. *
  811. * @param days
  812. * Number of days for which the availability computations are
  813. * made.
  814. * @param endTime
  815. * End time
  816. * @param catSections
  817. * Castors sections
  818. * @param label
  819. * Section name in the xml
  820. * @param descr
  821. * Section descr.
  822. */
  823. private void lastNDaysTotalAvailability(int days, long endTime, CatSections catSections, String label, String descr) {
  824. log.debug("Inside lastNDaysTotalAvailability");
  825. Rows rows = new Rows();
  826. int serviceCount = 0;
  827. long outage = 0;
  828. int numdays = 0;
  829. SimpleDateFormat fmt = new SimpleDateFormat("dd MMM, yyyy");
  830. String periodEnd = fmt.format(new java.util.Date(endTime));
  831. String periodFrom = "";
  832. while (numdays++ < days) {
  833. //
  834. // get the outage and service count.
  835. //
  836. for(Node node : m_nodes) {
  837. serviceCount += node.getServiceCount();
  838. outage += node.getOutage(endTime, ROLLING_WINDOW);
  839. }
  840. periodFrom = fmt.format(new java.util.Date(endTime)) + " to " + periodEnd;
  841. endTime -= ROLLING_WINDOW;
  842. }
  843. double percentAvail;
  844. if (serviceCount > 0)
  845. percentAvail = 100.0 * (1 - (outage * 1.0) / (1.0 * serviceCount * ROLLING_WINDOW));
  846. else
  847. percentAvail = 100.0;
  848. Value dateValue = new Value();
  849. dateValue.setContent(periodFrom);
  850. dateValue.setType("title");
  851. Value value = new Value();
  852. value.setContent(formatNumber(percentAvail + ""));
  853. value.setType("data");
  854. Row row = new Row();
  855. row.addValue(dateValue);
  856. row.addValue(value);
  857. rows.addRow(row);
  858. Col col = new Col();
  859. col.addColTitle(0, "Date");
  860. col.addColTitle(1, "Percentage Availability");
  861. ClassicTable table = new ClassicTable();
  862. table.setCol(col);
  863. table.setRows(rows);
  864. Section section = new Section();
  865. section.setClassicTable(table);
  866. section.setSectionName("Last" + days + "TotalAvailability");
  867. section.setSectionTitle(label);
  868. section.setSectionDescr(descr);
  869. section.setPeriod(periodFrom);
  870. section.setSectionIndex(m_sectionIndex);
  871. m_sectionIndex++;
  872. catSections.addSection(section);
  873. log.debug("Leaving lastNDaysTotalAvailability");
  874. }
  875. /**
  876. * Last N Months Availability
  877. *
  878. * @param nMonths
  879. * Number of months for which the availability computations are
  880. * made.
  881. * @param endTime
  882. * End time
  883. * @param catSections
  884. * Castors sections
  885. * @param label
  886. * Section name in the xml
  887. * @param descr
  888. * Section descr.
  889. */
  890. private void lastNMonthsAvailability(int nMonths, long endTime, CatSections catSections, String label, String descr) {
  891. log.debug("Inside lastNMonthsAvailability");
  892. Rows rows = new Rows();
  893. int numMonths = 0;
  894. int numDays = getDaysForMonth(endTime);
  895. Calendar calendar = new GregorianCalendar();
  896. calendar.setTime(new Date(endTime));
  897. int month = calendar.get(Calendar.MONTH);
  898. int year = calendar.get(Calendar.YEAR);
  899. calendar.set(year, month, numDays, 23, 59, 59);
  900. endTime = calendar.getTime().getTime();
  901. SimpleDateFormat fmt = new SimpleDateFormat("MMM, yyyy");
  902. String periodEnd = fmt.format(new java.util.Date(endTime));
  903. TreeMap<Date, String> treeMap = new TreeMap<Date, String>(); // Holds all the month/percent
  904. // values to be displayed in order
  905. // on pdf.
  906. String periodFrom = null;
  907. while (numMonths++ < nMonths) {
  908. int serviceCount = 0;
  909. long outage = 0;
  910. log.debug("Number of days " + numDays + " in month of " + new Date(endTime));
  911. long rollingWindow = numDays * ROLLING_WINDOW * 1l;
  912. //
  913. // get the outage and service count.
  914. //
  915. for(Node node : m_nodes) {
  916. serviceCount += node.getServiceCount();
  917. outage += node.getOutage(endTime, rollingWindow);
  918. }
  919. double percentAvail;
  920. if (serviceCount > 0)
  921. percentAvail = 100.0 * (1 - (outage * 1.0) / (1.0 * serviceCount * rollingWindow));
  922. else
  923. percentAvail = 100.0;
  924. treeMap.put(new java.util.Date(endTime), formatNumber(percentAvail + ""));
  925. periodFrom = fmt.format(new java.util.Date(endTime));
  926. calendar = new GregorianCalendar();
  927. calendar.setTime(new Date(endTime));
  928. month = calendar.get(Calendar.MONTH);
  929. year = calendar.get(Calendar.YEAR);
  930. calendar.set(year, month - 1, 1, 0, 0, 0);
  931. endTime = calendar.getTime().getTime();
  932. month = calendar.get(Calendar.MONTH);
  933. year = calendar.get(Calendar.YEAR);
  934. numDays = getDaysForMonth(endTime);
  935. calendar.set(year, month, numDays, 23, 59, 59);
  936. endTime = calendar.getTime().getTime();
  937. }
  938. Set<Date> keyDates = treeMap.keySet();
  939. Iterator<Date> iter = keyDates.iterator();
  940. while (iter.hasNext()) {
  941. Date key = iter.next();
  942. Value dateValue = new Value();
  943. SimpleDateFormat fmtmp = new SimpleDateFormat("MMM");
  944. dateValue.setContent(fmtmp.format(key) + "");
  945. dateValue.setType("title");
  946. Value value = new Value();
  947. String percent = treeMap.get(key);
  948. value.setContent(percent);
  949. value.setType("data");
  950. Row row = new Row();
  951. row.addValue(dateValue);
  952. row.addValue(value);
  953. rows.addRow(row);
  954. }
  955. Col col = new Col();
  956. col.addColTitle(0, "Date");
  957. col.addColTitle(1, "Percentage Availability");
  958. ClassicTable table = new ClassicTable();
  959. table.setCol(col);
  960. table.setRows(rows);
  961. Section section = new Section();
  962. section.setClassicTable(table);
  963. section.setSectionName("last12MoAvail");
  964. section.setSectionTitle(label);
  965. section.setSectionDescr(descr);
  966. section.setPeriod(periodFrom + " to " + periodEnd);
  967. section.setSectionIndex(m_sectionIndex);
  968. m_sectionIndex++;
  969. catSections.addSection(section);
  970. log.debug("Leaving lastNMonthsAvailability");
  971. }
  972. /**
  973. * Returns the number of days in the month, also considers checks for leap
  974. * year.
  975. *
  976. * @param isLeap
  977. * the leap year flag.
  978. * @param month
  979. * The month whose days count is reqd
  980. */
  981. private static synchronized int getDays(boolean isLeap, int month) {
  982. switch (month) {
  983. case 0:
  984. case 2:
  985. case 4:
  986. case 6:
  987. case 7:
  988. case 9:
  989. case 11:
  990. return 31;
  991. case 3:
  992. case 5:
  993. case 8:
  994. case 10:
  995. return 30;
  996. case 1:
  997. if (isLeap)
  998. return 29;
  999. else
  1000. return 28;
  1001. }
  1002. return -1;
  1003. }
  1004. /**
  1005. * Returns the number of Days in the month
  1006. *
  1007. * @param endTime
  1008. * The end of the month (time in milliseconds)
  1009. */
  1010. private int getDaysForMonth(long endTime) {
  1011. GregorianCalendar calendar = new GregorianCalendar();
  1012. calendar.setTime(new java.util.Date(endTime));
  1013. int month = calendar.get(Calendar.MONTH);
  1014. int year = calendar.get(Calendar.YEAR);
  1015. return (getDays(calendar.isLeapYear(year), month));
  1016. }
  1017. /**
  1018. * Compute N days daily service availability.
  1019. *
  1020. * @param endTime
  1021. * End time
  1022. * @param catSections
  1023. * Castors sections
  1024. * @param label
  1025. * Section name in the xml
  1026. * @param descr
  1027. * Section descr.
  1028. */
  1029. private void lastNDaysDailyServiceAvailability(int days, long endTime, CatSections catSections, String label, String descr) {
  1030. log.debug("Inside lastNDaysDailyServiceAvailability " + days);
  1031. long outage;
  1032. String periodFrom = "";
  1033. SimpleDateFormat fmtmp = new SimpleDateFormat("dd MMM, yyyy");
  1034. String periodTo = "";
  1035. periodTo = fmtmp.format(new java.util.Date(endTime));
  1036. for(String service : m_monitoredServices) {
  1037. TreeMap<Date, Double> treeMap = new TreeMap<Date, Double>();
  1038. Rows rows = new Rows();
  1039. log.debug("SERvice " + service);
  1040. long curTime = endTime;
  1041. Map<IfService, OutageSvcTimesList> svcOutages = null;
  1042. if (m_services != null)
  1043. svcOutages = m_services.get(service);
  1044. if (svcOutages == null || svcOutages.size() <= 0) {
  1045. int daysCnt = 0;
  1046. while (daysCnt++ < days) {
  1047. log.debug("DAy 100 % : " + daysCnt);
  1048. periodFrom = fmtmp.format(new java.util.Date(curTime));
  1049. treeMap.put(new java.util.Date(curTime), new Double(100.0));
  1050. curTime -= ROLLING_WINDOW;
  1051. }
  1052. Set<Date> keys = treeMap.keySet();
  1053. Iterator<Date> iter = keys.iterator();
  1054. while (iter.hasNext()) {
  1055. Date tmp = iter.next();
  1056. Value dateValue = new Value();
  1057. SimpleDateFormat fmt = new SimpleDateFormat("dd");
  1058. dateValue.setContent(fmt.format(tmp) + "");
  1059. dateValue.setType("title");
  1060. Value value = new Value();
  1061. value.setContent("100.0");
  1062. value.setType("data");
  1063. Row row = new Row();
  1064. row.addValue(dateValue);
  1065. row.addValue(value);
  1066. rows.addRow(row);
  1067. }
  1068. Col col = new Col();
  1069. col.addColTitle(0, "Date");
  1070. col.addColTitle(1, "Percentage Availability");
  1071. ClassicTable table = new ClassicTable();
  1072. table.setCol(col);
  1073. table.setRows(rows);
  1074. Section section = new Section();
  1075. section.setClassicTable(table);
  1076. section.setPeriod(periodFrom + " to " + periodTo);
  1077. section.setSectionName(label + " " + service);
  1078. section.setSectionTitle(label + " " + service);
  1079. section.setSectionDescr(descr + " " + service);
  1080. section.setSectionIndex(m_sectionIndex);
  1081. m_sectionIndex++;
  1082. catSections.addSection(section);
  1083. } else {
  1084. int daysCnt = 0;
  1085. while (daysCnt++ < days) {
  1086. log.debug("DAy : " + daysCnt + " end time " + new Date(curTime) + " " + " ROLLING_WINDOW " + ROLLING_WINDOW);
  1087. int serviceCnt = 0;
  1088. long outageTime = 0;
  1089. // For each node in the service table.
  1090. //
  1091. // Iterate each svc node for getting the ifservice
  1092. Set<IfService> keys = svcOutages.keySet();
  1093. Iterator<IfService> iter = keys.iterator();
  1094. while (iter.hasNext()) {
  1095. IfService ifservice = (IfService) iter.next();
  1096. log.debug(ifservice.toString());
  1097. OutageSvcTimesList outageList = (OutageSvcTimesList) svcOutages.get(ifservice);
  1098. if (outageList != null) {
  1099. outage = outageList.getDownTime(curTime, ROLLING_WINDOW);
  1100. // Keep track of the number of services being
  1101. // monitored.
  1102. //
  1103. outageTime += outage;
  1104. }
  1105. serviceCnt++;
  1106. }
  1107. log.debug("Outage Time " + outageTime);
  1108. long den = (ROLLING_WINDOW * serviceCnt);
  1109. double outag = 1.0 * outageTime;
  1110. double denom = 1.0 * den;
  1111. double cal = 0;
  1112. if (den > 0)
  1113. cal = 100.0 * (1 - (outag / denom));
  1114. treeMap.put(new java.util.Date(curTime), new Double(cal));
  1115. periodFrom = fmtmp.format(new java.util.Date(curTime));
  1116. log.debug("Added to svc list " + new java.util.Date(curTime));
  1117. curTime -= ROLLING_WINDOW;
  1118. }
  1119. Set<Date> keys = treeMap.keySet();
  1120. Iterator<Date> iter = keys.iterator();
  1121. while (iter.hasNext()) {
  1122. Date tmp = iter.next();
  1123. Value dateValue = new Value();
  1124. SimpleDateFormat fmt = new SimpleDateFormat("dd");
  1125. dateValue.setContent(fmt.format(tmp) + "");
  1126. dateValue.setType("title");
  1127. Double val = treeMap.get(tmp);
  1128. Value value = new Value();
  1129. value.setContent("" + val);
  1130. value.setType("data");
  1131. Row row = new Row();
  1132. row.addValue(dateValue);
  1133. row.addValue(value);
  1134. rows.addRow(row);
  1135. }
  1136. Col col = new Col();
  1137. col.addColTitle(0, "Date");
  1138. col.addColTitle(1, "Percentage Availability");
  1139. ClassicTable table = new ClassicTable();
  1140. table.setCol(col);
  1141. table.setRows(rows);
  1142. Section section = new Section();
  1143. section.setClassicTable(table);
  1144. section.setPeriod(periodFrom + " to " + periodTo);
  1145. section.setSectionName(label + " " + service);
  1146. section.setSectionTitle(label + " " + service);
  1147. section.setSectionDescr(descr + " " + service);
  1148. section.setSectionIndex(m_sectionIndex);
  1149. m_sectionIndex++;
  1150. catSections.addSection(section);
  1151. }
  1152. }
  1153. log.debug("Leaving lastNDaysDailyServiceAvailability");
  1154. }
  1155. /**
  1156. * Format the number (String) and return 6 digits of the number
  1157. */
  1158. private String formatNumber(String num) {
  1159. if (num.indexOf('.') == 0) {
  1160. num = "0" + num;
  1161. }
  1162. if (num.indexOf('.') == -1) {
  1163. num = num + ".0";
  1164. }
  1165. num = num + "000000";
  1166. return (num.substring(0, num.indexOf('.') + 6));
  1167. }
  1168. }