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 / snmpn < prev    next >
Text File  |  1994-05-18  |  4KB  |  177 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.  
  42.   
  43.  
  44. struct ObjectType{
  45.            char Hostname[50];
  46.            char Obj_Name[50];
  47.            int Pre_Value;
  48.            int Diff_Value;
  49.            ObjectType *Next;
  50.            struct timeval time_left;   /*time left*/
  51.            struct timeval time_interval; /*time default*/
  52.            };
  53.  
  54. void snmp_usage()
  55. {
  56.   fprintf(stderr,"\nusage: snmp start");
  57. }
  58.  
  59.  
  60. void start()
  61. {
  62.   struct snbentry test;
  63.   char *action;
  64.   struct ObjectType *First,*ObjPtr;
  65.   int i;
  66.  
  67.   char Hostname[50];
  68.   char Obj_name[50];
  69.   FILE *var_file;
  70.   long time_interval;
  71.  
  72.   int firsttime;
  73.   int Current_Value;
  74.  
  75.   printf("\n %s", action);
  76.  
  77.   NUM_MIBITEM=init_mib();
  78.   printf("\n num_mib_item:%d",NUM_MIBITEM);
  79.  
  80.  
  81.   /*
  82.    initialize database
  83.   */
  84.  
  85.  
  86.   if ((var_file=fopen("VAR.TXT","r")) == NULL) {
  87.      printf("\n VAR.TXT can not be opened");
  88.      exit(0);
  89.      };
  90.  
  91.   firsttime=1;
  92.   while (1) {
  93.     bzero(Hostname,sizeof(Hostname));
  94.     bzero(Obj_name,sizeof(Obj_name));
  95.     fscanf(var_file,"%s",Hostname);
  96.     if (!strcmp(Hostname,"END")){
  97.        break;
  98.        }
  99.     fscanf(var_file,"%s",Obj_name);
  100.     fscanf(var_file,"%d",&time_interval);
  101.     ObjPtr=new struct ObjectType;
  102.     strcpy(ObjPtr->Hostname,Hostname);
  103.     strcpy(ObjPtr->Obj_Name,Obj_name);
  104.     ObjPtr->time_interval.tv_sec=time_interval;
  105.     ObjPtr->time_left.tv_sec=time_interval;
  106.     if (firsttime ==0 ) {
  107.         ObjPtr->Next=First;
  108.         First=ObjPtr;
  109.         }
  110.     else {
  111.         firsttime=0;
  112.         ObjPtr->Next=NULL;
  113.         First=ObjPtr;
  114.         }
  115.     }
  116.    close(var_file);
  117.  
  118.    /*
  119.    ObjPtr=First;
  120.    while (ObjPtr != NULL ) {
  121.      printf("\n %s",ObjPtr->Obj_Name);
  122.      ObjPtr=ObjPtr->Next;
  123.      };
  124.    */
  125.  
  126.     printf("\n initializing  finished");
  127.  
  128.     /*initialize the value in database
  129.       get the value first time
  130.     */
  131.     ObjPtr=First;
  132.     while (ObjPtr != NULL ) {
  133.        test=get_value(ObjPtr->Obj_Name,ObjPtr->Hostname);
  134.        ObjPtr->Pre_Value=test.sb_val.sv_val.sv_int;
  135.        ObjPtr=ObjPtr->Next;
  136.        };
  137.     printf("\n\n wait 5 seconds\n\n");
  138.     system("sleep 5");
  139.  
  140.  
  141.    /*
  142.      do retrieve
  143.    */
  144.   
  145.    do {
  146.  
  147.       /*
  148.       printf("\n awake");
  149.       */
  150.       ObjPtr=First;
  151.       printf("\n Hostname                  Objname           Current_val      Diff     Period");
  152.       printf("\n******************************************************************************");
  153.       while (ObjPtr != NULL ) {
  154.          if (ObjPtr->time_left.tv_sec == 0) {
  155.              test=get_value(ObjPtr->Obj_Name,ObjPtr->Hostname);
  156.              Current_Value=test.sb_val.sv_val.sv_int;
  157.              ObjPtr->Diff_Value=Current_Value - ObjPtr->Pre_Value;
  158.              ObjPtr->Pre_Value=Current_Value;
  159.              ObjPtr->time_left.tv_sec=ObjPtr->time_interval.tv_sec;
  160.              printf("\n%-25s",ObjPtr->Hostname);
  161.              printf(" %-20s",ObjPtr->Obj_Name);
  162.              printf("%-10d      %-5d ",Current_Value,ObjPtr->Diff_Value);
  163.              printf("     %-4d",ObjPtr->time_interval.tv_sec);
  164.              printf("\n-----------------------------------------------------------------------------");
  165.              }
  166.           else {
  167.              ObjPtr->time_left.tv_sec=ObjPtr->time_left.tv_sec - 5;
  168.              }
  169.           ObjPtr=ObjPtr->Next;
  170.           }
  171.  
  172.        printf("\n\n wait 5 seconds\n\n");
  173.        system("sleep 5");
  174.  
  175.       } while (1);
  176. }
  177.