Hi  Jatin!

I don't know much about SNMP but your problem might come from your function 
temp.
What exactly is temp returning if popen or fscanf fails? There is no return 
statement in these pathes. So the function might return anything. Probably 0.

I would return a specific value, so that you can see that something went wrong. 
For example -1, as this is invalid in your value range (0-100).
And as far as I know, fscanf returns the number of successfully read items. 
That would be 1 in your case, so compare with ==1 instead of !=-1 (which should 
be EOF anyway).

And as I see in the other function, you are using snmp_log, so why don't use it 
in case of errors in the function temp?

Come to think of it, I didn't even know that functions and variables can have 
the same name (temp). Thinking a little bit more about it, I see that it should 
work. But even so, I wouldn't do that.

And I would do it a little bit my way:

int temp()
{
      int val=0;
      FILE *pipe = NULL;

      /* cpu.sh is a script which returns value between 0-100      */
      pipe=popen("/home/jatin/net-snmp-5.7.2.rc1/agent/mibgroup/cpu.sh","r");
      if(!pipe)
      {
            snmp_log(LOG_ERR, "popen failed in temp(), errno: %d\n", errno);
            return -1;
      }
      if( fscanf(pipe,"%d",&val) != 1)
      {
            snmp_log(LOG_ERR, "fscanf failed in temp() \n");
            return -1;
      }
      return val;
}

And just a tip: Turn on compiler warnings and  don't ignore them afterwards 
:-). Every more or less recent compiler I know would have issued a warning for 
not having a return in all pathes for a function that is not "void". Let the 
compiler help you :-).

I hope this helps
Walter

From: Jatin Bodarya [mailto:jatin.boda...@elitecore.com]
Sent: Mittwoch, 12. September 2012 08:49
To: Net-snmp-coders@lists.sourceforge.net
Subject: net-snmp agent problem

Hello everyone,

               I have generated a C code with mib2c tool, than implemented it 
according to my need. Everything is working fine .I have assigned values with 
system calls. These system call calling a shell script and getting output from 
there.  When I am using SNMPGET or WALK it gives correct values . I am running 
these scripts with infinite for( ; ; ) loop  to get the data. Bt when I am 
trying to  get these data with SNMPWALK  after  10-15 times  it is returning me 
all the values " 0 " .What may be  the reason behind it?  I want that it should 
return a correct value continuously, What methods or changes I can do ??
Please help me ..

Below are my functions..
Thanks in advance.

int 
handle_cpuPercentUsage(netsnmp_mib_handler*handler,netsnmp_handler_registration 
*reginfo,
            netsnmp_agent_request_info *reqinfo,netsnmp_request_info *requests) 
{

      for(;;)
      {
            int *dat=(int *)temp();       /// temp function for getting value
            switch(reqinfo->mode)
            {
                  case MODE_GET:
                              snmp_set_var_typed_value(requests->requestvb, 
ASN_INTEGER,
                                                      (u_char 
*)&dat,sizeof(dat));
                              break;
                  default:
                  snmp_log(LOG_ERR, "unknown mode (%d) in 
handle_cpuPercentUsage\n",
                                    reqinfo->mode );
                  return SNMP_ERR_GENERR;
            }
            return SNMP_ERR_NOERROR;
            sleep(1);
      }
}

int temp()
{
      int temp=0;
      FILE *pipe = NULL;

      /* cpu.sh is a script which returns value between 0-100      */
      pipe=popen("/home/jatin/net-snmp-5.7.2.rc1/agent/mibgroup/cpu.sh","r");
      if(pipe != NULL)
      {
            if( fscanf(pipe,"%d",&temp) != -1)
            {
                  return temp;
            }
      }
}


#####################################################################################
This message and any attachments are solely for the use of the intended 
recipients. They may contain privileged and/or confidential information or 
other information protected from disclosure. If you are not an intended 
recipient, you are hereby notified that you received this email in error and 
that any review, dissemination, distribution or copying of this email and any 
attachment is strictly prohibited. If you have received this email in error, 
please contact the sender and delete the message and any attachment from your 
system.
Thank You.
  ANDRITZ HYDRO GmbH
  Rechtsform/ Legal form: Gesellschaft mit beschränkter Haftung / Corporation
  Firmensitz/ Registered seat: Wien
  Firmenbuchgericht/ Court of registry: Handelsgericht Wien
  Firmenbuchnummer/ Company registration: FN 61833 g
  DVR: 0605077
  UID-Nr.: ATU14756806
#####################################################################################


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to