diff --git a/saidump/saidump.cpp b/saidump/saidump.cpp old mode 100644 new mode 100755 index 4677f48aa..42441b4a3 --- a/saidump/saidump.cpp +++ b/saidump/saidump.cpp @@ -502,37 +502,20 @@ static sai_status_t preProcessFile(const std::string file_name) return SAI_STATUS_SUCCESS; } -static sai_status_t dumpFromRedisRdbJson(const std::string file_name) +static void traverseJson(const json & jsn) { SWSS_LOG_ENTER(); - - std::ifstream input_file(file_name); - - if (!input_file.is_open()) - { - SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", file_name.c_str()); - return SAI_STATUS_FAILURE; - } - - try + if (jsn.is_object()) { - // Parse the JSON data from the file (validation) - nlohmann::json jsonData; - input_file >> jsonData; - - SWSS_LOG_DEBUG("JSON file is valid."); - - for (json::iterator it = jsonData.begin(); it != jsonData.end(); ++it) + for (auto it = jsn.begin(); it != jsn.end(); ++it) { - json jj_key = it.key(); - - std::string keystr = jj_key; + std::string keystr = it.key(); std::string item_name = keystr; size_t pos = keystr.find_first_of(":"); if (pos != std::string::npos) { - if(ASIC_STATE_TABLE != keystr.substr(0, pos)) // filter out non ASIC_STATE + if(ASIC_STATE_TABLE != keystr.substr(0, pos)) // filter out non "ASIC_STATE" items { continue; } @@ -550,8 +533,7 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name) } std::cout << item_name << " " << std::endl; - - json jj = it.value(); + json jsn_sub = it.value(); if (!it->is_object()) { @@ -560,11 +542,11 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name) TableMap map; - for (json::iterator itt = jj.begin(); itt != jj.end(); ++itt) + for (auto it_sub = jsn_sub.begin(); it_sub != jsn_sub.end(); ++it_sub) { - if (itt.key() != "NULL") + if (it_sub.key() != "NULL") { - map[itt.key()] = itt.value(); + map[it_sub.key()] = it_sub.value(); } } @@ -577,10 +559,39 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name) std::cout << str_indent << pad_string(field.first, max_len) << " : "; std::cout << field.second << std::endl; } - std::cout << std::endl; } + } + else if(jsn.is_array()) + { + for (const auto& element : jsn) + { + if (element.is_object() || element.is_array()) + { + traverseJson(element); + } + } + } +} + +static sai_status_t dumpFromRedisRdbJson(const std::string file_name) +{ + SWSS_LOG_ENTER(); + + std::ifstream input_file(file_name); + if (!input_file.is_open()) + { + SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", file_name.c_str()); + return SAI_STATUS_FAILURE; + } + + try + { + // Parse the JSON data from the file (validation) + json jsonData; + input_file >> jsonData; + traverseJson(jsonData); return SAI_STATUS_SUCCESS; } catch (std::exception &ex) diff --git a/syncd/scripts/saidump.sh b/syncd/scripts/saidump.sh index 034322ff3..60836861c 100755 --- a/syncd/scripts/saidump.sh +++ b/syncd/scripts/saidump.sh @@ -10,7 +10,8 @@ save_saidump_by_rdb() import json with open('$filepath') as json_file: data = json.load(json_file) - print(data['INSTANCES']['redis']['hostname'], data['INSTANCES']['redis']['port'], data['INSTANCES']['redis']['unix_socket_path'])") + print(data['INSTANCES']['redis']['hostname'], data['INSTANCES']['redis']['port'], data['INSTANCES']['redis']['unix_socket_path']) + json_file.close()") # split redis_config=(${redis_config// / }) @@ -19,19 +20,16 @@ with open('$filepath') as json_file: local redis_dir=`dirname ${redis_config[2]}` logger "saidump.sh: hostname:$hostname, port:$port, redis_dir:$redis_dir" - logger "saidump.sh: [1] Config Redis consistency directory." - redis-cli -h $hostname -p $port CONFIG SET dir $redis_dir > /dev/null + logger "saidump.sh: [1] Get the remote backups of RDB file." + redis-cli -h $hostname -p $port --rdb $redis_dir/dump.rdb > /dev/null 2>&1 - logger "saidump.sh: [2] SAVE." - redis-cli -h $hostname -p $port SAVE > /dev/null + logger "saidump.sh: [2] Run rdb-cli command to convert the dump files into JSON files." + rdb-cli $redis_dir/dump.rdb json | tee $redis_dir/dump.json > /dev/null - logger "saidump.sh: [3] Run rdb command to convert the dump files into JSON files." - rdb --command json $redis_dir/dump.rdb | tee $redis_dir/dump.json > /dev/null - - logger "saidump.sh: [4] Run saidump -r to update the JSON files' format as same as the saidump before. Then we can get the saidump's result in standard output." + logger "saidump.sh: [3] Run saidump -r to update the JSON files' format as same as the saidump before. Then we can get the saidump's result in standard output." saidump -r $redis_dir/dump.json -m 100 - logger "saidump.sh: [5] Clear the temporary files." + logger "saidump.sh: [4] Clear the temporary files." rm -f $redis_dir/dump.rdb rm -f $redis_dir/dump.json }