| 36 | } |
| 37 | |
| 38 | int info_get(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, char field_type) |
| 39 | { |
| 40 | if (argc != 3 && argc != 4) { |
| 41 | RedisModule_WrongArity(ctx); |
| 42 | return REDISMODULE_OK; |
| 43 | } |
| 44 | int err = REDISMODULE_OK; |
| 45 | const char *section, *field; |
| 46 | section = RedisModule_StringPtrLen(argv[1], NULL); |
| 47 | field = RedisModule_StringPtrLen(argv[2], NULL); |
| 48 | RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, section); |
| 49 | if (field_type=='i') { |
| 50 | long long ll = RedisModule_ServerInfoGetFieldSigned(info, field, &err); |
| 51 | if (err==REDISMODULE_OK) |
| 52 | RedisModule_ReplyWithLongLong(ctx, ll); |
| 53 | } else if (field_type=='u') { |
| 54 | unsigned long long ll = (unsigned long long)RedisModule_ServerInfoGetFieldUnsigned(info, field, &err); |
| 55 | if (err==REDISMODULE_OK) |
| 56 | RedisModule_ReplyWithLongLong(ctx, ll); |
| 57 | } else if (field_type=='d') { |
| 58 | double d = RedisModule_ServerInfoGetFieldDouble(info, field, &err); |
| 59 | if (err==REDISMODULE_OK) |
| 60 | RedisModule_ReplyWithDouble(ctx, d); |
| 61 | } else if (field_type=='c') { |
| 62 | const char *str = RedisModule_ServerInfoGetFieldC(info, field); |
| 63 | if (str) |
| 64 | RedisModule_ReplyWithCString(ctx, str); |
| 65 | } else { |
| 66 | RedisModuleString *str = RedisModule_ServerInfoGetField(ctx, info, field); |
| 67 | if (str) { |
| 68 | RedisModule_ReplyWithString(ctx, str); |
| 69 | RedisModule_FreeString(ctx, str); |
| 70 | } else |
| 71 | err=REDISMODULE_ERR; |
| 72 | } |
| 73 | if (err!=REDISMODULE_OK) |
| 74 | RedisModule_ReplyWithError(ctx, "not found"); |
| 75 | RedisModule_FreeServerInfo(ctx, info); |
| 76 | return REDISMODULE_OK; |
| 77 | } |
| 78 | |
| 79 | int info_gets(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { |
| 80 | return info_get(ctx, argv, argc, 's'); |