58 #include "safec_lib.h"
62 #ifdef USE_TR181_CCSP_MESSAGEBUS
64 #define TR181BUF_LENGTH 512
65 #define OBJ_DELIMITER "{i}"
66 #define DELIMITER_SIZE 3
67 #define NOE strlen("NumberOfEntries")
71 #define INVALID_COUNT "-406"
73 #define LOG_FILE "dcmscript.log"
75 #define NUM_OF_LOG_TYPES (sizeof(log_type_table)/sizeof(log_type_table[0]))
88 {
"top_log.txt", TOPLOG },
89 #ifdef USE_TR181_CCSP_MESSAGEBUS
90 {
"<message_bus>", MESSAGE_BUS },
94 int get_log_type_from_name(
char *name,
enum logType_e *type_ptr)
101 if((name == NULL) || (type_ptr == NULL))
104 strsize = strlen(name);
106 for (i = 0 ; i < NUM_OF_LOG_TYPES ; ++i)
108 rc = strcmp_s(name, strsize, log_type_table[i].name, &ind);
110 if((rc == EOK) && (!ind))
112 *type_ptr = log_type_table[i].type;
125 char *PERSISTENT_PATH = NULL;
126 char *LOG_PATH = NULL;
127 char *DEVICE_TYPE = NULL;
128 cJSON *SEARCH_RESULT_JSON = NULL, *ROOT_JSON = NULL;
129 int CUR_EXEC_COUNT = 0;
130 long LAST_SEEK_VALUE = 0;
152 UNREFERENCED_PARAMETER(logfile);
153 UNREFERENCED_PARAMETER(pcIndex);
154 GList *tlist = pchead;
156 while (NULL != tlist) {
159 if ((NULL != tmp->header) && (NULL != strstr(tmp->header,
"Load_Average"))) {
161 LOG(
"getLoadAvg() Failed with error");
164 if (NULL != tmp->pattern) {
169 tlist = g_list_next(tlist);
174 #ifdef USE_TR181_CCSP_MESSAGEBUS
181 static void appendData(
pcdata_t* dst,
const char* src)
185 if(NULL == dst || NULL == src)
188 int dst_len, src_len = 0;
191 src_len = strlen(src) + 1;
193 if(NULL == dst->data) {
194 if((dst->data = strdup(src)) == NULL)
196 LOG(
"Failed to allocate memory for telemetry node data\n");
199 dst_len = strlen(dst->data) + 1;
200 dst->data = (
char*)realloc(dst->data, dst_len+src_len);
201 if(NULL != dst->data) {
202 rc = sprintf_s((dst->data)+(dst_len-1),src_len+1,
",%s", src);
211 LOG(
"Failed to re-allocate memory for telemetry node data\n");
226 static int processTr181Objects(
char *logfile, GList *pchead,
int pcIndex)
228 UNREFERENCED_PARAMETER(logfile);
229 UNREFERENCED_PARAMETER(pcIndex);
230 int ret_val,length, obj_count, i = 0;
233 char tr181data_buff[TR181BUF_LENGTH];
234 int bufflength = TR181BUF_LENGTH + NOE;
235 char tr181obj_buff[bufflength];
236 char *tck, *first_tck = NULL;
242 if ( 0 != ret_val ) {
243 LOG(
"ccsp_handler_init is failed\n");
248 ret_val =
get_tr181param_value(
"Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.Telemetry.MessageBusSource.Enable", tr181data_buff, TR181BUF_LENGTH);
252 rc = strcmp_s(
"true",strlen(
"true"),tr181data_buff,&ind);
254 if((rc == EOK) && (ind == 0))
257 if(entry_flag == 1 ) {
259 for(tlist=pchead; tlist!=NULL; tlist=g_list_next(tlist)) {
262 if(NULL != tmp->header && NULL != tmp->pattern && strlen(tmp->pattern) < TR181BUF_LENGTH && NULL == tmp->data) {
266 tck = strstr(tmp->pattern, OBJ_DELIMITER);
270 appendData(tmp, tr181data_buff);
272 LOG(
"Telemetry data source not found. Type = <message_bus>. Content string = %s\n", tmp->pattern);
277 tck = strstr(tck+DELIMITER_SIZE, OBJ_DELIMITER);
280 length = first_tck - tmp->pattern;
281 rc = strncpy_s(tr181obj_buff,
sizeof(tr181obj_buff),tmp->pattern,(length-1));
287 tr181obj_buff[length-1] =
'\0';
288 rc = strcat_s(tr181obj_buff,
sizeof(tr181obj_buff),
"NumberOfEntries");
296 obj_count = atoi(tr181data_buff);
299 for(i=1; i<=obj_count; i++) {
301 rc = strncpy_s(tr181obj_buff,
sizeof(tr181obj_buff),tmp->pattern,length);
307 tr181obj_buff[length] =
'\0';
308 ret_val = sprintf_s(tr181obj_buff+length, bufflength-length,
"%d%s", i, (tmp->pattern + length + DELIMITER_SIZE));
309 if (ret_val < EOK || ret_val >= (bufflength-length))
312 LOG(
"Error: Buffer overflow - return value:%d", ret_val);
316 appendData(tmp, tr181data_buff);
318 LOG(
"Telemetry data source not found. Type = <message_bus>. Content string = %s\n", tr181obj_buff);
323 LOG(
"Failed to get NumberOfEntries. Type = <message_bus>. Content string = %s\n", tr181obj_buff);
326 LOG(
"Skipping Telemetry object due to invalid format. Type = <message_bus>. Content string = %s\n", tmp->pattern);
333 LOG(
"The TR181 MessageBusSource is disabled via RFC\n");
349 GList *tlist = pchead;
352 while (NULL != tlist) {
357 if (tmp->d_type == INT) {
358 if (tmp->count != 0) {
359 char tmp_str[5] = {
'\0'};
360 rc = sprintf_s(tmp_str,
sizeof(tmp_str),
"%d", tmp->count);
369 }
else if(tmp->d_type == STR) {
370 if ((NULL != tmp->data) && !((tmp->data[0] ==
'0') && (tmp->data[1] ==
'\0'))) {
376 tlist = g_list_next(tlist);
392 char *strFound = NULL;
393 strFound = strstr(line, pcnode->pattern);
396 if (strFound != NULL ) {
397 int tlen = 0, plen = 0, vlen = 0;
398 tlen = (int)strlen(line);
399 plen = (int)strlen(pcnode->pattern);
400 strFound = strFound + plen ;
402 vlen = strlen(strFound) ;
405 if (( 1 == vlen ) && isspace(strFound[plen]))
409 if (NULL == pcnode->data){
410 pcnode->data = (
char *) malloc(MAXLINE);
411 memset(pcnode->data,
'\0', MAXLINE);
414 if (NULL == pcnode->data)
417 rc = strcpy_s(pcnode->data,MAXLINE,strFound);
423 pcnode->data[tlen-plen] =
'\0';
441 int i = 0, j = 0, len = strlen(str);
442 char tmpEC[LEN] = {0};
445 while (str[i] !=
'\0') {
446 if (len >=4 && str[i] ==
'R' && str[i+1] ==
'D' && str[i+2] ==
'K' && str[i+3] ==
'-') {
449 if (str[i] ==
'0' || str[i] ==
'1') {
452 if (str[i] ==
'0' || str[i] ==
'3') {
455 if (0 != isdigit(str[i])) {
456 while (i<=len && 0 != isdigit(str[i]) && j<RDK_EC_MAXLEN) {
460 rc = strcpy_s(ec,ec_length, tmpEC);
488 char err_code[20] = {0}, rdkec[20] = {0};
493 if (err_code[0] !=
'\0') {
494 rc = sprintf_s(rdkec,
sizeof(rdkec),
"%s%s",
"RDK-",err_code);
526 UNREFERENCED_PARAMETER(pcIndex);
530 int len = strlen(temp);
531 if (len > 0 && temp[len-1] ==
'\n')
534 if (NULL != pc_node) {
535 if (pc_node->d_type == INT) {
538 if (NULL != pc_node->header) {
539 if (NULL != strstr(pc_node->header,
"split")) {
546 if (NULL != strstr(temp,
"RDK-") && ( 0 != strncmp(logfile, LOG_FILE, MAXLINE))) {
567 int processPattern(
char **prev_file,
char *logfile, GList **rdkec_head, GList *pchead,
int pcIndex)
573 if (NULL != logfile) {
575 if (NULL == *prev_file)
581 rc = strcmp_s(logfile, strlen(logfile), *prev_file , &ind);
583 if((ind) && (rc == EOK))
589 if (1 == entry_flag) {
590 if (NULL == *prev_file) {
591 *prev_file = malloc(strlen(logfile) + 1);
595 tmp = realloc(*prev_file, strlen(logfile) + 1);
604 if (NULL != *prev_file) {
605 rc = strcpy_s(*prev_file, strlen(logfile) + 1,logfile);
618 if (NULL != pchead) {
621 if(get_log_type_from_name(logfile, &type))
627 #ifdef USE_TR181_CCSP_MESSAGEBUS
628 else if (type == MESSAGE_BUS )
630 processTr181Objects(logfile, pchead, pcIndex);
659 static char *next_str;
666 if (NULL == next_str) {
670 last = strstr(next_str, delim);
672 char *ret = next_str;
677 char *ret = next_str;
679 next_str = last + strlen(delim);
692 void getDType(
char *filename,
char *header, DType_t *dtype)
696 if (NULL != header) {
698 if (NULL != strstr(header,
"split")) {
701 else if(get_log_type_from_name(filename, &type))
707 #ifdef USE_TR181_CCSP_MESSAGEBUS
708 else if (type == MESSAGE_BUS )
736 char *filename = NULL, *prevfile = NULL;
738 GList *pchead = NULL, *rdkec_head = NULL;
741 int snmp_len = strlen(
"snmp");
743 if (NULL == (sfp = fopen(fname,
"r"))) {
747 while (NULL != fgets(line, MAXLINE, sfp)) {
749 int len = strlen(line);
750 if (len > 0 && line[len-1] ==
'\n')
754 char *temp_header =
strSplit(line, DELIMITER);
755 char *temp_pattern =
strSplit(NULL, DELIMITER);
756 char *temp_file =
strSplit(NULL, DELIMITER);
757 char *temp_skip_interval =
strSplit(NULL, DELIMITER);
758 int tmp_skip_interval, is_skip_param;
761 if (NULL == temp_file || NULL == temp_pattern || NULL == temp_header || NULL == temp_skip_interval)
765 if ((temp_pattern[0] ==
'\0' ) || (temp_file[0] ==
'\0') )
767 rc = strcasecmp_s(
"snmp",snmp_len,temp_file,&ind);
769 if((0 == ind) && (rc == EOK))
772 getDType(temp_file, temp_header, &dtype);
773 tmp_skip_interval = atoi(temp_skip_interval);
775 if (tmp_skip_interval <= 0)
776 tmp_skip_interval = 0;
781 if (NULL == filename) {
782 filename = malloc(strlen(temp_file) + 1);
784 if (is_skip_param == 0 && (0 ==
insertPCNode(&pchead, temp_pattern, temp_header, dtype, 0, NULL))) {
788 rc = strcmp_s(filename, strlen(filename), temp_file , &ind);
791 if ((!ind) && (rc == EOK) && pcIndex <= MAX_PROCESS ) {
792 if (is_skip_param == 0 && (0 ==
insertPCNode(&pchead, temp_pattern, temp_header, dtype, 0, NULL))) {
797 processPattern(&prevfile, filename, &rdkec_head, pchead, pcIndex);
799 tmp = realloc(filename, strlen(temp_file) + 1);
806 if (is_skip_param == 0 && (0 ==
insertPCNode(&pchead, temp_pattern, temp_header, dtype, 0, NULL))) {
811 if (NULL != filename) {
812 rc = strcpy_s(filename,strlen(temp_file) + 1,temp_file);
825 processPattern(&prevfile, filename, &rdkec_head, pchead, pcIndex);
830 if (NULL != rdkec_head) {
839 if (NULL != filename)
842 if (NULL != prevfile)
857 int main(
int argc,
char *argv[]) {
859 char *logPath = NULL;
860 char *persistentPath = NULL;
870 persistentPath = argv[3];
874 LOG(
"Conf File = %s ", fname);
875 if (NULL != logPath) {
876 LOG(
"Log Path = %s ", logPath);
878 if (NULL != persistentPath) {
879 LOG(
"Persistent Path = %s ", persistentPath);