5a96f6bcb9afa0982e0d00138279abb4246d771c
[public/netxms.git] / src / java / client / netxms-client / src / test / java / org / netxms / client / DataCollectionTest.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.client;
20
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import org.netxms.client.constants.AggregationFunction;
25 import org.netxms.client.constants.RCC;
26 import org.netxms.client.datacollection.DataCollectionConfiguration;
27 import org.netxms.client.datacollection.DataCollectionItem;
28 import org.netxms.client.datacollection.DataCollectionObject;
29 import org.netxms.client.datacollection.DciSummaryTable;
30 import org.netxms.client.datacollection.DciSummaryTableColumn;
31 import org.netxms.client.datacollection.DciSummaryTableDescriptor;
32 import org.netxms.client.datacollection.DciValue;
33 import org.netxms.client.datacollection.PerfTabDci;
34 import org.netxms.client.datacollection.Threshold;
35 import org.netxms.client.datacollection.ThresholdViolationSummary;
36 import org.netxms.client.objects.DataCollectionTarget;
37
38 /**
39 * Test cases for data collection
40 */
41 public class DataCollectionTest extends AbstractSessionTest
42 {
43 public void testGetLastValues() throws Exception
44 {
45 final NXCSession session = connect();
46
47 DciValue[] list = session.getLastValues(TestConstants.NODE_ID);
48 assertEquals(true, list.length > 0);
49
50 boolean statusFound = false;
51 for(int i = 0; i < list.length; i++)
52 {
53 System.out.println(list[i].getDescription() + " = " + list[i].getValue());
54 if ((list[i].getName().equalsIgnoreCase("Status")) && (list[i].getSource() == DataCollectionObject.INTERNAL))
55 statusFound = true;
56 }
57 assertEquals(true, statusFound);
58
59 session.disconnect();
60 }
61
62 public void testGetLastValuesForMap() throws Exception
63 {
64 final NXCSession session = connect();
65
66 DciValue[] list = session.getLastValues(TestConstants.LOCAL_NODE_ID, true, false, false);
67 assertEquals(true, list.length > 0);
68
69 boolean statusFound = false;
70 for(int i = 0; i < list.length; i++)
71 {
72 System.out.println(list[i].getDescription() + " = " + list[i].getValue());
73 if ((list[i].getName().equalsIgnoreCase("Status")) && (list[i].getSource() == DataCollectionObject.INTERNAL))
74 statusFound = true;
75 }
76 assertEquals(true, statusFound);
77
78 session.disconnect();
79 }
80
81 public void testGetThresholds() throws Exception
82 {
83 final NXCSession session = connect();
84
85 DataCollectionConfiguration dc = session.openDataCollectionConfiguration(TestConstants.NODE_ID);
86 final long dciId = dc.createItem(null);
87 DataCollectionItem dci = (DataCollectionItem)dc.findItem(dciId, DataCollectionItem.class);
88 dci.setName("TEST");
89 dci.getThresholds().add(new Threshold());
90 dc.modifyObject(dciId);
91
92 Threshold[] thresholds = session.getThresholds(TestConstants.NODE_ID, dciId);
93 assertNotNull(thresholds);
94 assertEquals(1, thresholds.length);
95
96 dc.deleteObject(dciId);
97 dc.close();
98 session.disconnect();
99 }
100
101 public void testGetThresholdSummary() throws Exception
102 {
103 final NXCSession session = connect();
104
105 session.syncObjects();
106
107 final List<ThresholdViolationSummary> list = session.getThresholdSummary(1);
108 for(ThresholdViolationSummary s : list)
109 {
110 DataCollectionTarget target = (DataCollectionTarget)session.findObjectById(s.getNodeId(), DataCollectionTarget.class);
111 System.out.println("* " + target.getObjectName());
112 if (s.getDciList().size() > 0)
113 {
114 for(DciValue v : s.getDciList())
115 {
116 System.out.println(" + " + v.getDescription());
117 }
118 }
119 else
120 {
121 System.out.println(" --- no threshold violations");
122 }
123 }
124
125 session.disconnect();
126 }
127
128 public void testGetPerfTabItems() throws Exception
129 {
130 final NXCSession session = connect();
131
132 PerfTabDci[] list = session.getPerfTabItems(TestConstants.NODE_ID);
133 assertNotNull(list);
134 assertTrue(list.length > 0);
135
136 for(PerfTabDci p : list)
137 {
138 System.out.println("id=" + p.getId() + " descr='" + p.getDescription() + "' settings='" + p.getPerfTabSettings() + "'");
139 }
140
141 session.disconnect();
142 }
143
144 public void testDciSummaryTables() throws Exception
145 {
146 final NXCSession session = connect();
147
148 DciSummaryTable t = new DciSummaryTable("test", "Test Table");
149 t.getColumns().add(new DciSummaryTableColumn("Idle", "System.CPU.Idle", 0, ";"));
150 t.getColumns().add(new DciSummaryTableColumn("I/O Wait", "System.CPU.IOWait", 0, ";"));
151
152 int id = session.modifyDciSummaryTable(t);
153 System.out.println("Assigned ID: " + id);
154 t.setId(id);
155
156 t.getColumns().add(new DciSummaryTableColumn("System", "^System\\.CPU\\.Sys.*", DciSummaryTableColumn.REGEXP_MATCH, ";"));
157 session.modifyDciSummaryTable(t);
158
159 List<DciSummaryTableDescriptor> list = session.listDciSummaryTables();
160 for(DciSummaryTableDescriptor d : list)
161 System.out.println(d.getId() + ": " + d.getMenuPath() + " " + d.getTitle());
162
163 session.getDciSummaryTable(id);
164 session.deleteDciSummaryTable(id);
165
166 try
167 {
168 session.getDciSummaryTable(id);
169 assertTrue(false);
170 }
171 catch(NXCException e)
172 {
173 if (e.getErrorCode() != RCC.INVALID_SUMMARY_TABLE_ID)
174 throw e;
175 }
176
177 session.disconnect();
178 }
179
180 private void printTable(Table t)
181 {
182 System.out.println(t.getRowCount() + " rows in result set");
183 for(int i = 0; i < t.getColumnCount(); i++)
184 System.out.print(String.format(" | %-20s", t.getColumnDisplayName(i)));
185 System.out.println(" |");
186 for(TableRow r : t.getAllRows())
187 {
188 for(int i = 0; i < t.getColumnCount(); i++)
189 System.out.print(String.format(" | %-20s", r.get(i).getValue()));
190 System.out.println(" |");
191 }
192 }
193
194 public void testAdHocDciSummaryTables() throws Exception
195 {
196 final NXCSession session = connect();
197
198 // single instance
199 List<DciSummaryTableColumn> columns = new ArrayList<DciSummaryTableColumn>();
200 columns.add(new DciSummaryTableColumn("Usage", "System.CPU.Usage", 0, ";"));
201 columns.add(new DciSummaryTableColumn("I/O Wait", "System.CPU.IOWait", 0, ";"));
202
203 Table result = session.queryAdHocDciSummaryTable(2, columns, AggregationFunction.AVERAGE, new Date(System.currentTimeMillis() - 86400000), new Date(), false);
204 printTable(result);
205
206 // multi instance
207 columns.clear();
208 columns.add(new DciSummaryTableColumn("Free %", "FileSystem\\.FreePerc\\(.*\\)", DciSummaryTableColumn.REGEXP_MATCH, ";"));
209 columns.add(new DciSummaryTableColumn("Free bytes", "FileSystem\\.Free\\(.*\\)", DciSummaryTableColumn.REGEXP_MATCH, ";"));
210
211 result = session.queryAdHocDciSummaryTable(2, columns, AggregationFunction.LAST, null, null, true);
212 printTable(result);
213
214 session.disconnect();
215 }
216 }