home *** CD-ROM | disk | FTP | other *** search
/ ftp.umcs.maine.edu / 2015-02-07.ftp.umcs.maine.edu.tar / ftp.umcs.maine.edu / pub / thesis / zhongy / snmp / start.c < prev    next >
C/C++ Source or Header  |  1994-12-15  |  9KB  |  342 lines

  1. /*
  2.   This is a generalized snmp client package.
  3.  
  4.   Following main program will do statistic on the MIB object defined 
  5.     on VAR.TXT file.
  6.  
  7.   This package will do statistic on "any" MIB object on "any host" on an
  8.     interval defined by user.
  9.  
  10.   (Right now, this package only support the integer based MIB ogject: integer,
  11.     counter,timeticks,gauge,)
  12.  
  13.   All MIB objects are defined in  MIB.TXT file
  14.   
  15.  
  16.   programer: Zhong Yunxiang
  17.  
  18. */
  19.  
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <netdb.h>
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h> 
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31.  
  32. #include <setjmp.h>
  33. #include <signal.h>
  34.  
  35. #include "asn1.h"
  36. #include "snmp.h"
  37. #include "snmparse.h"
  38. #include "snmpencode.h"
  39. #include "snmprint.h"
  40. #include "snmpbasic.h"
  41. #include "../rule/rule.h"
  42. #include "../rule/rule_based.h"
  43. #include "../prob/prob_based.h"
  44.  
  45.  
  46. /* extern ObjectType *FirstObject;*/
  47.   
  48. /*
  49. struct ObjectType{
  50.            char Hostname[50];
  51.            char Obj_Name[50];
  52.            int Pre_Value;
  53.            int Diff_Value;
  54.            ObjectType *Next;
  55.            struct timeval time_left;   
  56.            struct timeval time_interval; 
  57.            };
  58. */
  59.  
  60.  
  61.  
  62.  
  63. void snmp_usage()
  64. {
  65.   fprintf(stderr,"\nusage: snmp start");
  66. }
  67.  
  68.  
  69. /**check event and problem
  70.  
  71.    multi-problem: report
  72.  
  73.    single-problem: report, update problem and event on the PDB file
  74.  
  75.    no-problem: antipate problem according to the event
  76. **/
  77.  
  78. void UPDATE_PDB(void)
  79. {
  80.   ObjectType *ObjPtr; 
  81.   char problem[NMaxProblems][NMaxCharactersProblem];
  82.   char event[NMaxSymptoms][NMaxCharactersSymptom];
  83.   int i,j,h;
  84.   struct ProblemSymptomType *SymptomPtr;
  85.   int num_problem, num_event;
  86.  
  87.  
  88.   time_t t;
  89.   FILE *file;
  90.  
  91.   /*check and record all happening problem and event*/
  92.  
  93.   ObjPtr=FirstObject;
  94.   num_problem=0;
  95.   num_event=0;
  96.   while (ObjPtr != NULL ) {
  97.      switch ((*ObjPtr).Type()) {
  98.         case MIB_VARIANCE:
  99.              break;
  100.         case MIB_VALUE:
  101.              break;
  102.         case PROBLEM:
  103.              if(((*ObjPtr).Known()==1) && (!strcmp( (*ObjPtr).Value(),"T"))) {
  104.                 num_problem++;
  105.                 printf("\nPROBLEM FOUND!!!: %s", (*ObjPtr).Name());
  106.                 /* system("(*ObjPtr).Name()"); */
  107.                 bzero(problem[num_problem],sizeof(problem[num_problem]));
  108.                 strcpy(problem[num_problem], (*ObjPtr).Name() );
  109.                 }
  110.              break;
  111.         case EVENT:
  112.              if(((*ObjPtr).Known()==1) && (!strcmp( (*ObjPtr).Value(),"T"))) {
  113.                 num_event++;
  114.                 bzero(event[num_event], sizeof(event[num_event]));
  115.                 strcpy(event[num_event], (*ObjPtr).Name() );
  116.                 }
  117.              break;
  118.         default:
  119.              break;
  120.         }
  121.      ObjPtr=(*ObjPtr).Next();
  122.      };
  123.  
  124.     /*
  125.        num_problem > 1
  126.  
  127.        if multiproblem happen, we don't update the PDB, because we can not
  128.        tell which event has relationship with which problem.
  129.  
  130.        We have a way to change the problem occuring times only. That need
  131.        a little bit of work
  132.     */
  133.  
  134.     /*
  135.        one problem happen, we would better update the PDB
  136.     */
  137.  
  138.     if (num_problem == 1) {
  139.          
  140.        /*update problem number*/
  141.        i=SearchProblem(problem[num_problem]);
  142.        Problem_Times[i]++;
  143.  
  144.        /*update event num to corresponding problem*/
  145.        for (j=1; j<=num_event; j++) {
  146.            SymptomPtr=SymptomsProblem[i];
  147.            while (SymptomPtr !=NULL) {
  148.                if ( !strcmp(SymptomName[SymptomPtr->Number], event[j])) {
  149.                     SymptomPtr->times++;
  150.                     SymptomPtr->Likehood=(float)SymptomPtr->times/(float)Problem_Times[i];
  151.                     break;
  152.                     }
  153.                 SymptomPtr=SymptomPtr->Next;
  154.                 }
  155.  
  156.            if (SymptomPtr == NULL ) {
  157.                  SymptomPtr= new ProblemSymptomType;
  158.                  SymptomPtr->times=1;
  159.                  SymptomPtr->Likehood=1.0/(float)Problem_Times[i];
  160.                  SymptomPtr->Number=SearchSymptom(event[j]);
  161.                  SymptomPtr->Next=SymptomsProblem[i];
  162.                  SymptomsProblem[i]=SymptomPtr;
  163.                  }
  164.            }
  165.  
  166.        /*update PDB.DBF file*/
  167.        SaveInFile();
  168.        }
  169.  
  170.      printf("\nnum_problem: %d, num_event:%d\n",num_problem, num_event);
  171.  
  172.      if ((num_problem == 0) && (num_event >0)) {
  173.  
  174.         /*log the current happening events*/
  175.         if ((file=fopen("EVENT_LOG","a")) == NULL) {
  176.             printf("\n EVENT.LOG can not be opened");
  177.             exit(0);
  178.             }
  179.  
  180.         t=time(NULL);
  181. printf("\n second");
  182.         fprintf(file,"\n\n**%s**", ctime(&t));           /*put in time*/
  183.         for (i=1;i<=num_event;i++)                      /*log all events*/
  184.             fprintf(file, "\n------------%s",event[i]);
  185.         fclose(file);
  186.  
  187.  
  188.         /*Do diagnosis problems accoring to current happening events*/
  189.         Diagnosis_START(); /*no problem situation, problem based inference*/
  190.         }
  191. }
  192.  
  193.  
  194. void start()
  195. {
  196.   struct snbentry test;
  197.   ObjectType *ObjPtr;
  198.   int i;
  199.   int num_problem,num_event;
  200.  
  201.   char Hostname[50];
  202.   char Obj_name[50];
  203.   FILE *var_file;
  204.   long time_interval;
  205.   struct timeval t;
  206.  
  207.   int firsttime;
  208.   int Current_Value;
  209.  
  210.   NUM_MIBITEM=init_mib();
  211.   printf("\n num_mib_item:%d",NUM_MIBITEM);
  212.  
  213.     printf("\n begin load database");
  214.     Load_DB();
  215.     ReadFromFile();
  216.  
  217.     printf("\n begin initialize database");
  218.  
  219.     /*initialize the value in database
  220.       get the value first time
  221.     */
  222.  
  223.  
  224.     ObjPtr=FirstObject;
  225.     while (ObjPtr != NULL ) {
  226.        /* C struct
  227.        test=get_value(ObjPtr->Obj_Name,ObjPtr->Hostname);
  228.        ObjPtr->Pre_Value=test.sb_val.sv_val.sv_int;
  229.        ObjPtr=ObjPtr->Next;
  230.        */
  231.        
  232.       printf("\n hostname:%s, objname:%s",(*ObjPtr).Hostname(),(*ObjPtr).Obj_Name());
  233.        switch ((*ObjPtr).Type()) {
  234.          case MIB_VARIANCE: 
  235.            test=get_value((*ObjPtr).Obj_Name(),(*ObjPtr).Hostname());
  236.            (*ObjPtr).set_prevalue(test.sb_val.sv_val.sv_int);
  237.            (*ObjPtr).set_Known(0);
  238.            break;
  239.          case MIB_VALUE:
  240.            test=get_value((*ObjPtr).Obj_Name(),(*ObjPtr).Hostname());
  241.            (*ObjPtr).set_prevalue(test.sb_val.sv_val.sv_int);
  242.            (*ObjPtr).set_diff(test.sb_val.sv_val.sv_int);
  243.            (*ObjPtr).set_Known(0);
  244.            break;
  245.          case PROBLEM:
  246.          case EVENT:
  247.            break;
  248.          default:
  249.            break;
  250.          }
  251.        ObjPtr=(*ObjPtr).Next();
  252.        };
  253.  
  254.  
  255.     printf("\n\n wait 5 seconds\n\n");
  256.     system("sleep 5");
  257.     
  258.  
  259.    /*
  260.      do retrieve
  261.    */
  262.   
  263.  
  264.    do {
  265.  
  266.       ObjPtr=FirstObject;
  267.       printf("\n OBJECT_TYPE  Hostname                  Objname           Current_val      Diff     Period");
  268.       printf("\n******************************************************************************");
  269.       while (ObjPtr != NULL ) {
  270.         switch ((*ObjPtr).Type()) {
  271.            case MIB_VARIANCE:
  272.              if (((*ObjPtr).Time_Left()).tv_sec == 0) {
  273.                test=get_value((*ObjPtr).Obj_Name(),(*ObjPtr).Hostname());
  274.                Current_Value=test.sb_val.sv_val.sv_int;
  275.                (*ObjPtr).set_diff(Current_Value -(*ObjPtr).Pre_Value());
  276.                (*ObjPtr).set_prevalue(Current_Value);
  277.                (*ObjPtr).set_timeleft((*ObjPtr).Time_Interval());
  278.                printf("\n%-4d", 1);
  279.                printf("%-25s",(*ObjPtr).Hostname());
  280.                printf(" %-20s",(*ObjPtr).Obj_Name());
  281.                printf("%-10d      %-5d ",Current_Value,(*ObjPtr).Diff());
  282.                printf("     %-4d",((*ObjPtr).Time_Interval()).tv_sec);
  283.                printf("\n-----------------------------------------------------------------------------");
  284.                }
  285.              else {
  286.                t.tv_sec=((*ObjPtr).Time_Left()).tv_sec - 5;
  287.                (*ObjPtr).set_timeleft(t);
  288.                }
  289.              break;
  290.            case MIB_VALUE:
  291.              if (((*ObjPtr).Time_Left()).tv_sec == 0) {
  292.                test=get_value((*ObjPtr).Obj_Name(),(*ObjPtr).Hostname());
  293.                Current_Value=test.sb_val.sv_val.sv_int;
  294.                (*ObjPtr).set_prevalue((*ObjPtr).Diff()); /*save the old value*/
  295.                (*ObjPtr).set_diff(Current_Value); /*set the current value*/
  296.                (*ObjPtr).set_timeleft((*ObjPtr).Time_Interval());
  297.                printf("\n%-4d", 0);
  298.                printf("\n%-25s",(*ObjPtr).Hostname());
  299.                printf(" %-20s",(*ObjPtr).Obj_Name());
  300.                printf("%-10d      %-5d ",Current_Value,(*ObjPtr).Diff());
  301.                printf("     %-4d",((*ObjPtr).Time_Interval()).tv_sec);
  302.                printf("\n-------------------------------------------------------
  303. ----------------------");
  304.                }
  305.              else {
  306.                t.tv_sec=((*ObjPtr).Time_Left()).tv_sec - 5;
  307.                (*ObjPtr).set_timeleft(t);
  308.                }
  309.              break;
  310.  
  311.  
  312.            case PROBLEM:
  313.            case EVENT:
  314.              break;
  315.            default:
  316.              break;
  317.            }
  318.          
  319.          ObjPtr=(*ObjPtr).Next();
  320.          }
  321.  
  322.        printf("\n\n\n");
  323.        /**Forward chaining**********************************************/
  324.        ForwardChaining(); 
  325.      
  326.        UPDATE_PDB();
  327.   
  328.      /*  Diagnosis_START(); */
  329.  
  330.        Clear_DB();
  331.  
  332.        printf("\n\n wait 5 seconds\n\n");
  333.        system("sleep 5");
  334.        
  335.        /* EraseScreen(); */
  336.       system("clear"); 
  337.       } while (1);
  338.  
  339.  
  340. }
  341.  
  342.