TSG-1723 日志中填写“设备编号”字段

This commit is contained in:
fengweihao
2020-05-21 17:15:40 +08:00
parent 481f754275
commit 55ecb52ad0
8 changed files with 109 additions and 58 deletions

View File

@@ -170,6 +170,8 @@ int tfe_scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const void *, const void *));
char *tfe_read_file(const char *filename, size_t *filelen);
const char * tfe_version();
int __wrapper_MESA_htable_set_opt(MESA_htable_handle table, enum MESA_htable_opt opt_type, unsigned value);
int __wrapper_MESA_htable_set_opt(MESA_htable_handle table, enum MESA_htable_opt opt_type, void * val, size_t len);

View File

@@ -103,3 +103,55 @@ int tfe_scandir(const char *dir, struct dirent ***namelist,
}
char *tfe_read_file(const char *filename, size_t *filelen)
{
FILE *file = NULL;
long length = 0;
char *content = NULL;
size_t read_chars = 0;
file = fopen(filename, "rb");
if (file == NULL)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_END) != 0)
{
goto cleanup;
}
length = ftell(file);
if (length < 0)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_SET) != 0)
{
goto cleanup;
}
/* allocate content buffer */
content = (char*)malloc((size_t)length + sizeof(""));
if (content == NULL)
{
goto cleanup;
}
/* read the file into memory */
read_chars = fread(content, sizeof(char), (size_t)length, file);
if ((long)read_chars != length)
{
free(content);
content = NULL;
goto cleanup;
}
*filelen = read_chars;
content[read_chars] = '\0';
cleanup:
if (file != NULL)
{
fclose(file);
}
return content;
}

View File

@@ -4,6 +4,7 @@ log_level=10
[log]
nic_name=eth4
entrance_id=0
device_id_filepath=/opt/tsg/etc/tsg_sn.json
kafka_brokerlist=10.4.34.10:9092,10.4.34.11:9092,10.4.34.12:9092,10.4.34.13:9092,10.4.34.14:9092,10.4.34.15:9092,10.4.34.16:9092,10.4.34.17:9092,10.4.34.18:9092,10.4.34.19:9092
kafka_topic=policy-event-log
#Addresses of minio. Format is defined by WiredLB.

View File

@@ -706,7 +706,7 @@ void ma_profile_table_new_cb(int table_id, const char* key, const char* table_li
ply_profile->tpl = ctemplate::Template::GetTemplate(profile_path, ctemplate::DO_NOT_STRIP);
}else
{
ply_profile->profile_msg = execute_read_file(profile_path, &ply_profile->msg_len);
ply_profile->profile_msg = tfe_read_file(profile_path, &ply_profile->msg_len);
if (ply_profile->profile_msg == NULL)
{
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Read file failed %d:%s:%s", profile_id, profile_name, profile_path);
@@ -745,7 +745,7 @@ void ma_insert_profile_table_new_cb(int table_id, const char* key, const char* t
ply_profile->tpl = ctemplate::Template::GetTemplate(profile_path, ctemplate::DO_NOT_STRIP);
}else
{
ply_profile->profile_msg = execute_read_file(profile_path, &ply_profile->msg_len);
ply_profile->profile_msg = tfe_read_file(profile_path, &ply_profile->msg_len);
if (ply_profile->profile_msg == NULL)
{
TFE_LOG_ERROR(g_pangu_rt->local_logger, "Read file failed %d:%s:%s", profile_id, profile_name, profile_path);
@@ -1765,7 +1765,7 @@ static void http_hijack(const struct tfe_http_session * session, enum tfe_http_e
char * hijack_buff=NULL; size_t hijack_size=0;
hijack_buff = execute_read_file(hijack_profile->profile_msg, &hijack_size);
hijack_buff = tfe_read_file(hijack_profile->profile_msg, &hijack_size);
if (NULL == hijack_buff){
TFE_LOG_ERROR(g_pangu_rt->local_logger, "read hijack file faild, path = %s", hijack_profile->profile_msg);
ctx->action = PG_ACTION_NONE;

View File

@@ -2,6 +2,7 @@
#include <MESA/MESA_prof_load.h>
#include <tfe_kafka_logger.h>
#include <cache_evbase_client.h>
#include <tfe_utils.h>
#include "pangu_logger.h"
@@ -18,6 +19,7 @@ struct pangu_logger
unsigned int en_sendlog_meta;
unsigned int en_sendlog_body;
const char *device_id;
void* global_logger;
void* local_logger;
@@ -43,6 +45,46 @@ enum _log_action //Bigger action number is prior.
__LG_ACTION_MAX
};
static const char* tfe_device_id_create(const char* profile, const char* section, void* local_logger)
{
int ret=-1;
size_t device_id_size=0;
char *tsg_sn_file=NULL, *device_id;
const char *device_def_id = "DFT2201925000001";
cJSON *json=NULL,*item=NULL;
char device_id_filepath[TFE_STRING_MAX] = { 0 };
ret = MESA_load_profile_string_def(profile, section, "device_id_filepath",device_id_filepath,sizeof(device_id_filepath), NULL);
if(ret<0)
{
TFE_LOG_ERROR(local_logger,"Pangu log init failed, no device_path in profile %s section %s.", profile, section);
goto finish;
}
tsg_sn_file = tfe_read_file(device_id_filepath, &device_id_size);
if(tsg_sn_file==NULL)
{
TFE_LOG_ERROR(local_logger,"Pangu log init failed, %s not existed.", tsg_sn_file);
goto finish;
}
json=cJSON_Parse(tsg_sn_file);
if(json==NULL)
{
TFE_LOG_ERROR(local_logger, "invalid device parameter: file = %s", tsg_sn_file);
goto finish;
}
item=cJSON_GetObjectItem(json, "sn");
if(unlikely(!item || !cJSON_IsString(item)))
{
TFE_LOG_ERROR(local_logger, "Invalid device parameter: %s invalid json format", tsg_sn_file);
}
device_id = tfe_strdup(item->valuestring);
cJSON_Delete(json);
return device_id;
finish:
return device_def_id;
}
struct pangu_logger* pangu_log_handle_create(const char* profile, const char* section, void* local_logger)
{
int ret=-1;
@@ -77,6 +119,10 @@ struct pangu_logger* pangu_log_handle_create(const char* profile, const char* s
MESA_load_profile_string_def(profile, section, "NIC_NAME",nic_name,sizeof(nic_name),"eth0");
MESA_load_profile_int_def(profile, section, "ENTRANCE_ID",&(instance->entry_id),0);
instance->device_id = tfe_device_id_create(profile, section, local_logger);
TFE_LOG_INFO(local_logger, "Pangu device id : %s", instance->device_id);
ret=MESA_load_profile_string_def(profile, section,"KAFKA_BROKERLIST", brokerlist, sizeof(brokerlist), NULL);
if(ret<0)
{
@@ -207,7 +253,7 @@ int pangu_send_log(struct pangu_logger* handle, const struct pangu_log* log_msg)
cJSON_AddNumberToObject(common_obj, "common_stream_dir", 3); //1:c2s, 2:s2c, 3:double
cJSON_AddStringToObject(common_obj, "common_sled_ip", handle->kafka_logger->local_ip_str);
cJSON_AddNumberToObject(common_obj, "common_entrance_id", handle->entry_id);
cJSON_AddNumberToObject(common_obj, "common_device_id", 0);
cJSON_AddStringToObject(common_obj, "common_device_id", handle->device_id);
cJSON_AddStringToObject(common_obj, "http_url", http->req->req_spec.url);
cJSON_AddStringToObject(common_obj, "http_host", http->req->req_spec.host);
for(size_t i=0;i<sizeof(req_fields)/sizeof(struct json_spec);i++)

View File

@@ -78,56 +78,6 @@ strtok_r_esc(char * s, const char delim, char ** save_ptr)
return s;
}
char *execute_read_file(const char *filename, size_t *filelen)
{
FILE *file = NULL;
long length = 0;
char *content = NULL;
size_t read_chars = 0;
file = fopen(filename, "rb");
if (file == NULL)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_END) != 0)
{
goto cleanup;
}
length = ftell(file);
if (length < 0)
{
goto cleanup;
}
if (fseek(file, 0, SEEK_SET) != 0)
{
goto cleanup;
}
/* allocate content buffer */
content = (char*)malloc((size_t)length + sizeof(""));
if (content == NULL)
{
goto cleanup;
}
/* read the file into memory */
read_chars = fread(content, sizeof(char), (size_t)length, file);
if ((long)read_chars != length)
{
free(content);
content = NULL;
goto cleanup;
}
*filelen = read_chars;
content[read_chars] = '\0';
cleanup:
if (file != NULL)
{
fclose(file);
}
return content;
}
size_t __attribute__((__unused__))
format_replace_rule(const char * exec_para, struct replace_rule * replace, size_t n_replace)
{

View File

@@ -38,6 +38,5 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char
void simple_replace(const char* find, const char* replacement, const char* input, size_t in_sz, char** output, size_t *output_sz, int options);
enum replace_zone zone_name_to_id(const char * name);
char *execute_read_file(const char *filename, size_t *filelen);

View File

@@ -1,3 +1,4 @@
#include <tfe_utils.h>
#include "pattern_replace.h"
#include <sys/types.h>//fstat
@@ -201,7 +202,7 @@ TEST(PatternInsert, CSS)
char *input=NULL, *output=NULL;
size_t output_sz=0, input_sz = 0;
input = execute_read_file(filename, &input_sz);
input = tfe_read_file(filename, &input_sz);
EXPECT_TRUE(input_sz>0);
output_sz = insert_string(input, input_sz, NULL, custom, "css", &output);
@@ -220,7 +221,7 @@ TEST(PatternInsert, AfterBody)
char *input=NULL, *output=NULL;
size_t output_sz=0, input_sz = 0;
input = execute_read_file(filename, &input_sz);
input = tfe_read_file(filename, &input_sz);
EXPECT_TRUE(input_sz>0);
output_sz = insert_string(input, input_sz, "after-page-load", custom, "js", &output);
@@ -238,7 +239,7 @@ TEST(PatternInsert, BeforeBody)
char *input=NULL, *output=NULL;
size_t output_sz=0, input_sz = 0;
input = execute_read_file(filename, &input_sz);
input = tfe_read_file(filename, &input_sz);
EXPECT_TRUE(input_sz>0);
output_sz = insert_string(input, input_sz, "before-page-load", custom, "js", &output);