Notes17文件监控
Posted 码农编程录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Notes17文件监控相关的知识,希望对你有一定的参考价值。
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);
以上是关于Notes17文件监控的主要内容,如果未能解决你的问题,请参考以下文章
新增系统监控功能,支持Excel文件格式对资源导入/导出,JumpServer堡垒机v2.6.0发布丨Release Notes
[Python Study Notes]实现对键盘控制与监控