//******************************************************
// Skill methods
//******************************************************
// Construct a servant
void *servantConstruct(nbCELL context,void *skillHandle,nbCELL arglist,char *text){
  nbServant *servant;
  static unsigned char filecount=0;
  //int len;

  if(strlen(text)>=sizeof(servant->cmd)){
    nbLogMsg(context,0,'E',"Command text len=%d too long for buffer len=%d\n",strlen(text),sizeof(servant->cmd));
    return(NULL);
    }
  servant=nbAlloc(sizeof(nbServant));
  servant->context=context;
  servant->process=NULL;
  snprintf(servant->cmd,sizeof(servant->cmd),"%s",text);  // 2012-10-16 eat - length checked above // 2013-01-17 eat - VID 6572
  filecount++;  // modulo 256
  snprintf(servant->log,sizeof(servant->log),"servant.%8.8d.%3.3d.out",(int)time(NULL),filecount);
  nbListenerEnableOnDaemon(context);
  return(servant);
  }
/*
*  construct() method
*
*    define <term> node <skill>[(<args>)][:<text>]
*
*    <args> - port_number or "interface_address[:port_number]"
*    <text> - flag keywords
*               trace   - display input packets
*               dump    - display dump of SNMP UDP packets
*               silent  - don't echo generated NodeBrain commands 
*
*    define snmptrap node snmptrap;
*    define snmptrap node snmptrap:dump,silent;
*    define snmptrap node snmptrap("127.0.0.1");
*    define snmptrap node snmptrap(50162);
*    define snmptrap node snmptrap("127.0.0.1:50162");
*    define snmptrap node snmptrap("127.0.0.1:50162"):silent;
*/
static void *serverConstruct(nbCELL context,void *skillHandle,nbCELL arglist,char *text){
  NB_MOD_Snmptrap *snmptrap;
  nbCELL cell=NULL;
  nbSET argSet;
  char *cursor=text,*delim,saveDelim;
  double r,d;
  char interfaceAddr[16];
  unsigned int port=162;
  int type,trace=0,dump=0,echo=1;
  int len;
  char *str;

  *interfaceAddr=0;

  argSet=nbListOpen(context,arglist);   
  cell=nbListGetCellValue(context,&argSet); 
  if(cell!=NULL){
    type=nbCellGetType(context,cell);
    if(type==NB_TYPE_STRING){
      str=nbCellGetString(context,cell);
      delim=strchr(str,':');
      if(delim==NULL) len=strlen(str);
      else len=delim-str;
      if(len>15){
        nbLogMsg(context,0,'E',"Inteface IP address may not be greater than 15 characters");
        nbCellDrop(context,cell);
        return(NULL);
        }
      strncpy(interfaceAddr,str,len);
      *(interfaceAddr+len)=0; 
      if(delim!=NULL){
        delim++;
        port=(unsigned int)atoi(delim);
        }
      nbCellDrop(context,cell);
      }
    else if(type==NB_TYPE_REAL){
      r=nbCellGetReal(context,cell);
      nbCellDrop(context,cell);
      port=(unsigned int)r;
      d=port;
      if(d!=r || d==0){
        nbLogMsg(context,0,'E',"Expecting non-zero integer UDP port number");
        return(NULL);
        }
      }
    else{
      nbLogMsg(context,0,'E',"Expecting interface (\"address[:port]\") or (port) as argument list");
      return(NULL);
      }
    cell=nbListGetCellValue(context,&argSet); 
    if(cell!=NULL){
      nbLogMsg(context,0,'E',"Only one argument expected - ignoring additional arguments");
      nbCellDrop(context,cell);
      }
    }
  if(*text!=0){
    cursor=text;
    while(*cursor==' ') cursor++;
    while(*cursor!=';' && *cursor!=0){
      delim=strchr(cursor,' ');
      if(delim==NULL) delim=strchr(cursor,',');
      if(delim==NULL) delim=strchr(cursor,';');
      if(delim==NULL) delim=cursor+strlen(cursor);  // 2013-01-14 eat - VID 969-0.8.13-3 FP but replaced strchr(cursor,0)
      saveDelim=*delim;
      *delim=0;
      if(strcmp(cursor,"trace")==0){trace=1;}
      else if(strcmp(cursor,"dump")==0){trace=1;dump=1;}
      else if(strcmp(cursor,"silent")==0) echo=0; 
      *delim=saveDelim;
      cursor=delim;
      if(*cursor==',') cursor++;
      while(*cursor==' ') cursor++;
      }
    }
  snmptrap=nbAlloc(sizeof(NB_MOD_Snmptrap));
  snmptrap->socket=0;
  strcpy(snmptrap->interfaceAddr,interfaceAddr);
  snmptrap->port=port;
  snmptrap->trace=trace;
  snmptrap->dump=dump;
  snmptrap->echo=echo;
  snmptrap->handlerContext=NULL;
  snmptrap->syntaxContext=NULL;
  snmptrap->attributeContext=NULL;
  nbListenerEnableOnDaemon(context);  // sign up to enable when we daemonize
  return(snmptrap);
  }
Beispiel #3
0
/*
*  construct() method
*
*    define <term> node <skill>[(<args>)][:<text>]
*
*    define <term> node audit("<filename>","<translator>",<schedule>);
*/
static void *auditConstruct(nbCELL context,void *skillHandle,nbCELL arglist,char *text){
  nbAudit *audit;
  nbCELL fileNameCell,translatorNameCell,scheduleCell,nullCell;
  nbSET argSet;
  int type;
  char *fileName,*translatorName;
  nbCELL translatorCell;

  //nbLogMsg(context,0,'T',"auditConstruct: called");
  argSet=nbListOpen(context,arglist);
  fileNameCell=nbListGetCellValue(context,&argSet);
  if(fileNameCell==NULL){
    nbLogMsg(context,0,'E',"Expecting string file name as first parameter");
    return(NULL);
    }
  type=nbCellGetType(context,fileNameCell);
  if(type!=NB_TYPE_STRING){
    nbLogMsg(context,0,'E',"Expecting string file name as first parameter");
    return(NULL);
    }
  fileName=nbCellGetString(context,fileNameCell); 

  translatorNameCell=nbListGetCellValue(context,&argSet);
  if(translatorNameCell==NULL){
    nbLogMsg(context,0,'E',"Expecting string translator name as second parameter");
    return(NULL);
    }
  type=nbCellGetType(context,translatorNameCell);
  if(type!=NB_TYPE_STRING){
    nbLogMsg(context,0,'E',"Expecting string translator name as second parameter");
    return(NULL);
    }
  translatorName=nbCellGetString(context,translatorNameCell);  

  scheduleCell=nbListGetCell(context,&argSet);  // get schedule cell - not value
 
  nullCell=nbListGetCellValue(context,&argSet);
  if(nullCell!=NULL){
    nbLogMsg(context,0,'E',"The audit skill only accepts three parameters.");
    return(NULL);
    }

  translatorCell=nbTranslatorCompile(context,0,translatorName);
  if(translatorCell==NULL){
    nbLogMsg(context,0,'E',"Unable to load translator '%s'",translatorName);
    return(NULL);
    }
  audit=nbAlloc(sizeof(struct NB_MOD_AUDIT));
  audit->file=NULL;
  audit->pos=0;
  audit->fileNameCell=fileNameCell;
  audit->fileName=fileName;
  audit->translatorNameCell=translatorNameCell;
  audit->translatorName=translatorName;
  audit->translatorCell=nbCellGrab(context,translatorCell);
  audit->scheduleCell=scheduleCell;
  audit->synapseCell=NULL;
  audit->trace=0;
  
  nbListenerEnableOnDaemon(context);  // sign up to enable when we daemonize
  if(audit->trace) nbLogMsg(context,0,'T',"auditConstruct: returning");
  return(audit);
  }
Beispiel #4
0
/*
*  construct() method
*
*    define <term> node <skill>("<translator>",[<binding>])[:<text>]
*
*    <translator> - name of translator file
*    <binding>    - port_number or "interface_address[:port_number]"
*    <text>       - flag keywords
*                     trace   - display input packets
*                     dump    - display dump of syslog packets
*                     silent  - don't echo generated NodeBrain commands 
*
*    define syslog node syslog.server("syslog.nbx");
*    define syslog node syslog.server("syslog.nbx"):dump,silent;
*    define syslog node syslog.server("syslog.nbx","127.0.0.1");
*    define syslog node syslog.server("syslog.nbx",50162);
*    define syslog node syslog.server("syslog.nbx","127.0.0.1:50162");
*    define syslog node syslog.server("syslog.nbx","127.0.0.1:50162"):silent;
*/
void *serverConstruct(nbCELL context,void *skillHandle,nbCELL arglist,char *text){
  NB_MOD_Server *server;
  nbCELL cell=NULL;
  nbSET argSet;
  char *cursor=text,*delim,saveDelim;
  double r,d;
  char interfaceAddr[512];
  unsigned int port=514;
  int type,trace=0,dump=0,echo=1;
  int len;
  char *str;
  char *transfilename;
  nbCELL translator;
  char *uri="";;

  *interfaceAddr=0;

  argSet=nbListOpen(context,arglist);
  cell=nbListGetCellValue(context,&argSet);
  if(cell==NULL){
    nbLogMsg(context,0,'E',"Translator configuration file required as first argument");
    return(NULL);
    }
  type=nbCellGetType(context,cell);
  if(type!=NB_TYPE_STRING){
    nbLogMsg(context,0,'E',"First argument must be string identifying translator configuration file");
    return(NULL);
    }
  transfilename=nbCellGetString(context,cell);
  translator=nbTranslatorCompile(context,0,transfilename);
  if(translator==NULL){
    nbLogMsg(context,0,'E',"Unable to load translator '%s'",transfilename);
    return(NULL);
    }
  cell=nbListGetCellValue(context,&argSet);
  if(cell!=NULL){
    type=nbCellGetType(context,cell);
    if(type==NB_TYPE_STRING){
      str=nbCellGetString(context,cell);
      uri=strdup(str);
      if(!uri) nbExit("serverConstruct: Out of memory - terminating");
      if(strncmp(str,"udp://",6)==0) str+=6;  // allow for uri
      delim=strchr(str,':');
      if(delim==NULL) len=strlen(str);
      else len=delim-str;
      if(len>15 && *str>='0' && *str<='9'){
        nbLogMsg(context,0,'E',"Inteface IP address may not be greater than 15 characters");
        nbCellDrop(context,cell);
        return(NULL);
        }
      if(len>sizeof(interfaceAddr)-1){
        nbLogMsg(context,0,'E',"Socket specification too long for buffer at--->%s",str);
        nbCellDrop(context,cell);
        return(NULL);
        }
      strncpy(interfaceAddr,str,len);
      *(interfaceAddr+len)=0; 
      if(delim!=NULL){
        delim++;
        port=(unsigned int)atoi(delim);
        }
      nbCellDrop(context,cell);
      }
    else if(type==NB_TYPE_REAL){
      r=nbCellGetReal(context,cell);
      nbCellDrop(context,cell);
      port=(unsigned int)r;
      d=port;
      if(d!=r || d==0){
        nbLogMsg(context,0,'E',"Expecting non-zero integer UDP port number");
        return(NULL);
        }
      }
    else{
      nbLogMsg(context,0,'E',"Expecting (\"file\") or (\"address[:port]\") or (port) as argument list");
      return(NULL);
      }
    cell=nbListGetCellValue(context,&argSet);
    if(cell!=NULL){
      nbLogMsg(context,0,'E',"The syslog skill only accepts two argument.");
      return(NULL);
      }
    }
  while(*cursor==' ') cursor++;
  while(*cursor!=';' && *cursor!=0){
    delim=strchr(cursor,' ');
    if(delim==NULL) delim=strchr(cursor,',');
    if(delim==NULL) delim=strchr(cursor,';');
    if(delim==NULL) delim=cursor+strlen(cursor);
    saveDelim=*delim;
    *delim=0;
    if(strcmp(cursor,"trace")==0){trace=1;}
    else if(strcmp(cursor,"dump")==0){trace=1;dump=1;}
    else if(strcmp(cursor,"silent")==0) echo=0; 
    *delim=saveDelim;
    cursor=delim;
    if(*cursor==',') cursor++;
    while(*cursor==' ' || *cursor==',') cursor++;
    }
  server=nbAlloc(sizeof(NB_MOD_Server));
  server->uri=uri;
  server->socket=0;
  strcpy(server->interfaceAddr,interfaceAddr);
  server->port=port;
  server->translator=translator;
  server->trace=trace;
  server->dump=dump;
  server->echo=echo;
  nbLogMsg(context,0,'I',"calling nbListenerEnableOnDaemon");
  nbListenerEnableOnDaemon(context);  // sign up to enable when we daemonize
  return(server);
  }
/*
*  construct() method
*
*    define <term> node <skill>[(<args>)][:<text>]
*
*    define <term> node baseline("<directory>",<weight>,<tolerance>,<cycle>,<interval>)[:<options>];
*/
static void *baselineConstruct(nbCELL context,BTreeSkill *skillHandle,nbCELL arglist,char *text){
  int options=0;
  nbCELL found=NULL;
  nbCELL notfound=NULL;
  nbCELL cell;
  BTree *tree;
  char *cursor=text,*delim,ident[256];
  int len;
  nbSET  argSet;
  char *directory;
  double weight=0.5,tolerance=3,cycle=7*24*60,periods=24;
  double interval=60;

  argSet=nbListOpen(context,arglist);
  cell=nbListGetCellValue(context,&argSet);
  if(cell==NULL || nbCellGetType(context,cell)!=NB_TYPE_STRING){
    nbLogMsg(context,0,'E',"Baseline directory string required as first argument");
    return(NULL);
    }
  directory=nbCellGetString(context,cell);  // we don't drop this cell because we reference it
  cell=nbListGetCellValue(context,&argSet);
  if(cell!=NULL){
    if(nbCellGetType(context,cell)!=NB_TYPE_REAL || (weight=nbCellGetReal(context,cell))<0 || weight>1){
      nbLogMsg(context,0,'E',"Second argument must be numeric weight between 0 and 1");
      return(NULL);
      }
    nbCellDrop(context,cell);
    cell=nbListGetCellValue(context,&argSet);
    if(cell!=NULL){
      if(nbCellGetType(context,cell)!=NB_TYPE_REAL || (tolerance=nbCellGetReal(context,cell))<0){
        nbLogMsg(context,0,'E',"Third argument must be non-negative numeric tolerance");
        return(NULL);
        }
      nbCellDrop(context,cell);
      cell=nbListGetCellValue(context,&argSet);
      if(cell!=NULL){
        if(nbCellGetType(context,cell)!=NB_TYPE_REAL || (cycle=nbCellGetReal(context,cell))<1 || cycle!=(int)cycle){
          nbLogMsg(context,0,'E',"Forth argument must be positive integer number of minutes in a cycle");
          return(NULL);
          }
        nbCellDrop(context,cell);
        cell=nbListGetCellValue(context,&argSet);
        if(cell!=NULL){
          if(nbCellGetType(context,cell)!=NB_TYPE_REAL || (interval=nbCellGetReal(context,cell))<1 || interval!=(int)interval || (periods=cycle/interval)<1 || periods!=(int)periods){
            nbLogMsg(context,0,'E',"Fifth argument must be positive integer number of minutes in an interval by which %d is evenly divisable",cycle);
            return(NULL);
            }
          nbCellDrop(context,cell);
          cell=nbListGetCellValue(context,&argSet);
          if(cell!=NULL){
            nbLogMsg(context,0,'E',"Only five arguments supported");
            return(NULL);
            }
          }
        }
      }
    }
  if(skillHandle!=NULL && skillHandle->trace) nbLogMsg(context,0,'T',"baselineConstruct() called");
  while(*cursor==' ') cursor++;
  while(*cursor!=0 && *cursor!=';'){
    delim=cursor;
    while(*delim>='a' && *delim<='z') delim++;
    len=delim-cursor;
    if(len>sizeof(ident)-1){
      nbLogMsg(context,0,'E',"Option not recognized at \"%s\".",cursor);
      return(NULL);
      }
    strncpy(ident,cursor,len);
    *(ident+len)=0;
    cursor=delim; 
    while(*cursor==' ') cursor++;
    if(strcmp(ident,"trace")==0) options|=BTREE_OPTION_TRACE;
    else if(strcmp(ident,"order")==0) options|=BTREE_OPTION_ORDER;
    else if(strcmp(ident,"partition")==0) options|=BTREE_OPTION_PARTITION|BTREE_OPTION_ORDER;
    else if(strcmp(ident,"sum")==0) options|=BTREE_OPTION_SUM;
    else if(strcmp(ident,"static")==0) options|=BTREE_OPTION_STATIC;
    else if(strcmp(ident,"found")==0 || strcmp(ident,"notfound")==0){
      if(*cursor!='='){
        nbLogMsg(context,0,'E',"Expecting '=' at \"%s\".",cursor);
        return(NULL);
        }
      cursor++;
      cell=nbCellParse(context,&cursor);
      if(cell==NULL){
        nbLogMsg(context,0,'E',"Syntax error in cell expression.");
        return(NULL);
        }
      if(strcmp(ident,"found")==0) found=cell;
      else notfound=cell;
      }
    else{
      nbLogMsg(context,0,'E',"Option not recognized at \"%s\".",cursor);
      return(NULL);
      }
    while(*cursor==' ') cursor++;
    if(*cursor==','){
      cursor++;
      while(*cursor==' ') cursor++;
      }
    else if(*cursor!=0 && *cursor!=';'){
      nbLogMsg(context,0,'E',"Expecting ',' ';' or end of line at \"%s\".",cursor);
      return(NULL);
      }
    }
  if(notfound==NULL) notfound=NB_CELL_UNKNOWN;
  if(found==NULL) found=notfound;
  tree=(BTree *)nbAlloc(sizeof(BTree));
  tree->options=options;
  tree->notfound=notfound;
  tree->found=found;
  tree->root=NULL;
  tree->directory=directory;
  tree->cycle=cycle*60;
  tree->periods=periods;
  tree->interval=interval*60;
  tree->weight=weight;
  tree->tolerance=tolerance*1.25;
  nbListenerEnableOnDaemon(context);  // sign up to enable when we daemonize
  return(tree);
  }