【Notes19】文件监控

文章目录


1.拼接字符串再fprintf进文件

char reason[LOG_BUF_SIZE]={0};
int reason_index=0;

int WriteLed(char *led_color_code,led_node_t *led_node)
{
    FILE *fpLedCtrl=fopen(led_node->led_ctrl,"w");
    FILE *fpLedColor=fopen(led_node->led_color,"w");
    fprintf(fpLedCtrl,"%s",LED_CTRL_EN_CODE);
    fprintf(fpLedColor,"%s",led_color_code);
    fclose(fpLedCtrl);
    fclose(fpLedColor);
    return 0;
}

int WriteSensorMonResult(char *result,char *led_color)
{
    FILE *fpLedLog=fopen(SENSOR_MON_LED_LOG_FILE,"w");
    fprintf(fpLedLog,"Result : %s\r\nColor  : %s\r\nReason : \n%s\n",result,led_color,reason);
    fclose(fpLedLog);
    return 0;
}

static void CompareValueThreshold(int realvalue, sensor_node_t * node)
{
    int min_value,max_value;
    char unit;
    sen_sta_type_t sta = SENSOR_NORMAL;
    min_value = node->minvalue;
    max_value = node->maxvalue;
    unit = node->unit;
    if( realvalue < min_value || realvalue > max_value)
    {
        sta = SENSOR_ABNORMAL;
        reason_index++;
        char buf[LOG_BUF_SIZE]={0};
        sprintf(buf,"(%d) %s is abnormal, current value is %.2f %c, threshold is %.2f ~ %.2f %c\n",
            reason_index,node->desc,(double)realvalue/1000,unit,(double)min_value/1000,(double)max_value/1000,unit);
        strcat(reason,buf);
    }
    if(node->sen_status != sta)
    {
        node->sen_status = sta;
        if(sta)
            RecordEventLog(LOG_ERR,"%s is abnormal, current value is %.2f %c, threshold is %.2f ~ %.2f %c\n",node->desc,(double)realvalue/1000,unit,(double)min_value/1000,(double)max_value/1000,unit);
        else
            RecordEventLog(LOG_INFO,"%s is normal, current value is %.2f %c\n",node->desc,(double)realvalue/1000,unit);
    }
    return;
}

static void RunSensorMon(sensor_node_t * node,led_node_t *led_node)
{
    int i=0,realvalue;
    RecordEventLog(LOG_INFO,"Sensor-mon started...\n");
    while (1)
    {
        memset(reason,0,sizeof(reason));
        reason_index=0;
        for(i=0;i<arraysize;i++)
        {
            realvalue=ReadNodeValue(&node[i]);
            if( realvalue >= 0 )
            {
                CompareValueThreshold(realvalue,&node[i]);
                if(node->sen_status != SENSOR_NORMAL)
                {
                    node->sen_status = SENSOR_NORMAL;
                    RecordEventLog(LOG_INFO,"%s is normal,%s can found\n",node[i].desc,node[i].path);

                    reason_index++;
                    char buf[LOG_BUF_SIZE]={0};
                    sprintf(buf,"(%d) %s is normal,%s can found\n", reason_index,node[i].desc,node[i].path);
                    strcat(reason,buf);
                }
            }
            else
            {
                if(node->sen_status != SENSOR_ABNORMAL)
                {
                    node->sen_status = SENSOR_ABNORMAL;
                    RecordEventLog(LOG_INFO,"%s is abnormal,%s not found\n",node[i].desc,node[i].path);

                    reason_index++;
                    char buf[LOG_BUF_SIZE]={0};
                    sprintf(buf,"(%d) %s is abnormal,%s not found\n", reason_index,node[i].desc,node[i].path);
                    strcat(reason,buf);
                }
            }
        }

        if (reason_index == 0)
        {
            WriteLed(LED_GREEN_CODE,led_node);
            WriteSensorMonResult("OK","Green");
        }
        else
        {
            WriteLed(LED_YELLOW_CODE,led_node);
            WriteSensorMonResult("Bad","Yellow");
        }
        sleep(MIN_POLL_INTERVAL);
    }
    return;
}

2.获取调用函数的参数字符串

2.1 main.c

#include <unistd.h>
#include "sensor-mon.h"
static int arraysize=0;
int reason_index=0;

//open,	fill, Run(Read,Com)
static void CompareValueThreshold(int realvalue, sensor_node_t * node)
{
    int min_value,max_value;
    char unit;
    sen_sta_type_t sta = SENSOR_NORMAL;
    min_value = node->minvalue;
    max_value = node->maxvalue;
    unit = node->unit;
    if( realvalue < min_value || realvalue > max_value)
    {
        sta = SENSOR_ABNORMAL;
        WriteSensorMonResult(sta,"(%d) %s is abnormal, current value is %.2f %c, threshold is %.2f ~ %.2f %c\n",
            ++reason_index,node->desc,(double)realvalue/1000,unit,(double)min_value/1000,(double)max_value/1000,unit);
    }
    
	//一轮结束后:不正常AA , 正常NN
    if(node->sen_sta_compare_value_threshold != sta)
    {
        node->sen_sta_compare_value_threshold = sta;
        if(sta)
            RecordEventLog(LOG_ERR,"%s is abnormal, current value is %.2f %c, threshold is %.2f ~ %.2f %c\n",node->desc,(double)realvalue/1000,unit,(double)min_value/1000,(double)max_value/1000,unit);
        else
            RecordEventLog(LOG_ERR,"%s is normal, current value is %.2f %c\n",node->desc,(double)realvalue/1000,unit);
    }
    return;
}

static void RunSensorMon(sensor_node_t * node,led_node_t *led_node)
{
    int i=0,realvalue;
    RecordEventLog(LOG_INFO,"Sensor-mon started...\n");
    while (1)
    {
        FILE *fpLedLog=fopen(SENSOR_MON_LED_LOG_FILE,"w");
        fclose(fpLedLog);
        //RecordEventLog(LOG_INFO,"\n-----------------------------------------------------------------\n");
        reason_index=0;
        for(i=0;i<arraysize;i++)
        {
            sen_sta_type_t sta = SENSOR_NORMAL;
            realvalue=ReadNodeValue(&node[i]);
            if( realvalue >= 0 )
                CompareValueThreshold(realvalue,&node[i]);
            else
            {
                sta=SENSOR_ABNORMAL;
                WriteSensorMonResult(sta,"(%d) %s %s %d: [%d] %s is abnormal,%s not found\n",++reason_index,__FILE__,__func__,__LINE__,i,node[i].desc,node[i].path);
            }

            if(node[i].sen_sta_run_sensor_mon != sta)
            {
                node[i].sen_sta_run_sensor_mon = sta;
                if(sta)
                    RecordEventLog(LOG_INFO,"\n%s %s %d: [%d] %s is abnormal,%s not found\n",__FILE__,__func__,__LINE__,i,node[i].desc,node[i].path);
                else
                    RecordEventLog(LOG_INFO,"\n%s %s %d: [%d] %s is normal,%s can found\n",__FILE__,__func__,__LINE__,i,node[i].desc,node[i].path);
            }
        }

        if (reason_index == 0)
        {
            WriteLed(LED_GREEN_CODE,led_node);
            WriteSensorMonResult(SENSOR_NORMAL,"");
        }
        else
        {
            WriteLed(LED_YELLOW_CODE,led_node);
        }
        sleep(MIN_POLL_INTERVAL);
    }
    return;
}

static int FillSensorNodeArray(const char*content,sensor_node_t ** sen_node,led_node_t *led_node)
{
    cJSON *json_all,*json_array,*json_element;
    sensor_node_t * node=NULL;
    char fullpath[MAX_FILE_PATH_LEN];
    int i,rc=0;

    json_all=cJSON_Parse(content);
    if (!json_all)
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Parse cJSON fail : [%s]\n",__FILE__,__func__,__LINE__,cJSON_GetErrorPtr());
        goto err;
    }

    json_array=cJSON_GetObjectItem(json_all,SENSOR_MON_KEY);
    if( !json_array )  
    {  
        RecordEventLog(LOG_ERR,"%s %s %d:Get %s failed !!!\n",__FILE__,__func__,__LINE__,SENSOR_MON_KEY);
        goto err;
    }
    arraysize=cJSON_GetArraySize(json_array);
    node = (sensor_node_t *)malloc(sizeof(sensor_node_t)*arraysize);
    if(node == 0)
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Malloc memory failed\n",__FILE__,__func__,__LINE__);
        goto err;
    }

    RecordEventLog(LOG_INFO,"The totally monitored sensor quantity is %d\n",arraysize);
    for(i=0;i<arraysize;i++)
    {
        json_element=cJSON_GetArrayItem(json_array,i);
        rc = 0;
        rc |= GetObjectStrValue(json_element,SENSOR_MON_PATH_STR,node[i].path);
        rc |= GetObjectStrValue(json_element,SENSOR_MON_NODE_STR,node[i].node);
        rc |= GetObjectStrValue(json_element,SENSOR_MON_NAME_STR,node[i].name);
        rc |= GetObjectStrValue(json_element,SENSOR_MON_DESC_STR,node[i].desc);
        rc |= GetObjectStrValue(json_element,SENSOR_MON_UNIT_STR,&node[i].unit);
        rc |= GetObjectIntValue(json_element,SENSOR_MON_MIN_STR,&node[i].minvalue);
        rc |= GetObjectIntValue(json_element,SENSOR_MON_MAX_STR,&node[i].maxvalue);
        rc |= GetObjectDoubleValue(json_element,SENSOR_MON_RATIO_STR,&node[i].ratio);
        node[i].sen_status                       = SENSOR_NORMAL;
        node[i].sen_sta_compare_value_threshold  = SENSOR_NORMAL;
        node[i].sen_sta_run_sensor_mon           = SENSOR_NORMAL;
        node[i].sen_sta_read_node_value_open_file= SENSOR_NORMAL;
        node[i].sen_sta_read_node_value_read_file= SENSOR_NORMAL;
        memset(fullpath,0,MAX_FILE_PATH_LEN);
        QueryFileFullPath(node[i].path,node[i].node,fullpath);
        if (fullpath[0] == 0)
        {
            RecordEventLog(LOG_ERR,"%s %s %d:Query %s %s fullpath fail\n",__FILE__,__func__,__LINE__,node[i].path,node[i].node);
            continue;
        }
        else
            strcpy(node[i].path,fullpath);

        if(rc)
        {
            RecordEventLog(LOG_ERR,"%s %s %d:Fill array fail\n",__FILE__,__func__,__LINE__);
            goto err;
        }
    }

    cJSON *json_led=cJSON_GetObjectItem(json_all,SENSOR_MON_LED_PATH_STR);
    if( !json_led )
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Get %s failed !!!\n",__FILE__,__func__,__LINE__,SENSOR_MON_LED_PATH_STR);
        goto err;
    }
    rc |= GetObjectStrValue(json_led,SENSOR_MON_LED_CTRL_KEY,led_node->led_ctrl);
    rc |= GetObjectStrValue(json_led,SENSOR_MON_LED_COLOR_KEY,led_node->led_color);
    rc |= GetObjectStrValue(json_led,SENSOR_MON_LED_FREQUENCY_KEY,led_node->led_frequency);
    rc |= GetObjectStrValue(json_led,SENSOR_MON_LED_STATUS_KEY,led_node->led_status);
    if(rc)
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Fill array fail\n",__FILE__,__func__,__LINE__);
        goto err;
    }

    *sen_node=node;
    cJSON_Delete(json_all); 

    FILE *fright=fopen("/tmp/right","w");
    for(i=0;i<arraysize;i++)
    {
        fprintf(fright,"[%d] %s (%s)\r\n",i,node[i].path,node[i].desc);
    }
    fclose(fright);

    return 0;

err:
    {
        cJSON_Delete(json_all);
        if(!node)
            free(node);
        return -1;
    }
}

static long OpenCjsonFile(char **content)
{
    FILE * fp;
    long len;
    int rc;
    char * str=NULL;

    fp=fopen(CJSON_FILE_PATH,"rb");
    if(!fp)
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Open file %s fail,eixt!!\n",__FILE__,__func__,__LINE__,CJSON_FILE_PATH);
        return -1;
    }

    fseek(fp,0,SEEK_END);
    len=ftell(fp);
    fseek(fp,0,SEEK_SET);

    str=(char*)malloc(len+1);
    rc= fread(str,1,len,fp);
    fclose(fp);
    if( rc <0 )
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Read file %s fail,rc is %d, exit!!\n",__FILE__,__func__,__LINE__,CJSON_FILE_PATH,rc);
        free(str);
        return -1;
    }

    *content=str;
    return len;
}

int main(int argc, char **argv) 
{
    int rc, pid_file;
    char *content=NULL;      //file centent
    pid_file = open("/var/run/sensor-mon.pid", O_CREAT | O_RDWR, 0666);
    rc = flock(pid_file, LOCK_EX | LOCK_NB);
    if(rc)
    {
        if(EWOULDBLOCK == errno)
        {
            RecordEventLog(LOG_WARNING,"Another sensor-mon instance is running...\n");
            exit(-1);
        }
    }

    rc = OpenCjsonFile(&content);
    if(rc<0)
        goto err;
    sensor_node_t * node=NULL;
    led_node_t led_node;
    rc = FillSensorNodeArray((const char*)content,&node,&led_node);
    free(content);
    if(rc<0)
        goto err;

    RunSensorMon(node,&led_node);
    free(node);
    return 0;

err:
    {
        if(!content)
            free(content);
        if(!node)
            free(node);
        exit(-1);
    }
}

2.2 sensor-mon.c

#include "sensor-mon.h"
extern int reason_index;

int WriteLed(char *led_color_code,led_node_t *led_node)
{
    FILE *fpLedCtrl=fopen(led_node->led_ctrl,"w");
    FILE *fpLedColor=fopen(led_node->led_color,"w");
    fprintf(fpLedCtrl,"%s",LED_CTRL_EN_CODE);
    fprintf(fpLedColor,"%s",led_color_code);
    fclose(fpLedCtrl);
    fclose(fpLedColor);
    return 0;
}

int WriteSensorMonResult(sen_sta_type_t sta, char *format, ...)
{
    char str[]="Result :            \nColor :            \nReason : \n";
    if (sta)
    {
        sprintf(str,"Result : %10s\nColor : %10s\nReason : \n","BAD","YELLOW");
        FILE *fpLedLog=fopen(SENSOR_MON_LED_LOG_FILE,"r+");
        fwrite(str, strlen(str) , 1, fpLedLog);
        fseek(fpLedLog, 0, SEEK_END);
        time_t time_log = time(NULL);
        struct tm* tm_log = localtime(&time_log);
        fprintf(fpLedLog, "%04d-%02d-%02d %02d:%02d:%02d ", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);

        char buf[LOG_BUF_SIZE]={0};
        va_list aptr;
        int ret;
        va_start(aptr, format);
        ret = vsprintf(buf, format, aptr);
        va_end(aptr);
        fwrite(buf, strlen(buf) , 1, fpLedLog);
        fclose(fpLedLog);
        return(ret);
    }
    else
    {
        sprintf(str,"Result : %10s\nColor : %10s\n","GOOD","GREEN");
        FILE *fpLedLog=fopen(SENSOR_MON_LED_LOG_FILE,"w");
        fwrite(str, strlen(str) , 1, fpLedLog);
        fclose(fpLedLog);
        return 0;
    }
}

int GetObjectIntValue(const cJSON * json_element, const char * key, int * value)
{
    cJSON * item = cJSON_GetObjectItem(json_element,key);
    if(!item)
    {
        RecordEventLog(LOG_ERR,"GetObjectIntValue: type=%d, string is %s,value is %d\n",item->type,item->string,item->valueint);
        return -1;
    }
    else
        *value=item->valueint;
    return 0;
}

int GetObjectDoubleValue(const cJSON * json_element, const char * key, double * value)
{
    cJSON * item = cJSON_GetObjectItem(json_element,key);
    if(!item)
    {
        RecordEventLog(LOG_ERR,"GetObjectDoubleValue: type=%d, string is %s,value is %.3f\n",item->type,item->string,item->valuedouble);
        return -1;
    }
    else
        *value=item->valuedouble;
    return 0;
}

int GetObjectStrValue(const cJSON * json_element, const char * key, char* value)
{
    cJSON * item = cJSON_GetObjectItem(json_element,key);
    if(!item)
    {
        RecordEventLog(LOG_ERR,"GetObjectStrValue: type=%d, string is %s,value is %s\n",item->type,item->string,item->valuestring);
        return -1;
    }
    else
        strcpy(value,item->valuestring);
    return 0;
}

int QueryFileFullPath(const char * path,const char * filename,char * fullpath)
{
    DIR *dir;
    struct dirent *node;
    char tmp_path[MAX_FILE_PATH_LEN]={0};

    dir=opendir(path);
    if ( !dir )
    {
        RecordEventLog(LOG_ERR,"%s %s %d:Open dir %s fail\n",__FILE__,__func__,__LINE__,path);
        return -1;
    }

    while( (node=readdir(dir)) != NULL )
    {
        if( node->d_type != T_DIR && node->d_type != T_REG )
            continue;
        if( strncmp(node->d_name,".",1) != 0 )  //ingore file which begin with "."
        {
            memset(tmp_path,0,MAX_FILE_PATH_LEN);
            strcpy(tmp_path, path);
            if(path[strlen(path)-1] != PATH_END_CHAR)   //append "/" in the end
                tmp_path[strlen(tmp_path)]=PATH_END_CHAR;

            strcat(tmp_path,node->d_name);

            if(node->d_type==T_REG)
            {
                //compare if the file is required
                if(strcmp(node->d_name,filename) == 0)
                {
                    strcpy(fullpath,tmp_path);
                    closedir(dir);
                    return 0;
                }
            }
            else if(node->d_type==T_DIR)
            {
                if( strncmp(node->d_name,".",1) != 0 )
                    QueryFileFullPath(tmp_path,filename,fullpath);
            }
        }
    }
    closedir(dir);
    return 0;
}

int ReadNodeValue(sensor_node_t * node, int *realvalue)
{
    char * path=node->path;
    double ratio=node->ratio;
    FILE * fp;
    char value_out[MAX_FILE_READ_LEN]={0};
    fp=fopen(path,"rb");
    if(fgets(value_out,MAX_FILE_READ_LEN,fp) == NULL)
    {
        fclose(fp);
        return 0;
    }
    fclose(fp);
    *realvalue=(atoi(value_out)*ratio);
    return 1;
}

int RecordEventLog(int log_level, const char *format, ...)
{
    va_list arg;
    FILE* pFile = fopen(LOG_FILE, "a");
    char log_str[258]= {0};
    va_start (arg, format);

    time_t time_log = time(NULL);
    struct tm* tm_log = localtime(&time_log);
    fprintf(pFile, "%04d-%02d-%02d %02d:%02d:%02d ", tm_log->tm_year + 1900, tm_log->tm_mon + 1, tm_log->tm_mday, tm_log->tm_hour, tm_log->tm_min, tm_log->tm_sec);
    vsprintf(log_str, format, arg);

    fprintf(pFile, "%s", log_str);
    LOG(log_level,"%s",log_str);

    va_end (arg);
    fflush(pFile);
    return 0;
}

2.3 sensor-mon.h

#ifndef SENSOR_MON__h
#define SENSOR_MON__h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
#include <stdarg.h>
#include <time.h>
#include <syslog.h>
#include <facebook/cJSON.h>

#define SENSOR_MON_KEY          "DCDC"
#define SENSOR_MON_NAME_STR     "Name"
#define SENSOR_MON_DESC_STR     "Desc"
#define SENSOR_MON_PATH_STR     "Path"
#define SENSOR_MON_MIN_STR      "Min"
#define SENSOR_MON_MAX_STR      "Max"
#define SENSOR_MON_RATIO_STR    "Ratio"
#define SENSOR_MON_UNIT_STR     "Unit"
#define SENSOR_MON_NODE_STR     "Node"

#define MAX_FILE_PATH_LEN       128
#define MAX_SENSOR_DESC_LEN     64
#define MAX_FILE_NAME_LEN       28
#define MAX_FILE_READ_LEN       24
#define PATH_END_CHAR           '/'

#define MIN_POLL_INTERVAL       5

#define LOG_FILE                "/var/log/sensor-mon.log"
#define LOG(mode,format, ...)   syslog(mode, format, __VA_ARGS__)

#define SENSOR_MON_LED_PATH_STR       "DCDC_LED"
#define SENSOR_MON_LED_CTRL_KEY       "led_ctrl"
#define SENSOR_MON_LED_COLOR_KEY      "led_color"
#define SENSOR_MON_LED_FREQUENCY_KEY  "led_frequency"
#define SENSOR_MON_LED_STATUS_KEY     "led_status"
#define LED_CTRL_EN_CODE             "0x1"
#define LED_GREEN_CODE               "0x0"
#define LED_YELLOW_CODE              "0x1"
#define LOG_BUF_SIZE 256
#define SENSOR_MON_LED_LOG_FILE      "/tmp/SensorMonResult"

typedef enum
{
    SENSOR_NORMAL,
    SENSOR_ABNORMAL,
    SENSOR_UNKNOWN
}sen_sta_type_t;

typedef enum
{
    T_UNKNOWN = 0,
    T_FIFO = 1,
    T_CHR = 2,
    T_DIR = 4,
    T_BLK = 6,
    T_REG = 8,
    T_LNK = 10,
    T_SOCK = 12,
    T_WHT = 14
}file_type_t;

typedef struct
{
    char unit;
    double ratio;
    int minvalue;
    int maxvalue;
    int sen_status;

    int sen_sta_compare_value_threshold;
    int sen_sta_run_sensor_mon;
    int sen_sta_read_node_value_open_file;
    int sen_sta_read_node_value_read_file;
    
    char name[MAX_FILE_NAME_LEN];
    char node[MAX_FILE_NAME_LEN];
    char path[MAX_FILE_PATH_LEN];
    char desc[MAX_SENSOR_DESC_LEN];
} sensor_node_t;

typedef struct
{
    char led_ctrl[MAX_FILE_PATH_LEN];
    char led_color[MAX_FILE_PATH_LEN];
    char led_frequency[MAX_FILE_PATH_LEN];
    char led_status[MAX_FILE_PATH_LEN];
}led_node_t;

int GetObjectIntValue(const cJSON * json_element, const char * key, int * value);
int GetObjectDoubleValue(const cJSON * json_element, const char * key, double * value);
int GetObjectStrValue(const cJSON * json_element, const char * key, char* value);
int QueryFileFullPath(const char * path,const char * filename,char * fullpath);
int ReadNodeValue(sensor_node_t * node);
int RecordEventLog(int log_level, const char *format, ...);
int WriteLed(char *led_color_code,led_node_t *led_node);
int WriteSensorMonResult(sen_sta_type_t sta, char *format, ...);
#endif

【Notes19】文件监控

上一篇:I.MX6U-裸机程序(4)——GPIO模块


下一篇:K210应用5-使用中断方式通过UART接收数据