#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#include <sys/ddi.h>
#include <unistd.h>
#include "snmp.h"
#include "asn1.h"
#include "snmp_impl.h"
#include "snmp_api.h"
#include "snmp_client.h"
int snmp_dump_packet = 0;
/*#define MYDEBUG*/
#define ETHERFILE "/home/uel/cce/dcop/mestrado/cinthyan/work1/ether.log"
#define SERFILE "/home/uel/cce/dcop/mestrado/cinthyan/work1/serial.log"
#define HTMETHER "/home/uel/cce/dcop/mestrado/cinthyan/public_html/cint33.htm"
#define HTMSERIAL "/home/uel/cce/dcop/mestrado/cinthyan/public_html/cint34.htm"
/*tempo em segundos entre as amostragens */
#define MYTIMETOWAIT 300
main()
{
/************************************************
Estruturas utilizadas pelas funcoes do cmu-snmp.
*************************************************/
struct snmp_session session, *ss;
struct snmp_pdu *pdu, *response;
struct variable_list *vars,*headvars ;
oid name[MAX_NAME_LEN];
int name_length;
int status;
/* some of my vars*/
int counter;
int i;
int k=0;
char *mypstr;
struct tm *ptrtime;
time_t loctime;
int loopcont=0;
int samplecont=0;
unsigned parasleep;
FILE *fp, *fht;
char mystraux1[255];
char mystraux2[255];
char Xtime[288][30];
int XifInOctets1[288];
int XifOutOctets1[288];
int XifInDiscards1[288];
int XifInErrors1[288];
int XifInUnknownProtos1[288];
int XifOutDiscards1[288];
int XifOutErrors1[288];
int XifOutQLen1[288];
float TR1=0;
int totINDISC1=0;
int totINERR1=0;
int totINUNK1=0;
int totOUTDIS1=0;
int totOUTERR1=0;
float M_TR1=0;
float M_INDISC1=0;
float M_INERR1=0;
float M_INUNK1=0;
float M_OUTDIS1=0;
float M_OUTERR1=0;
int XifInOctets2[288];
int XifOutOctets2[288];
int XifInDiscards2[288];
int XifInErrors2[288];
int XifInUnknownProtos2[288];
int XifOutDiscards2[288];
int XifOutErrors2[288];
int XifOutQLen2[288];
int maiordoTR=0;
float TR2=0;
int totINDISC2=0;
int totINERR2=0;
int totINUNK2=0;
int totOUTDIS2=0;
int totOUTERR2=0;
float M_TR2=0;
float M_INDISC2=0;
float M_INERR2=0;
float M_INUNK2=0;
float M_OUTDIS2=0;
float M_OUTERR2=0;
/********* Vars and Tables to data sampling: Warning> do not touch this,
some further data types conversion are position dependents!
Snif, snif... ************/
int objs_qtd= 19;
char mybuff[128][255];
char objetos[128][128]={
/*01*/"system.sysName.0\0",
/*---------ET--------*/
/*02*/"interfaces.ifTable.ifEntry.ifType.1\0",
/*03*/"interfaces.ifTable.ifEntry.ifInOctets.1\0",
/*04*/"interfaces.ifTable.ifEntry.ifOutOctets.1\0",
/*05*/"interfaces.ifTable.ifEntry.ifInDiscards.1\0",
/*06*/"interfaces.ifTable.ifEntry.ifInErrors.1\0",
/*07*/"interfaces.ifTable.ifEntry.ifInUnknownProtos.1\0",
/*08*/"interfaces.ifTable.ifEntry.ifOutDiscards.1\0",
/*09*/"interfaces.ifTable.ifEntry.ifOutErrors.1\0",
/*10*/"interfaces.ifTable.ifEntry.ifOutQLen.1\0",
/*--------Serie------*/
/*11*/"interfaces.ifTable.ifEntry.ifType.2\0",
/*12*/"interfaces.ifTable.ifEntry.ifInOctets.2\0",
/*13*/"interfaces.ifTable.ifEntry.ifOutOctets.2\0",
/*14*/"interfaces.ifTable.ifEntry.ifInDiscards.2\0",
/*15*/"interfaces.ifTable.ifEntry.ifInErrors.2\0",
/*16*/"interfaces.ifTable.ifEntry.ifInUnknownProtos.2\0",
/*17*/"interfaces.ifTable.ifEntry.ifOutDiscards.2\0",
/*18*/"interfaces.ifTable.ifEntry.ifOutErrors.2\0",
/*19*/"interfaces.ifTable.ifEntry.ifOutQLen.2\0"
};
/*****end do not touch this session******/
/***********************************
Definicao do objeto a ser buscado
***********************************/
char *maquina="200.250.10.127"
char *comunidade = "public"
/*******MIB and structures initializing; open conection****/
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;
while (1){
snmp_synch_setup(&session);
ss = snmp_open(&session);
if (ss == NULL){
printf("Nao conseguiu abrir sessao\n");
return NULL;
}
/****END MIB and structures initializing; and conection is open!****/
/******** BEGIN SAMPLE: just sampling some data at router *********/
for (counter=0;counter<objs_qtd;counter++)
{
name_length = MAX_NAME_LEN;
pdu = snmp_pdu_create(GET_REQ_MSG);
if (!read_objid(objetos[counter], name, &name_length))
{ printf("Objeto inexistente: %s\n", objetos[counter]);
exit(1);
}
snmp_add_null_var(pdu, name, name_length);
/*------get time-----*/
loctime=time(NULL);ptrtime=localtime(&loctime);
sprintf(Xtime[loopcont],"%s",asctime(ptrtime));
/*------time done----*/
status = snmp_synch_response(ss, pdu, &response);
if(status == STAT_SUCCESS)
{ if(response->errstat == SNMP_ERR_NOERROR)
{ vars=response->variables;
sprint_variable(mybuff[counter],vars->name,vars->name_length,vars);
}
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!\n");
if (response)
snmp_free_pdu(response);
}
/*converting strīs to intīs*/
mypstr=(char *)strpbrk(mybuff[2],"=");mypstr+=2;
XifInOctets1[loopcont]=atoi(mypstr);
mypstr=(char *)strpbrk(mybuff[3],"=");mypstr+=2;
XifOutOctets1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[4],"=");mypstr+=2;
XifInDiscards1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[5],"=");mypstr+=2;
XifInErrors1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[6],"=");mypstr+=2;
XifInUnknownProtos1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[7],"=");mypstr+=2;
XifOutDiscards1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[8],"=");mypstr+=2;
XifOutErrors1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[9],"=");mypstr+=2;
XifOutQLen1[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[11],"=");mypstr+=2;
XifInOctets2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[12],"=");mypstr+=2;
XifOutOctets2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[13],"=");mypstr+=2;
XifInDiscards2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[14],"=");mypstr+=2;
XifInErrors2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[15],"=");mypstr+=2;
XifInUnknownProtos2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[16],"=");mypstr+=2;
XifOutDiscards2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[17],"=");mypstr+=2;
XifOutErrors2[loopcont]=atol(mypstr);
mypstr=(char *)strpbrk(mybuff[18],"=");mypstr+=2;
XifOutQLen2[loopcont]=atol(mypstr);
/***** END SAMPLE *****/
snmp_close(ss);
loopcont++;
#ifdef MYDEBUG
/*ETHER*/
printf("-----------------------------\n");
for (i=0;i<loopcont;i++){
printf("ETHER %s" ,Xtime[i]);
printf(" InOct[%d]" ,XifInOctets1[i]);
printf("OutOct[%d]" ,XifOutOctets1[i]);
printf("InDisc[%d]" ,XifInDiscards1[i]);
printf("InErro[%d]" ,XifInErrors1[i]);
printf("InUPro[%d]" ,XifInUnknownProtos1[i]);
printf("OutDis[%d]" ,XifOutDiscards1[i]);
printf("OutErr[%d]\n",XifOutErrors1[i]);
}
/*serial*/
for (i=0;i<loopcont;i++){
printf("SERIAL %s" ,Xtime[i]);
printf(" InOct[%d]" ,XifInOctets2[i]);
printf("OutOct[%d]" ,XifOutOctets2[i]);
printf("InDisc[%d]" ,XifInDiscards2[i]);
printf("InErro[%d]" ,XifInErrors2[i]);
printf("InUPro[%d]" ,XifInUnknownProtos2[i]);
printf("OutDis[%d]" ,XifOutDiscards2[i]);
printf("OutErr[%d]\n",XifOutErrors2[i]);
}
#endif