diff --git a/common/include/tfe_utils.h b/common/include/tfe_utils.h index 70752e4..079c826 100644 --- a/common/include/tfe_utils.h +++ b/common/include/tfe_utils.h @@ -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); diff --git a/common/src/tfe_utils.cpp b/common/src/tfe_utils.cpp index 04ea83b..bf20fbb 100644 --- a/common/src/tfe_utils.cpp +++ b/common/src/tfe_utils.cpp @@ -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; +} + + diff --git a/conf/pangu/pangu_pxy.conf b/conf/pangu/pangu_pxy.conf index ddb592d..6f6d8a3 100644 --- a/conf/pangu/pangu_pxy.conf +++ b/conf/pangu/pangu_pxy.conf @@ -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. diff --git a/plugin/business/pangu-http/src/pangu_http.cpp b/plugin/business/pangu-http/src/pangu_http.cpp index 2b7779b..ea85e9d 100644 --- a/plugin/business/pangu-http/src/pangu_http.cpp +++ b/plugin/business/pangu-http/src/pangu_http.cpp @@ -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; diff --git a/plugin/business/pangu-http/src/pangu_logger.cpp b/plugin/business/pangu-http/src/pangu_logger.cpp index 8479646..ef29b97 100644 --- a/plugin/business/pangu-http/src/pangu_logger.cpp +++ b/plugin/business/pangu-http/src/pangu_logger.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #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 #include "pattern_replace.h" #include //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);