#include <stdio.h>
#include <stdlib.com
// 假设这是从Redis的INFO Replication命令获取的信息
const char *info_replication =
"role:master\n"
"connected_slaves:1\n"
"slave0:ip=192.168.1.100,port=6379,state=online,offset=145332505716,lag=1\n";
// 解析连接的从服务器信息
void parse_connected_slaves(const char *info) {
const char *role = strstr(info, "role:");
if (role) {
role += strlen("role:");
printf("角色: %s\n", role);
}
const char *slave_count = strstr(info, "connected_slaves:");
if (slave_count) {
slave_count += strlen("connected_slaves:");
int count = atoi(slave_count);
printf("连接的从服务器数量: %d\n", count);
}
const char *slave_info = strstr(info, "slave");
while (slave_info) {
const char *ip_start = slave_info + strlen("slave0:ip=");
const char *ip_end = strstr(ip_start, ",port=");
if (ip_end) {
char ip[20];
strncpy(ip, ip_start, ip_end - ip_start);
ip[ip_end - ip_start] = '\0';
printf("从服务器IP: %s\n", ip);
}
const char *port_start = ip_end + strlen(",port=");
const char *port_end = strstr(port_start, ",state=");
if (port_end) {
int port = atoi(port_start);
printf("从服务器端口: %d\n", port);
}
const char *state_start = port_end + strlen(",state=");
const char *state_end = strstr(state_start, ",offset=");
if (state_end) {
char state[20];
strncpy(state, state_start, state_end - state_start);
state[state_end - state_start] = '\0';
printf("从服务器状态: %s\n", state);
}
const char *offset_start = state_end + strlen(",offset=");
const char *offset_end = strstr(offset_start, ",lag=");
if (offset_end) {
long long offset = atoll(offset_start);
printf("数据复制偏移量: %lld\n", offset);
}
const char *lag_start = offset_end + strlen(",lag=");
const char *lag_end = strchr(lag_start, '\n');
if (lag_end) {
int lag = atoi(lag_start);
printf("复制延迟时间(秒): %d\n", lag);
}
// 移动到下一个从服务器信息
slave_info
评论已关闭