implemented nxsl class "Interface" and function GetInterfaceObject; manual updated
[public/netxms.git] / src / server / core / nxsl_classes.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2011 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 ** File: nxsl_classes.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25
26 //
27 // Implementation of "NetXMS object" class
28 //
29
30 NXSL_NetObjClass::NXSL_NetObjClass()
31 :NXSL_Class()
32 {
33 _tcscpy(m_szName, _T("NetObj"));
34 }
35
36 NXSL_Value *NXSL_NetObjClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
37 {
38 NetObj *object;
39 NXSL_Value *pValue = NULL;
40 TCHAR szBuffer[256];
41
42 object = (NetObj *)pObject->getData();
43 if (!_tcscmp(pszAttr, _T("name")))
44 {
45 pValue = new NXSL_Value(object->Name());
46 }
47 else if (!_tcscmp(pszAttr, _T("id")))
48 {
49 pValue = new NXSL_Value(object->Id());
50 }
51 else if (!_tcscmp(pszAttr, _T("status")))
52 {
53 pValue = new NXSL_Value((LONG)object->Status());
54 }
55 else if (!_tcscmp(pszAttr, _T("ipAddr")))
56 {
57 IpToStr(object->IpAddr(), szBuffer);
58 pValue = new NXSL_Value(szBuffer);
59 }
60 else if (!_tcscmp(pszAttr, _T("type")))
61 {
62 pValue = new NXSL_Value((LONG)object->Type());
63 }
64 else if (!_tcscmp(pszAttr, _T("comments")))
65 {
66 pValue = new NXSL_Value(object->getComments());
67 }
68 else
69 {
70 const TCHAR *attrValue = object->GetCustomAttribute(pszAttr);
71 if (attrValue != NULL)
72 {
73 pValue = new NXSL_Value(attrValue);
74 }
75 }
76 return pValue;
77 }
78
79
80 //
81 // Implementation of "NetXMS node" class
82 //
83
84 NXSL_NodeClass::NXSL_NodeClass()
85 :NXSL_Class()
86 {
87 _tcscpy(m_szName, _T("Node"));
88 }
89
90 NXSL_Value *NXSL_NodeClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
91 {
92 Node *pNode;
93 NXSL_Value *pValue = NULL;
94 TCHAR szBuffer[256];
95
96 pNode = (Node *)pObject->getData();
97 if (!_tcscmp(pszAttr, _T("name")))
98 {
99 pValue = new NXSL_Value(pNode->Name());
100 }
101 else if (!_tcscmp(pszAttr, _T("id")))
102 {
103 pValue = new NXSL_Value(pNode->Id());
104 }
105 else if (!_tcscmp(pszAttr, _T("status")))
106 {
107 pValue = new NXSL_Value((LONG)pNode->Status());
108 }
109 else if (!_tcscmp(pszAttr, _T("ipAddr")))
110 {
111 IpToStr(pNode->IpAddr(), szBuffer);
112 pValue = new NXSL_Value(szBuffer);
113 }
114 else if (!_tcscmp(pszAttr, _T("isAgent")))
115 {
116 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_NATIVE_AGENT) ? 1 : 0));
117 }
118 else if (!_tcscmp(pszAttr, _T("isSNMP")))
119 {
120 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_SNMP) ? 1 : 0));
121 }
122 else if (!_tcscmp(pszAttr, _T("isBridge")))
123 {
124 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_BRIDGE) ? 1 : 0));
125 }
126 else if (!_tcscmp(pszAttr, _T("isRouter")))
127 {
128 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_ROUTER) ? 1 : 0));
129 }
130 else if (!_tcscmp(pszAttr, _T("isPrinter")))
131 {
132 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_PRINTER) ? 1 : 0));
133 }
134 else if (!_tcscmp(pszAttr, _T("isCDP")))
135 {
136 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_CDP) ? 1 : 0));
137 }
138 else if (!_tcscmp(pszAttr, _T("isSONMP")))
139 {
140 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_SONMP) ? 1 : 0));
141 }
142 else if (!_tcscmp(pszAttr, _T("isLLDP")))
143 {
144 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_LLDP) ? 1 : 0));
145 }
146 else if (!_tcscmp(pszAttr, _T("isPAE")) || !_tcscmp(pszAttr, _T("is802_1x")))
147 {
148 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_8021X) ? 1 : 0));
149 }
150 else if (!_tcscmp(pszAttr, _T("snmpVersion")))
151 {
152 pValue = new NXSL_Value((LONG)pNode->getSNMPVersion());
153 }
154 else if (!_tcscmp(pszAttr, _T("snmpOID")))
155 {
156 pValue = new NXSL_Value(pNode->getSNMPObjectId());
157 }
158 else if (!_tcscmp(pszAttr, _T("agentVersion")))
159 {
160 pValue = new NXSL_Value(pNode->getAgentVersion());
161 }
162 else if (!_tcscmp(pszAttr, _T("platformName")))
163 {
164 pValue = new NXSL_Value(pNode->getPlatformName());
165 }
166 else if (!_tcscmp(pszAttr, _T("snmpSysName")))
167 {
168 pValue = new NXSL_Value(pNode->getSysName());
169 }
170 else if (!_tcscmp(pszAttr, _T("sysDescription")))
171 {
172 pValue = new NXSL_Value(pNode->getSysDescription());
173 }
174 else if (!_tcscmp(pszAttr, _T("comments")))
175 {
176 pValue = new NXSL_Value(pNode->getComments());
177 }
178 else
179 {
180 const TCHAR *attrValue = pNode->GetCustomAttribute(pszAttr);
181 if (attrValue != NULL)
182 {
183 pValue = new NXSL_Value(attrValue);
184 }
185 }
186 return pValue;
187 }
188
189
190 //
191 // Implementation of "NetXMS interface" class
192 //
193
194 NXSL_InterfaceClass::NXSL_InterfaceClass() : NXSL_Class()
195 {
196 _tcscpy(m_szName, _T("Interface"));
197 }
198
199 NXSL_Value *NXSL_InterfaceClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
200 {
201 Interface *iface;
202 NXSL_Value *pValue = NULL;
203 TCHAR szBuffer[256];
204
205 iface = (Interface *)pObject->getData();
206 if (!_tcscmp(pszAttr, _T("name")))
207 {
208 pValue = new NXSL_Value(iface->Name());
209 }
210 else if (!_tcscmp(pszAttr, _T("id")))
211 {
212 pValue = new NXSL_Value(iface->Id());
213 }
214 else if (!_tcscmp(pszAttr, _T("status")))
215 {
216 pValue = new NXSL_Value((LONG)iface->Status());
217 }
218 else if (!_tcscmp(pszAttr, _T("ipAddr")))
219 {
220 IpToStr(iface->IpAddr(), szBuffer);
221 pValue = new NXSL_Value(szBuffer);
222 }
223 else if (!_tcscmp(pszAttr, _T("ipNetMask")))
224 {
225 IpToStr(iface->getIpNetMask(), szBuffer);
226 pValue = new NXSL_Value(szBuffer);
227 }
228 else if (!_tcscmp(pszAttr, _T("isLoopback")))
229 {
230 pValue = new NXSL_Value((LONG)(iface->isLoopback() ? 1 : 0));
231 }
232 else if (!_tcscmp(pszAttr, _T("isPhysicalPort")))
233 {
234 pValue = new NXSL_Value((LONG)(iface->isPhysicalPort() ? 1 : 0));
235 }
236 else if (!_tcscmp(pszAttr, _T("isManuallyCreated")))
237 {
238 pValue = new NXSL_Value((LONG)(iface->isManuallyCreated() ? 1 : 0));
239 }
240 else if (!_tcscmp(pszAttr, _T("isExcludedFromTopology")))
241 {
242 pValue = new NXSL_Value((LONG)(iface->isExcludedFromTopology() ? 1 : 0));
243 }
244 else if (!_tcscmp(pszAttr, _T("description")))
245 {
246 pValue = new NXSL_Value(iface->getDescription());
247 }
248 else if (!_tcscmp(pszAttr, _T("comments")))
249 {
250 pValue = new NXSL_Value(iface->getComments());
251 }
252 else if (!_tcscmp(pszAttr, _T("ifIndex")))
253 {
254 pValue = new NXSL_Value(iface->getIfIndex());
255 }
256 else if (!_tcscmp(pszAttr, _T("ifType")))
257 {
258 pValue = new NXSL_Value(iface->getIfType());
259 }
260 else if (!_tcscmp(pszAttr, _T("bridgePortNumber")))
261 {
262 pValue = new NXSL_Value(iface->getBridgePortNumber());
263 }
264 else
265 {
266 const TCHAR *attrValue = iface->GetCustomAttribute(pszAttr);
267 if (attrValue != NULL)
268 {
269 pValue = new NXSL_Value(attrValue);
270 }
271 }
272 return pValue;
273 }
274
275
276 //
277 // Implementation of "NetXMS event" class
278 //
279
280 NXSL_EventClass::NXSL_EventClass() : NXSL_Class()
281 {
282 _tcscpy(m_szName, _T("Event"));
283 }
284
285 NXSL_Value *NXSL_EventClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
286 {
287 Event *event;
288 NXSL_Value *value = NULL;
289
290 event = (Event *)pObject->getData();
291 if (!_tcscmp(pszAttr, _T("code")))
292 {
293 value = new NXSL_Value(event->getCode());
294 }
295 else if (!_tcscmp(pszAttr, _T("name")))
296 {
297 value = new NXSL_Value(event->getName());
298 }
299 else if (!_tcscmp(pszAttr, _T("id")))
300 {
301 value = new NXSL_Value(event->getId());
302 }
303 else if (!_tcscmp(pszAttr, _T("severity")))
304 {
305 value = new NXSL_Value(event->getSeverity());
306 }
307 else if (!_tcscmp(pszAttr, _T("timestamp")))
308 {
309 value = new NXSL_Value((INT64)event->getTimeStamp());
310 }
311 else if (!_tcscmp(pszAttr, _T("message")))
312 {
313 value = new NXSL_Value(event->getMessage());
314 }
315 else if (!_tcscmp(pszAttr, _T("customMessage")))
316 {
317 value = new NXSL_Value(event->getCustomMessage());
318 }
319 else if (!_tcscmp(pszAttr, _T("userTag")))
320 {
321 value = new NXSL_Value(event->getUserTag());
322 }
323 else if (!_tcscmp(pszAttr, _T("parameters")))
324 {
325 NXSL_Array *array = new NXSL_Array;
326 DWORD i;
327
328 for(i = 0; i < event->getParametersCount(); i++)
329 array->set((int)(i + 1), new NXSL_Value(event->getParameter(i)));
330 value = new NXSL_Value(array);
331 }
332 return value;
333 }
334
335
336 //
337 // Implementation of "DCI" class
338 //
339
340 NXSL_DciClass::NXSL_DciClass()
341 :NXSL_Class()
342 {
343 _tcscpy(m_szName, _T("DCI"));
344 }
345
346 NXSL_Value *NXSL_DciClass::getAttr(NXSL_Object *object, const TCHAR *attr)
347 {
348 DCItem *dci;
349 NXSL_Value *value = NULL;
350
351 dci = (DCItem *)object->getData();
352 if (!_tcscmp(attr, _T("id")))
353 {
354 value = new NXSL_Value(dci->getId());
355 }
356 else if (!_tcscmp(attr, _T("name")))
357 {
358 value = new NXSL_Value(dci->getName());
359 }
360 else if (!_tcscmp(attr, _T("description")))
361 {
362 value = new NXSL_Value(dci->getDescription());
363 }
364 else if (!_tcscmp(attr, _T("origin")))
365 {
366 value = new NXSL_Value((LONG)dci->getDataSource());
367 }
368 else if (!_tcscmp(attr, _T("dataType")))
369 {
370 value = new NXSL_Value((LONG)dci->getDataType());
371 }
372 else if (!_tcscmp(attr, _T("status")))
373 {
374 value = new NXSL_Value((LONG)dci->getStatus());
375 }
376 else if (!_tcscmp(attr, _T("errorCount")))
377 {
378 value = new NXSL_Value(dci->getErrorCount());
379 }
380 else if (!_tcscmp(attr, _T("lastPollTime")))
381 {
382 value = new NXSL_Value((INT64)dci->getLastPollTime());
383 }
384 else if (!_tcscmp(attr, _T("systemTag")))
385 {
386 value = new NXSL_Value(dci->getSystemTag());
387 }
388 return value;
389 }
390
391
392 //
393 // Class objects
394 //
395
396 NXSL_NetObjClass g_nxslNetObjClass;
397 NXSL_NodeClass g_nxslNodeClass;
398 NXSL_InterfaceClass g_nxslInterfaceClass;
399 NXSL_EventClass g_nxslEventClass;
400 NXSL_DciClass g_nxslDciClass;