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 / snmp_back.c < prev    next >
C/C++ Source or Header  |  1994-05-10  |  3KB  |  159 lines

  1. /*
  2.  
  3.  
  4.   programer: Zhong Yunxiang
  5.  
  6. */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <netdb.h>
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <sys/time.h>
  18. #include <sys/types.h>
  19.  
  20. #include "asn1.h"
  21. #include "snmp.h"
  22. #include "snmparse.h"
  23. #include "snmpencode.h"
  24. #include "snmprint.h"
  25. #include "snmpbasic.h"
  26.  
  27.   
  28.  
  29. struct ObjectType{
  30.            char Hostname[50];
  31.            char Obj_Name[50];
  32.            int Pre_Value;
  33.            int Diff_Value;
  34.            ObjectType *Next;
  35.            struct timeval time_left;   /*time left*/
  36.            struct timeval time_interval; /*time default*/
  37.            };
  38.  
  39. void snmp_usage()
  40. {
  41.   fprintf(stderr,"\nusage: snmp: start");
  42. }
  43.  
  44.  
  45. main(int argc,char *argv[])
  46. {
  47.   struct snbentry test;
  48.   char *action;
  49.   struct ObjectType *First,*ObjPtr;
  50.   int i;
  51.  
  52.   char Hostname[50];
  53.   char Obj_name[50];
  54.   FILE *var_file;
  55.   long time_interval;
  56.  
  57.   int firsttime;
  58.   int Current_Value;
  59.  
  60.   if (argc != 2) 
  61.      snmp_usage();
  62.   action=argv[1];
  63.   if (strcmp(action,"start")) 
  64.      exit(0);
  65.  
  66.  
  67. printf("\n %s", action);
  68.  
  69.   NUM_MIBITEM=init_mib();
  70.   printf("\n num_mib_item:%d",NUM_MIBITEM);
  71.  
  72.  
  73.   /*
  74.    initialize database
  75.   */
  76.  
  77.  
  78.   if ((var_file=fopen("VAR.TXT","r")) == NULL) {
  79.      printf("\n VAR.TXT can not be opened");
  80.      exit(0);
  81.      };
  82.  
  83.   firsttime=1;
  84.   while (1) {
  85.     bzero(Hostname,sizeof(Hostname));
  86.     bzero(Obj_name,sizeof(Obj_name));
  87.     fscanf(var_file,"%s",Hostname);
  88.     if (!strcmp(Hostname,"END")){
  89.        break;
  90.        }
  91.     fscanf(var_file,"%s",Obj_name);
  92.     fscanf(var_file,"%d",&time_interval);
  93.     ObjPtr=new struct ObjectType;
  94.     strcpy(ObjPtr->Hostname,Hostname);
  95.     strcpy(ObjPtr->Obj_Name,Obj_name);
  96.     ObjPtr->time_interval.tv_sec=time_interval;
  97.     ObjPtr->time_left.tv_sec=time_interval;
  98.     if (firsttime ==0 ) {
  99.         ObjPtr->Next=First;
  100.         First=ObjPtr;
  101.         }
  102.     else {
  103.         firsttime=0;
  104.         ObjPtr->Next=NULL;
  105.         First=ObjPtr;
  106.         }
  107.     }
  108.    close(var_file);
  109.  
  110.    /*
  111.    ObjPtr=First;
  112.    while (ObjPtr != NULL ) {
  113.      printf("\n %s",ObjPtr->Obj_Name);
  114.      ObjPtr=ObjPtr->Next;
  115.      };
  116.    */
  117.  
  118.     printf("\n initializing  finished");
  119.  
  120.     /*initialize the value in database
  121.       get the value first time
  122.     */
  123.     ObjPtr=First;
  124.     while (ObjPtr != NULL ) {
  125.        test=get_value(ObjPtr->Obj_Name,ObjPtr->Hostname);
  126.        ObjPtr->Pre_Value=test.sb_val.sv_val.sv_int;
  127.        ObjPtr=ObjPtr->Next;
  128.        };
  129.  
  130.  
  131.   
  132.    do {
  133.  
  134.       printf("\n sleep");
  135.       system("sleep 5");
  136.  
  137.       printf("\n awake");
  138.       ObjPtr=First;
  139.       while (ObjPtr != NULL ) {
  140.          if (ObjPtr->time_left.tv_sec == 0) {
  141.              test=get_value(ObjPtr->Obj_Name,ObjPtr->Hostname);
  142.              Current_Value=test.sb_val.sv_val.sv_int;
  143.              ObjPtr->Diff_Value=Current_Value - ObjPtr->Pre_Value;
  144.              ObjPtr->Pre_Value=Current_Value;
  145.              ObjPtr->time_left.tv_sec=ObjPtr->time_interval.tv_sec;
  146.              printf("\n******************************************************");
  147.              printf("\n hostname: %s: ",ObjPtr->Hostname);
  148.              printf("Obj_name: %s\n",ObjPtr->Obj_Name);
  149.              printf("\n Current:%d, Diff:%d ",Current_Value,ObjPtr->Diff_Value);
  150.              printf("(in %d second)\n",ObjPtr->time_interval.tv_sec);
  151.              }
  152.           else {
  153.              ObjPtr->time_left.tv_sec=ObjPtr->time_left.tv_sec - 5;
  154.              }
  155.           ObjPtr=ObjPtr->Next;
  156.           }
  157.       } while (1);
  158. }
  159.