

#include<stdio.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<string.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"



#define MIB_BUFLEN 1024


// #define SNMP_DEBUG

// char * snmp_test(char *, char *, char *, char *, int *, int);


/* snmp_test() is get oid value                               */
/* 0:outdatabuf 1:objname 2:host or IP 3:send OID 4:return status(0>error) 5:out put option 0,1,2 */
/* opt = 0 -> true opt > 0 ->format checkd */
/* chk is returnd fomat check status. */
char * snmp_test(char * data, char * name, char * host, char * getoid, int * stat, int opt, int * chk, int timeout)
{
int ret;
char comm[BUF_LEN];
char * buf;
char * comdata = comm;
char * tmp;
char * compath = "/usr/local/zither/bin/snmp_command";

ret = 0;

/* init buf */
buf = (char *)calloc(MIB_BUFLEN, sizeof(char));
memset(comdata, 0x00, BUF_LEN);
data = safe_memcpy(data, "Not data Not used bufer!\n", BUF_MAX);

// $snmp_command -n <section_name> -h <host:port> -o <OID> -t <timout>

if((strlen(compath) + strlen(name) + strlen(host) + strlen(getoid) + 17) < BUF_LEN){
   sprintf(comdata,"%s -n %s -h %s -o %s -t %d 2>&-", compath, name, host, getoid, timeout);
   }
else{
   sprintf(comdata,"/bin/true -a data_size_over_err");
   }

#ifdef SNMP_DEBUG
printf("SNMP_DEBUG: scan_comand(0) %s\n", comdata);
#endif

buf = execom(comdata, buf, (timeout + 1), 1);
swap_mem(buf, '\n', 0x00);

if(1 < strlen(buf)){
   #ifdef SNMP_DEBUG
   printf("SNMP_DEBUG: scan_comand(2) %s の標準出力、エラー出力（テストモード）\n", buf);
   #endif
   }
if(1 >= strlen(buf)){
   #ifdef SNMP_DEBUG
   printf("SNMP_DEBUG: scan_comand(3) %s は正常終了（テストモード）\n", comdata);
   #endif
   buf = safe_sprintf(buf, BUF_MAX,"SNMP_ERR: %s\n", COM_3);
   }



if(opt != 0){ 
   if(* stat >= 0){

      #ifdef SNMP_DEBUG
      printf("SNMP_DEBUG: snmp_test(4): %ld, %s, %s, %s, %s\n", time(NULL), host, name, getoid, buf);
      #endif

      if(0 == memcmp(buf, "Gauge32: ", 9)){
         * chk = 1;                          // returnd chk
         if(9 < (int)strlen(buf)){
            data = delim_get(buf, data, ' ', &ret, 1);
            #ifdef SNMP_DEBUG
            printf("SNMP_DEBUG: snmp_test(5): Gauge32 OUT DATA = <%s>\n", data);
            #endif
            }
         }
      else if(0 == memcmp(buf, "Counter32: ", 11) || 0 == memcmp(buf, "Counter64: ", 11)){
         * chk = 2;                          // returnd chk
         if(11 < (int)strlen(buf)){
            data = delim_get(buf, data, ' ', &ret, 1);
            #ifdef SNMP_DEBUG
            printf("SNMP_DEBUG: snmp_test(6): Counter32,64 OUT DATA = <%s>\n", data);
            #endif
            }
         }
      else if(0 == memcmp(buf, "INTEGER: ", 9)){
         * chk = 3;                          // returnd chk
         if(9 < (int)strlen(buf)){
            if(opt > 1){
               if(0 < char_point(buf, '(', 1) || 0 < char_point(buf, ')', 1)){
                  strtok_r(buf, "(", &tmp);
                  data = safe_memcpy(data, strtok_r(NULL, ")", &tmp), BUF_MAX);
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: snmp_test(7): %s INTEGER OUT DATA = <%s>\n", buf, data);
                  #endif
                  }
               else{ 
                  data = delim_get(buf, data, ' ', &ret, 1);
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: snmp_test(8): %s INTEGER OUT DATA opt2 = <%s> \n",buf, data);
                  #endif
                  }
               }
            else{
               data = delim_get(buf, data, ' ', &ret, 1);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(9): %s INTEGER OUT DATA opt1 = %s\n",buf, data);
               #endif
               }
            }
         }
      else if(0 == memcmp(buf, "Timeticks: ", 11)){
         * chk = 3;                          // returnd chk
         if(9 < (int)strlen(buf)){
            if(opt > 1){
               if(0 < char_point(buf, '(', 1) || 0 < char_point(buf, ')', 1)){
                  strtok_r(buf, "(", &tmp);
                  data = safe_memcpy(data, strtok_r(NULL, ")", &tmp), BUF_MAX);
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: snmp_test(10): Timeticks OUT DATA = <%s>\n", data);
                  #endif
                  }
               else{ 
                  data = delim_get(buf, data, ' ', &ret, 1);
                  #ifdef SNMP_DEBUG
                  printf("SNMP_DEBUG: snmp_test(11): Timeticks OUT DATA opt2 = <%s> \n", data);
                  #endif
                  }
               }
            else{
               data = delim_get(buf, data, ' ', &ret, 1);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(12): Timeticks OUT DATA opt1 = %s\n", data);
               #endif
               }
            }
         }


      else if(0 == memcmp(buf, "SNMP_ERR: ", 10)){
         * chk = 4;                          // returnd chk
         int sta = char_point(buf, ':', 1);
         int sto = (int)strlen(buf);
         if(sta > 1 && buf[sta + 1] == ' ' && sto > 3){
            data = safe_memcpy(data, (strchr(buf, ':'))+1, BUF_MAX);
            data = delim_get(buf, data, ' ', &ret, 1);
            #ifdef SNMP_DEBUG
            printf("SNMP_DEBUG: snmp_test(13): STRING or... DATA = <%s>\n", data);
            #endif
            }
         else{
            if(sto > 1){
               data = safe_memcpy(data, buf, BUF_MAX);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(14); NOT STRING or... DATA = <%s>\n", data);
               #endif
               }
            else{
               data = safe_memcpy(data, "NOT", BUF_MAX);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(15): NOT STRING or... DATA = <%s>\n", data);
               #endif
               }
            }
         }


      else{  /* STRING or ... */
         * chk = 4;                          // returnd chk
         int sta = char_point(buf, ':', 1);
         int sto = (int)strlen(buf);
         if(sta > 1 && buf[sta + 1] == ' ' && sto > 3){
            data = safe_memcpy(data, (strchr(buf, ':'))+1, BUF_MAX);
            data = delim_get(buf, data, ' ', &ret, 1);
            #ifdef SNMP_DEBUG
            printf("SNMP_DEBUG: snmp_test(16): STRING or... DATA = <%s>\n", data);
            #endif
            }
         else{
            if(sto > 1){
               data = safe_memcpy(data, buf, BUF_MAX);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(17); NOT STRING or... DATA = <%s>\n", data);
               #endif
               }
            else{
               data = safe_memcpy(data, "NOT", BUF_MAX);
               #ifdef SNMP_DEBUG
               printf("SNMP_DEBUG: snmp_test(18): NOT STRING or... DATA = <%s>\n", data);
               #endif
               }
            }
         }
      }
   else{
      data = safe_memcpy(data, buf, BUF_MAX);
      #ifdef SNMP_DEBUG
      printf("SNMP_DEBUG: snmp_test(16): ERROR DATA = <%s>\n", data);
      #endif
      }
   }
else{
   data = safe_memcpy(data, buf, BUF_MAX);
   }

free(buf);
return data;
}






