📄 sybase_func.c
字号:
#include "sybase_func.h"/*******************************************************************************\| Name : getMultValueFromShell || Description : get values from shell || Input Param : || Output Param : || Author : yangyong || History : Aug.20.2003 yangyong create || ||*******************************************************************************/int getMultValueFromShell(char *cpShellName,struct KPIValue *pKPIValues){ char FuncName[128] = {"getMultValueFromShell"}; char PipeStr[PIPE_STRING_LENGTH]; char command_Str[MAX_STRING_LENGTH]; int n = 0; int ret = 0; FILE *fin; memset(PipeStr,'\0',sizeof(PipeStr)); memset(command_Str,'\0',sizeof(command_Str));#ifdef MY_DEBUG sprintf(DebugMsg,"getMultValueFromShell Begin\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif sprintf(command_Str,"%s",cpShellName); fin = popen(command_Str,"r"); n = read(fileno(fin),PipeStr,sizeof(PipeStr)); if((ret = pclose(fin)) != 0) { /* means database server config file is not exist */#ifdef MY_DEBUG sprintf(DebugMsg,"Execute shell failed:%s\n",cpShellName}; writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_ERROR; } if(n <= 0) { /* means not found the config item in database server config file*/#ifdef MY_DEBUG sprintf(DebugMsg,"Pipe is NULL\n"); writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_NOTFOUND; } else {#ifdef MY_DEBUG sprintf(DebugMsg,"Pipe is:%s\n",PipeStr); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif } /* Because there maybe more than one value,so we will get each value from pipe */ parsePipeForValue(PipeStr,pKPIValues);#ifdef MY_DEBUG sprintf(DebugMsg,"getMultValueFromShell end OK\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif return RTN_SUCCESS;}/*******************************************************************************\| Name : parsePipeForValue || Description : parse pipe string for value || Input Param : || Output Param : || Author : yangyong || History : Aug.20.2003 yangyong create || ||*******************************************************************************/void parsePipeForValue(char *cpPipeStr,struct KPIValue *pKPIValues){ char FuncName[128] = {"parsePipeForValue"}; char *pHead = NULL,*pTail = NULL; char tempStr[MAX_STRING_LENGTH]; char tempPipeStr[PIPE_STRING_LENGTH]; int nodePos = QUEUE_HEAD;#ifdef MY_DEBUG sprintf(DebugMsg,"parsePipeForValue Begin\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif memset(tempStr,'\0',sizeof(tempStr)); memset(tempPipeStr,'\0',sizeof(tempPipeStr)); /* get each value */ sprintf(tempPipeStr,"%s",cpPipeStr); while(1) { pHead = strstr(tempPipeStr,SEPARATOR); if(!pHead) { break; } strncpy(tempStr,tempPipeStr,(pHead - tempPipeStr)); constructPutValues(tempStr,pKPIValues,nodePos); nodePos = QUEUE_NOT_HEAD; memset(tempStr,'\0',sizeof(tempStr)); sprintf(tempPipeStr,pHead+1); }#ifdef MY_DEBUG sprintf(DebugMsg,"parsePipeForValue End\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif return;}/*******************************************************************************\| Name : constructPutValues || Description : construct put values for KPI || Input Param : || Output Param : || Author : yangyong || History : Aug.20.2003 yangyong create || ||*******************************************************************************/void constructPutValues(char *tempStr,struct KPIValue *pKPIValues,int nodePos){ char FuncName[128] = {"constructPutValues"}; struct KPIValue *temp = NULL;#ifdef MY_DEBUG sprintf(DebugMsg,"constructPutValues Begin\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif if(tempStr == NULL || pKPIValues == NULL) { return; } if(nodePos == QUEUE_HEAD) { sprintf(pKPIValues->KPIValue,"%s",tempStr); } else { temp = malloc(sizeof(struct KPIValue)); if(!temp) {#ifdef MY_DEBUG sprintf(DebugMsg,"malloc memory failed\n"); writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return; } memset(temp,'\0',sizeof(struct KPIValue)); sprintf(temp->EntityID,"%s",pKPIValues->EntityID); sprintf(temp->CollTime,"%s",pKPIValues->CollTime); sprintf(temp->KPIID,"%s",pKPIValues->KPIID); sprintf(temp->KPIValue,"%s",StrTrim(tempStr)); temp->next = NULL; pKPIValues->next = temp; }#ifdef MY_DEBUG sprintf(DebugMsg,"constructPutValues End\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif return;}/*******************************************************************************\| Name : getSingleValueFromShell || Description : get Single Value From Shell || Input Param : || Output Param : || Author : yangyong || History : Aug.20.2003 yangyong create || ||*******************************************************************************/int getSingleValueFromShell(char *cpShellName,struct KPIValue *pKPIValues){ char FuncName[128] = {"getSingleValueFromShell"}; char PipeStr[PIPE_STRING_LENGTH]; int n = 0; int ret = 0; int nodePos = QUEUE_HEAD; FILE *fin;#ifdef MY_DEBUG sprintf(DebugMsg,"getSingleValueFromShell Begin\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif memset(PipeStr,'\0',sizeof(PipeStr)); fin = popen(cpShellName,"r"); n = read(fileno(fin),PipeStr,sizeof(PipeStr)); if((ret = pclose(fin)) != 0) { /* means database server config file is not exist */#ifdef MY_DEBUG sprintf(DebugMsg,"Execute shell failed:%s\n",cpShellName}; writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_ERROR; } if(n <= 0) { /* means not found the config item in database server config file*/#ifdef MY_DEBUG sprintf(DebugMsg,"Pipe is NULL\n"); writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_NOTFOUND; } else {#ifdef MY_DEBUG sprintf(DebugMsg,"Pipe is:%s\n",PipeStr); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif } constructPutValues(StrTrim(PipeStr),pKPIValues,nodePos); nodePos = QUEUE_NOT_HEAD;#ifdef MY_DEBUG sprintf(DebugMsg,"getMultValueFromShell end OK\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif return RTN_SUCCESS;}/*******************************************************************************\| Name : getShellValueFromFile || Description : get Shell Value From File || Input Param : || Output Param : || Author : yangyong || History : Aug.20.2003 yangyong create || ||*******************************************************************************/int getShellValueFromFile(char *cpShellName,struct KPIValue *pKPIValues,char *cpFileName){ char FuncName[128] = {"getShellValueFromFile"}; FILE *file = NULL; char line[MAX_STRING_LENGTH]; int nodePos = QUEUE_HEAD;#ifdef MY_DEBUG sprintf(DebugMsg,"getShellValueFromFile Begin\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif memset(line,'\0',sizeof(line)); /* execute this command */ system(cpShellName); /* read value from file */ file = fopen(cpFileName,"r"); if(file == NULL) {#ifdef MY_DEBUG sprintf(DebugMsg,"open file %s failed\n"); writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_ERROR; } while(1) { /* end of file */ if(feof(file) != 0){ break; } /* error of file */ if(ferror(file) != 0) {#ifdef MY_DEBUG sprintf(DebugMsg,"file %s error\n"); writeLog(FuncName,DebugMsg,ERROR_MSG);#endif return RTN_FAIL; } memset(line,'\0',sizeof(line)); /* read file as each line */ if(fgets(line,1024,file)) { /* means this line is null */ if(strlen(trim(line)) == 0 || line[0] == '\n') { continue; } /* now get this line's content,send it as KPI Value */ /* construct Put Values */ constructPutValues(line,pKPIValues,nodePos); nodePos = QUEUE_NOT_HEAD; } } fclose(file); /* put KPI Value to Queue */ putKPIValueQueue(pKPIValues);#ifdef MY_DEBUG sprintf(DebugMsg,"getShellValueFromFile end OK\n"); writeLog(FuncName,DebugMsg,DEBUG_MSG);#endif return RTN_SUCCESS;}void writeLog(char *FuncName,char *DebugMsg,int level){ if(level == DEBUG_MSG) { printf("Debug: %s %s\n",FuncName,DebugMsg); } else { printf("Error: %s %s\n",FuncName,DebugMsg); }}void printKPIValue(struct KPIValue *pKPIValues){ struct KPIValue *temp = NULL; printf("\n====== KPIValues is:=========\n"); for(temp = pKPIValues;temp;temp=temp->next) { printf("EntityID is: %s\n",temp->EntityID); printf("CollTime is: %s\n",temp->CollTime); printf("KPIID is: %s\n",temp->KPIID); printf("KPIValue is: %s\n",temp->KPIValue); if(temp->next) { printf("\n"); } } printf("====== KPIValues End =========\n\n");}/*******************************************************************************\| Name : warning_handler || Description : warning handler || Input Param : || Output Param : || Author : yangyong || History : Sept.15.2003 yangyong create || ||*******************************************************************************//*char *StrTrim(char *pString){ int length = MAX_STRING_LENGTH; int i; int j=0; char temp1[length]; char temp2[length]; if(!pString) { return NULL; } memset(temp1,'\0',sizeof(temp1)); memset(temp2,'\0',sizeof(temp2)); sprintf(temp1,"%s",pString); for(i=0;i<length;++i) { if(temp1[i] != '\n' && temp1[i] != '\0' && temp1[i] != ' ' && temp1[i] != '\t') { temp2[j]=temp1[i]; j++; } } temp2[i] = '\0'; memset(pString,'\0',sizeof(pString)); sprintf(pString,"%s",temp2); return pString;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -