6c62672687e2d641e8b41a8fa6ed25c5bfc55ee3
[public/netxms.git] / src / server / core / nxsl_classes.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2014 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 * setStatusCalculation(type, ...)
27 */
28 NXSL_METHOD_DEFINITION(setStatusCalculation)
29 {
30 if (argc < 1)
31 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
32
33 if (!argv[0]->isInteger())
34 return NXSL_ERR_NOT_INTEGER;
35
36 INT32 success = 1;
37 int method = argv[0]->getValueAsInt32();
38 NetObj *netobj = (NetObj *)object->getData();
39 switch(method)
40 {
41 case SA_CALCULATE_DEFAULT:
42 case SA_CALCULATE_MOST_CRITICAL:
43 netobj->setStatusCalculation(method);
44 break;
45 case SA_CALCULATE_SINGLE_THRESHOLD:
46 if (argc < 2)
47 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
48 if (!argv[1]->isInteger())
49 return NXSL_ERR_NOT_INTEGER;
50 netobj->setStatusCalculation(method, argv[1]->getValueAsInt32());
51 break;
52 case SA_CALCULATE_MULTIPLE_THRESHOLDS:
53 if (argc < 5)
54 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
55 for(int i = 1; i <= 4; i++)
56 {
57 if (!argv[i]->isInteger())
58 return NXSL_ERR_NOT_INTEGER;
59 }
60 netobj->setStatusCalculation(method, argv[1]->getValueAsInt32(), argv[2]->getValueAsInt32(), argv[3]->getValueAsInt32(), argv[4]->getValueAsInt32());
61 break;
62 default:
63 success = 0; // invalid method
64 break;
65 }
66 *result = new NXSL_Value(success);
67 return 0;
68 }
69
70 /**
71 * setStatusPropagation(type, ...)
72 */
73 NXSL_METHOD_DEFINITION(setStatusPropagation)
74 {
75 if (argc < 1)
76 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
77
78 if (!argv[0]->isInteger())
79 return NXSL_ERR_NOT_INTEGER;
80
81 INT32 success = 1;
82 int method = argv[0]->getValueAsInt32();
83 NetObj *netobj = (NetObj *)object->getData();
84 switch(method)
85 {
86 case SA_PROPAGATE_DEFAULT:
87 case SA_PROPAGATE_UNCHANGED:
88 netobj->setStatusPropagation(method);
89 break;
90 case SA_PROPAGATE_FIXED:
91 case SA_PROPAGATE_RELATIVE:
92 if (argc < 2)
93 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
94 if (!argv[1]->isInteger())
95 return NXSL_ERR_NOT_INTEGER;
96 netobj->setStatusPropagation(method, argv[1]->getValueAsInt32());
97 break;
98 case SA_PROPAGATE_TRANSLATED:
99 if (argc < 5)
100 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
101 for(int i = 1; i <= 4; i++)
102 {
103 if (!argv[i]->isInteger())
104 return NXSL_ERR_NOT_INTEGER;
105 }
106 netobj->setStatusPropagation(method, argv[1]->getValueAsInt32(), argv[2]->getValueAsInt32(), argv[3]->getValueAsInt32(), argv[4]->getValueAsInt32());
107 break;
108 default:
109 success = 0; // invalid method
110 break;
111 }
112 *result = new NXSL_Value(success);
113 return 0;
114 }
115
116 /**
117 * NXSL class NetObj: constructor
118 */
119 NXSL_NetObjClass::NXSL_NetObjClass() : NXSL_Class()
120 {
121 _tcscpy(m_name, _T("NetObj"));
122
123 NXSL_REGISTER_METHOD(setStatusCalculation, -1);
124 NXSL_REGISTER_METHOD(setStatusPropagation, -1);
125 }
126
127 /**
128 * NXSL class NetObj: get attribute
129 */
130 NXSL_Value *NXSL_NetObjClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
131 {
132 NXSL_Value *pValue = NULL;
133 NetObj *object = (NetObj *)pObject->getData();
134 if (!_tcscmp(pszAttr, _T("name")))
135 {
136 pValue = new NXSL_Value(object->getName());
137 }
138 else if (!_tcscmp(pszAttr, _T("id")))
139 {
140 pValue = new NXSL_Value(object->getId());
141 }
142 else if (!_tcscmp(pszAttr, _T("guid")))
143 {
144 uuid_t guid;
145 object->getGuid(guid);
146 TCHAR buffer[128];
147 pValue = new NXSL_Value(uuid_to_string(guid, buffer));
148 }
149 else if (!_tcscmp(pszAttr, _T("status")))
150 {
151 pValue = new NXSL_Value((LONG)object->Status());
152 }
153 else if (!_tcscmp(pszAttr, _T("ipAddr")))
154 {
155 TCHAR buffer[64];
156 GetObjectIpAddress(object).toString(buffer);
157 pValue = new NXSL_Value(buffer);
158 }
159 else if (!_tcscmp(pszAttr, _T("type")))
160 {
161 pValue = new NXSL_Value((LONG)object->getObjectClass());
162 }
163 else if (!_tcscmp(pszAttr, _T("comments")))
164 {
165 pValue = new NXSL_Value(object->getComments());
166 }
167 else if (!_tcscmp(pszAttr, _T("country")))
168 {
169 pValue = new NXSL_Value(object->getPostalAddress()->getCountry());
170 }
171 else if (!_tcscmp(pszAttr, _T("city")))
172 {
173 pValue = new NXSL_Value(object->getPostalAddress()->getCity());
174 }
175 else if (!_tcscmp(pszAttr, _T("streetAddress")))
176 {
177 pValue = new NXSL_Value(object->getPostalAddress()->getStreetAddress());
178 }
179 else if (!_tcscmp(pszAttr, _T("postcode")))
180 {
181 pValue = new NXSL_Value(object->getPostalAddress()->getPostCode());
182 }
183 else
184 {
185 const TCHAR *attrValue = object->getCustomAttribute(pszAttr);
186 if (attrValue != NULL)
187 {
188 pValue = new NXSL_Value(attrValue);
189 }
190 }
191 return pValue;
192 }
193
194 /**
195 * NXSL class Zone: constructor
196 */
197 NXSL_ZoneClass::NXSL_ZoneClass() : NXSL_Class()
198 {
199 _tcscpy(m_name, _T("Zone"));
200
201 NXSL_REGISTER_METHOD(setStatusCalculation, -1);
202 NXSL_REGISTER_METHOD(setStatusPropagation, -1);
203 }
204
205 /**
206 * NXSL class Zone: get attribute
207 */
208 NXSL_Value *NXSL_ZoneClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
209 {
210 NXSL_Value *pValue = NULL;
211 Zone *zone = (Zone *)pObject->getData();
212 if (!_tcscmp(pszAttr, _T("agentProxy")))
213 {
214 pValue = new NXSL_Value(zone->getAgentProxy());
215 }
216 else if (!_tcscmp(pszAttr, _T("city")))
217 {
218 pValue = new NXSL_Value(zone->getPostalAddress()->getCity());
219 }
220 else if (!_tcscmp(pszAttr, _T("comments")))
221 {
222 pValue = new NXSL_Value(zone->getComments());
223 }
224 else if (!_tcscmp(pszAttr, _T("country")))
225 {
226 pValue = new NXSL_Value(zone->getPostalAddress()->getCountry());
227 }
228 else if (!_tcscmp(pszAttr, _T("guid")))
229 {
230 uuid_t guid;
231 zone->getGuid(guid);
232 TCHAR buffer[128];
233 pValue = new NXSL_Value(uuid_to_string(guid, buffer));
234 }
235 else if (!_tcscmp(pszAttr, _T("icmpProxy")))
236 {
237 pValue = new NXSL_Value(zone->getIcmpProxy());
238 }
239 else if (!_tcscmp(pszAttr, _T("id")))
240 {
241 pValue = new NXSL_Value(zone->getId());
242 }
243 else if (!_tcscmp(pszAttr, _T("name")))
244 {
245 pValue = new NXSL_Value(zone->getName());
246 }
247 else if (!_tcscmp(pszAttr, _T("postcode")))
248 {
249 pValue = new NXSL_Value(zone->getPostalAddress()->getPostCode());
250 }
251 else if (!_tcscmp(pszAttr, _T("snmpProxy")))
252 {
253 pValue = new NXSL_Value(zone->getSnmpProxy());
254 }
255 else if (!_tcscmp(pszAttr, _T("status")))
256 {
257 pValue = new NXSL_Value((LONG)zone->Status());
258 }
259 else if (!_tcscmp(pszAttr, _T("streetAddress")))
260 {
261 pValue = new NXSL_Value(zone->getPostalAddress()->getStreetAddress());
262 }
263 else if (!_tcscmp(pszAttr, _T("zoneId")))
264 {
265 pValue = new NXSL_Value(zone->getZoneId());
266 }
267 else
268 {
269 const TCHAR *attrValue = zone->getCustomAttribute(pszAttr);
270 if (attrValue != NULL)
271 {
272 pValue = new NXSL_Value(attrValue);
273 }
274 }
275 return pValue;
276 }
277
278 /**
279 * Generic implementation for flag changing methods
280 */
281 static int ChangeFlagMethod(NXSL_Object *object, NXSL_Value *arg, NXSL_Value **result, UINT32 flag)
282 {
283 if (!arg->isInteger())
284 return NXSL_ERR_NOT_INTEGER;
285
286 Node *node = (Node *)object->getData();
287 if (arg->getValueAsInt32())
288 node->clearFlag(flag);
289 else
290 node->setFlag(flag);
291
292 *result = new NXSL_Value;
293 return 0;
294 }
295
296 /**
297 * enableAgent(enabled) method
298 */
299 NXSL_METHOD_DEFINITION(enableAgent)
300 {
301 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_NXCP);
302 }
303
304 /**
305 * enableConfigurationPolling(enabled) method
306 */
307 NXSL_METHOD_DEFINITION(enableConfigurationPolling)
308 {
309 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_CONF_POLL);
310 }
311
312 /**
313 * enableIcmp(enabled) method
314 */
315 NXSL_METHOD_DEFINITION(enableIcmp)
316 {
317 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_ICMP);
318 }
319
320 /**
321 * enableSnmp(enabled) method
322 */
323 NXSL_METHOD_DEFINITION(enableSnmp)
324 {
325 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_SNMP);
326 }
327
328 /**
329 * enableStatusPolling(enabled) method
330 */
331 NXSL_METHOD_DEFINITION(enableStatusPolling)
332 {
333 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_STATUS_POLL);
334 }
335
336 /**
337 * enableTopologyPolling(enabled) method
338 */
339 NXSL_METHOD_DEFINITION(enableTopologyPolling)
340 {
341 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_TOPOLOGY_POLL);
342 }
343
344 /**
345 * NXSL class Node: constructor
346 */
347 NXSL_NodeClass::NXSL_NodeClass() : NXSL_Class()
348 {
349 _tcscpy(m_name, _T("Node"));
350
351 NXSL_REGISTER_METHOD(enableAgent, 1);
352 NXSL_REGISTER_METHOD(enableConfigurationPolling, 1);
353 NXSL_REGISTER_METHOD(enableIcmp, 1);
354 NXSL_REGISTER_METHOD(enableSnmp, 1);
355 NXSL_REGISTER_METHOD(enableStatusPolling, 1);
356 NXSL_REGISTER_METHOD(enableTopologyPolling, 1);
357 NXSL_REGISTER_METHOD(setStatusCalculation, -1);
358 NXSL_REGISTER_METHOD(setStatusPropagation, -1);
359 }
360
361 /**
362 * NXSL class Node: get attribute
363 */
364 NXSL_Value *NXSL_NodeClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
365 {
366 Node *pNode;
367 NXSL_Value *pValue = NULL;
368
369 pNode = (Node *)pObject->getData();
370 if (!_tcscmp(pszAttr, _T("agentVersion")))
371 {
372 pValue = new NXSL_Value(pNode->getAgentVersion());
373 }
374 else if (!_tcscmp(pszAttr, _T("bootTime")))
375 {
376 pValue = new NXSL_Value((INT64)pNode->getBootTime());
377 }
378 else if (!_tcscmp(pszAttr, _T("city")))
379 {
380 pValue = new NXSL_Value(pNode->getPostalAddress()->getCity());
381 }
382 else if (!_tcscmp(pszAttr, _T("comments")))
383 {
384 pValue = new NXSL_Value(pNode->getComments());
385 }
386 else if (!_tcscmp(pszAttr, _T("country")))
387 {
388 pValue = new NXSL_Value(pNode->getPostalAddress()->getCountry());
389 }
390 else if (!_tcscmp(pszAttr, _T("driver")))
391 {
392 pValue = new NXSL_Value(pNode->getDriverName());
393 }
394 else if (!_tcscmp(pszAttr, _T("flags")))
395 {
396 pValue = new NXSL_Value(pNode->getFlags());
397 }
398 else if (!_tcscmp(pszAttr, _T("guid")))
399 {
400 uuid_t guid;
401 pNode->getGuid(guid);
402 TCHAR buffer[128];
403 pValue = new NXSL_Value(uuid_to_string(guid, buffer));
404 }
405 else if (!_tcscmp(pszAttr, _T("id")))
406 {
407 pValue = new NXSL_Value(pNode->getId());
408 }
409 else if (!_tcscmp(pszAttr, _T("ipAddr")))
410 {
411 TCHAR buffer[64];
412 pNode->getIpAddress().toString(buffer);
413 pValue = new NXSL_Value(buffer);
414 }
415 else if (!_tcscmp(pszAttr, _T("isAgent")))
416 {
417 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_NATIVE_AGENT) ? 1 : 0));
418 }
419 else if (!_tcscmp(pszAttr, _T("isBridge")))
420 {
421 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_BRIDGE) ? 1 : 0));
422 }
423 else if (!_tcscmp(pszAttr, _T("isCDP")))
424 {
425 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_CDP) ? 1 : 0));
426 }
427 else if (!_tcscmp(pszAttr, _T("isLLDP")))
428 {
429 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_LLDP) ? 1 : 0));
430 }
431 else if (!_tcscmp(pszAttr, _T("isLocalMgmt")) || !_tcscmp(pszAttr, _T("isLocalManagement")))
432 {
433 pValue = new NXSL_Value((LONG)((pNode->isLocalManagement()) ? 1 : 0));
434 }
435 else if (!_tcscmp(pszAttr, _T("isPAE")) || !_tcscmp(pszAttr, _T("is802_1x")))
436 {
437 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_8021X) ? 1 : 0));
438 }
439 else if (!_tcscmp(pszAttr, _T("isPrinter")))
440 {
441 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_PRINTER) ? 1 : 0));
442 }
443 else if (!_tcscmp(pszAttr, _T("isRouter")))
444 {
445 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_ROUTER) ? 1 : 0));
446 }
447 else if (!_tcscmp(pszAttr, _T("isSMCLP")))
448 {
449 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_SMCLP) ? 1 : 0));
450 }
451 else if (!_tcscmp(pszAttr, _T("isSNMP")))
452 {
453 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_SNMP) ? 1 : 0));
454 }
455 else if (!_tcscmp(pszAttr, _T("isSONMP")))
456 {
457 pValue = new NXSL_Value((LONG)((pNode->getFlags() & NF_IS_SONMP) ? 1 : 0));
458 }
459 else if (!_tcscmp(pszAttr, _T("name")))
460 {
461 pValue = new NXSL_Value(pNode->getName());
462 }
463 else if (!_tcscmp(pszAttr, _T("platformName")))
464 {
465 pValue = new NXSL_Value(pNode->getPlatformName());
466 }
467 else if (!_tcscmp(pszAttr, _T("postcode")))
468 {
469 pValue = new NXSL_Value(pNode->getPostalAddress()->getPostCode());
470 }
471 else if (!_tcscmp(pszAttr, _T("runtimeFlags")))
472 {
473 pValue = new NXSL_Value(pNode->getRuntimeFlags());
474 }
475 else if (!_tcscmp(pszAttr, _T("snmpOID")))
476 {
477 pValue = new NXSL_Value(pNode->getSNMPObjectId());
478 }
479 else if (!_tcscmp(pszAttr, _T("snmpSysName")))
480 {
481 pValue = new NXSL_Value(pNode->getSysName());
482 }
483 else if (!_tcscmp(pszAttr, _T("snmpVersion")))
484 {
485 pValue = new NXSL_Value((LONG)pNode->getSNMPVersion());
486 }
487 else if (!_tcscmp(pszAttr, _T("status")))
488 {
489 pValue = new NXSL_Value((LONG)pNode->Status());
490 }
491 else if (!_tcscmp(pszAttr, _T("streetAddress")))
492 {
493 pValue = new NXSL_Value(pNode->getPostalAddress()->getStreetAddress());
494 }
495 else if (!_tcscmp(pszAttr, _T("sysDescription")))
496 {
497 pValue = new NXSL_Value(pNode->getSysDescription());
498 }
499 else if (!_tcscmp(pszAttr, _T("zone")))
500 {
501 if (g_flags & AF_ENABLE_ZONING)
502 {
503 Zone *zone = FindZoneByGUID(pNode->getZoneId());
504 if (zone != NULL)
505 {
506 pValue = new NXSL_Value(new NXSL_Object(&g_nxslZoneClass, zone));
507 }
508 else
509 {
510 pValue = new NXSL_Value;
511 }
512 }
513 else
514 {
515 pValue = new NXSL_Value;
516 }
517 }
518 else if (!_tcscmp(pszAttr, _T("zoneId")))
519 {
520 pValue = new NXSL_Value(pNode->getZoneId());
521 }
522 else
523 {
524 const TCHAR *attrValue = pNode->getCustomAttribute(pszAttr);
525 if (attrValue != NULL)
526 {
527 pValue = new NXSL_Value(attrValue);
528 }
529 }
530 return pValue;
531 }
532
533 /**
534 * NXSL class Interface: constructor
535 */
536 NXSL_InterfaceClass::NXSL_InterfaceClass() : NXSL_Class()
537 {
538 _tcscpy(m_name, _T("Interface"));
539
540 NXSL_REGISTER_METHOD(setStatusCalculation, -1);
541 NXSL_REGISTER_METHOD(setStatusPropagation, -1);
542 }
543
544 /**
545 * NXSL class Interface: get attribute
546 */
547 NXSL_Value *NXSL_InterfaceClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
548 {
549 Interface *iface;
550 NXSL_Value *pValue = NULL;
551
552 iface = (Interface *)pObject->getData();
553 if (!_tcscmp(pszAttr, _T("adminState")))
554 {
555 pValue = new NXSL_Value((LONG)iface->getAdminState());
556 }
557 else if (!_tcscmp(pszAttr, _T("alias")))
558 {
559 pValue = new NXSL_Value(iface->getAlias());
560 }
561 else if (!_tcscmp(pszAttr, _T("bridgePortNumber")))
562 {
563 pValue = new NXSL_Value(iface->getBridgePortNumber());
564 }
565 else if (!_tcscmp(pszAttr, _T("comments")))
566 {
567 pValue = new NXSL_Value(iface->getComments());
568 }
569 else if (!_tcscmp(pszAttr, _T("description")))
570 {
571 pValue = new NXSL_Value(iface->getDescription());
572 }
573 else if (!_tcscmp(pszAttr, _T("dot1xBackendAuthState")))
574 {
575 pValue = new NXSL_Value((LONG)iface->getDot1xBackendAuthState());
576 }
577 else if (!_tcscmp(pszAttr, _T("dot1xPaeAuthState")))
578 {
579 pValue = new NXSL_Value((LONG)iface->getDot1xPaeAuthState());
580 }
581 else if (!_tcscmp(pszAttr, _T("expectedState")))
582 {
583 pValue = new NXSL_Value((iface->getFlags() & IF_EXPECTED_STATE_MASK) >> 28);
584 }
585 else if (!_tcscmp(pszAttr, _T("flags")))
586 {
587 pValue = new NXSL_Value(iface->getFlags());
588 }
589 else if (!_tcscmp(pszAttr, _T("guid")))
590 {
591 uuid_t guid;
592 iface->getGuid(guid);
593 TCHAR buffer[128];
594 pValue = new NXSL_Value(uuid_to_string(guid, buffer));
595 }
596 else if (!_tcscmp(pszAttr, _T("id")))
597 {
598 pValue = new NXSL_Value(iface->getId());
599 }
600 else if (!_tcscmp(pszAttr, _T("ifIndex")))
601 {
602 pValue = new NXSL_Value(iface->getIfIndex());
603 }
604 else if (!_tcscmp(pszAttr, _T("ifType")))
605 {
606 pValue = new NXSL_Value(iface->getIfType());
607 }
608 else if (!_tcscmp(pszAttr, _T("ipAddr")))
609 {
610 TCHAR buffer[64];
611 iface->getIpAddressList()->getFirstUnicastAddress().toString(buffer);
612 pValue = new NXSL_Value(buffer);
613 }
614 else if (!_tcscmp(pszAttr, _T("ipNetMask")))
615 {
616 pValue = new NXSL_Value(iface->getIpAddressList()->getFirstUnicastAddress().getMaskBits());
617 }
618 else if (!_tcscmp(pszAttr, _T("isExcludedFromTopology")))
619 {
620 pValue = new NXSL_Value((LONG)(iface->isExcludedFromTopology() ? 1 : 0));
621 }
622 else if (!_tcscmp(pszAttr, _T("isLoopback")))
623 {
624 pValue = new NXSL_Value((LONG)(iface->isLoopback() ? 1 : 0));
625 }
626 else if (!_tcscmp(pszAttr, _T("isManuallyCreated")))
627 {
628 pValue = new NXSL_Value((LONG)(iface->isManuallyCreated() ? 1 : 0));
629 }
630 else if (!_tcscmp(pszAttr, _T("isPhysicalPort")))
631 {
632 pValue = new NXSL_Value((LONG)(iface->isPhysicalPort() ? 1 : 0));
633 }
634 else if (!_tcscmp(pszAttr, _T("macAddr")))
635 {
636 TCHAR buffer[256];
637 pValue = new NXSL_Value(BinToStr(iface->getMacAddr(), MAC_ADDR_LENGTH, buffer));
638 }
639 else if (!_tcscmp(pszAttr, _T("mtu")))
640 {
641 pValue = new NXSL_Value(iface->getMTU());
642 }
643 else if (!_tcscmp(pszAttr, _T("name")))
644 {
645 pValue = new NXSL_Value(iface->getName());
646 }
647 else if (!_tcscmp(pszAttr, _T("node")))
648 {
649 Node *parentNode = iface->getParentNode();
650 if (parentNode != NULL)
651 {
652 pValue = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, parentNode));
653 }
654 else
655 {
656 pValue = new NXSL_Value;
657 }
658 }
659 else if (!_tcscmp(pszAttr, _T("operState")))
660 {
661 pValue = new NXSL_Value((LONG)iface->getOperState());
662 }
663 else if (!_tcscmp(pszAttr, _T("peerInterface")))
664 {
665 Interface *peerIface = (Interface *)FindObjectById(iface->getPeerInterfaceId(), OBJECT_INTERFACE);
666 if (peerIface != NULL)
667 {
668 if (g_flags & AF_CHECK_TRUSTED_NODES)
669 {
670 Node *parentNode = iface->getParentNode();
671 Node *peerNode = peerIface->getParentNode();
672 if ((parentNode != NULL) && (peerNode != NULL))
673 {
674 if (peerNode->isTrustedNode(parentNode->getId()))
675 {
676 pValue = new NXSL_Value(new NXSL_Object(&g_nxslInterfaceClass, peerIface));
677 }
678 else
679 {
680 // No access, return null
681 pValue = new NXSL_Value;
682 DbgPrintf(4, _T("NXSL::Interface::peerInterface(%s [%d]): access denied for node %s [%d]"),
683 iface->getName(), iface->getId(), peerNode->getName(), peerNode->getId());
684 }
685 }
686 else
687 {
688 pValue = new NXSL_Value;
689 DbgPrintf(4, _T("NXSL::Interface::peerInterface(%s [%d]): parentNode=%p peerNode=%p"), iface->getName(), iface->getId(), parentNode, peerNode);
690 }
691 }
692 else
693 {
694 pValue = new NXSL_Value(new NXSL_Object(&g_nxslInterfaceClass, peerIface));
695 }
696 }
697 else
698 {
699 pValue = new NXSL_Value;
700 }
701 }
702 else if (!_tcscmp(pszAttr, _T("peerNode")))
703 {
704 Node *peerNode = (Node *)FindObjectById(iface->getPeerNodeId(), OBJECT_NODE);
705 if (peerNode != NULL)
706 {
707 if (g_flags & AF_CHECK_TRUSTED_NODES)
708 {
709 Node *parentNode = iface->getParentNode();
710 if ((parentNode != NULL) && (peerNode->isTrustedNode(parentNode->getId())))
711 {
712 pValue = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, peerNode));
713 }
714 else
715 {
716 // No access, return null
717 pValue = new NXSL_Value;
718 DbgPrintf(4, _T("NXSL::Interface::peerNode(%s [%d]): access denied for node %s [%d]"),
719 iface->getName(), iface->getId(), peerNode->getName(), peerNode->getId());
720 }
721 }
722 else
723 {
724 pValue = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, peerNode));
725 }
726 }
727 else
728 {
729 pValue = new NXSL_Value;
730 }
731 }
732 else if (!_tcscmp(pszAttr, _T("port")))
733 {
734 pValue = new NXSL_Value(iface->getPortNumber());
735 }
736 else if (!_tcscmp(pszAttr, _T("slot")))
737 {
738 pValue = new NXSL_Value(iface->getSlotNumber());
739 }
740 else if (!_tcscmp(pszAttr, _T("status")))
741 {
742 pValue = new NXSL_Value((LONG)iface->Status());
743 }
744 else if (!_tcscmp(pszAttr, _T("zone")))
745 {
746 if (g_flags & AF_ENABLE_ZONING)
747 {
748 Zone *zone = FindZoneByGUID(iface->getZoneId());
749 if (zone != NULL)
750 {
751 pValue = new NXSL_Value(new NXSL_Object(&g_nxslZoneClass, zone));
752 }
753 else
754 {
755 pValue = new NXSL_Value;
756 }
757 }
758 else
759 {
760 pValue = new NXSL_Value;
761 }
762 }
763 else if (!_tcscmp(pszAttr, _T("zoneId")))
764 {
765 pValue = new NXSL_Value(iface->getZoneId());
766 }
767 else
768 {
769 const TCHAR *attrValue = iface->getCustomAttribute(pszAttr);
770 if (attrValue != NULL)
771 {
772 pValue = new NXSL_Value(attrValue);
773 }
774 }
775 return pValue;
776 }
777
778 /**
779 * Event::setMessage() method
780 */
781 NXSL_METHOD_DEFINITION(setMessage)
782 {
783 if (!argv[0]->isString())
784 return NXSL_ERR_NOT_STRING;
785
786 Event *event = (Event *)object->getData();
787 event->setMessage(argv[0]->getValueAsCString());
788 *result = new NXSL_Value();
789 return 0;
790 }
791
792 /**
793 * Event::setSeverity() method
794 */
795 NXSL_METHOD_DEFINITION(setSeverity)
796 {
797 if (!argv[0]->isInteger())
798 return NXSL_ERR_NOT_STRING;
799
800 int s = argv[0]->getValueAsInt32();
801 if ((s >= SEVERITY_NORMAL) && (s <= SEVERITY_CRITICAL))
802 {
803 Event *event = (Event *)object->getData();
804 event->setSeverity(s);
805 }
806 *result = new NXSL_Value();
807 return 0;
808 }
809
810 /**
811 * Event::setUserTag() method
812 */
813 NXSL_METHOD_DEFINITION(setUserTag)
814 {
815 if (!argv[0]->isString())
816 return NXSL_ERR_NOT_STRING;
817
818 Event *event = (Event *)object->getData();
819 event->setUserTag(argv[0]->getValueAsCString());
820 *result = new NXSL_Value();
821 return 0;
822 }
823
824 /**
825 * NXSL class Event: constructor
826 */
827 NXSL_EventClass::NXSL_EventClass() : NXSL_Class()
828 {
829 _tcscpy(m_name, _T("Event"));
830
831 NXSL_REGISTER_METHOD(setMessage, 1);
832 NXSL_REGISTER_METHOD(setSeverity, 1);
833 NXSL_REGISTER_METHOD(setUserTag, 1);
834 }
835
836 /**
837 * NXSL class Event: get attribute
838 */
839 NXSL_Value *NXSL_EventClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
840 {
841 NXSL_Value *value = NULL;
842
843 Event *event = (Event *)pObject->getData();
844 if (!_tcscmp(pszAttr, _T("code")))
845 {
846 value = new NXSL_Value(event->getCode());
847 }
848 else if (!_tcscmp(pszAttr, _T("name")))
849 {
850 value = new NXSL_Value(event->getName());
851 }
852 else if (!_tcscmp(pszAttr, _T("id")))
853 {
854 value = new NXSL_Value(event->getId());
855 }
856 else if (!_tcscmp(pszAttr, _T("severity")))
857 {
858 value = new NXSL_Value(event->getSeverity());
859 }
860 else if (!_tcscmp(pszAttr, _T("timestamp")))
861 {
862 value = new NXSL_Value((INT64)event->getTimeStamp());
863 }
864 else if (!_tcscmp(pszAttr, _T("message")))
865 {
866 value = new NXSL_Value(event->getMessage());
867 }
868 else if (!_tcscmp(pszAttr, _T("customMessage")))
869 {
870 value = new NXSL_Value(event->getCustomMessage());
871 }
872 else if (!_tcscmp(pszAttr, _T("userTag")))
873 {
874 value = new NXSL_Value(event->getUserTag());
875 }
876 else if (!_tcscmp(pszAttr, _T("parameters")))
877 {
878 NXSL_Array *array = new NXSL_Array;
879 UINT32 i;
880
881 for(i = 0; i < event->getParametersCount(); i++)
882 array->set((int)(i + 1), new NXSL_Value(event->getParameter(i)));
883 value = new NXSL_Value(array);
884 }
885 return value;
886 }
887
888 /**
889 * Alarm::acknowledge() method
890 */
891 NXSL_METHOD_DEFINITION(acknowledge)
892 {
893 NXC_ALARM *alarm = (NXC_ALARM *)object->getData();
894 *result = new NXSL_Value(AckAlarmById(alarm->dwAlarmId, NULL, false, 0));
895 return 0;
896 }
897
898 /**
899 * Alarm::resolve() method
900 */
901 NXSL_METHOD_DEFINITION(resolve)
902 {
903 NXC_ALARM *alarm = (NXC_ALARM *)object->getData();
904 *result = new NXSL_Value(ResolveAlarmById(alarm->dwAlarmId, NULL, false));
905 return 0;
906 }
907
908 /**
909 * Alarm::terminate() method
910 */
911 NXSL_METHOD_DEFINITION(terminate)
912 {
913 NXC_ALARM *alarm = (NXC_ALARM *)object->getData();
914 *result = new NXSL_Value(ResolveAlarmById(alarm->dwAlarmId, NULL, true));
915 return 0;
916 }
917
918 /**
919 * NXSL class Alarm: constructor
920 */
921 NXSL_AlarmClass::NXSL_AlarmClass() : NXSL_Class()
922 {
923 _tcscpy(m_name, _T("Alarm"));
924
925 NXSL_REGISTER_METHOD(acknowledge, 0);
926 NXSL_REGISTER_METHOD(resolve, 0);
927 NXSL_REGISTER_METHOD(terminate, 0);
928 }
929
930 /**
931 * NXSL object destructor
932 */
933 void NXSL_AlarmClass::onObjectDelete(NXSL_Object *object)
934 {
935 free(object->getData());
936 }
937
938 /**
939 * NXSL class Alarm: get attribute
940 */
941 NXSL_Value *NXSL_AlarmClass::getAttr(NXSL_Object *pObject, const TCHAR *pszAttr)
942 {
943 NXSL_Value *value = NULL;
944 NXC_ALARM *alarm = (NXC_ALARM *)pObject->getData();
945
946 if (!_tcscmp(pszAttr, _T("ackBy")))
947 {
948 value = new NXSL_Value(alarm->dwAckByUser);
949 }
950 else if (!_tcscmp(pszAttr, _T("creationTime")))
951 {
952 value = new NXSL_Value(alarm->dwCreationTime);
953 }
954 else if (!_tcscmp(pszAttr, _T("eventCode")))
955 {
956 value = new NXSL_Value(alarm->dwSourceEventCode);
957 }
958 else if (!_tcscmp(pszAttr, _T("eventId")))
959 {
960 value = new NXSL_Value(alarm->qwSourceEventId);
961 }
962 else if (!_tcscmp(pszAttr, _T("helpdeskReference")))
963 {
964 value = new NXSL_Value(alarm->szHelpDeskRef);
965 }
966 else if (!_tcscmp(pszAttr, _T("helpdeskState")))
967 {
968 value = new NXSL_Value(alarm->nHelpDeskState);
969 }
970 else if (!_tcscmp(pszAttr, _T("id")))
971 {
972 value = new NXSL_Value(alarm->dwAlarmId);
973 }
974 else if (!_tcscmp(pszAttr, _T("key")))
975 {
976 value = new NXSL_Value(alarm->szKey);
977 }
978 else if (!_tcscmp(pszAttr, _T("lastChangeTime")))
979 {
980 value = new NXSL_Value(alarm->dwLastChangeTime);
981 }
982 else if (!_tcscmp(pszAttr, _T("message")))
983 {
984 value = new NXSL_Value(alarm->szMessage);
985 }
986 else if (!_tcscmp(pszAttr, _T("originalSeverity")))
987 {
988 value = new NXSL_Value(alarm->nOriginalSeverity);
989 }
990 else if (!_tcscmp(pszAttr, _T("repeatCount")))
991 {
992 value = new NXSL_Value(alarm->dwRepeatCount);
993 }
994 else if (!_tcscmp(pszAttr, _T("resolvedBy")))
995 {
996 value = new NXSL_Value(alarm->dwResolvedByUser);
997 }
998 else if (!_tcscmp(pszAttr, _T("severity")))
999 {
1000 value = new NXSL_Value(alarm->nCurrentSeverity);
1001 }
1002 else if (!_tcscmp(pszAttr, _T("sourceObject")))
1003 {
1004 value = new NXSL_Value(alarm->dwSourceObject);
1005 }
1006 else if (!_tcscmp(pszAttr, _T("state")))
1007 {
1008 value = new NXSL_Value(alarm->nState);
1009 }
1010 return value;
1011 }
1012
1013 /**
1014 * Implementation of "DCI" class: constructor
1015 */
1016 NXSL_DciClass::NXSL_DciClass() : NXSL_Class()
1017 {
1018 _tcscpy(m_name, _T("DCI"));
1019 }
1020
1021 /**
1022 * Implementation of "DCI" class: get attribute
1023 */
1024 NXSL_Value *NXSL_DciClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1025 {
1026 DCObject *dci;
1027 NXSL_Value *value = NULL;
1028
1029 dci = (DCObject *)object->getData();
1030 if (!_tcscmp(attr, _T("comments")))
1031 {
1032 value = new NXSL_Value(dci->getComments());
1033 }
1034 else if (!_tcscmp(attr, _T("dataType")) && (dci->getType() == DCO_TYPE_ITEM))
1035 {
1036 value = new NXSL_Value((LONG)((DCItem *)dci)->getDataType());
1037 }
1038 else if (!_tcscmp(attr, _T("description")))
1039 {
1040 value = new NXSL_Value(dci->getDescription());
1041 }
1042 else if (!_tcscmp(attr, _T("errorCount")))
1043 {
1044 value = new NXSL_Value(dci->getErrorCount());
1045 }
1046 else if (!_tcscmp(attr, _T("id")))
1047 {
1048 value = new NXSL_Value(dci->getId());
1049 }
1050 else if ((dci->getType() == DCO_TYPE_ITEM) && !_tcscmp(attr, _T("instance")))
1051 {
1052 value = new NXSL_Value(((DCItem *)dci)->getInstance());
1053 }
1054 else if (!_tcscmp(attr, _T("lastPollTime")))
1055 {
1056 value = new NXSL_Value((INT64)dci->getLastPollTime());
1057 }
1058 else if (!_tcscmp(attr, _T("name")))
1059 {
1060 value = new NXSL_Value(dci->getName());
1061 }
1062 else if (!_tcscmp(attr, _T("origin")))
1063 {
1064 value = new NXSL_Value((LONG)dci->getDataSource());
1065 }
1066 else if (!_tcscmp(attr, _T("status")))
1067 {
1068 value = new NXSL_Value((LONG)dci->getStatus());
1069 }
1070 else if (!_tcscmp(attr, _T("systemTag")))
1071 {
1072 value = new NXSL_Value(dci->getSystemTag());
1073 }
1074 else if (!_tcscmp(attr, _T("type")))
1075 {
1076 value = new NXSL_Value((LONG)dci->getType());
1077 }
1078 return value;
1079 }
1080
1081 /**
1082 * Implementation of "SNMP_Transport" class: constructor
1083 */
1084 NXSL_SNMPTransportClass::NXSL_SNMPTransportClass() : NXSL_Class()
1085 {
1086 _tcscpy(m_name, _T("SNMP_Transport"));
1087 }
1088
1089 /**
1090 * Implementation of "SNMP_Transport" class: get attribute
1091 */
1092 NXSL_Value *NXSL_SNMPTransportClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1093 {
1094 NXSL_Value *value = NULL;
1095 SNMP_Transport *t;
1096
1097 t = (SNMP_Transport*)object->getData();
1098 if (!_tcscmp(attr, _T("snmpVersion")))
1099 {
1100 const TCHAR *versions[] = { _T("1"), _T("2c"), _T("3") };
1101 value = new NXSL_Value((const TCHAR*)versions[t->getSnmpVersion()]);
1102 }
1103
1104 return value;
1105 }
1106
1107 /**
1108 * Implementation of "SNMP_Transport" class: NXSL object destructor
1109 */
1110 void NXSL_SNMPTransportClass::onObjectDelete(NXSL_Object *object)
1111 {
1112 delete (SNMP_Transport *)object->getData();
1113 }
1114
1115 /**
1116 * NXSL class SNMP_VarBind: constructor
1117 */
1118 NXSL_SNMPVarBindClass::NXSL_SNMPVarBindClass() : NXSL_Class()
1119 {
1120 _tcscpy(m_name, _T("SNMP_VarBind"));
1121 }
1122
1123 /**
1124 * NXSL class SNMP_VarBind: get attribute
1125 */
1126 NXSL_Value *NXSL_SNMPVarBindClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1127 {
1128 NXSL_Value *value = NULL;
1129 SNMP_Variable *t = (SNMP_Variable *)object->getData();
1130 if (!_tcscmp(attr, _T("type")))
1131 {
1132 value = new NXSL_Value((UINT32)t->getType());
1133 }
1134 else if (!_tcscmp(attr, _T("name")))
1135 {
1136 value = new NXSL_Value(t->getName()->getValueAsText());
1137 }
1138 else if (!_tcscmp(attr, _T("value")))
1139 {
1140 TCHAR strValue[1024];
1141 value = new NXSL_Value(t->getValueAsString(strValue, 1024));
1142 }
1143 else if (!_tcscmp(attr, _T("printableValue")))
1144 {
1145 TCHAR strValue[1024];
1146 bool convToHex = true;
1147 t->getValueAsPrintableString(strValue, 1024, &convToHex);
1148 value = new NXSL_Value(strValue);
1149 }
1150 else if (!_tcscmp(attr, _T("valueAsIp")))
1151 {
1152 TCHAR strValue[128];
1153 t->getValueAsIPAddr(strValue);
1154 value = new NXSL_Value(strValue);
1155 }
1156 else if (!_tcscmp(attr, _T("valueAsMac")))
1157 {
1158 TCHAR strValue[128];
1159 t->getValueAsMACAddr(strValue);
1160 value = new NXSL_Value(strValue);
1161 }
1162
1163 return value;
1164 }
1165
1166 /**
1167 * NXSL class SNMP_VarBind: NXSL object desctructor
1168 */
1169 void NXSL_SNMPVarBindClass::onObjectDelete(NXSL_Object *object)
1170 {
1171 delete (SNMP_Variable *)object->getData();
1172 }
1173
1174 /**
1175 * Class objects
1176 */
1177 NXSL_AlarmClass g_nxslAlarmClass;
1178 NXSL_DciClass g_nxslDciClass;
1179 NXSL_EventClass g_nxslEventClass;
1180 NXSL_InterfaceClass g_nxslInterfaceClass;
1181 NXSL_NetObjClass g_nxslNetObjClass;
1182 NXSL_NodeClass g_nxslNodeClass;
1183 NXSL_SNMPTransportClass g_nxslSnmpTransportClass;
1184 NXSL_SNMPVarBindClass g_nxslSnmpVarBindClass;
1185 NXSL_ZoneClass g_nxslZoneClass;