forked from cdemi/PRTG-Redis-Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
150 lines (143 loc) · 6.19 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using StackExchange.Redis;
using System.Linq;
using System;
using Newtonsoft.Json;
namespace PRTG_Redis_Sensor
{
class Program
{
static void Main(string[] args)
{
var serverHostAndPort = args[0];
var connectionConfig = $"{serverHostAndPort},AllowAdmin=True";
connectionConfig += GetPasswordFromArgs(args);
var redis = ConnectionMultiplexer.Connect(connectionConfig);
var server = redis.GetServer(serverHostAndPort);
var info = server.Info();
var serverInfo = info.SingleOrDefault(i => i.Key.Equals("Server", StringComparison.InvariantCultureIgnoreCase));
var clientsInfo = info.SingleOrDefault(i => i.Key.Equals("Clients", StringComparison.InvariantCultureIgnoreCase));
var memoryInfo = info.SingleOrDefault(i => i.Key.Equals("Memory", StringComparison.InvariantCultureIgnoreCase));
var statsInfo = info.SingleOrDefault(i => i.Key.Equals("Stats", StringComparison.InvariantCultureIgnoreCase));
var replicationInfo = info.SingleOrDefault(i => i.Key.Equals("Replication", StringComparison.InvariantCultureIgnoreCase));
var response =
new
{
prtg = new PRTGResponse()
{
result = new System.Collections.Generic.List<PRTGResult>
{
{
new PRTGResult()
{
channel = "Uptime",
unit = PRTGUnit.TimeSeconds,
value = serverInfo.SingleOrDefault(i => i.Key.Equals("uptime_in_seconds")).Value
}
},
{
new PRTGResult()
{
channel = "Connected Clients",
unit = PRTGUnit.Count,
value = clientsInfo.SingleOrDefault(i => i.Key.Equals("connected_clients")).Value
}
},
{
new PRTGResult()
{
channel = "Blocked Clients",
unit = PRTGUnit.Count,
value = clientsInfo.SingleOrDefault(i => i.Key.Equals("blocked_clients")).Value
}
},
{
new PRTGResult()
{
channel = "Used Memory",
unit = PRTGUnit.BytesMemory,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("used_memory")).Value
}
},
{
new PRTGResult()
{
channel = "Memory Fragmentation Ratio",
unit = PRTGUnit.Custom,
Float = 1,
value = memoryInfo.SingleOrDefault(i => i.Key.Equals("mem_fragmentation_ratio")).Value
}
},
{
new PRTGResult()
{
channel = "Total Connections Received",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_connections_received")).Value
}
},
{
new PRTGResult()
{
channel = "Total Commands Processed",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("total_commands_processed")).Value
}
},
{
new PRTGResult()
{
channel = "Instantaneous Operations per Second",
unit = PRTGUnit.Count,
value = statsInfo.SingleOrDefault(i => i.Key.Equals("instantaneous_ops_per_sec")).Value
}
},
{
new PRTGResult()
{
channel = "Is Master",
unit = PRTGUnit.Count,
ShowChart = 0,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("role")).Value.Equals("master", StringComparison.InvariantCultureIgnoreCase)?"1":"0"
}
},
{
new PRTGResult()
{
channel = "Connected Slaves",
unit = PRTGUnit.Count,
value = replicationInfo.SingleOrDefault(i => i.Key.Equals("connected_slaves")).Value
}
},
{
new PRTGResult()
{
channel = "Keys",
unit = PRTGUnit.Count,
value = server.DatabaseSize(0).ToString()
}
}
}
}
};
Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}));
}
/// <summary>
/// Gets the Redis login password if it is present in the
/// second program argument.
/// </summary>
/// <param name="args">Array of program arguments</param>
/// <returns>The formatted password string</returns>
private static string GetPasswordFromArgs(string[] args)
{
if (args.Length > 1)
{
var redisPassword = args[1];
return $",Password={redisPassword}";
}
return string.Empty;
}
}
}