Know Your Redis Server Information/Status Simply via HTTP API
Tags: redis, go
Published: 2019-12-30 11:39:16

Redis CLI provides a nice [command "INFO"](https://redis.io/commands/info) to allow users to understand the information and statistics about the server. The information it provides include
- general information about the Redis server
- client connections
- memory consumption
- CPU consumption
- etc.

Now with [`Rediseen`](https://github.com/XD-DENG/rediseen) (as from version 2.1.0), you can expose & read these information much more conveniently, simply via HTTP API.

`Rediseen` (https://github.com/XD-DENG/rediseen) is a small software written with Golang. The main feature of it is to help start a REST-like API so that clients can query records in your Redis database in a simple and secure way. As from version 2.1.0, it also allows you to expose and query the Redis database information and statistics so that you can use it as a "connector" between Redis database and any monitoring mechanism.

### Install & Start the Service

```bash
brew install XD-DENG/rediseen/rediseen

export REDISEEN_REDIS_URI="redis://:@localhost:6379"
export REDISEEN_DB_EXPOSED=0
export REDISEEN_KEY_PATTERN_EXPOSED="key pattern you would like to expose"

rediseen start
```

(`REDISEEN_KEY_PATTERN_EXPOSED` is intended to help you specify the Redis keys you expose to clients (that's what `Rediseen` is mainly designed for). If you prefer to run `Rediseen` ONLY for exposing results of `INFO` command, simply put a value which doesn't match with any of your keys in Redis.)


### Get the Redis Server Information/Statistics

Now you're ready to go!

```bash
curl -s http://localhost:8000/info

{
  "CPU": {
    "used_cpu_sys": "0.434104",
    ... ...
  },
  "Clients": {
    ... ...
  },
  "Cluster": {
    ... ...
  },
  "Commandstats": {
    ... ...
  },
  "Keyspace": {
    ... ...
  },
  "Memory": {
    ... ...
  },
  "Persistence": {
    ... ...
  },
  "Replication": {
    ... ...
  },
  "Server": {
    "arch_bits": "64",
    "executable": "/Users/XD/Downloads/redis-stable/src/./redis-server",
    "gcc_version": "4.2.1",
    ... ...
  },
  "Stats": {
    ... ...
  }
}

```


You can also specify which exact section you want, so the network I/O will be much smaller. For example, if you are only interested in CPU consumption, you can specify "cpu" in the URL.

```bash
curl -s http://localhost:8000/info/cpu

{
  "CPU": {
    "used_cpu_sys": "0.422091",
    "used_cpu_sys_children": "0.000000",
    "used_cpu_user": "0.255020",
    "used_cpu_user_children": "0.000000"
  }
}

```

### Authentication

`Rediseen` also supports API Key authentication. Simply run the line below before your start `Rediseen` service.

```bash
export REDISEEN_API_KEY="demo_key"
```

Once it is set, client will have to add the API key as X-API-KEY in their HTTP header in order to access the service, otherwise 401 error (`Unauthorized`) will be returned.

```bash
# REJECTED: No X-API-KEY is given in HTTP header
curl -s http://localhost:8000/info
{
  "error": "unauthorized"
}

# REJECTED: Wrong X-API-KEY is given in HTTP header
curl -s -H "X-API-KEY: wrong_key" http://localhost:8000/info
{
  "error": "unauthorized"
}

# ACCEPTED: Correct X-API-KEY is given in HTTP header
curl -s -H "X-API-KEY: demo_key" http://localhost:8000/info/server

{
  "Server": {
    "arch_bits": "64",
    "atomicvar_api": "atomic-builtin",
    "configured_hz": "10",
    "gcc_version": "4.2.1",
    ... ...
  }
}
```