/* Estes sao os arquivos comumente incluidos  */

#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include  


#include "snmp.h"
#include "asn1.h"
#include "snmp_impl.h"
#include "snmp_api.h"
#include "snmp_client.h"



int	snmp_dump_packet = 0;


main()
{



/************************************************
Estruturas utilizadas pelas funcoes do cmu-snmp.
*************************************************/

struct snmp_session session, *ss;
struct snmp_pdu *pdu, *response;
struct variable_list *vars;
oid name[MAX_NAME_LEN];
int name_length;
int status;

/*****************************************************
 DEFINICAO DA ESTRURA DO ARQUIVO A SER GERADO  
*****************************************************/

    struct arquivo {
        int a_hora;
        unsigned int a_inte;
        unsigned long int a_status;
        unsigned long int a_in_octets ;
        unsigned long int a_in_descar ;
        unsigned long int a_in_erros  ;
        unsigned long int a_out_octets ;
        unsigned long int a_out_descar ;
        unsigned long int a_out_erros ;
        unsigned int mail_trafe ;
        unsigned int mail_erros ;
        unsigned int mail_status;
        } arq;
        
    unsigned char arq_mesant[6] ;
    
    struct mensagem {
        int men_hora ;
        int men_codigo ;
       } mens ;


/***********************************
Definicao do objeto a ser buscado
***********************************/

	char *maquina="200.6.40.99";
	char *comunidade = "public";
	char *nome1 = "interfaces.ifTable.ifentry.ifOperStatus.1";
	char *nome2 = "interfaces.ifTable.ifEntry.ifInOctets.1" ;
	char *nome3 = "interfaces.ifTable.ifEntry.ifInDiscards.1" ;
	char *nome4 = "interfaces.ifTable.ifEntry.ifInErrors.1" ;
	char *nome5 = "interfaces.ifTable.ifEntry.ifOutOctets.1" ;
	char *nome6 = "interfaces.ifTable.ifEntry.ifOutDiscards.1" ;
	char *nome7 = "interfaces.ifTable.ifEntry.ifOutErrors.1" ;
	char *nome8 = "interfaces.ifTable.ifEntry.ifOperStatus.2";
	char *nome9 = "interfaces.ifTable.ifEntry.ifInOctets.2" ;
	char *nome10 = "interfaces.ifTable.ifEntry.ifInDiscards.2" ;
	char *nome11 = "interfaces.ifTable.ifEntry.ifInErrors.2" ;  
	char *nome12 = "interfaces.ifTable.ifEntry.ifOutOctets.2" ;
	char *nome13 = "interfaces.ifTable.ifEntry.ifOutDiscards.2" ;  
	char *nome14 = "interfaces.ifTable.ifEntry.ifOutErrors.2" ;    
	int cont ;
    unsigned char data[24];
    char nome_arq1[6];
    char nome_arq2[6];
    char nome_mens[10];
    unsigned char hora[03];
    int hora_ant ;
    int i ;
    int sim;
    FILE *fp,*fp1,*fp2,*fp3;
    time_t lt;
    unsigned long int as_1_l_in_octets ;
    unsigned long int as_1_l_out_octets ;
    unsigned long int as_1_l_in_disc;
    unsigned long int as_1_l_out_disc;
    unsigned long int as_1_l_in_erros;
    unsigned long int as_1_l_out_erros;
    unsigned long int as_1_a_in_octets ;
    unsigned long int as_1_a_out_octets ;
    unsigned long int as_1_a_in_disc;
    unsigned long int as_1_a_out_disc;
    unsigned long int as_1_a_in_erros;
    unsigned long int as_1_a_out_erros;
    unsigned long int as_2_l_in_octets ;
    unsigned long int as_2_l_out_octets ;
    unsigned long int as_2_l_in_disc;
    unsigned long int as_2_l_out_disc;
    unsigned long int as_2_l_in_erros;
    unsigned long int as_2_l_out_erros;
    unsigned long int as_2_a_in_octets ;
    unsigned long int as_2_a_out_octets ;
    unsigned long int as_2_a_in_disc;
    unsigned long int as_2_a_out_disc;
    unsigned long int as_2_a_in_erros;
    unsigned long int as_2_a_out_erros;
    unsigned long int as_1_status ;
    unsigned long int as_2_status ;
    unsigned long int as_val1;
    unsigned long int as_val2;
    double as_val3;
    char diretorio1[46];
    char diretorio2[46];
    char diretorio3[50];


/*******************************************
 CARREGA A DATA E HORA LOCAL
*********************************************/
        
        lt=time(NULL);
    	strcpy(data,ctime(<));
        strcpy(nome_arq1,"      ");
        strcpy(nome_arq2,"      ");
        strcpy(nome_mens,"          ");
        strcpy(diretorio1,"/internet/celepar/bonato/mestrado/teste/");
        strcpy(diretorio2,"/internet/celepar/bonato/mestrado/teste/");
        strcpy(diretorio3,"/internet/celepar/bonato/mestrado/teste/");   
 	strcpy(hora,"   ");
    	hora[0] = data[11];
    	hora[1] = data[12];   
        nome_arq1[0] = data[4]; 
        nome_arq1[1] = data[5]; 
        nome_arq1[2] = data[6];
        nome_arq1[3] = data[8];
        nome_arq1[4] = data[9];
        nome_arq1[5] = '\0';
        diretorio1[45] = '\0';   
        diretorio2[45] = '\0';
        diretorio3[49] = '\0';
        sim = 0;
   if ((fp=fopen("/internet/celepar/bonato/mestrado/teste/mesant","rb"))==NULL)
             exit(1);
        if (fread(&arq_mesant,sizeof(arq_mesant),1,fp)!=1)
             exit(1) ;
        strcpy(nome_arq2,arq_mesant);
        nome_arq2[5] = '\0';
        fclose(fp);     

/*****************************************
inicializacao da MIB e inicializa estrutura
*****************************************/
    init_mib();
  
    bzero((char *)&session, sizeof(struct snmp_session));

    session.peername = maquina;
    session.version = SNMP_VERSION_1;
    session.community = (u_char *)comunidade;
    session.community_len = strlen((char *)comunidade);
    session.retries = SNMP_DEFAULT_RETRIES;
    session.timeout = SNMP_DEFAULT_TIMEOUT;
    session.authenticator = NULL;


/***************************************************
Sincroniza a sessao snmp
***************************************************/
    snmp_synch_setup(&session);

    ss = snmp_open(&session);

    if (ss == NULL){
	printf("Nao conseguiu abrir sessao\n");
	return NULL;
    }


/***********************************************************
Cria a PDU e coloca os objetos que serao buscados
Podem ser colocados varios objetos na mesma PDU.
Basta se repetir as funcoes read_objid e snmp_add_null_var
************************************************************/
    pdu = snmp_pdu_create(GET_REQ_MSG);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome1, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome2, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);    

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome3, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome4, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome5, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome6, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome7, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome8, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length); 

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome9, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome10, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);  

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome11, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length); 

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome12, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome13, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);

    name_length = MAX_NAME_LEN;
    if (!read_objid(nome14, name, &name_length))
       {
       exit(1);
       }
    snmp_add_null_var(pdu, name, name_length);


/******************************************************
 Envia o comando get e analisa a resposta
****************************************************/
    status = snmp_synch_response(ss, pdu, &response);


    if(status == STAT_SUCCESS) 
       {
       if(response->errstat == SNMP_ERR_NOERROR)
         {
          cont = 0;
          if (atoi(hora) == 0)
             {
             strcat(diretorio1,nome_arq1);
             if((fp1=fopen(diretorio1,"a+b"))==NULL)
                exit(1);
             strcat(diretorio2,nome_arq2);
             if((fp2=fopen(diretorio2,"a+b"))==NULL)
                 exit(1) ;
             }
          else      
             {
             strcat(diretorio1,nome_arq1);      
             if((fp1=fopen(diretorio1,"a+b"))==NULL)
                 exit(1) ;
             }
          for(vars = response->variables; vars; vars = vars->next_variable)
                {
                cont++;
                if (cont == 1)
                   {
                   arq.a_hora = atoi(hora);
                   arq.a_inte = 1 ;
                   arq.a_status = *vars->val.integer ;
                   }
                if (cont == 2)
                   arq.a_in_octets = *vars->val.integer ; 
                if (cont == 3)
                   arq.a_in_descar = *vars->val.integer ;
                if (cont == 4)
                   arq.a_in_erros = *vars->val.integer ; 
                if (cont == 5)
                   arq.a_out_octets = *vars->val.integer ;
                if (cont == 6)
                   arq.a_out_descar = *vars->val.integer ;
                if (cont == 7)
                   {
                   arq.a_out_erros = *vars->val.integer ; 
                   if (fwrite(&arq,sizeof(struct arquivo),1,fp1)!=1)
                      exit(1);
                   if (atoi(hora) == 0) 
                      if (fwrite(&arq,sizeof(struct arquivo),1,fp2)!=1)
                          exit(1);
                   }
                if (cont == 8)
                   {
                   arq.a_hora = atoi(hora);
                   arq.a_inte = 2;
                   arq.a_status = *vars->val.integer ;
                   }
                if (cont == 9)
                   arq.a_in_octets = *vars->val.integer ;
                if (cont == 10)
                   arq.a_in_descar = *vars->val.integer ;
                if (cont == 11)
                   arq.a_in_erros = *vars->val.integer ;
                if (cont == 12)
                   arq.a_out_octets = *vars->val.integer ;
                if (cont == 13)
                   arq.a_out_descar = *vars->val.integer ;
                if (cont == 14)
                   {
                   arq.a_out_erros = *vars->val.integer ; 
                   if (fwrite(&arq,sizeof(struct arquivo),1,fp1)!=1)
                      exit(1);
                   if (atoi(hora) == 0)   
                      {
                      sim = 100 ;  
                      if (fwrite(&arq,sizeof(struct arquivo),1,fp2)!=1)
                          exit(1);
   if ((fp=fopen("/internet/celepar/bonato/mestrado/teste/mesant","wb"))==NULL)
                          exit(1);
                      strcpy(arq_mesant,nome_arq1) ;
                      if (fwrite(&arq_mesant,sizeof(arq_mesant),1,fp)!=1)
                          exit(1);
                      fclose(fp) ;
                      fclose(fp2);    
                      }
                   fclose(fp1);
                   }
              }
          }
	  else 
	     printf("Erro no pacote: %s\n",snmp_errstring(response->errstat));
	 		   
       }
   else 
       if (status == STAT_TIMEOUT)
 	   printf("Sem resposta - %s\n", maquina);
       else    /* status == STAT_ERROR */
	   printf("An error occurred, Quitting\n");

   if (response)
      snmp_free_pdu(response);
   snmp_close(ss);
   if(sim > 0)
     {
     hora_ant = 23 ; 
     if ((fp1=fopen(diretorio2,"rb"))==NULL)
         exit(1);
     }
   else
     {
     hora_ant = atoi(hora) - 1;
     if ((fp1=fopen(diretorio1,"rb"))==NULL)
         exit(1);
     }
   nome_mens[0] = nome_arq1[0];  
   nome_mens[1] = nome_arq1[1];
   nome_mens[2] = nome_arq1[2];
   nome_mens[3] = nome_arq1[3]; 
   nome_mens[4] = nome_arq1[4];
   nome_mens[5] = 'm' ;
   nome_mens[6] = 'e' ;
   nome_mens[7] = 'n' ;
   nome_mens[8] = 's' ;
   nome_mens[9] = '\0';
   strcat(diretorio3,nome_mens);
   if ((fp3=fopen(diretorio3,"a+b"))==NULL)
         exit(1);
   for(;;)
         {
         if (fread(&arq,sizeof(struct arquivo),1,fp)!=1)
            break;
         if(arq.a_hora == atoi(hora) && arq.a_inte == 1)
            {
            as_1_status = arq.a_status ;
            as_1_a_in_octets = arq.a_in_octets ;
            as_1_a_out_octets = arq.a_out_octets ;
            as_1_a_in_erros = arq.a_in_erros;
            as_1_a_out_erros = arq.a_out_erros;
            as_1_a_in_disc = arq.a_in_descar;
            as_1_a_out_disc = arq.a_in_descar;
            }   
         if(arq.a_hora == hora_ant && arq.a_inte == 1)
            {
            as_1_l_in_octets = arq.a_in_octets ;
            as_1_l_out_octets = arq.a_out_octets ;
            as_1_l_in_erros = arq.a_in_erros;
            as_1_l_out_erros = arq.a_out_erros;
            as_1_l_in_disc = arq.a_in_descar;
            as_1_l_out_disc = arq.a_in_descar;
            } 
         if(arq.a_hora == atoi(hora) && arq.a_inte == 2)
            {
            as_2_status = arq.a_status ;
            as_2_a_in_octets = arq.a_in_octets ;
            as_2_a_out_octets = arq.a_out_octets ;
            as_2_a_in_erros = arq.a_in_erros;
            as_2_a_out_erros = arq.a_out_erros;
            as_2_a_in_disc = arq.a_in_descar;
            as_2_a_out_disc = arq.a_in_descar;
            }   
         if(arq.a_hora == hora_ant && arq.a_inte == 2)
            {
            as_2_l_in_octets = arq.a_in_octets ;
            as_2_l_out_octets = arq.a_out_octets ;
            as_2_l_in_erros = arq.a_in_erros;
            as_2_l_out_erros = arq.a_out_erros;
            as_2_l_in_disc = arq.a_in_descar;
            as_2_l_out_disc = arq.a_in_descar;
            }
         }
       if (as_1_a_in_octets < as_1_l_in_octets)
          as_val1 = ((4294967295 - as_1_l_in_octets) + as_1_a_in_octets) ;
       else
          as_val1 = (as_1_a_in_octets - as_1_l_in_octets) ;
       if (as_1_a_out_octets < as_1_l_out_octets)
          as_val2 = ((4294967295 - as_1_l_out_octets) + as_1_a_out_octets) ;
       else
          as_val2 = (as_1_a_out_octets - as_1_l_out_octets) ;
       as_val3 = as_val1 + as_val2;
       as_val3 = as_val3 / 3600 ;
       as_val3 = as_val3 * 8 ;
       as_val3 = as_val3 / 100000;

       if(as_val3 >= 50)
         {
         systema("mail -s gerencia bonato@lepus.celepar.br= 80)
         {
         systema("mail -s gerencia bonato@lepus.celepar.br 1)
         {
         systema("mail -s gerencia bonato@lepus.celepar.br 1)
         {
         systema("mail -s gerencia bonato@lepus.celepar.br 80)
          {
          systema("mail -s gerencia bonato@lepus.celepar.br 80)
          {
          systema("mail -s gerencia bonato@lepus.celepar.br