About auto add the interface dci source code
-
hi,victor:
I want to auto add 4 DCI for the interfaces when the server found the Node object first time.So I try to do it in server when the node object load the Templates "@System.Agent" first timeI define a function "ApplyInterfaceDCI()" to do it.
Its's source code is below.
It come true is like it: 1.Get the agent's interface list 2.Create interface's DCI with the interface index 3.Add the DCI to the Node object.void Node::ApplyInterfaceDCI() { //try to get the interface list,if try 100 times it also can not get it,it will break. InterfaceList *pIfList = NULL; int k=100; while (pIfList == NULL && k>0) { pIfList = getInterfaceList(); k--; } //set the DCI for all the interface if(pIfList != NULL) { for(int j = 0; j < pIfList->getSize(); j++) { DWORD ifIndex = 0; ifIndex = pIfList->get(j)->dwIndex; char parametrName[4][100]; sprintf(parametrName[0],_T("Net.Interface.BytesIn(%d)"),ifIndex); sprintf(parametrName[1],_T("Net.Interface.BytesOut(%d)"),ifIndex); sprintf(parametrName[2],_T("Net.Interface.PacketsIn(%d)"),ifIndex); sprintf(parametrName[3],_T("Net.Interface.PacketsOut(%d)"),ifIndex); //set DCI for one interface for (int i=0; i<=3; i++) { DCItem *pItem = NULL; pItem = new DCItem(CreateUniqueId(IDG_ITEM), parametrName[i], DS_NATIVE_AGENT, DCI_DT_INT, 60, 30, this,DCM_AVERAGE_PER_SECOND); pItem->setStatus(ITEM_STATUS_ACTIVE , false); if (pItem != NULL) { BOOL bSuccess; bSuccess = this->addItem(pItem); if (!bSuccess) { delete pItem; } } }//for }//for }//if if (pIfList != NULL) { delete pIfList; pIfList = NULL; } }There are a few question for my source code
1.BeforeI use " pIfList = getInterfaceList();" to get the interface list ,should I add a "lock" to avoid the source conflict?
2.When I use " pIfList = getInterfaceList();" to get the interface list , it may not success get the interface list sometimes, why? I have to try to call "getInterfaceList()" in maximum 100 time if it always fail, will it cause some potential problem?
3.When I use pItem = new DCItem() to create a DCI and add it success, I did not free that DCIiem object, will it cause memory leak?
4.Is my code have some potential fault? Please point to it,Thanks!Best wish!
-
Hi!
Better point for such function could be configuration poller. That way you will know that all communication settings are discovered, and getInterfaceList will not fail if node has an agent. Calling getInterfaceList before first configuration poll will always return error. This is why you get errors.
You should not call lock around getInterfaceList calls.
You don't need to delete DCItem if it was successfully added.
Otherwise code looks good, except that I would suggest add check that node actually has agent, because getInterfaceList will succeed if there are no agent, but node supports SNMP. But this depends on your setup, if you only have nodes with agents, then this is not necessary.
Best regards,
Victor -
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login