Basicamente, eu escrevi algumas linhas de código para ler os dados dos arquivos de configuração (descritos no post anterior) e salvá-los na estrutura de dados da mib ENTITY-SENSOR-MIB. Os arquivos alterados foram o entPhySensorTable_data_access.c e o entPhySensorTable_data_access.h.
O parâmetro SensorsQuantity do arquivo "sensoresconfig" é utilizado neste programa pra definir o tamanho da tabela de dados da mib. (Depois explicarei com maior detalhes, pois ainda isso é meio obscuro pra mim!)
Os parâmetros SensorType, SensorScale, SensorPrecision, SensorOperStatus, SensorSampleValue, SensorUnitsDisplay, SensorValueTimeStamp, SensorValueUpdateRate do arquivo "sensorX" são passados para a estrutura de dados criada para cada sensor para serem consultados quando o usuário chama as entidades da mib.
entPhySensorType = SensorType
entPhySensorScale = SensorScale
entPhySensorPrecision = SensorPrecision
entPhySensorValue = SensorValue (após conversão com os valores de SensorSampleValue, SensorARef e SensorConversionValue)
entPhySensorOperStatus = SensorOperStatus
entPhySensorValueTimeStamp = SensorValueTimeStamp
entPhySensorValueUpdateRate = SensorValueUpdateRate
Lembrando que o parâmetro SensorSampleValue, SensorARef e SensorConversionValue são convertidos dentro do programa pra gerar o parâmetro SensorValue.
Função alterada no arquivo entPhySensorTable_data_access.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | int entPhySensorTable_container_load(netsnmp_container *container) { entPhySensorTable_rowreq_ctx *rowreq_ctx; long entPhysicalIndex = 0; char conteudoBuf[MAX_BUFFER_CONTEUDO]; char bufAuxiliar[MAX_BUFFER_CONTEUDO]; unsigned int quantidadeSensores, i; struct sensorTable_data sensorTemp; DEBUGMSGTL(("verbose:entPhySensorTable:entPhySensorTable_container_load","called\n")); if(leDadosArquivo(LOCAL_ARQ_SENSORESCONFIG, -1, conteudoBuf) == OK) { for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorsQuantityText, bufAuxiliar) == OK) { quantidadeSensores = atoi(bufAuxiliar); if((quantidadeSensores > 0) && (quantidadeSensores <= MAX_SENSORES)) ;//flagAmostraStatus = TRUE; else { printf("\t> ERRO: Quantidade de sensores configurado incorretamente (Valor deve ser maior entre 1 a %d).", MAX_SENSORES); printf("\t Refaca as configuracoes dos sensores para obter as leituras.\n"); } } else { printf("\t> AVISO: Dados incorretos.\n"); printf("\t> ERRO: Refaca as configuracoes dos sensores para obter as leituras.\n"); } } else printf("\t> AVISO: Arquivo nao pode ser lido.\n"); rowreq_ctx = malloc(sizeof(entPhySensorTable_rowreq_ctx)*quantidadeSensores); while( entPhysicalIndex < quantidadeSensores ) { for(i=0; i < sizeof conteudoBuf; i++) conteudoBuf[i] = 0; for(i=0; i < sizeof sensorTemp.sensorUnitsDisplay; i++) sensorTemp.sensorUnitsDisplay[i] = 0; if(leDadosArquivo(LOCAL_ARQ_SENSORCONFIG, entPhysicalIndex, conteudoBuf) == OK) { for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorTypeText, bufAuxiliar) == OK) { sensorTemp.sensorType = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorScaleText, bufAuxiliar) == OK) { sensorTemp.sensorScale = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorPrecisionText, bufAuxiliar) == OK) { sensorTemp.sensorPrecision = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorSampleValueText, bufAuxiliar) == OK) { sensorTemp.sensorValue = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorOperStatusText, bufAuxiliar) == OK) { sensorTemp.sensorOperStatus = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorValueTimeStampText, bufAuxiliar) == OK) { sensorTemp.sensorValueTimeStamp = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorValueUpdateRateText, bufAuxiliar) == OK) { sensorTemp.sensorValueUpdateRate = atoi(bufAuxiliar); } else { } for(i=0; i < sizeof bufAuxiliar; i++) bufAuxiliar[i] = 0; if(leConteudo(conteudoBuf, SensorUnitsDisplayText, bufAuxiliar) == OK) { memcpy(sensorTemp.sensorUnitsDisplay, bufAuxiliar, ((strlen(bufAuxiliar)+1) < MAX_LINE_SIZE) ? (strlen(bufAuxiliar)+1) : MAX_LINE_SIZE); sensorTemp.sensorUnitsDisplay_len = strlen(sensorTemp.sensorUnitsDisplay) * sizeof(char); } else { } } else { printf("\t> AVISO: Arquivo %s%d nao pode ser lido.\n", LOCAL_ARQ_SENSORCONFIG, entPhysicalIndex); printf("\t> Dados DEFAULT retornados!\n"); sensorTemp.sensorType = SENSOR_TYPE_DEFAULT; sensorTemp.sensorScale = SENSOR_SCALE_DEFAULT; sensorTemp.sensorPrecision = SENSOR_PRECISION_DEFAULT; sensorTemp.sensorValue = SENSOR_SAMPLE_VALUE_DEFAULT; sensorTemp.sensorOperStatus = SENSOR_OPER_STATUS_DEFAULT; memcpy(sensorTemp.sensorUnitsDisplay, SENSOR_UNITS_DISPLAY_DEFAULT, ((strlen(SENSOR_UNITS_DISPLAY_DEFAULT)+1) < MAX_LINE_SIZE) ? (strlen(SENSOR_UNITS_DISPLAY_DEFAULT)+1) : MAX_LINE_SIZE); sensorTemp.sensorUnitsDisplay_len = strlen(sensorTemp.sensorUnitsDisplay) * sizeof(char); sensorTemp.sensorValueTimeStamp = SENSOR_VALUE_TIMESTAMP_DEFAULT; sensorTemp.sensorValueUpdateRate = SENSOR_VALUE_UPDATE_RATE_DEFAULT; } /* ***---------------------------------------------*** *** END EXAMPLE CODE *** ***************************************************/ /* * TODO:352:M: | |-> set indexes in new entPhySensorTable rowreq context. * data context will be set from the param (unless NULL, * in which case a new data context will be allocated) */ rowreq_ctx = entPhySensorTable_allocate_rowreq_ctx(NULL); if (NULL == rowreq_ctx) { printf("rowreq_ctx==NULL\n"); snmp_log(LOG_ERR, "memory allocation failed\n"); return MFD_RESOURCE_UNAVAILABLE; } if(MFD_SUCCESS != entPhySensorTable_indexes_set(rowreq_ctx , entPhysicalIndex )) { printf("entPhySensorTable_indexes_set!=MFD_SUCCESS\n"); snmp_log(LOG_ERR,"error setting index while loading " "entPhySensorTable data.\n"); entPhySensorTable_release_rowreq_ctx(rowreq_ctx); continue; } printf("\nrowreq_ctx.tbl_idx.entPhysicalIndex = %d \n", rowreq_ctx->tbl_idx.entPhysicalIndex); /* * TODO:352:r: | |-> populate entPhySensorTable data context. * Populate data context here. (optionally, delay until row prep) */ rowreq_ctx->data.entPhySensorType = sensorTemp.sensorType; rowreq_ctx->data.entPhySensorScale = sensorTemp.sensorScale; rowreq_ctx->data.entPhySensorPrecision = sensorTemp.sensorPrecision; rowreq_ctx->data.entPhySensorValue = sensorTemp.sensorValue; rowreq_ctx->data.entPhySensorOperStatus = sensorTemp.sensorOperStatus; rowreq_ctx->data.entPhySensorUnitsDisplay_len = sensorTemp.sensorUnitsDisplay_len; memcpy( rowreq_ctx->data.entPhySensorUnitsDisplay, sensorTemp.sensorUnitsDisplay, sensorTemp.sensorUnitsDisplay_len ); rowreq_ctx->data.entPhySensorValueTimeStamp = sensorTemp.sensorValueTimeStamp; rowreq_ctx->data.entPhySensorValueUpdateRate = sensorTemp.sensorValueUpdateRate; //snmp_log(LOG_ERR,"rowreq_ctx->data.entPhySensorUnitsDisplay_len = %d\n", rowreq_ctx->data.entPhySensorUnitsDisplay_len); //snmp_log(LOG_ERR,"rowreq_ctx->data.entPhySensorUnitsDisplay = %s\n", rowreq_ctx->data.entPhySensorUnitsDisplay); /* * insert into table container */ entPhysicalIndex++; CONTAINER_INSERT(container, rowreq_ctx); } printf("inserted %d records\n", entPhysicalIndex); return MFD_SUCCESS; } /* entPhySensorTable_container_load */ |
Funções criadas para manipular arquivos (estão no arquivo entPhySensorTable_data_access.c):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | /******************************************************************************* * Nome da Função : int leDadosArquivo(char *, int, char *) * Retorna : inteiro (0 ou -1) p/ verificação de erro * Criado por : Everson Osvanir da Silva * Data De Criação : 17/03/2016 * Descrição : Abre o arquivo e armazena todo seu conteudo no buffer apontado * pelo char *conteudoBuf * Notas : *******************************************************************************/ int leDadosArquivo(char *caminho, int indice, char *conteudoBuf) { FILE *fp; char arquivoLocal[MAX_BUFFER_ARQUIVO]; unsigned int i = 0; //char conteudoBuffer[MAX_BUFFER_CONTEUDO]; if(indice >= 0) sprintf(arquivoLocal, "%s%d", caminho, indice); else sprintf(arquivoLocal, "%s", caminho); fp = fopen(arquivoLocal, "r"); if(fp == NULL) { //DBG(DBG_TRACE, "Arquivo modo leitura nao pode ser aberto."); return -1; } i=0; while(!feof(fp)){ /* Enquanto n�o chegar ao final do arquivo */ conteudoBuf[i]=getc(fp); /* imprime o caracter lido */ i++; } fclose(fp); i--; conteudoBuf[i] = 0; //Limpa "lixo" no final do arquivo //DBG(DBG_TRACE, "CONTEUDO ORIGINAL:\n%s\n", conteudoBuf); return 0; } /******************************************************************************* * Nome da Função : int leConteudo(char *, char *, char *) * Retorna : inteiro (0 ou -1) p/ verificação de erro * Criado por : Everson Osvanir da Silva * Data De Criação : 17/03/2016 * Descrição : Le o valor do conteudo do arquivo referente a linha que contem o * char *partConteudo. * Notas : *******************************************************************************/ int leConteudoArquivo(char *caminho, char *partConteudo, char *valor) { char buf[MAX_BUFFER_CONTEUDO]; char *pBuf; unsigned int x, y=0; if(leDadosArquivo(caminho, -1, buf) == ERROR) { //DBG(DBG_TRACE, "Arquivo modo leitura nao pode ser aberto."); return -1; } pBuf = strstr(buf, partConteudo); if(pBuf == NULL) { //DBG(DBG_TRACE, "Arquivo nao contem o dado solicitado: %s", partConteudo); return -1; } for(x=0; &buf[x] != pBuf; x++); for(;buf[x] != CARACTER_SEPARACAO; x++); x++; for(;buf[x] != CARACTER_FIMLINHA; x++) { valor[y] = buf[x]; y++; } return 0; } /******************************************************************************* * Nome da Função : int leConteudo(char *, char *, char *) * Retorna : inteiro (0 ou -1) p/ verificação de erro * Criado por : Everson Osvanir da Silva * Data De Criação : 17/03/2016 * Descrição : Le o valor do conteudo do buffer referente a linha que contem o * char *partConteudo. * Notas : *******************************************************************************/ int leConteudo(char *conteudoBuf, char *partConteudo, char *valor) { char *pBuf; unsigned int x, y=0; //i = strlen(conteudoBuf); pBuf = strstr(conteudoBuf, partConteudo); if(pBuf == NULL) { //DBG(DBG_TRACE, "Arquivo nao contem o dado solicitado: %s", partConteudo); return -1; } for(x=0; &conteudoBuf[x] != pBuf; x++); for(;conteudoBuf[x] != CARACTER_SEPARACAO; x++); x++; for(;conteudoBuf[x] != CARACTER_FIMLINHA; x++) { valor[y] = conteudoBuf[x]; y++; } return 0; } |
Alterações no arquivo entPhySensorTable_data_access.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | /* *************************************************** *** START EXAMPLE CODE *** ***---------------------------------------------***/ /* ********************************************************************* * Since we have no idea how you really access your data, we'll go with * a worst case example: a flat text file. */ #define MAX_LINE_SIZE 255 #define MAX_BUFFER_CONTEUDO 1024 #define MAX_BUFFER_ARQUIVO 256 #define OK 0 #define ERROR -1 /* DADOS DEFAULT PARA entPhySensorTable */ #define SENSOR_TYPE_DEFAULT 2 //unknown(2): unknown measurement, or arbitrary, relative numbers #define SENSOR_SCALE_DEFAULT 9 //units(9), -- 10^0 #define SENSOR_PRECISION_DEFAULT 1 #define SENSOR_UNITS_DISPLAY_DEFAULT "desconhecido ou nao operacional" #define SENSOR_VALUE_UPDATE_RATE_DEFAULT 0 #define SENSOR_SAMPLE_VALUE_DEFAULT 0 #define SENSOR_VALUE_TIMESTAMP_DEFAULT 0 #define SENSOR_OPER_STATUS_DEFAULT 3 //nonoperational(3) /* SENSOR CONFIGURACAO */ #define SensorTypeText "SensorType" #define SensorScaleText "SensorScale" #define SensorPrecisionText "SensorPrecision" #define SensorUnitsDisplayText "SensorUnitsDisplay" #define SensorValueUpdateRateText "SensorValueUpdateRate" #define SensorSampleValueText "SensorSampleValue" #define SensorValueTimeStampText "SensorValueTimeStamp" #define SensorOperStatusText "SensorOperStatus" /* SENSORES CONFIGURACAO */ #define MAX_SENSORES 7 #define SensorsQuantityText "SensorsQuantity" #define SensorsWaitTimeSampleText "SensorsWaitTimeSample" #define CARACTER_SEPARACAO '\t' #define CARACTER_FIMLINHA '\n' #define LOCAL_ARQ_SENSORCONFIG "/home/debian/cebra/confs/sensor" #define LOCAL_ARQ_SENSORESCONFIG "/home/debian/cebra/confs/sensoresconfig" #define N_TENTATIVAS 3 #define TENTATIVAS_OVERFLOW N_TENTATIVAS+1 struct sensorTable_data { unsigned long int sensorType; unsigned long int sensorScale; long int sensorPrecision; long int sensorValue; unsigned long int sensorOperStatus; char sensorUnitsDisplay[MAX_LINE_SIZE]; size_t sensorUnitsDisplay_len; unsigned long int sensorValueTimeStamp; unsigned long int sensorValueUpdateRate; }; int leDadosArquivo(char *caminho, int indice, char *conteudoBuf); int leConteudoArquivo(char *caminho, char *partConteudo, char *valor); int leConteudo(char *conteudoBuf, char *partConteudo, char *valor); /* ***---------------------------------------------*** *** END EXAMPLE CODE *** ***************************************************/ |
Pode haver alterações futuras nesses arquivos.
Nenhum comentário:
Postar um comentário