Click here to Skip to main content
15,884,040 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting response for the command -sentinel masters against redis server.

public string GetMasterFromSentinel(string sentinelAddress)
        {
            TcpClient server;

            try
            {
                var splittedAddress = sentinelAddress.Split(':');
                server = new TcpClient(splittedAddress[0], int.Parse(splittedAddress[1]));
            }
            catch (SocketException)
            {
                // _log.Error("Unable to connect to server");
                return string.Empty;
            }
            NetworkStream ns = server.GetStream();
            var payload = new byte[] { 0x2a, 0x32, 0x0d, 0x0a, 0x24, 0x38, 0x0d, 0x0a, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x6e, 0x65, 0x6c, 
                0x0d, 0x0a, 0x24, 0x37, 0x0d, 0x0a, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x73, 0x0d, 0x0a };
            ns.Write(payload, 0, payload.Length);
            ns.Flush();

            var data = new byte[1024];
            ns.Read(data, 0, data.Length);

            ns.Close();
            server.Close();
            string output = string.Empty;
           
            foreach (byte b in data)
                output += (Char)b;
           
           //i have put remove nulls from the string
            output = output.Replace("\r\n", string.Empty).Replace("$","|");

            return output;
        }


Value of output before removing nulls:"*1\r\n*40\r\n$4\r\nname\r\n$8\r\nmymaster\r\n$2\r\nip\r\n$12\r\n10.93.247.96\r\n$4\r\nport\r\n$4\r\n6379\r\n$5\r\nrunid\r\n$40\r\n03ba9934d84efe28e583f4dca03f5b6c710bd712\r\n$5\r\nflags\r\n$6\r\nmaster\r\n$21\r\nlink-pending-commands\r\n$1\r\n0\r\n$13\r\nlink-refcount\r\n$1\r\n1\r\n$14\r\nlast-ping-sent\r\n$1\r\n0\r\n$18\r\nlast-ok-ping-reply\r\n$3\r\n134\r\n$15\r\nlast-ping-reply\r\n$3\r\n134\r\n$23\r\ndown-after-milliseconds\r\n$5\r\n10000\r\n$12\r\ninfo-refresh\r\n$4\r\n6802\r\n$13\r\nrole-reported\r\n$6\r\nmaster\r\n$18\r\nrole-reported-time\r\n$8\r\n76627249\r\n$12\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9934d84efe28e583f4dca03f5b6c710bd712$5flags$6master$21link-pending-commands$10$13link-refcount$11$14last-ping-sent$10$18last-ok-ping-reply$3958$15last-ping-reply$3958$23down-after-milliseconds$510000$12info-refresh$48689$13role-reported$6master$18role-reported-time$876879953$12config-epoch$16$10num-slaves$11$19num-other-sentinels$11$6quorum$12$16failover-timeout$6180000$14parallel-syncs$11\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"


I need to convert the respective key and values in a dictionary or object.

What I have tried:

I have tried removing the nulls from output.
Is there proper way to filter/parse the information sent by redis server?
The actual response from Redis will be like:
1) 1) "name"
2) "resque"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "7789"
...
Posted
Updated 16-May-17 23:17pm
v2
Comments
Richard MacCutchan 17-May-17 4:53am    
"Is there proper way to filter/parse the information"
Yes, check the documentation to find out the exact structure of the returned information.
Member 1097736 17-May-17 4:57am    
I have updated it. Can you check?
Richard MacCutchan 17-May-17 4:59am    
No, because I do not have the documentation.

1 solution

These parts of redist are not documented as far as I know...
However here how you can parse it...

First notice that the data is separated by classic C carriage-return+newline (\r\n)... So you should interpret it line by line...
Lines beginning with * (asterisk) are counters...
So the first line (*1) means you have 1 block (one master)
The second line (*40) means, that there are 40 values... but as every value preceded with its length you actually have 80 lines...
Now the lines with $ (dollar) are length indicator of the lines after them...
Like '$4' and 'name' are two lines connected (4 is the length of the string 'name')
If you break it down, what you have got here is this:
*1
*40
$4
name
$8
mymaster
$2
ip
$12
10.93.247.96
$4
port
$4
6379
$5
runid
$40
03ba9934d84efe28e583f4dca03f5b6c710bd712
$5
flags
$6
master
$21
link-pending-commands
$1
0
$13
link-refcount
$1
1
$14
last-ping-sent
$1
0
$18
last-ok-ping-reply
$3
134
$15
last-ping-reply
$3
134
$23
down-after-milliseconds
$5
10000
$12
info-refresh
$4
6802
$13
role-reported
$6
master
$18
role-reported-time
$8
76627249
$12
config-epoch
$1
6
$10
num-slaves
$1
1
$19
num-other-sentinels
$1
1
$6
quorum
$1
2
$16
failover-timeout
$6
180000
$14
parallel-syncs
$1
1

And if you clean it up:
name					=	mymaster
ip						=	10.93.247.96
port					=	6379
runid					=	03ba9934d84efe28e583f4dca03f5b6c710bd712
flags					=	master
link-pending-commands	=	0
link-refcount			=	1
last-ping-sent			=	0
last-ok-ping-reply		=	134
last-ping-reply			=	134
down-after-milliseconds	=	10000
info-refresh			=	6802
role-reported			=	master
role-reported-time		=	76627249
config-epoch			=	6
num-slaves				=	1
num-other-sentinels		=	1
quorum					=	2
failover-timeout		=	180000
parallel-syncs			=	1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900